Skip to content

Commit

Permalink
Merge branch 'main' into go-zcount-command
Browse files Browse the repository at this point in the history
Signed-off-by: prateek-kumar-improving <[email protected]>
  • Loading branch information
prateek-kumar-improving authored Jan 11, 2025
2 parents eb5bd11 + 0b0b467 commit 3f6ee4b
Show file tree
Hide file tree
Showing 27 changed files with 1,131 additions and 51 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
* Go: Add `ZPopMin` and `ZPopMax` ([#2850](https://github.com/valkey-io/valkey-glide/pull/2850))
* Java: Add binary version of `ZRANK WITHSCORE` ([#2896](https://github.com/valkey-io/valkey-glide/pull/2896))
* Go: Add `ZCARD` ([#2838](https://github.com/valkey-io/valkey-glide/pull/2838))
* Java, Node, Python: Update documentation for CONFIG SET and CONFIG GET ([#2919](https://github.com/valkey-io/valkey-glide/pull/2919))
* Go: Add `BZPopMin` ([#2849](https://github.com/valkey-io/valkey-glide/pull/2849))

#### Breaking Changes

#### Fixes

* Node: Fix `zrangeWithScores` (disallow `RangeByLex` as it is not supported) ([#2926](https://github.com/valkey-io/valkey-glide/pull/2926))
* Core: improve fix in #2381 ([#2929](https://github.com/valkey-io/valkey-glide/pull/2929))

#### Operational Enhancements
Expand Down
143 changes: 143 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,101 @@ func (client *baseClient) BZPopMin(keys []string, timeoutSecs float64) (Result[K
return handleKeyWithMemberAndScoreResponse(result)
}

// Returns the specified range of elements in the sorted set stored at `key`.
// `ZRANGE` can perform different types of range queries: by index (rank), by the score, or by lexicographical order.
//
// To get the elements with their scores, see [ZRangeWithScores].
//
// See [valkey.io] for more details.
//
// Parameters:
//
// key - The key of the 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:
//
// An array of elements within the specified range.
// If `key` does not exist, it is treated as an empty sorted set, and the command returns an empty array.
//
// Example:
//
// // Retrieve all members of a sorted set in ascending order
// result, err := client.ZRange("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()
// result, err := client.ZRange("my_sorted_set", query)
// // `result` contains members which have scores within the range of negative infinity to 3, in descending order
//
// [valkey.io]: https://valkey.io/commands/zrange/
func (client *baseClient) ZRange(key string, rangeQuery options.ZRangeQuery) ([]Result[string], error) {
args := make([]string, 0, 10)
args = append(args, key)
args = append(args, rangeQuery.ToArgs()...)
result, err := client.executeCommand(C.ZRange, args)
if err != nil {
return nil, err
}

return handleStringArrayResponse(result)
}

// Returns the specified range of elements with their scores in the sorted set stored at `key`.
// `ZRANGE` can perform different types of range queries: by index (rank), by the score, or by lexicographical order.
//
// See [valkey.io] for more details.
//
// Parameters:
//
// key - The key of the 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 score, use [RangeByScore].
//
// Return value:
//
// A map of elements and their scores within the specified range.
// If `key` does not exist, it is treated as an empty sorted set, and the command returns an empty map.
//
// Example:
//
// // Retrieve all members of a sorted set in ascending order
// result, err := client.ZRangeWithScores("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()
// result, err := client.ZRangeWithScores("my_sorted_set", query)
// // `result` contains members with scores within the range of negative infinity to 3, in descending order
//
// [valkey.io]: https://valkey.io/commands/zrange/
func (client *baseClient) ZRangeWithScores(
key string,
rangeQuery options.ZRangeQueryWithScores,
) (map[Result[string]]Result[float64], error) {
args := make([]string, 0, 10)
args = append(args, key)
args = append(args, rangeQuery.ToArgs()...)
args = append(args, "WITHSCORES")
result, err := client.executeCommand(C.ZRange, args)
if err != nil {
return nil, err
}

return handleStringDoubleMapResponse(result)
}

func (client *baseClient) Persist(key string) (Result[bool], error) {
result, err := client.executeCommand(C.Persist, []string{key})
if err != nil {
Expand All @@ -1490,6 +1585,54 @@ func (client *baseClient) ZCount(key string, rangeOptions *options.ZCountRange)
return CreateNilInt64Result(), err
}
result, err := client.executeCommand(C.ZCount, append([]string{key}, zCountRangeArgs...))
}

func (client *baseClient) ZRank(key string, member string) (Result[int64], error) {
result, err := client.executeCommand(C.ZRank, []string{key, member})
if err != nil {
return CreateNilInt64Result(), err
}
return handleLongOrNullResponse(result)
}

func (client *baseClient) ZRankWithScore(key string, member string) (Result[int64], Result[float64], error) {
result, err := client.executeCommand(C.ZRank, []string{key, member, options.WithScore})
if err != nil {
return CreateNilInt64Result(), CreateNilFloat64Result(), err
}
return handleLongAndDoubleOrNullResponse(result)
}

func (client *baseClient) ZRevRank(key string, member string) (Result[int64], error) {
result, err := client.executeCommand(C.ZRevRank, []string{key, member})
if err != nil {
return CreateNilInt64Result(), err
}
return handleLongOrNullResponse(result)
}

func (client *baseClient) ZRevRankWithScore(key string, member string) (Result[int64], Result[float64], error) {
result, err := client.executeCommand(C.ZRevRank, []string{key, member, options.WithScore})
if err != nil {
return CreateNilInt64Result(), CreateNilFloat64Result(), err
}
return handleLongAndDoubleOrNullResponse(result)
}

func (client *baseClient) XTrim(key string, options *options.XTrimOptions) (Result[int64], error) {
xTrimArgs, err := options.ToArgs()
if err != nil {
return CreateNilInt64Result(), err
}
result, err := client.executeCommand(C.XTrim, append([]string{key}, xTrimArgs...))
if err != nil {
return CreateNilInt64Result(), err
}
return handleLongResponse(result)
}

func (client *baseClient) XLen(key string) (Result[int64], error) {
result, err := client.executeCommand(C.XLen, []string{key})
if err != nil {
return CreateNilInt64Result(), err
}
Expand Down
7 changes: 4 additions & 3 deletions go/api/options/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
package options

const (
CountKeyword string = "COUNT" // Valkey API keyword used to extract specific number of matching indices from a list.
MatchKeyword string = "MATCH" // Valkey API keyword used to indicate the match filter.
NoValue string = "NOVALUE" // Valkey API keyword for the no value option for hcsan command.
CountKeyword string = "COUNT" // Valkey API keyword used to extract specific number of matching indices from a list.
MatchKeyword string = "MATCH" // Valkey API keyword used to indicate the match filter.
NoValue string = "NOVALUE" // Valkey API keyword for the no value option for hcsan command.
WithScore string = "WITHSCORE" // Valkey API keyword for the with score option for zrank and zrevrank commands.
)
38 changes: 18 additions & 20 deletions go/api/options/stream_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,34 @@ func NewXTrimOptionsWithMaxLen(threshold int64) *XTrimOptions {
}

// Match exactly on the threshold.
func (xto *XTrimOptions) SetExactTrimming() *XTrimOptions {
xto.exact = triStateBoolTrue
return xto
func (xTrimOptions *XTrimOptions) SetExactTrimming() *XTrimOptions {
xTrimOptions.exact = triStateBoolTrue
return xTrimOptions
}

// Trim in a near-exact manner, which is more efficient.
func (xto *XTrimOptions) SetNearlyExactTrimming() *XTrimOptions {
xto.exact = triStateBoolFalse
return xto
func (xTrimOptions *XTrimOptions) SetNearlyExactTrimming() *XTrimOptions {
xTrimOptions.exact = triStateBoolFalse
return xTrimOptions
}

// Max number of stream entries to be trimmed for non-exact match.
func (xto *XTrimOptions) SetNearlyExactTrimmingAndLimit(limit int64) *XTrimOptions {
xto.exact = triStateBoolFalse
xto.limit = limit
return xto
func (xTrimOptions *XTrimOptions) SetNearlyExactTrimmingAndLimit(limit int64) *XTrimOptions {
xTrimOptions.exact = triStateBoolFalse
xTrimOptions.limit = limit
return xTrimOptions
}

func (xto *XTrimOptions) ToArgs() ([]string, error) {
args := []string{}
args = append(args, xto.method)
if xto.exact == triStateBoolTrue {
func (xTrimOptions *XTrimOptions) ToArgs() ([]string, error) {
args := []string{xTrimOptions.method}
if xTrimOptions.exact == triStateBoolTrue {
args = append(args, "=")
} else if xto.exact == triStateBoolFalse {
} else if xTrimOptions.exact == triStateBoolFalse {
args = append(args, "~")
}
args = append(args, xto.threshold)
if xto.limit > 0 {
args = append(args, "LIMIT", utils.IntToString(xto.limit))
args = append(args, xTrimOptions.threshold)
if xTrimOptions.limit > 0 {
args = append(args, "LIMIT", utils.IntToString(xTrimOptions.limit))
}
var err error
return args, err
return args, nil
}
Loading

0 comments on commit 3f6ee4b

Please sign in to comment.