From f6fc23d2fa45a343da89e81a24eb1176136b5712 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Fri, 9 Aug 2019 16:23:56 +0300 Subject: [PATCH] Move key to ZStore --- command.go | 2 +- commands.go | 22 +++++++++++----------- commands_test.go | 20 ++++++++++++-------- redis.go | 2 +- ring.go | 4 ++-- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/command.go b/command.go index 833a12b..c9665d8 100644 --- a/command.go +++ b/command.go @@ -571,7 +571,6 @@ func (cmd *BoolCmd) readReply(rd *proto.Reader) error { v, cmd.err = rd.ReadReply(nil) // `SET key value NX` returns nil when key already exists. But // `SETNX key value` returns bool (0/1). So convert nil to bool. - // TODO: is this okay? if cmd.err == Nil { cmd.val = false cmd.err = nil @@ -1655,6 +1654,7 @@ func newGeoLocationSliceParser(q *GeoRadiusQuery) proto.MultiBulkParse { Name: vv, }) case *GeoLocation: + //TODO: avoid copying locs = append(locs, *vv) default: return nil, fmt.Errorf("got %T, expected string or *GeoLocation", v) diff --git a/commands.go b/commands.go index 51bad63..9eb5001 100644 --- a/commands.go +++ b/commands.go @@ -201,7 +201,7 @@ type Cmdable interface { ZCount(key, min, max string) *IntCmd ZLexCount(key, min, max string) *IntCmd ZIncrBy(key string, increment float64, member string) *FloatCmd - ZInterStore(destination string, store *ZStore, keys ...string) *IntCmd + ZInterStore(destination string, store *ZStore) *IntCmd ZPopMax(key string, count ...int64) *ZSliceCmd ZPopMin(key string, count ...int64) *ZSliceCmd ZRange(key string, start, stop int64) *StringSliceCmd @@ -221,7 +221,7 @@ type Cmdable interface { ZRevRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd ZRevRank(key, member string) *IntCmd ZScore(key, member string) *FloatCmd - ZUnionStore(dest string, store *ZStore, keys ...string) *IntCmd + ZUnionStore(dest string, store *ZStore) *IntCmd PFAdd(key string, els ...interface{}) *IntCmd PFCount(keys ...string) *IntCmd PFMerge(dest string, keys ...string) *StatusCmd @@ -1587,6 +1587,7 @@ type ZWithKey struct { // ZStore is used as an arg to ZInterStore and ZUnionStore. type ZStore struct { + Keys []string Weights []float64 // Can be SUM, MIN or MAX. Aggregate string @@ -1736,12 +1737,12 @@ func (c cmdable) ZIncrBy(key string, increment float64, member string) *FloatCmd return cmd } -func (c cmdable) ZInterStore(destination string, store *ZStore, keys ...string) *IntCmd { - args := make([]interface{}, 3+len(keys)) +func (c cmdable) ZInterStore(destination string, store *ZStore) *IntCmd { + args := make([]interface{}, 3+len(store.Keys)) args[0] = "zinterstore" args[1] = destination - args[2] = len(keys) - for i, key := range keys { + args[2] = len(store.Keys) + for i, key := range store.Keys { args[3+i] = key } if len(store.Weights) > 0 { @@ -1970,13 +1971,12 @@ func (c cmdable) ZScore(key, member string) *FloatCmd { return cmd } -// TODO: move keys to ZStore? -func (c cmdable) ZUnionStore(dest string, store *ZStore, keys ...string) *IntCmd { - args := make([]interface{}, 3+len(keys)) +func (c cmdable) ZUnionStore(dest string, store *ZStore) *IntCmd { + args := make([]interface{}, 3+len(store.Keys)) args[0] = "zunionstore" args[1] = dest - args[2] = len(keys) - for i, key := range keys { + args[2] = len(store.Keys) + for i, key := range store.Keys { args[3+i] = key } if len(store.Weights) > 0 { diff --git a/commands_test.go b/commands_test.go index c84452f..70c448a 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2720,10 +2720,12 @@ var _ = Describe("Commands", func() { err = client.ZAdd("zset3", &redis.Z{Score: 3, Member: "two"}).Err() Expect(err).NotTo(HaveOccurred()) - zInterStore := client.ZInterStore( - "out", &redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2") - Expect(zInterStore.Err()).NotTo(HaveOccurred()) - Expect(zInterStore.Val()).To(Equal(int64(2))) + n, err := client.ZInterStore("out", &redis.ZStore{ + Keys: []string{"zset1", "zset2"}, + Weights: []float64{2, 3}, + }).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(n).To(Equal(int64(2))) vals, err := client.ZRangeWithScores("out", 0, -1).Result() Expect(err).NotTo(HaveOccurred()) @@ -3370,10 +3372,12 @@ var _ = Describe("Commands", func() { err = client.ZAdd("zset2", &redis.Z{Score: 3, Member: "three"}).Err() Expect(err).NotTo(HaveOccurred()) - zUnionStore := client.ZUnionStore( - "out", &redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2") - Expect(zUnionStore.Err()).NotTo(HaveOccurred()) - Expect(zUnionStore.Val()).To(Equal(int64(3))) + n, err := client.ZUnionStore("out", &redis.ZStore{ + Keys: []string{"zset1", "zset2"}, + Weights: []float64{2, 3}, + }).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(n).To(Equal(int64(3))) val, err := client.ZRangeWithScores("out", 0, -1).Result() Expect(err).NotTo(HaveOccurred()) diff --git a/redis.go b/redis.go index 6634b80..6058b4a 100644 --- a/redis.go +++ b/redis.go @@ -11,7 +11,7 @@ import ( "github.com/go-redis/redis/v7/internal/proto" ) -// Nil reply Redis returns when key does not exist. +// Nil reply returned by Redis when key does not exist. const Nil = proto.Nil func SetLogger(logger *log.Logger) { diff --git a/ring.go b/ring.go index 1aeb35a..9a53d04 100644 --- a/ring.go +++ b/ring.go @@ -460,7 +460,7 @@ func (c *Ring) Subscribe(channels ...string) *PubSub { shard, err := c.shards.GetByKey(channels[0]) if err != nil { - // TODO: return PubSub with sticky error + //TODO: return PubSub with sticky error panic(err) } return shard.Client.Subscribe(channels...) @@ -474,7 +474,7 @@ func (c *Ring) PSubscribe(channels ...string) *PubSub { shard, err := c.shards.GetByKey(channels[0]) if err != nil { - // TODO: return PubSub with sticky error + //TODO: return PubSub with sticky error panic(err) } return shard.Client.PSubscribe(channels...)