Skip to content

Commit

Permalink
Merge branch 'valkey-io:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
avifenesh authored Feb 10, 2025
2 parents 479ddc8 + 548c2c0 commit 081cfdc
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 17 deletions.
82 changes: 72 additions & 10 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,21 @@ func (client *baseClient) Set(key string, value string) (string, error) {
// Return value:
//
// If the value is successfully set, return api.Result[string] containing "OK".
// If value isn't set because of ConditionalSet.OnlyIfExists or ConditionalSet.OnlyIfDoesNotExist conditions, return
// api.CreateNilStringResult().
// If value isn't set because of ConditionalSet.OnlyIfExists or ConditionalSet.OnlyIfDoesNotExist
// or ConditionalSet.OnlyIfEquals conditions, return api.CreateNilStringResult().
// If SetOptions.returnOldValue is set, return the old value as a String.
//
// For example:
//
// key: initialValue
// result, err := client.SetWithOptions("key", "value", api.NewSetOptionsBuilder()
// .SetExpiry(api.NewExpiryBuilder()
// .SetType(api.Seconds)
// .SetCount(uint64(5)
// ))
// result.Value(): "OK"
// result.IsNil(): false
// key: initialValue
// result, err := client.SetWithOptions("key", "value", api.NewSetOptionsBuilder()
// .SetExpiry(api.NewExpiryBuilder()
// .SetOnlyIfExists()
// .SetType(api.Seconds)
// .SetCount(uint64(5)
// ))
// result.Value(): "OK"
// result.IsNil(): false
//
// [valkey.io]: https://valkey.io/commands/set/
func (client *baseClient) SetWithOptions(key string, value string, options *SetOptions) (Result[string], error) {
Expand Down Expand Up @@ -4711,6 +4712,67 @@ func (client *baseClient) ZRangeWithScores(
return handleStringDoubleMapResponse(result)
}

// Stores a specified range of elements from the sorted set at `key`, into a new
// sorted set at `destination`. If `destination` doesn't exist, a new sorted
// set is created; if it exists, it's overwritten.
//
// Note:
//
// When in cluster mode, all keys must map to the same hash slot.
//
// See [valkey.io] for more details.
//
// Parameters:
//
// destination - The key for the destination sorted set.
// key - The key of the source sorted set.
// rangeQuery - The range query object representing the type of range query to perform.
// - For range queries by index (rank), use [RangeByIndex].
// - For range queries by lexicographical order, use [RangeByLex].
// - For range queries by score, use [RangeByScore].
//
// Return value:
//
// The number of elements in the resulting sorted set.
//
// For example:
//
// client.ZAdd("my_sorted_set", map[string]float64{"a": 1.0, "b": 2.0, "c": 3.0})
//
// // Retrieve and store all members of a sorted set in ascending order
// res1, err := client.ZRangeStore("my_dest", "my_sorted_set", options.NewRangeByIndexQuery(0, -1))
//
// // Retrieve members within a score range in descending order
// query := options.NewRangeByScoreQuery(
// options.NewScoreBoundary(3, false),
// options.NewInfiniteScoreBoundary(options.NegativeInfinity)).
// SetReverse()
// res2, err := client.ZRange("my_dest", query)
// fmt.Println(res1)
// fmt.Println(res2)
//
// // Output:
// // 3
// // [b a]
//
// [valkey.io]: https://valkey.io/commands/zrangestore/
func (client *baseClient) ZRangeStore(
destination string,
key string,
rangeQuery options.ZRangeQuery,
) (int64, error) {
args := make([]string, 0, 10)
args = append(args, destination)
args = append(args, key)
args = append(args, rangeQuery.ToArgs()...)
result, err := client.executeCommand(C.ZRangeStore, args)
if err != nil {
return defaultIntResponse, err
}

return handleIntResponse(result)
}

// Removes the existing timeout on key, turning the key from volatile
// (a key with an expire set) to persistent (a key that will never expire as no timeout is associated).
//
Expand Down
48 changes: 48 additions & 0 deletions go/api/command_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type SetOptions struct {
// If ConditionalSet is not set the value will be set regardless of prior value existence. If value isn't set because of
// the condition, [api.StringCommands.SetWithOptions] will return a zero-value string ("").
ConditionalSet ConditionalSet
// Value to compare when [SetOptions.ConditionalSet] is set to `OnlyIfEquals`.
ComparisonValue string
// Set command to return the old value stored at the given key, or a zero-value string ("") if the key did not exist. An
// error is returned and [api.StringCommands.SetWithOptions] is aborted if the value stored at key is not a string.
// Equivalent to GET in the valkey API.
Expand All @@ -32,8 +34,46 @@ func NewSetOptionsBuilder() *SetOptions {
return &SetOptions{}
}

// Sets the condition to [SetOptions.ConditionalSet] for setting the value.
//
// This method overrides any previously set [SetOptions.ConditionalSet] and [SetOptions.ComparisonValue].
//
// Deprecated: Use [SetOptions.SetOnlyIfExists], [SetOptions.SetOnlyIfDoesNotExist], or [SetOptions.SetOnlyIfEquals] instead.
func (setOptions *SetOptions) SetConditionalSet(conditionalSet ConditionalSet) *SetOptions {
setOptions.ConditionalSet = conditionalSet
setOptions.ComparisonValue = ""
return setOptions
}

// Sets the condition to [SetOptions.OnlyIfExists] for setting the value. The key
// will be set if it already exists.
//
// This method overrides any previously set [SetOptions.ConditionalSet] and [SetOptions.ComparisonValue].
func (setOptions *SetOptions) SetOnlyIfExists() *SetOptions {
setOptions.ConditionalSet = OnlyIfExists
setOptions.ComparisonValue = ""
return setOptions
}

// Sets the condition to [SetOptions.OnlyIfDoesNotExist] for setting the value. The key
// will not be set if it already exists.
//
// This method overrides any previously set [SetOptions.ConditionalSet] and [SetOptions.ComparisonValue].
func (setOptions *SetOptions) SetOnlyIfDoesNotExist() *SetOptions {
setOptions.ConditionalSet = OnlyIfDoesNotExist
setOptions.ComparisonValue = ""
return setOptions
}

// Sets the condition to [SetOptions.OnlyIfEquals] for setting the value. The key
// will be set if the provided comparison value matches the existing value.
//
// This method overrides any previously set [SetOptions.ConditionalSet] and [SetOptions.ComparisonValue].
//
// since Valkey 8.1 and above.
func (setOptions *SetOptions) SetOnlyIfEquals(comparisonValue string) *SetOptions {
setOptions.ConditionalSet = OnlyIfEquals
setOptions.ComparisonValue = comparisonValue
return setOptions
}

Expand All @@ -52,6 +92,9 @@ func (opts *SetOptions) toArgs() ([]string, error) {
var err error
if opts.ConditionalSet != "" {
args = append(args, string(opts.ConditionalSet))
if opts.ConditionalSet == OnlyIfEquals {
args = append(args, opts.ComparisonValue)
}
}

if opts.ReturnOldValue {
Expand Down Expand Up @@ -120,6 +163,11 @@ const (
OnlyIfExists ConditionalSet = "XX"
// OnlyIfDoesNotExist only sets the key if it does not already exist. Equivalent to "NX" in the valkey API.
OnlyIfDoesNotExist ConditionalSet = "NX"
// OnlyIfEquals only sets the key if it already exists and the value is equal to the given value. Equivalent to "IFEQ" in
// the valkey API.
//
// since Valkey 8.1 and above.
OnlyIfEquals ConditionalSet = "IFEQ"
)

type ExpireCondition string
Expand Down
2 changes: 2 additions & 0 deletions go/api/sorted_set_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type SortedSetCommands interface {

ZRangeWithScores(key string, rangeQuery options.ZRangeQueryWithScores) (map[string]float64, error)

ZRangeStore(destination string, key string, rangeQuery options.ZRangeQuery) (int64, error)

ZRank(key string, member string) (Result[int64], error)

ZRankWithScore(key string, member string) (Result[int64], Result[float64], error)
Expand Down
Loading

0 comments on commit 081cfdc

Please sign in to comment.