forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Go: Add command ZAdd (valkey-io#2813)
* Go: Add command ZAdd Signed-off-by: TJ Zhang <tj.zhang@improving.com>
- Loading branch information
1 parent
b4231b0
commit 24a2dd0
Showing
7 changed files
with
398 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 | ||
|
||
package options | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/valkey-io/valkey-glide/go/glide/utils" | ||
) | ||
|
||
// Optional arguments to `ZAdd` in [SortedSetCommands] | ||
type ZAddOptions struct { | ||
conditionalChange ConditionalChange | ||
updateOptions UpdateOptions | ||
changed bool | ||
incr bool | ||
increment float64 | ||
member string | ||
} | ||
|
||
func NewZAddOptionsBuilder() *ZAddOptions { | ||
return &ZAddOptions{} | ||
} | ||
|
||
// `conditionalChange“ defines conditions for updating or adding elements with {@link SortedSetBaseCommands#zadd} | ||
// command. | ||
func (options *ZAddOptions) SetConditionalChange(c ConditionalChange) *ZAddOptions { | ||
options.conditionalChange = c | ||
return options | ||
} | ||
|
||
// `updateOptions` specifies conditions for updating scores with zadd command. | ||
func (options *ZAddOptions) SetUpdateOptions(u UpdateOptions) *ZAddOptions { | ||
options.updateOptions = u | ||
return options | ||
} | ||
|
||
// `Changed` changes the return value from the number of new elements added to the total number of elements changed. | ||
func (options *ZAddOptions) SetChanged(ch bool) (*ZAddOptions, error) { | ||
if options.incr { | ||
return nil, errors.New("changed cannot be set when incr is true") | ||
} | ||
options.changed = ch | ||
return options, nil | ||
} | ||
|
||
// `INCR` sets the increment value to use when incr is true. | ||
func (options *ZAddOptions) SetIncr(incr bool, increment float64, member string) (*ZAddOptions, error) { | ||
if options.changed { | ||
return nil, errors.New("incr cannot be set when changed is true") | ||
} | ||
options.incr = incr | ||
options.increment = increment | ||
options.member = member | ||
return options, nil | ||
} | ||
|
||
// `ToArgs` converts the options to a list of arguments. | ||
func (opts *ZAddOptions) ToArgs() ([]string, error) { | ||
args := []string{} | ||
var err error | ||
|
||
if opts.conditionalChange == OnlyIfExists || opts.conditionalChange == OnlyIfDoesNotExist { | ||
args = append(args, string(opts.conditionalChange)) | ||
} | ||
|
||
if opts.updateOptions == ScoreGreaterThanCurrent || opts.updateOptions == ScoreLessThanCurrent { | ||
args = append(args, string(opts.updateOptions)) | ||
} | ||
|
||
if opts.changed { | ||
args = append(args, ChangedKeyword) | ||
} | ||
|
||
if opts.incr { | ||
args = append(args, IncrKeyword, utils.FloatToString(opts.increment), opts.member) | ||
} | ||
|
||
return args, err | ||
} | ||
|
||
// A ConditionalSet defines whether a new value should be set or not. | ||
type ConditionalChange string | ||
|
||
const ( | ||
// Only update elements that already exist. Don't add new elements. Equivalent to "XX" in the Valkey API. | ||
OnlyIfExists ConditionalChange = "XX" | ||
// Only add new elements. Don't update already existing elements. Equivalent to "NX" in the Valkey API. | ||
OnlyIfDoesNotExist ConditionalChange = "NX" | ||
) | ||
|
||
type UpdateOptions string | ||
|
||
const ( | ||
// Only update existing elements if the new score is less than the current score. Equivalent to | ||
// "LT" in the Valkey API. | ||
ScoreLessThanCurrent UpdateOptions = "LT" | ||
// Only update existing elements if the new score is greater than the current score. Equivalent | ||
// to "GT" in the Valkey API. | ||
ScoreGreaterThanCurrent UpdateOptions = "GT" | ||
) | ||
|
||
const ( | ||
ChangedKeyword string = "CH" // Valkey API keyword used to return total number of elements changed | ||
IncrKeyword string = "INCR" // Valkey API keyword to make zadd act like ZINCRBY. | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 | ||
|
||
package api | ||
|
||
import ( | ||
"github.com/valkey-io/valkey-glide/go/glide/api/options" | ||
) | ||
|
||
// SortedSetCommands supports commands and transactions for the "Sorted Set Commands" group for standalone and cluster clients. | ||
// | ||
// See [valkey.io] for details. | ||
// | ||
// [valkey.io]: https://valkey.io/commands/#sorted-set | ||
type SortedSetCommands interface { | ||
// Adds one or more members to a sorted set, or updates their scores. Creates the key if it doesn't exist. | ||
// | ||
// See [valkey.io] for details. | ||
// | ||
// Parameters: | ||
// key - The key of the set. | ||
// membersScoreMap - A map of members to their scores. | ||
// | ||
// Return value: | ||
// Result[int64] - The number of members added to the set. | ||
// | ||
// Example: | ||
// res, err := client.ZAdd(key, map[string]float64{"one": 1.0, "two": 2.0, "three": 3.0}) | ||
// fmt.Println(res.Value()) // Output: 3 | ||
// | ||
// [valkey.io]: https://valkey.io/commands/zadd/ | ||
ZAdd(key string, membersScoreMap map[string]float64) (Result[int64], error) | ||
|
||
// Adds one or more members to a sorted set, or updates their scores. Creates the key if it doesn't exist. | ||
// | ||
// See [valkey.io] for details. | ||
// | ||
// Parameters: | ||
// key - The key of the set. | ||
// membersScoreMap - A map of members to their scores. | ||
// opts - The options for the command. See [ZAddOptions] for details. | ||
// | ||
// Return value: | ||
// Result[int64] - The number of members added to the set. If CHANGED is set, the number of members that were updated. | ||
// | ||
// Example: | ||
// res, err := client.ZAddWithOptions(key, map[string]float64{"one": 1.0, "two": 2.0, "three": 3.0}, | ||
// options.NewZAddOptionsBuilder().SetChanged(true).Build()) | ||
// fmt.Println(res.Value()) // Output: 3 | ||
// | ||
// [valkey.io]: https://valkey.io/commands/zadd/ | ||
ZAddWithOptions(key string, membersScoreMap map[string]float64, opts *options.ZAddOptions) (Result[int64], error) | ||
|
||
// Adds one or more members to a sorted set, or updates their scores. Creates the key if it doesn't exist. | ||
// | ||
// See [valkey.io] for details. | ||
// | ||
// Parameters: | ||
// key - The key of the set. | ||
// member - The member to add to. | ||
// increment - The increment to add to the member's score. | ||
// | ||
// Return value: | ||
// Result[float64] - The new score of the member. | ||
// | ||
// Example: | ||
// res, err := client.ZAddIncr(key, "one", 1.0) | ||
// fmt.Println(res.Value()) // Output: 1.0 | ||
// | ||
// [valkey.io]: https://valkey.io/commands/zadd/ | ||
ZAddIncr(key string, member string, increment float64) (Result[float64], error) | ||
|
||
// Adds one or more members to a sorted set, or updates their scores. Creates the key if it doesn't exist. | ||
// | ||
// See [valkey.io] for details. | ||
// | ||
// Parameters: | ||
// key - The key of the set. | ||
// member - The member to add to. | ||
// increment - The increment to add to the member's score. | ||
// opts - The options for the command. See [ZAddOptions] for details. | ||
// | ||
// Return value: | ||
// Result[float64] - The new score of the member. | ||
// | ||
// Example: | ||
// res, err := client.ZAddIncrWithOptions(key, "one", 1.0, options.NewZAddOptionsBuilder().SetChanged(true)) | ||
// fmt.Println(res.Value()) // Output: 1.0 | ||
// | ||
// [valkey.io]: https://valkey.io/commands/zadd/ | ||
ZAddIncrWithOptions(key string, member string, increment float64, opts *options.ZAddOptions) (Result[float64], error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.