Skip to content

Commit 8d2a022

Browse files
Add support for specifying bitcount unit as byte or bit, byte default (redis#2887)
* Add support for specifying bitcount unit as byte or bit, byte default * Add bitcount test * Test bitcount without unit specified --------- Co-authored-by: wanghongwei5 <[email protected]> Co-authored-by: ofekshenawa <[email protected]>
1 parent 36bab9c commit 8d2a022

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

bitmap_commands.go

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redis
22

33
import (
44
"context"
5+
"errors"
56
)
67

78
type BitMapCmdable interface {
@@ -37,15 +38,28 @@ func (c cmdable) SetBit(ctx context.Context, key string, offset int64, value int
3738

3839
type BitCount struct {
3940
Start, End int64
41+
Unit string // BYTE(default) | BIT
4042
}
4143

44+
const BitCountIndexByte string = "BYTE"
45+
const BitCountIndexBit string = "BIT"
46+
4247
func (c cmdable) BitCount(ctx context.Context, key string, bitCount *BitCount) *IntCmd {
4348
args := []interface{}{"bitcount", key}
4449
if bitCount != nil {
50+
if bitCount.Unit == "" {
51+
bitCount.Unit = "BYTE"
52+
}
53+
if bitCount.Unit != BitCountIndexByte && bitCount.Unit != BitCountIndexBit {
54+
cmd := NewIntCmd(ctx)
55+
cmd.SetErr(errors.New("redis: invalid bitcount index"))
56+
return cmd
57+
}
4558
args = append(
4659
args,
4760
bitCount.Start,
4861
bitCount.End,
62+
string(bitCount.Unit),
4963
)
5064
}
5165
cmd := NewIntCmd(ctx, args...)

bitmap_commands_test.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package redis_test
2+
3+
import (
4+
. "github.com/bsm/ginkgo/v2"
5+
. "github.com/bsm/gomega"
6+
"github.com/redis/go-redis/v9"
7+
)
8+
9+
type bitCountExpected struct {
10+
Start int64
11+
End int64
12+
Expected int64
13+
}
14+
15+
var _ = Describe("BitCountBite", func() {
16+
var client *redis.Client
17+
key := "bit_count_test"
18+
19+
BeforeEach(func() {
20+
client = redis.NewClient(redisOptions())
21+
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
22+
values := []int{0, 1, 0, 0, 1, 0, 1, 0, 1, 1}
23+
for i, v := range values {
24+
cmd := client.SetBit(ctx, key, int64(i), v)
25+
Expect(cmd.Err()).NotTo(HaveOccurred())
26+
}
27+
})
28+
29+
AfterEach(func() {
30+
Expect(client.Close()).NotTo(HaveOccurred())
31+
})
32+
33+
It("bit count bite", func() {
34+
var expected = []bitCountExpected{
35+
{0, 0, 0},
36+
{0, 1, 1},
37+
{0, 2, 1},
38+
{0, 3, 1},
39+
{0, 4, 2},
40+
{0, 5, 2},
41+
{0, 6, 3},
42+
{0, 7, 3},
43+
{0, 8, 4},
44+
{0, 9, 5},
45+
}
46+
47+
for _, e := range expected {
48+
cmd := client.BitCount(ctx, key, &redis.BitCount{Start: e.Start, End: e.End, Unit: redis.BitCountIndexBit})
49+
Expect(cmd.Err()).NotTo(HaveOccurred())
50+
Expect(cmd.Val()).To(Equal(e.Expected))
51+
}
52+
})
53+
})
54+
55+
var _ = Describe("BitCountByte", func() {
56+
var client *redis.Client
57+
key := "bit_count_test"
58+
59+
BeforeEach(func() {
60+
client = redis.NewClient(redisOptions())
61+
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
62+
values := []int{0, 0, 0, 0, 0, 0, 0, 1, 1, 1}
63+
for i, v := range values {
64+
cmd := client.SetBit(ctx, key, int64(i), v)
65+
Expect(cmd.Err()).NotTo(HaveOccurred())
66+
}
67+
})
68+
69+
AfterEach(func() {
70+
Expect(client.Close()).NotTo(HaveOccurred())
71+
})
72+
73+
It("bit count byte", func() {
74+
var expected = []bitCountExpected{
75+
{0, 0, 1},
76+
{0, 1, 3},
77+
}
78+
79+
for _, e := range expected {
80+
cmd := client.BitCount(ctx, key, &redis.BitCount{Start: e.Start, End: e.End, Unit: redis.BitCountIndexByte})
81+
Expect(cmd.Err()).NotTo(HaveOccurred())
82+
Expect(cmd.Val()).To(Equal(e.Expected))
83+
}
84+
})
85+
86+
It("bit count byte with no unit specified", func() {
87+
var expected = []bitCountExpected{
88+
{0, 0, 1},
89+
{0, 1, 3},
90+
}
91+
92+
for _, e := range expected {
93+
cmd := client.BitCount(ctx, key, &redis.BitCount{Start: e.Start, End: e.End})
94+
Expect(cmd.Err()).NotTo(HaveOccurred())
95+
Expect(cmd.Val()).To(Equal(e.Expected))
96+
}
97+
})
98+
})

0 commit comments

Comments
 (0)