Skip to content

Commit

Permalink
add ZDiffStore command (redis#1775)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrChenCode authored Jun 1, 2021
1 parent 74246e0 commit 036605d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ type Cmdable interface {
ZRandMember(ctx context.Context, key string, count int, withScores bool) *StringSliceCmd
ZDiff(ctx context.Context, keys ...string) *StringSliceCmd
ZDiffWithScores(ctx context.Context, keys ...string) *ZSliceCmd
ZDiffStore(ctx context.Context, destination string, keys ...string) *IntCmd

PFAdd(ctx context.Context, key string, els ...interface{}) *IntCmd
PFCount(ctx context.Context, keys ...string) *IntCmd
Expand Down Expand Up @@ -2458,6 +2459,18 @@ func (c cmdable) ZDiffWithScores(ctx context.Context, keys ...string) *ZSliceCmd
return cmd
}

// redis-server version >=6.2.0.
func (c cmdable) ZDiffStore(ctx context.Context, destination string, keys ...string) *IntCmd {
args := make([]interface{}, 0, 3+len(keys))
args = append(args, "zdiffstore", destination, len(keys))
for _, key := range keys {
args = append(args, key)
}
cmd := NewIntCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}

//------------------------------------------------------------------------------

func (c cmdable) PFAdd(ctx context.Context, key string, els ...interface{}) *IntCmd {
Expand Down
25 changes: 25 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4048,6 +4048,31 @@ var _ = Describe("Commands", func() {
},
}))
})

It("should ZDiffStore", func() {
err := client.ZAdd(ctx, "zset1", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
v, err := client.ZDiffStore(ctx, "out1", "zset1", "zset2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal(int64(0)))
v, err = client.ZDiffStore(ctx, "out1", "zset2", "zset1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal(int64(1)))
vals, err := client.ZRangeWithScores(ctx, "out1", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{
Score: 3,
Member: "three",
}}))
})
})

Describe("streams", func() {
Expand Down

0 comments on commit 036605d

Please sign in to comment.