Skip to content

Commit

Permalink
Move key to ZStore
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Aug 9, 2019
1 parent 22465b7 commit f6fc23d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 12 additions & 8 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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...)
Expand Down

0 comments on commit f6fc23d

Please sign in to comment.