Move key to ZStore

This commit is contained in:
Vladimir Mihailenco 2019-08-09 16:23:56 +03:00
parent 22465b7530
commit f6fc23d2fa
5 changed files with 27 additions and 23 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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())

View File

@ -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) {

View File

@ -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...)