Skip to content

Commit bac33e5

Browse files
authored
Go: Add command ZInterCard (valkey-io#3078)
* Go: Add command ZInterCard Signed-off-by: TJ Zhang <tj.zhang@improving.com>
1 parent 1f56325 commit bac33e5

File tree

6 files changed

+213
-3
lines changed

6 files changed

+213
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Go: Add `ZUNION` ([#3119](https://github.com/valkey-io/valkey-glide/pull/3119))
66
* Go: Add `ZUNIONSTORE` ([#3136](https://github.com/valkey-io/valkey-glide/pull/3136))
77
* Go: Add `XINFO GROUPS` ([#3106](https://github.com/valkey-io/valkey-glide/pull/3106))
8+
* Go: Add `ZInterCard` ([#3078](https://github.com/valkey-io/valkey-glide/issues/3078))
89

910
#### Breaking Changes
1011

@@ -30,7 +31,6 @@
3031
* Node, Python: Add `IFEQ` option ([#2909](https://github.com/valkey-io/valkey-glide/pull/2909), [#2962](https://github.com/valkey-io/valkey-glide/pull/2962))
3132
* Java: Add `IFEQ` option ([#2978](https://github.com/valkey-io/valkey-glide/pull/2978))
3233
* Core: Add AzAffinityReplicasAndPrimary Read Strategy([#2986](https://github.com/valkey-io/valkey-glide/pull/2986))
33-
3434
#### Breaking Changes
3535

3636
#### Fixes

go/api/base_client.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6031,7 +6031,7 @@ func (client *baseClient) ZInterWithScores(
60316031
//
60326032
// Return value:
60336033
//
6034-
// The number of elements in the resulting sorted set stored at <code>destination</code>.
6034+
// The number of elements in the resulting sorted set stored at `destination`.
60356035
//
60366036
// [valkey.io]: https://valkey.io/commands/zinterstore/
60376037
func (client *baseClient) ZInterStore(destination string, keysOrWeightedKeys options.KeysOrWeightedKeys) (int64, error) {
@@ -6060,7 +6060,7 @@ func (client *baseClient) ZInterStore(destination string, keysOrWeightedKeys opt
60606060
//
60616061
// Return value:
60626062
//
6063-
// The number of elements in the resulting sorted set stored at <code>destination</code>.
6063+
// The number of elements in the resulting sorted set stored at `destination`.
60646064
//
60656065
// [valkey.io]: https://valkey.io/commands/zinterstore/
60666066
func (client *baseClient) ZInterStoreWithOptions(
@@ -6330,3 +6330,52 @@ func (client *baseClient) ZUnionStoreWithOptions(
63306330
}
63316331
return handleIntResponse(result)
63326332
}
6333+
6334+
// Returns the cardinality of the intersection of the sorted sets specified by `keys`.
6335+
//
6336+
// See [valkey.io] for details.
6337+
//
6338+
// Parameters:
6339+
//
6340+
// keys - The keys of the sorted sets.
6341+
//
6342+
// Return value:
6343+
//
6344+
// The cardinality of the intersection of the sorted sets.
6345+
//
6346+
// [valkey.io]: https://valkey.io/commands/zintercard/
6347+
func (client *baseClient) ZInterCard(keys []string) (int64, error) {
6348+
return client.ZInterCardWithOptions(keys, nil)
6349+
}
6350+
6351+
// Returns the cardinality of the intersection of the sorted sets specified by `keys`.
6352+
// If the intersection cardinality reaches `options.limit` partway through the computation, the
6353+
// algorithm will exit early and yield `options.limit` as the cardinality.
6354+
//
6355+
// See [valkey.io] for details.
6356+
//
6357+
// Parameters:
6358+
//
6359+
// keys - The keys of the sorted sets.
6360+
// options - The options for the ZInterCard command, see - [options.ZInterCardOptions].
6361+
//
6362+
// Return value:
6363+
//
6364+
// The cardinality of the intersection of the sorted sets.
6365+
//
6366+
// [valkey.io]: https://valkey.io/commands/zintercard/
6367+
func (client *baseClient) ZInterCardWithOptions(keys []string, options *options.ZInterCardOptions) (int64, error) {
6368+
args := append([]string{strconv.Itoa(len(keys))}, keys...)
6369+
if options != nil {
6370+
optionsArgs, err := options.ToArgs()
6371+
if err != nil {
6372+
return defaultIntResponse, err
6373+
}
6374+
args = append(args, optionsArgs...)
6375+
}
6376+
result, err := client.executeCommand(C.ZInterCard, args)
6377+
if err != nil {
6378+
return defaultIntResponse, err
6379+
}
6380+
return handleIntResponse(result)
6381+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
2+
package options
3+
4+
import (
5+
"github.com/valkey-io/valkey-glide/go/utils"
6+
)
7+
8+
// This struct represents the optional arguments for the ZINTER command.
9+
type ZInterCardOptions struct {
10+
limit int64
11+
}
12+
13+
func NewZInterCardOptions() *ZInterCardOptions {
14+
return &ZInterCardOptions{limit: -1}
15+
}
16+
17+
// SetLimit sets the limit for the ZInterCard command.
18+
func (options *ZInterCardOptions) SetLimit(limit int64) *ZInterCardOptions {
19+
options.limit = limit
20+
return options
21+
}
22+
23+
func (options *ZInterCardOptions) ToArgs() ([]string, error) {
24+
args := []string{}
25+
26+
if options.limit != -1 {
27+
args = append(args, LimitKeyword, utils.IntToString(options.limit))
28+
}
29+
30+
return args, nil
31+
}

go/api/sorted_set_commands.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,8 @@ type SortedSetCommands interface {
110110
keysOrWeightedKeys options.KeysOrWeightedKeys,
111111
zUnionOptions *options.ZUnionOptions,
112112
) (int64, error)
113+
114+
ZInterCard(keys []string) (int64, error)
115+
116+
ZInterCardWithOptions(keys []string, options *options.ZInterCardOptions) (int64, error)
113117
}

go/api/sorted_set_commands_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,3 +1527,73 @@ func ExampleGlideClusterClient_ZUnionStoreWithOptions() {
15271527

15281528
// Output: 3
15291529
}
1530+
1531+
func ExampleGlideClient_ZInterCard() {
1532+
var client *GlideClient = getExampleGlideClient() // example helper function
1533+
1534+
client.ZAdd("key1", map[string]float64{"a": 1.0, "b": 2.0, "c": 3.0, "d": 4.0})
1535+
client.ZAdd("key2", map[string]float64{"e": 1.0, "f": 2.0, "g": 3.0, "h": 4.0})
1536+
1537+
res, err := client.ZInterCard([]string{"key1", "key2"})
1538+
if err != nil {
1539+
fmt.Println("Glide example failed with an error: ", err)
1540+
}
1541+
fmt.Println(res)
1542+
1543+
// Output:
1544+
// 3
1545+
}
1546+
1547+
func ExampleGlideClusterClient_ZInterCard() {
1548+
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
1549+
1550+
client.ZAdd("key1", map[string]float64{"a": 1.0, "b": 2.0, "c": 3.0, "d": 4.0})
1551+
client.ZAdd("key2", map[string]float64{"e": 1.0, "f": 2.0, "g": 3.0, "h": 4.0})
1552+
1553+
res, err := client.ZInterCard([]string{"key1", "key2"})
1554+
if err != nil {
1555+
fmt.Println("Glide example failed with an error: ", err)
1556+
}
1557+
fmt.Println(res)
1558+
1559+
// Output:
1560+
// 3
1561+
}
1562+
1563+
func ExampleGlideClient_ZInterCardWithOptions() {
1564+
var client *GlideClient = getExampleGlideClient() // example helper function
1565+
1566+
client.ZAdd("key1", map[string]float64{"a": 1.0, "b": 2.0, "c": 3.0, "d": 4.0})
1567+
client.ZAdd("key2", map[string]float64{"e": 1.0, "f": 2.0, "g": 3.0, "h": 4.0})
1568+
1569+
res, err := client.ZInterCardWithOptions(
1570+
[]string{"key1", "key2"},
1571+
options.NewZInterCardOptions().SetLimit(5),
1572+
)
1573+
if err != nil {
1574+
fmt.Println("Glide example failed with an error: ", err)
1575+
}
1576+
fmt.Println(res)
1577+
1578+
// Output:
1579+
// 3
1580+
}
1581+
1582+
func ExampleGlideClusterClient_ZInterCardWithOptions() {
1583+
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
1584+
1585+
client.ZAdd("key1", map[string]float64{"a": 1.0, "b": 2.0, "c": 3.0, "d": 4.0})
1586+
client.ZAdd("key2", map[string]float64{"e": 1.0, "f": 2.0, "g": 3.0, "h": 4.0})
1587+
1588+
res, err := client.ZInterCardWithOptions(
1589+
[]string{"key1", "key2"},
1590+
options.NewZInterCardOptions().SetLimit(5),
1591+
)
1592+
if err != nil {
1593+
fmt.Println("Glide example failed with an error: ", err)
1594+
}
1595+
fmt.Println(res)
1596+
1597+
// Output:
1598+
// 3
1599+
}

go/integTest/shared_commands_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8836,3 +8836,59 @@ func (suite *GlideTestSuite) TestZUnionStoreAndZUnionStoreWithOptions() {
88368836
assert.IsType(suite.T(), &errors.RequestError{}, err)
88378837
})
88388838
}
8839+
8840+
func (suite *GlideTestSuite) TestZInterCard() {
8841+
suite.SkipIfServerVersionLowerThanBy("7.0.0")
8842+
suite.runWithDefaultClients(func(client api.BaseClient) {
8843+
key1 := "{key}:1-" + uuid.NewString()
8844+
key2 := "{key}:2-" + uuid.NewString()
8845+
key3 := "{key}:3-" + uuid.NewString()
8846+
8847+
membersScores1 := map[string]float64{
8848+
"a": 1.0,
8849+
"b": 2.0,
8850+
"c": 3.0,
8851+
}
8852+
membersScores2 := map[string]float64{
8853+
"b": 1.0,
8854+
"c": 2.0,
8855+
"d": 3.0,
8856+
}
8857+
8858+
zAddResult1, err := client.ZAdd(key1, membersScores1)
8859+
assert.NoError(suite.T(), err)
8860+
assert.Equal(suite.T(), int64(3), zAddResult1)
8861+
8862+
zAddResult2, err := client.ZAdd(key2, membersScores2)
8863+
assert.NoError(suite.T(), err)
8864+
assert.Equal(suite.T(), int64(3), zAddResult2)
8865+
8866+
res, err := client.ZInterCard([]string{key1, key2})
8867+
assert.NoError(suite.T(), err)
8868+
assert.Equal(suite.T(), int64(2), res)
8869+
8870+
res, err = client.ZInterCard([]string{key1, key3})
8871+
assert.NoError(suite.T(), err)
8872+
assert.Equal(suite.T(), int64(0), res)
8873+
8874+
res, err = client.ZInterCardWithOptions([]string{key1, key2}, options.NewZInterCardOptions().SetLimit(0))
8875+
assert.NoError(suite.T(), err)
8876+
assert.Equal(suite.T(), int64(2), res)
8877+
8878+
res, err = client.ZInterCardWithOptions([]string{key1, key2}, options.NewZInterCardOptions().SetLimit(1))
8879+
assert.NoError(suite.T(), err)
8880+
assert.Equal(suite.T(), int64(1), res)
8881+
8882+
res, err = client.ZInterCardWithOptions([]string{key1, key2}, options.NewZInterCardOptions().SetLimit(3))
8883+
assert.NoError(suite.T(), err)
8884+
assert.Equal(suite.T(), int64(2), res)
8885+
8886+
// key exists but not a set
8887+
_, err = client.Set(key3, "bar")
8888+
assert.NoError(suite.T(), err)
8889+
8890+
_, err = client.ZInterCardWithOptions([]string{key1, key3}, options.NewZInterCardOptions().SetLimit(3))
8891+
assert.NotNil(suite.T(), err)
8892+
assert.IsType(suite.T(), &errors.RequestError{}, err)
8893+
})
8894+
}

0 commit comments

Comments
 (0)