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) v, cmd.err = rd.ReadReply(nil)
// `SET key value NX` returns nil when key already exists. But // `SET key value NX` returns nil when key already exists. But
// `SETNX key value` returns bool (0/1). So convert nil to bool. // `SETNX key value` returns bool (0/1). So convert nil to bool.
// TODO: is this okay?
if cmd.err == Nil { if cmd.err == Nil {
cmd.val = false cmd.val = false
cmd.err = nil cmd.err = nil
@ -1655,6 +1654,7 @@ func newGeoLocationSliceParser(q *GeoRadiusQuery) proto.MultiBulkParse {
Name: vv, Name: vv,
}) })
case *GeoLocation: case *GeoLocation:
//TODO: avoid copying
locs = append(locs, *vv) locs = append(locs, *vv)
default: default:
return nil, fmt.Errorf("got %T, expected string or *GeoLocation", v) 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 ZCount(key, min, max string) *IntCmd
ZLexCount(key, min, max string) *IntCmd ZLexCount(key, min, max string) *IntCmd
ZIncrBy(key string, increment float64, member string) *FloatCmd 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 ZPopMax(key string, count ...int64) *ZSliceCmd
ZPopMin(key string, count ...int64) *ZSliceCmd ZPopMin(key string, count ...int64) *ZSliceCmd
ZRange(key string, start, stop int64) *StringSliceCmd ZRange(key string, start, stop int64) *StringSliceCmd
@ -221,7 +221,7 @@ type Cmdable interface {
ZRevRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd ZRevRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd
ZRevRank(key, member string) *IntCmd ZRevRank(key, member string) *IntCmd
ZScore(key, member string) *FloatCmd 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 PFAdd(key string, els ...interface{}) *IntCmd
PFCount(keys ...string) *IntCmd PFCount(keys ...string) *IntCmd
PFMerge(dest string, keys ...string) *StatusCmd PFMerge(dest string, keys ...string) *StatusCmd
@ -1587,6 +1587,7 @@ type ZWithKey struct {
// ZStore is used as an arg to ZInterStore and ZUnionStore. // ZStore is used as an arg to ZInterStore and ZUnionStore.
type ZStore struct { type ZStore struct {
Keys []string
Weights []float64 Weights []float64
// Can be SUM, MIN or MAX. // Can be SUM, MIN or MAX.
Aggregate string Aggregate string
@ -1736,12 +1737,12 @@ func (c cmdable) ZIncrBy(key string, increment float64, member string) *FloatCmd
return cmd return cmd
} }
func (c cmdable) ZInterStore(destination string, store *ZStore, keys ...string) *IntCmd { func (c cmdable) ZInterStore(destination string, store *ZStore) *IntCmd {
args := make([]interface{}, 3+len(keys)) args := make([]interface{}, 3+len(store.Keys))
args[0] = "zinterstore" args[0] = "zinterstore"
args[1] = destination args[1] = destination
args[2] = len(keys) args[2] = len(store.Keys)
for i, key := range keys { for i, key := range store.Keys {
args[3+i] = key args[3+i] = key
} }
if len(store.Weights) > 0 { if len(store.Weights) > 0 {
@ -1970,13 +1971,12 @@ func (c cmdable) ZScore(key, member string) *FloatCmd {
return cmd return cmd
} }
// TODO: move keys to ZStore? func (c cmdable) ZUnionStore(dest string, store *ZStore) *IntCmd {
func (c cmdable) ZUnionStore(dest string, store *ZStore, keys ...string) *IntCmd { args := make([]interface{}, 3+len(store.Keys))
args := make([]interface{}, 3+len(keys))
args[0] = "zunionstore" args[0] = "zunionstore"
args[1] = dest args[1] = dest
args[2] = len(keys) args[2] = len(store.Keys)
for i, key := range keys { for i, key := range store.Keys {
args[3+i] = key args[3+i] = key
} }
if len(store.Weights) > 0 { 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() err = client.ZAdd("zset3", &redis.Z{Score: 3, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
zInterStore := client.ZInterStore( n, err := client.ZInterStore("out", &redis.ZStore{
"out", &redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2") Keys: []string{"zset1", "zset2"},
Expect(zInterStore.Err()).NotTo(HaveOccurred()) Weights: []float64{2, 3},
Expect(zInterStore.Val()).To(Equal(int64(2))) }).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
vals, err := client.ZRangeWithScores("out", 0, -1).Result() vals, err := client.ZRangeWithScores("out", 0, -1).Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -3370,10 +3372,12 @@ var _ = Describe("Commands", func() {
err = client.ZAdd("zset2", &redis.Z{Score: 3, Member: "three"}).Err() err = client.ZAdd("zset2", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
zUnionStore := client.ZUnionStore( n, err := client.ZUnionStore("out", &redis.ZStore{
"out", &redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2") Keys: []string{"zset1", "zset2"},
Expect(zUnionStore.Err()).NotTo(HaveOccurred()) Weights: []float64{2, 3},
Expect(zUnionStore.Val()).To(Equal(int64(3))) }).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(3)))
val, err := client.ZRangeWithScores("out", 0, -1).Result() val, err := client.ZRangeWithScores("out", 0, -1).Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())

View File

@ -11,7 +11,7 @@ import (
"github.com/go-redis/redis/v7/internal/proto" "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 const Nil = proto.Nil
func SetLogger(logger *log.Logger) { func SetLogger(logger *log.Logger) {