From 197c96531384c37406633563afd6814986ff50ff Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Wed, 8 Jan 2025 15:29:08 -0800 Subject: [PATCH 01/12] Go: Zcount command Signed-off-by: Prateek Kumar --- go/api/options/options.go | 7 +++ go/api/options/zcount_options.go | 80 ++++++++++++++++++++++++++++++++ go/api/sorted_set_commands.go | 17 +++++++ 3 files changed, 104 insertions(+) create mode 100644 go/api/options/options.go create mode 100644 go/api/options/zcount_options.go diff --git a/go/api/options/options.go b/go/api/options/options.go new file mode 100644 index 0000000000..b77be30272 --- /dev/null +++ b/go/api/options/options.go @@ -0,0 +1,7 @@ +// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 + +package options + +type BaseOptions interface { + ToArgs() ([]string, error) +} diff --git a/go/api/options/zcount_options.go b/go/api/options/zcount_options.go new file mode 100644 index 0000000000..364e2403c0 --- /dev/null +++ b/go/api/options/zcount_options.go @@ -0,0 +1,80 @@ +// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 + +package options + +import "github.com/valkey-io/valkey-glide/go/glide/utils" + +type ScoreRange interface { + ToArgs() []string +} + +type ( + InfBoundary string +) + +const ( + // The highest bound in the sorted set + PositiveInfinity InfBoundary = "+inf" + // The lowest bound in the sorted set + NegativeInfinity InfBoundary = "-inf" +) + +type InfScoreBound struct { + value InfBoundary +} + +func NewInfScoreBuilder() *InfScoreBound { + return &InfScoreBound{} +} + +func (infScoreBound *InfScoreBound) SetValue(value InfBoundary) *InfScoreBound { + infScoreBound.value = value + return infScoreBound +} + +func (infScoreBound *InfScoreBound) ToArgs() ([]string, error) { + args := []string{} + args = append(args, string(infScoreBound.value)) + return args, nil +} + +type ScoreBoundary struct { + bound float64 + isInclusive bool +} + +func NewScoreBoundaryBuilder() *ScoreBoundary { + return &ScoreBoundary{} +} + +func (scoreBoundary *ScoreBoundary) SetBound(bound float64) *ScoreBoundary { + scoreBoundary.bound = bound + return scoreBoundary +} + +func (scoreBoundary *ScoreBoundary) SetIsInclusive(isInclusive bool) *ScoreBoundary { + scoreBoundary.isInclusive = isInclusive + return scoreBoundary +} + +func (scoreBoundary *ScoreBoundary) ToArgs() ([]string, error) { + args := []string{} + if !scoreBoundary.isInclusive { + args = append(args, "("+utils.FloatToString(scoreBoundary.bound)) + } else { + args = append(args, utils.FloatToString(scoreBoundary.bound)) + } + return args, nil +} + +type ZCountRange struct { + min ScoreRange + max ScoreRange +} + +func (zCountRange *ZCountRange) ToArgs() ([]string, error) { + args := []string{} + args = append(args, zCountRange.min.ToArgs()...) + args = append(args, zCountRange.max.ToArgs()...) + return args, nil +} diff --git a/go/api/sorted_set_commands.go b/go/api/sorted_set_commands.go index 4b63b70091..8cd2012d85 100644 --- a/go/api/sorted_set_commands.go +++ b/go/api/sorted_set_commands.go @@ -262,4 +262,21 @@ type SortedSetCommands interface { // [valkey.io]: https://valkey.io/commands/bzpopmin/ // [blocking commands]: https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#blocking-commands BZPopMin(keys []string, timeoutSecs float64) (Result[KeyWithMemberAndScore], error) + + // Returns the number of members in the sorted set stored at `key` with scores between `minScore` and `maxScore`. + // + // See [valkey.io] for details. + // + // Parameters: + // key - The key of the set. + // minScore - The minimum score to count from. Can be positive/negative infinity, or specific score and inclusivity. + // maxScore - The maximum score to count up to. Can be positive/negative infinity, or specific score and inclusivity. + // + // Return value: + // Result[int64] - The number of members in the specified score range. + // + // Example: + // + // [valkey.io]: https://valkey.io/commands/zcount/ + ZCount(key string, minScore *options.ScoreRange, maxScore *options.ScoreRange) (Result[int64], error) } From ab1f6178b69cd0aa2ce0fd9841a1f02b496c7714 Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Wed, 8 Jan 2025 15:30:34 -0800 Subject: [PATCH 02/12] Go: Zcount remove options.go file Signed-off-by: Prateek Kumar --- go/api/options/options.go | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 go/api/options/options.go diff --git a/go/api/options/options.go b/go/api/options/options.go deleted file mode 100644 index b77be30272..0000000000 --- a/go/api/options/options.go +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 - -package options - -type BaseOptions interface { - ToArgs() ([]string, error) -} From 4494208f0c9bd222cfd8fe6b05af8639f69f942d Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Wed, 8 Jan 2025 15:44:13 -0800 Subject: [PATCH 03/12] Go: Added Range options Signed-off-by: Prateek Kumar --- go/api/base_client.go | 12 ++++++++++++ go/api/options/zcount_options.go | 14 +++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/go/api/base_client.go b/go/api/base_client.go index b954ddabb3..5979a93c9b 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -1483,3 +1483,15 @@ func (client *baseClient) Persist(key string) (Result[bool], error) { } return handleBooleanResponse(result) } + +func (client *baseClient) ZCount(key string, rangeOptions options.ZCountRange) (Result[int64], error) { + zCountRangeArgs, err := rangeOptions.ToArgs() + if err != nil { + return CreateNilInt64Result(), err + } + result, err := client.executeCommand(C.ZCount, append([]string{key}, zCountRangeArgs...)) + if err != nil { + return CreateNilInt64Result(), err + } + return handleLongResponse(result) +} diff --git a/go/api/options/zcount_options.go b/go/api/options/zcount_options.go index 364e2403c0..118115eabb 100644 --- a/go/api/options/zcount_options.go +++ b/go/api/options/zcount_options.go @@ -5,7 +5,7 @@ package options import "github.com/valkey-io/valkey-glide/go/glide/utils" type ScoreRange interface { - ToArgs() []string + ToArgs() ([]string, error) } type ( @@ -74,7 +74,15 @@ type ZCountRange struct { func (zCountRange *ZCountRange) ToArgs() ([]string, error) { args := []string{} - args = append(args, zCountRange.min.ToArgs()...) - args = append(args, zCountRange.max.ToArgs()...) + minArgs, err := zCountRange.min.ToArgs() + if err != nil { + return nil, err + } + args = append(args, minArgs...) + maxArgs, err := zCountRange.max.ToArgs() + if err != nil { + return nil, err + } + args = append(args, maxArgs...) return args, nil } From 01e3e672a18ab55e72428c902a55c01f6f8b6a68 Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Thu, 9 Jan 2025 01:45:17 -0800 Subject: [PATCH 04/12] Go: Update test cases Signed-off-by: Prateek Kumar --- go/api/base_client.go | 2 +- go/api/options/zcount_options.go | 16 +++++++- go/api/sorted_set_commands.go | 2 +- go/integTest/shared_commands_test.go | 57 ++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 3 deletions(-) diff --git a/go/api/base_client.go b/go/api/base_client.go index 5979a93c9b..a3c10587e2 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -1484,7 +1484,7 @@ func (client *baseClient) Persist(key string) (Result[bool], error) { return handleBooleanResponse(result) } -func (client *baseClient) ZCount(key string, rangeOptions options.ZCountRange) (Result[int64], error) { +func (client *baseClient) ZCount(key string, rangeOptions *options.ZCountRange) (Result[int64], error) { zCountRangeArgs, err := rangeOptions.ToArgs() if err != nil { return CreateNilInt64Result(), err diff --git a/go/api/options/zcount_options.go b/go/api/options/zcount_options.go index 118115eabb..6e4d811190 100644 --- a/go/api/options/zcount_options.go +++ b/go/api/options/zcount_options.go @@ -23,7 +23,7 @@ type InfScoreBound struct { value InfBoundary } -func NewInfScoreBuilder() *InfScoreBound { +func NewInfScoreBoundBuilder() *InfScoreBound { return &InfScoreBound{} } @@ -72,6 +72,20 @@ type ZCountRange struct { max ScoreRange } +func NewZCountRangeBuilder() *ZCountRange { + return &ZCountRange{} +} + +func (zCountRange *ZCountRange) SetMin(min ScoreRange) *ZCountRange { + zCountRange.min = min + return zCountRange +} + +func (zCountRange *ZCountRange) SetMax(max ScoreRange) *ZCountRange { + zCountRange.max = max + return zCountRange +} + func (zCountRange *ZCountRange) ToArgs() ([]string, error) { args := []string{} minArgs, err := zCountRange.min.ToArgs() diff --git a/go/api/sorted_set_commands.go b/go/api/sorted_set_commands.go index 8cd2012d85..50bb6f97ef 100644 --- a/go/api/sorted_set_commands.go +++ b/go/api/sorted_set_commands.go @@ -278,5 +278,5 @@ type SortedSetCommands interface { // Example: // // [valkey.io]: https://valkey.io/commands/zcount/ - ZCount(key string, minScore *options.ScoreRange, maxScore *options.ScoreRange) (Result[int64], error) + ZCount(key string, rangeOptions *options.ZCountRange) (Result[int64], error) } diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index df0568e3f4..c3b5a8c4cd 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -4484,3 +4484,60 @@ func (suite *GlideTestSuite) TestPersist() { assert.False(t, resultInvalidKey.Value()) }) } + +func (suite *GlideTestSuite) TestZCount() { + suite.runWithDefaultClients(func(client api.BaseClient) { + key1 := uuid.NewString() + // key2 := uuid.NewString() + membersScores := map[string]float64{ + "one": 1.0, + "two": 2.0, + "three": 3.0, + } + t := suite.T() + res1, err := client.ZAdd(key1, membersScores) + assert.Nil(t, err) + assert.Equal(t, int64(3), res1.Value()) + + zCountRange := options.NewZCountRangeBuilder() + zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) + zCountRange.SetMax(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) + zCountResult, err := client.ZCount(key1, zCountRange) + assert.Nil(t, err) + assert.Equal(t, int64(3), zCountResult.Value()) + }) +} + +// String key1 = UUID.randomUUID().toString(); +// String key2 = UUID.randomUUID().toString(); +// Map membersScores = Map.of("one", 1.0, "two", 2.0, "three", 3.0); +// assertEquals(3, client.zadd(key1, membersScores).get()); + +// // In range negative to positive infinity. +// assertEquals(3, client.zcount(key1, NEGATIVE_INFINITY, POSITIVE_INFINITY).get()); +/////////----Done +// assertEquals( +// 3, +// client +// .zcount( +// key1, +// new ScoreBoundary(Double.NEGATIVE_INFINITY), +// new ScoreBoundary(Double.POSITIVE_INFINITY)) +// .get()); +// // In range 1 (exclusive) to 3 (inclusive) +// assertEquals( +// 2, client.zcount(key1, new ScoreBoundary(1, false), new ScoreBoundary(3, true)).get()); +// // In range negative infinity to 3 (inclusive) +// assertEquals(3, client.zcount(key1, NEGATIVE_INFINITY, new ScoreBoundary(3, true)).get()); +// // Incorrect range start > end +// assertEquals(0, client.zcount(key1, POSITIVE_INFINITY, new ScoreBoundary(3, true)).get()); +// // Non-existing key +// assertEquals(0, client.zcount("non_existing_key", NEGATIVE_INFINITY, POSITIVE_INFINITY).get()); + +// // Key exists, but it is not a set +// assertEquals(OK, client.set(key2, "value").get()); +// ExecutionException executionException = +// assertThrows( +// ExecutionException.class, +// () -> client.zcount(key2, NEGATIVE_INFINITY, POSITIVE_INFINITY).get()); +// assertInstanceOf(RequestException.class, executionException.getCause()); From f919b62aecfa71611f5148bd5a6caba9fb67d943 Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Thu, 9 Jan 2025 09:44:05 -0800 Subject: [PATCH 05/12] Go: ZCount: Add test and update documentation Signed-off-by: Prateek Kumar --- go/api/options/zcount_options.go | 2 +- go/api/sorted_set_commands.go | 20 +++++-- go/integTest/shared_commands_test.go | 85 ++++++++++++++++------------ 3 files changed, 67 insertions(+), 40 deletions(-) diff --git a/go/api/options/zcount_options.go b/go/api/options/zcount_options.go index 6e4d811190..1a79e8d9e9 100644 --- a/go/api/options/zcount_options.go +++ b/go/api/options/zcount_options.go @@ -44,7 +44,7 @@ type ScoreBoundary struct { } func NewScoreBoundaryBuilder() *ScoreBoundary { - return &ScoreBoundary{} + return &ScoreBoundary{isInclusive: true} } func (scoreBoundary *ScoreBoundary) SetBound(bound float64) *ScoreBoundary { diff --git a/go/api/sorted_set_commands.go b/go/api/sorted_set_commands.go index 50bb6f97ef..e77664390b 100644 --- a/go/api/sorted_set_commands.go +++ b/go/api/sorted_set_commands.go @@ -263,19 +263,31 @@ type SortedSetCommands interface { // [blocking commands]: https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#blocking-commands BZPopMin(keys []string, timeoutSecs float64) (Result[KeyWithMemberAndScore], error) - // Returns the number of members in the sorted set stored at `key` with scores between `minScore` and `maxScore`. + // Returns the number of members in the sorted set stored at `key` with scores between `min` and `max` score. // // See [valkey.io] for details. // // Parameters: // key - The key of the set. - // minScore - The minimum score to count from. Can be positive/negative infinity, or specific score and inclusivity. - // maxScore - The maximum score to count up to. Can be positive/negative infinity, or specific score and inclusivity. + // rangeOptions - Contains `min` and `max` score. `min` contains the minimum score to count from. + // `max` contains the maximum score to count up to. Can be positive/negative infinity, or + // specific score and inclusivity. // // Return value: - // Result[int64] - The number of members in the specified score range. + // Result[int64] - The number of members in the specified score range. // // Example: + // key1 := uuid.NewString() + // membersScores := map[string]float64{"one": 1.0, "two": 2.0, "three": 3.0 } + // zAddResult, err := client.ZAdd(key1, membersScores) + // zCountRange := options.NewZCountRangeBuilder() + // zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) + // zCountRange.SetMax(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) + // zCountResult, err := client.ZCount(key1, zCountRange) + // if err!= nil { + // // Print err + // } + // fmt.Println(zCountResult.Value()) // Output: 3 // // [valkey.io]: https://valkey.io/commands/zcount/ ZCount(key string, rangeOptions *options.ZCountRange) (Result[int64], error) diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index c3b5a8c4cd..478cec6b3b 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -4488,7 +4488,7 @@ func (suite *GlideTestSuite) TestPersist() { func (suite *GlideTestSuite) TestZCount() { suite.runWithDefaultClients(func(client api.BaseClient) { key1 := uuid.NewString() - // key2 := uuid.NewString() + key2 := uuid.NewString() membersScores := map[string]float64{ "one": 1.0, "two": 2.0, @@ -4499,45 +4499,60 @@ func (suite *GlideTestSuite) TestZCount() { assert.Nil(t, err) assert.Equal(t, int64(3), res1.Value()) + // In range negative to positive infinity. zCountRange := options.NewZCountRangeBuilder() zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) zCountRange.SetMax(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) zCountResult, err := client.ZCount(key1, zCountRange) assert.Nil(t, err) assert.Equal(t, int64(3), zCountResult.Value()) + zCountRange = options.NewZCountRangeBuilder() + zCountRange.SetMin(options.NewScoreBoundaryBuilder().SetBound(math.Inf(-1))) + zCountRange.SetMax(options.NewScoreBoundaryBuilder().SetBound(math.Inf(+1))) + zCountResult, err = client.ZCount(key1, zCountRange) + assert.Nil(t, err) + assert.Equal(t, int64(3), zCountResult.Value()) + + // In range 1 (exclusive) to 3 (inclusive) + zCountRange = options.NewZCountRangeBuilder() + zCountRange.SetMin(options.NewScoreBoundaryBuilder().SetBound(1).SetIsInclusive(false)) + zCountRange.SetMax(options.NewScoreBoundaryBuilder().SetBound(3).SetIsInclusive(true)) + zCountResult, err = client.ZCount(key1, zCountRange) + assert.Nil(t, err) + assert.Equal(t, int64(2), zCountResult.Value()) + + // In range negative infinity to 3 (inclusive) + zCountRange = options.NewZCountRangeBuilder() + zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) + zCountRange.SetMax(options.NewScoreBoundaryBuilder().SetBound(3).SetIsInclusive(true)) + zCountResult, err = client.ZCount(key1, zCountRange) + assert.Nil(t, err) + assert.Equal(t, int64(3), zCountResult.Value()) + + // Incorrect range start > end + zCountRange = options.NewZCountRangeBuilder() + zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) + zCountRange.SetMax(options.NewScoreBoundaryBuilder().SetBound(3).SetIsInclusive(true)) + zCountResult, err = client.ZCount(key1, zCountRange) + assert.Nil(t, err) + assert.Equal(t, int64(0), zCountResult.Value()) + + // Non-existing key + zCountRange = options.NewZCountRangeBuilder() + zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) + zCountRange.SetMax(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) + zCountResult, err = client.ZCount("non_existing_key", zCountRange) + assert.Nil(t, err) + assert.Equal(t, int64(0), zCountResult.Value()) + + // Key exists, but it is not a set + setResult, err := client.Set(key2, "value") + assert.Equal(t, setResult.Value(), "OK") + zCountRange = options.NewZCountRangeBuilder() + zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) + zCountRange.SetMax(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) + zCountResult, err = client.ZCount(key2, zCountRange) + assert.NotNil(t, err) + assert.IsType(suite.T(), &api.RequestError{}, err) }) } - -// String key1 = UUID.randomUUID().toString(); -// String key2 = UUID.randomUUID().toString(); -// Map membersScores = Map.of("one", 1.0, "two", 2.0, "three", 3.0); -// assertEquals(3, client.zadd(key1, membersScores).get()); - -// // In range negative to positive infinity. -// assertEquals(3, client.zcount(key1, NEGATIVE_INFINITY, POSITIVE_INFINITY).get()); -/////////----Done -// assertEquals( -// 3, -// client -// .zcount( -// key1, -// new ScoreBoundary(Double.NEGATIVE_INFINITY), -// new ScoreBoundary(Double.POSITIVE_INFINITY)) -// .get()); -// // In range 1 (exclusive) to 3 (inclusive) -// assertEquals( -// 2, client.zcount(key1, new ScoreBoundary(1, false), new ScoreBoundary(3, true)).get()); -// // In range negative infinity to 3 (inclusive) -// assertEquals(3, client.zcount(key1, NEGATIVE_INFINITY, new ScoreBoundary(3, true)).get()); -// // Incorrect range start > end -// assertEquals(0, client.zcount(key1, POSITIVE_INFINITY, new ScoreBoundary(3, true)).get()); -// // Non-existing key -// assertEquals(0, client.zcount("non_existing_key", NEGATIVE_INFINITY, POSITIVE_INFINITY).get()); - -// // Key exists, but it is not a set -// assertEquals(OK, client.set(key2, "value").get()); -// ExecutionException executionException = -// assertThrows( -// ExecutionException.class, -// () -> client.zcount(key2, NEGATIVE_INFINITY, POSITIVE_INFINITY).get()); -// assertInstanceOf(RequestException.class, executionException.getCause()); From 0f7df19845c1e3f0931a5fc0b5e1cf54b4ecf71c Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Thu, 9 Jan 2025 09:48:55 -0800 Subject: [PATCH 06/12] Go: changelog.md file updated Signed-off-by: Prateek Kumar --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c724e6c52..5ffc348436 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ #### Changes - +* Go: Add Zcount command ([#2930](https://github.com/valkey-io/valkey-glide/pull/2930)) * Go: Add `HScan` command ([#2917](https://github.com/valkey-io/valkey-glide/pull/2917)) * Java, Node, Python: Add transaction commands for JSON module ([#2862](https://github.com/valkey-io/valkey-glide/pull/2862)) * Go: Add HINCRBY command ([#2847](https://github.com/valkey-io/valkey-glide/pull/2847)) From 6c66640be38592cc96ac6232254b5a96e7c430be Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Thu, 9 Jan 2025 09:58:28 -0800 Subject: [PATCH 07/12] Go: Zcount documentation updated Signed-off-by: Prateek Kumar --- go/api/options/zcount_options.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/go/api/options/zcount_options.go b/go/api/options/zcount_options.go index 1a79e8d9e9..e82b0d6650 100644 --- a/go/api/options/zcount_options.go +++ b/go/api/options/zcount_options.go @@ -4,6 +4,7 @@ package options import "github.com/valkey-io/valkey-glide/go/glide/utils" +// The common interface for representing all the range type for Zcount command. type ScoreRange interface { ToArgs() ([]string, error) } @@ -19,14 +20,17 @@ const ( NegativeInfinity InfBoundary = "-inf" ) +// This struct represents the infinity boundary for a score range. type InfScoreBound struct { value InfBoundary } +// Create a new infinite score boundary func NewInfScoreBoundBuilder() *InfScoreBound { return &InfScoreBound{} } +// The value of the infinite score bound. func (infScoreBound *InfScoreBound) SetValue(value InfBoundary) *InfScoreBound { infScoreBound.value = value return infScoreBound @@ -38,20 +42,24 @@ func (infScoreBound *InfScoreBound) ToArgs() ([]string, error) { return args, nil } +// This struct represents score boundary for a bound. type ScoreBoundary struct { bound float64 isInclusive bool } +// Create a new score boundary. func NewScoreBoundaryBuilder() *ScoreBoundary { return &ScoreBoundary{isInclusive: true} } +// Set the bound for a score boundary. func (scoreBoundary *ScoreBoundary) SetBound(bound float64) *ScoreBoundary { scoreBoundary.bound = bound return scoreBoundary } +// Set if the bound for a score boundary is inclusive or not inclusive in the boundary. func (scoreBoundary *ScoreBoundary) SetIsInclusive(isInclusive bool) *ScoreBoundary { scoreBoundary.isInclusive = isInclusive return scoreBoundary @@ -67,20 +75,24 @@ func (scoreBoundary *ScoreBoundary) ToArgs() ([]string, error) { return args, nil } +// This struct represents the min and max boundary for the Zcount command. type ZCountRange struct { min ScoreRange max ScoreRange } +// Create a new Zcount range. func NewZCountRangeBuilder() *ZCountRange { return &ZCountRange{} } +// Set the minimum value for the Zcount command range. func (zCountRange *ZCountRange) SetMin(min ScoreRange) *ZCountRange { zCountRange.min = min return zCountRange } +// Set the maximum value for the Zcount command range. func (zCountRange *ZCountRange) SetMax(max ScoreRange) *ZCountRange { zCountRange.max = max return zCountRange From acd4f7d32d998769ec27bf5f4742de1e6453a099 Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Thu, 9 Jan 2025 10:09:29 -0800 Subject: [PATCH 08/12] Go: Zcount fix formatting Signed-off-by: Prateek Kumar --- go/integTest/shared_commands_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index 478cec6b3b..64ec11d949 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -4546,12 +4546,12 @@ func (suite *GlideTestSuite) TestZCount() { assert.Equal(t, int64(0), zCountResult.Value()) // Key exists, but it is not a set - setResult, err := client.Set(key2, "value") + setResult, _ := client.Set(key2, "value") assert.Equal(t, setResult.Value(), "OK") zCountRange = options.NewZCountRangeBuilder() zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) zCountRange.SetMax(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) - zCountResult, err = client.ZCount(key2, zCountRange) + _, err = client.ZCount(key2, zCountRange) assert.NotNil(t, err) assert.IsType(suite.T(), &api.RequestError{}, err) }) From e187bf76d9666cecc0f00be1f68e60826c8d51e6 Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Thu, 9 Jan 2025 12:08:42 -0800 Subject: [PATCH 09/12] Update ZCountRange builder Signed-off-by: Prateek Kumar --- go/api/options/zcount_options.go | 13 ++------ go/integTest/shared_commands_test.go | 49 ++++++++++++++++------------ 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/go/api/options/zcount_options.go b/go/api/options/zcount_options.go index e82b0d6650..48c764c965 100644 --- a/go/api/options/zcount_options.go +++ b/go/api/options/zcount_options.go @@ -82,18 +82,9 @@ type ZCountRange struct { } // Create a new Zcount range. -func NewZCountRangeBuilder() *ZCountRange { - return &ZCountRange{} -} - -// Set the minimum value for the Zcount command range. -func (zCountRange *ZCountRange) SetMin(min ScoreRange) *ZCountRange { +func NewZCountRangeBuilder(min ScoreRange, max ScoreRange) *ZCountRange { + zCountRange := &ZCountRange{} zCountRange.min = min - return zCountRange -} - -// Set the maximum value for the Zcount command range. -func (zCountRange *ZCountRange) SetMax(max ScoreRange) *ZCountRange { zCountRange.max = max return zCountRange } diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index 64ec11d949..8f8c072602 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -4500,47 +4500,53 @@ func (suite *GlideTestSuite) TestZCount() { assert.Equal(t, int64(3), res1.Value()) // In range negative to positive infinity. - zCountRange := options.NewZCountRangeBuilder() - zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) - zCountRange.SetMax(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) + zCountRange := options.NewZCountRangeBuilder( + options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity), + options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity), + ) zCountResult, err := client.ZCount(key1, zCountRange) assert.Nil(t, err) assert.Equal(t, int64(3), zCountResult.Value()) - zCountRange = options.NewZCountRangeBuilder() - zCountRange.SetMin(options.NewScoreBoundaryBuilder().SetBound(math.Inf(-1))) - zCountRange.SetMax(options.NewScoreBoundaryBuilder().SetBound(math.Inf(+1))) + zCountRange = options.NewZCountRangeBuilder( + options.NewScoreBoundaryBuilder().SetBound(math.Inf(-1)), + options.NewScoreBoundaryBuilder().SetBound(math.Inf(+1)), + ) zCountResult, err = client.ZCount(key1, zCountRange) assert.Nil(t, err) assert.Equal(t, int64(3), zCountResult.Value()) // In range 1 (exclusive) to 3 (inclusive) - zCountRange = options.NewZCountRangeBuilder() - zCountRange.SetMin(options.NewScoreBoundaryBuilder().SetBound(1).SetIsInclusive(false)) - zCountRange.SetMax(options.NewScoreBoundaryBuilder().SetBound(3).SetIsInclusive(true)) + zCountRange = options.NewZCountRangeBuilder( + options.NewScoreBoundaryBuilder().SetBound(1).SetIsInclusive(false), + options.NewScoreBoundaryBuilder().SetBound(3).SetIsInclusive(true), + ) zCountResult, err = client.ZCount(key1, zCountRange) assert.Nil(t, err) assert.Equal(t, int64(2), zCountResult.Value()) // In range negative infinity to 3 (inclusive) - zCountRange = options.NewZCountRangeBuilder() - zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) - zCountRange.SetMax(options.NewScoreBoundaryBuilder().SetBound(3).SetIsInclusive(true)) + zCountRange = options.NewZCountRangeBuilder( + options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity), + options.NewScoreBoundaryBuilder().SetBound(3).SetIsInclusive(true), + ) zCountResult, err = client.ZCount(key1, zCountRange) assert.Nil(t, err) assert.Equal(t, int64(3), zCountResult.Value()) // Incorrect range start > end - zCountRange = options.NewZCountRangeBuilder() - zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) - zCountRange.SetMax(options.NewScoreBoundaryBuilder().SetBound(3).SetIsInclusive(true)) + zCountRange = options.NewZCountRangeBuilder( + options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity), + options.NewScoreBoundaryBuilder().SetBound(3).SetIsInclusive(true), + ) zCountResult, err = client.ZCount(key1, zCountRange) assert.Nil(t, err) assert.Equal(t, int64(0), zCountResult.Value()) // Non-existing key - zCountRange = options.NewZCountRangeBuilder() - zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) - zCountRange.SetMax(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) + zCountRange = options.NewZCountRangeBuilder( + options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity), + options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity), + ) zCountResult, err = client.ZCount("non_existing_key", zCountRange) assert.Nil(t, err) assert.Equal(t, int64(0), zCountResult.Value()) @@ -4548,9 +4554,10 @@ func (suite *GlideTestSuite) TestZCount() { // Key exists, but it is not a set setResult, _ := client.Set(key2, "value") assert.Equal(t, setResult.Value(), "OK") - zCountRange = options.NewZCountRangeBuilder() - zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) - zCountRange.SetMax(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) + zCountRange = options.NewZCountRangeBuilder( + options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity), + options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity), + ) _, err = client.ZCount(key2, zCountRange) assert.NotNil(t, err) assert.IsType(suite.T(), &api.RequestError{}, err) From ee3ff0e109afb136a5ae5d6b06d2b2c5c9daa5bd Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Thu, 9 Jan 2025 18:12:13 -0800 Subject: [PATCH 10/12] Go: Review comments fixed Signed-off-by: Prateek Kumar --- go/api/options/zcount_options.go | 14 +++----------- go/integTest/shared_commands_test.go | 16 ++++++++-------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/go/api/options/zcount_options.go b/go/api/options/zcount_options.go index 48c764c965..ac9f366ddd 100644 --- a/go/api/options/zcount_options.go +++ b/go/api/options/zcount_options.go @@ -26,20 +26,12 @@ type InfScoreBound struct { } // Create a new infinite score boundary -func NewInfScoreBoundBuilder() *InfScoreBound { - return &InfScoreBound{} -} - -// The value of the infinite score bound. -func (infScoreBound *InfScoreBound) SetValue(value InfBoundary) *InfScoreBound { - infScoreBound.value = value - return infScoreBound +func NewInfScoreBoundBuilder(value InfBoundary) *InfScoreBound { + return &InfScoreBound{value: value} } func (infScoreBound *InfScoreBound) ToArgs() ([]string, error) { - args := []string{} - args = append(args, string(infScoreBound.value)) - return args, nil + return []string{string(infScoreBound.value)}, nil } // This struct represents score boundary for a bound. diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index 8f8c072602..cd8c0e12d1 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -4501,8 +4501,8 @@ func (suite *GlideTestSuite) TestZCount() { // In range negative to positive infinity. zCountRange := options.NewZCountRangeBuilder( - options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity), - options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity), + options.NewInfScoreBoundBuilder(options.NegativeInfinity), + options.NewInfScoreBoundBuilder(options.PositiveInfinity), ) zCountResult, err := client.ZCount(key1, zCountRange) assert.Nil(t, err) @@ -4526,7 +4526,7 @@ func (suite *GlideTestSuite) TestZCount() { // In range negative infinity to 3 (inclusive) zCountRange = options.NewZCountRangeBuilder( - options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity), + options.NewInfScoreBoundBuilder(options.NegativeInfinity), options.NewScoreBoundaryBuilder().SetBound(3).SetIsInclusive(true), ) zCountResult, err = client.ZCount(key1, zCountRange) @@ -4535,7 +4535,7 @@ func (suite *GlideTestSuite) TestZCount() { // Incorrect range start > end zCountRange = options.NewZCountRangeBuilder( - options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity), + options.NewInfScoreBoundBuilder(options.PositiveInfinity), options.NewScoreBoundaryBuilder().SetBound(3).SetIsInclusive(true), ) zCountResult, err = client.ZCount(key1, zCountRange) @@ -4544,8 +4544,8 @@ func (suite *GlideTestSuite) TestZCount() { // Non-existing key zCountRange = options.NewZCountRangeBuilder( - options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity), - options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity), + options.NewInfScoreBoundBuilder(options.NegativeInfinity), + options.NewInfScoreBoundBuilder(options.PositiveInfinity), ) zCountResult, err = client.ZCount("non_existing_key", zCountRange) assert.Nil(t, err) @@ -4555,8 +4555,8 @@ func (suite *GlideTestSuite) TestZCount() { setResult, _ := client.Set(key2, "value") assert.Equal(t, setResult.Value(), "OK") zCountRange = options.NewZCountRangeBuilder( - options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity), - options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity), + options.NewInfScoreBoundBuilder(options.NegativeInfinity), + options.NewInfScoreBoundBuilder(options.PositiveInfinity), ) _, err = client.ZCount(key2, zCountRange) assert.NotNil(t, err) From dee504343fa544659f26821678076b366f476935 Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Thu, 9 Jan 2025 18:13:54 -0800 Subject: [PATCH 11/12] Go: Fix review comments Signed-off-by: Prateek Kumar --- go/api/options/zcount_options.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/go/api/options/zcount_options.go b/go/api/options/zcount_options.go index ac9f366ddd..e225be6f23 100644 --- a/go/api/options/zcount_options.go +++ b/go/api/options/zcount_options.go @@ -75,10 +75,7 @@ type ZCountRange struct { // Create a new Zcount range. func NewZCountRangeBuilder(min ScoreRange, max ScoreRange) *ZCountRange { - zCountRange := &ZCountRange{} - zCountRange.min = min - zCountRange.max = max - return zCountRange + return &ZCountRange{min, max} } func (zCountRange *ZCountRange) ToArgs() ([]string, error) { From eb5bd11b4ee2bfb5a6435b1ffd7cd510216fb7ec Mon Sep 17 00:00:00 2001 From: Prateek Kumar Date: Thu, 9 Jan 2025 18:16:37 -0800 Subject: [PATCH 12/12] Go: review comments fixed Signed-off-by: Prateek Kumar --- go/api/sorted_set_commands.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/go/api/sorted_set_commands.go b/go/api/sorted_set_commands.go index e77664390b..032c794436 100644 --- a/go/api/sorted_set_commands.go +++ b/go/api/sorted_set_commands.go @@ -280,9 +280,10 @@ type SortedSetCommands interface { // key1 := uuid.NewString() // membersScores := map[string]float64{"one": 1.0, "two": 2.0, "three": 3.0 } // zAddResult, err := client.ZAdd(key1, membersScores) - // zCountRange := options.NewZCountRangeBuilder() - // zCountRange.SetMin(options.NewInfScoreBoundBuilder().SetValue(options.NegativeInfinity)) - // zCountRange.SetMax(options.NewInfScoreBoundBuilder().SetValue(options.PositiveInfinity)) + // zCountRange := options.NewZCountRangeBuilder( + // options.NewInfScoreBoundBuilder(options.NegativeInfinity), + // options.NewInfScoreBoundBuilder(options.PositiveInfinity), + // ) // zCountResult, err := client.ZCount(key1, zCountRange) // if err!= nil { // // Print err