Skip to content

Commit

Permalink
feat: add unit field to bitcount
Browse files Browse the repository at this point in the history
  • Loading branch information
SoulPancake committed Apr 26, 2024
1 parent 80122c0 commit 716daf0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
25 changes: 21 additions & 4 deletions rueidiscompat/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,14 +1093,31 @@ func (c *Compat) SetBit(ctx context.Context, key string, offset int64, value int
return newIntCmd(resp)
}

const (
BitCountIndexByte = "BYTE"
BitCountIndexBit = "BIT"
)

func (c *Compat) BitCount(ctx context.Context, key string, bitCount *BitCount) *IntCmd {
var resp rueidis.RedisResult
ret := &IntCmd{}
if bitCount == nil {
resp = c.client.Do(ctx, c.client.B().Bitcount().Key(key).Build())
resp := c.client.Do(ctx, c.client.B().Bitcount().Key(key).Build())
ret.val, ret.err = resp.ToInt64()
return ret
}

builder := c.client.B().Bitcount().Key(key).Start(bitCount.Start).End(bitCount.End)
var resp rueidis.RedisResult
if bitCount.Unit == BitCountIndexBit {
resp = c.client.Do(ctx, builder.Bit().Build())
} else if bitCount.Unit == BitCountIndexByte {
resp = c.client.Do(ctx, builder.Byte().Build())
} else {
resp = c.client.Do(ctx, c.client.B().Bitcount().Key(key).Start(bitCount.Start).End(bitCount.End).Build())
panic("unsupported unit")
}
return newIntCmd(resp)

ret.val, ret.err = resp.ToInt64()
return ret
}

func (c *Compat) BitOpAnd(ctx context.Context, destKey string, keys ...string) *IntCmd {
Expand Down
1 change: 1 addition & 0 deletions rueidiscompat/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,7 @@ type SetArgs struct {

type BitCount struct {
Start, End int64
Unit string // Added the Unit field to store BYTE or BIT
}

//type BitPos struct {
Expand Down

0 comments on commit 716daf0

Please sign in to comment.