Skip to content

feat(cmd): support for adding byte,bit parameters to the bitpos command #2498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ type Cmdable interface {
BitOpXor(ctx context.Context, destKey string, keys ...string) *IntCmd
BitOpNot(ctx context.Context, destKey string, key string) *IntCmd
BitPos(ctx context.Context, key string, bit int64, pos ...int64) *IntCmd
BitPosSpan(ctx context.Context, key string, bit int8, start, end int64, span string) *IntCmd
BitField(ctx context.Context, key string, args ...interface{}) *IntSliceCmd

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

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

// BitPosSpan supports the `byte | bit` parameters in redis version 7.0,
// the bitpos command defaults to using byte type for the `start-end` range,
// which means it counts in bytes from start to end. you can set the value
// of "span" to determine the type of `start-end`.
// span = "bit", cmd: bitpos key bit start end bit
// span = "byte", cmd: bitpos key bit start end byte
func (c cmdable) BitPosSpan(ctx context.Context, key string, bit int8, start, end int64, span string) *IntCmd {
cmd := NewIntCmd(ctx, "bitpos", key, bit, start, end, span)
_ = c(ctx, cmd)
return cmd
}

func (c cmdable) BitField(ctx context.Context, key string, args ...interface{}) *IntSliceCmd {
a := make([]interface{}, 0, 2+len(args))
a = append(a, "bitfield")
Expand Down
13 changes: 13 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,19 @@ var _ = Describe("Commands", func() {
Expect(pos).To(Equal(int64(-1)))
})

It("should BitPosSpan", func() {
err := client.Set(ctx, "mykey", "\x00\xff\x00", 0).Err()
Expect(err).NotTo(HaveOccurred())

pos, err := client.BitPosSpan(ctx, "mykey", 0, 1, 3, "byte").Result()
Expect(err).NotTo(HaveOccurred())
Expect(pos).To(Equal(int64(16)))

pos, err = client.BitPosSpan(ctx, "mykey", 0, 1, 3, "bit").Result()
Expect(err).NotTo(HaveOccurred())
Expect(pos).To(Equal(int64(1)))
})

It("should BitField", func() {
nn, err := client.BitField(ctx, "mykey", "INCRBY", "i5", 100, 1, "GET", "u4", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expand Down