Skip to content

Commit 5405540

Browse files
authored
feat(cmd): support for adding byte,bit parameters to the bitpos command (redis#2498)
Signed-off-by: monkey92t <golang@88.com>
1 parent 153a9ef commit 5405540

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

commands.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ type Cmdable interface {
194194
BitOpXor(ctx context.Context, destKey string, keys ...string) *IntCmd
195195
BitOpNot(ctx context.Context, destKey string, key string) *IntCmd
196196
BitPos(ctx context.Context, key string, bit int64, pos ...int64) *IntCmd
197+
BitPosSpan(ctx context.Context, key string, bit int8, start, end int64, span string) *IntCmd
197198
BitField(ctx context.Context, key string, args ...interface{}) *IntSliceCmd
198199

199200
Scan(ctx context.Context, cursor uint64, match string, count int64) *ScanCmd
@@ -1211,6 +1212,8 @@ func (c cmdable) BitOpNot(ctx context.Context, destKey string, key string) *IntC
12111212
return c.bitOp(ctx, "not", destKey, key)
12121213
}
12131214

1215+
// BitPos is an API before Redis version 7.0, cmd: bitpos key bit start end
1216+
// if you need the `byte | bit` parameter, please use `BitPosSpan`.
12141217
func (c cmdable) BitPos(ctx context.Context, key string, bit int64, pos ...int64) *IntCmd {
12151218
args := make([]interface{}, 3+len(pos))
12161219
args[0] = "bitpos"
@@ -1231,6 +1234,18 @@ func (c cmdable) BitPos(ctx context.Context, key string, bit int64, pos ...int64
12311234
return cmd
12321235
}
12331236

1237+
// BitPosSpan supports the `byte | bit` parameters in redis version 7.0,
1238+
// the bitpos command defaults to using byte type for the `start-end` range,
1239+
// which means it counts in bytes from start to end. you can set the value
1240+
// of "span" to determine the type of `start-end`.
1241+
// span = "bit", cmd: bitpos key bit start end bit
1242+
// span = "byte", cmd: bitpos key bit start end byte
1243+
func (c cmdable) BitPosSpan(ctx context.Context, key string, bit int8, start, end int64, span string) *IntCmd {
1244+
cmd := NewIntCmd(ctx, "bitpos", key, bit, start, end, span)
1245+
_ = c(ctx, cmd)
1246+
return cmd
1247+
}
1248+
12341249
func (c cmdable) BitField(ctx context.Context, key string, args ...interface{}) *IntSliceCmd {
12351250
a := make([]interface{}, 0, 2+len(args))
12361251
a = append(a, "bitfield")

commands_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,19 @@ var _ = Describe("Commands", func() {
10911091
Expect(pos).To(Equal(int64(-1)))
10921092
})
10931093

1094+
It("should BitPosSpan", func() {
1095+
err := client.Set(ctx, "mykey", "\x00\xff\x00", 0).Err()
1096+
Expect(err).NotTo(HaveOccurred())
1097+
1098+
pos, err := client.BitPosSpan(ctx, "mykey", 0, 1, 3, "byte").Result()
1099+
Expect(err).NotTo(HaveOccurred())
1100+
Expect(pos).To(Equal(int64(16)))
1101+
1102+
pos, err = client.BitPosSpan(ctx, "mykey", 0, 1, 3, "bit").Result()
1103+
Expect(err).NotTo(HaveOccurred())
1104+
Expect(pos).To(Equal(int64(1)))
1105+
})
1106+
10941107
It("should BitField", func() {
10951108
nn, err := client.BitField(ctx, "mykey", "INCRBY", "i5", 100, 1, "GET", "u4", 0).Result()
10961109
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)