Selaa lähdekoodia

support zunionstore in redis (#410)

Kevin Wan 3 vuotta sitten
vanhempi
commit
a784982030
2 muutettua tiedostoa jossa 30 lisäystä ja 1 poistoa
  1. 16 1
      core/stores/redis/redis.go
  2. 14 0
      core/stores/redis/redis_test.go

+ 16 - 1
core/stores/redis/redis.go

@@ -51,7 +51,8 @@ type (
 	Pipeliner = red.Pipeliner
 
 	// Z represents sorted set member.
-	Z = red.Z
+	Z      = red.Z
+	ZStore = red.ZStore
 
 	IntCmd   = red.IntCmd
 	FloatCmd = red.FloatCmd
@@ -1394,6 +1395,20 @@ func (s *Redis) Zrevrank(key string, field string) (val int64, err error) {
 	return
 }
 
+func (s *Redis) Zunionstore(dest string, store ZStore, keys ...string) (val int64, err error) {
+	err = s.brk.DoWithAcceptable(func() error {
+		conn, err := getRedis(s)
+		if err != nil {
+			return err
+		}
+
+		val, err = conn.ZUnionStore(dest, store, keys...).Result()
+		return err
+	}, acceptable)
+
+	return
+}
+
 func (s *Redis) String() string {
 	return s.Addr
 }

+ 14 - 0
core/stores/redis/redis_test.go

@@ -715,6 +715,20 @@ func TestRedis_SortedSet(t *testing.T) {
 		assert.Equal(t, 0, len(pairs))
 		_, err = NewRedis(client.Addr, "").Zrevrank("key", "value")
 		assert.NotNil(t, err)
+		client.Zadd("second", 2, "aa")
+		client.Zadd("third", 3, "bbb")
+		val, err = client.Zunionstore("union", ZStore{
+			Weights:   []float64{1, 2},
+			Aggregate: "SUM",
+		}, "second", "third")
+		assert.Nil(t, err)
+		assert.Equal(t, int64(2), val)
+		vals, err = client.Zrange("union", 0, 10000)
+		assert.Nil(t, err)
+		assert.EqualValues(t, []string{"aa", "bbb"}, vals)
+		ival, err := client.Zcard("union")
+		assert.Nil(t, err)
+		assert.Equal(t, 2, ival)
 	})
 }