Преглед на файлове

redis add bitcount (#483)

Co-authored-by: zhoudeyu <zhoudeyu@xiaoheiban.cn>
Zcc、 преди 3 години
родител
ревизия
72580dee38
променени са 2 файла, в които са добавени 50 реда и са изтрити 0 реда
  1. 17 0
      core/stores/redis/redis.go
  2. 33 0
      core/stores/redis/redis_test.go

+ 17 - 0
core/stores/redis/redis.go

@@ -108,6 +108,23 @@ func (s *Redis) BlpopEx(redisNode RedisNode, key string) (string, bool, error) {
 	return vals[1], true, nil
 }
 
+func (s *Redis) BitCount(key string, start, end int64) (val int64, err error) {
+	err = s.brk.DoWithAcceptable(func() error {
+		conn, err := getRedis(s)
+		if err != nil {
+			return err
+		}
+
+		val, err = conn.BitCount(key, &red.BitCount{
+			Start: start,
+			End:   end,
+		}).Result()
+		return err
+	}, acceptable)
+
+	return
+}
+
 func (s *Redis) Del(keys ...string) (val int, err error) {
 	err = s.brk.DoWithAcceptable(func() error {
 		conn, err := getRedis(s)

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

@@ -341,6 +341,39 @@ func TestRedis_GetBit(t *testing.T) {
 	})
 }
 
+func TestRedis_BitCount(t *testing.T)  {
+	runOnRedis(t, func(client *Redis) {
+		for i := 0; i < 11; i++{
+			err := client.SetBit("key", int64(i), 1)
+			assert.Nil(t, err)
+		}
+
+		_, err := NewRedis(client.Addr,"").BitCount("key",0,-1)
+		assert.NotNil(t, err)
+		val, err := client.BitCount("key", 0,-1)
+		assert.Nil(t, err)
+		assert.Equal(t, int64(11), val)
+
+		val, err = client.BitCount("key", 0,0)
+		assert.Nil(t, err)
+		assert.Equal(t, int64(8), val)
+
+		val, err = client.BitCount("key", 1,1)
+		assert.Nil(t, err)
+		assert.Equal(t, int64(3), val)
+
+		val, err = client.BitCount("key", 0,1)
+		assert.Nil(t, err)
+		assert.Equal(t, int64(11), val)
+
+		val, err = client.BitCount("key", 2,2)
+		assert.Nil(t, err)
+		assert.Equal(t, int64(0), val)
+
+	})
+}
+
+
 func TestRedis_Persist(t *testing.T) {
 	runOnRedis(t, func(client *Redis) {
 		_, err := NewRedis(client.Addr, "").Persist("key")