Skip to content
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

feat: add unit field to bitcount #538

Merged
merged 6 commits into from
May 18, 2024
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
33 changes: 30 additions & 3 deletions rueidiscompat/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ import (
"github.com/redis/rueidis/internal/util"
)

const KeepTTL = -1
const (
KeepTTL = -1
BitCountIndexByte = "BYTE"
BitCountIndexBit = "BIT"
)

type Cmdable interface {
Cache(ttl time.Duration) CacheCompat
Expand Down Expand Up @@ -1093,11 +1097,23 @@ func (c *Compat) SetBit(ctx context.Context, key string, offset int64, value int
}

func (c *Compat) BitCount(ctx context.Context, key string, bitCount *BitCount) *IntCmd {

var resp rueidis.RedisResult
if bitCount == nil {
resp = c.client.Do(ctx, c.client.B().Bitcount().Key(key).Build())
} else {
return newIntCmd(resp)
}

if bitCount.Unit == "" {
resp = c.client.Do(ctx, c.client.B().Bitcount().Key(key).Start(bitCount.Start).End(bitCount.End).Build())
return newIntCmd(resp)
}

switch bitCount.Unit {
case BitCountIndexByte:
resp = c.client.Do(ctx, c.client.B().Bitcount().Key(key).Start(bitCount.Start).End(bitCount.End).Byte().Build())
case BitCountIndexBit:
resp = c.client.Do(ctx, c.client.B().Bitcount().Key(key).Start(bitCount.Start).End(bitCount.End).Bit().Build())
}
return newIntCmd(resp)
}
Expand Down Expand Up @@ -4584,8 +4600,19 @@ func (c CacheCompat) BitCount(ctx context.Context, key string, bitCount *BitCoun
var resp rueidis.RedisResult
if bitCount == nil {
resp = c.client.DoCache(ctx, c.client.B().Bitcount().Key(key).Cache(), c.ttl)
} else {
return newIntCmd(resp)
}

if bitCount.Unit == "" {
resp = c.client.DoCache(ctx, c.client.B().Bitcount().Key(key).Start(bitCount.Start).End(bitCount.End).Cache(), c.ttl)
return newIntCmd(resp)
}

switch bitCount.Unit {
case BitCountIndexByte:
resp = c.client.DoCache(ctx, c.client.B().Bitcount().Key(key).Start(bitCount.Start).End(bitCount.End).Byte().Cache(), c.ttl)
case BitCountIndexBit:
resp = c.client.DoCache(ctx, c.client.B().Bitcount().Key(key).Start(bitCount.Start).End(bitCount.End).Bit().Cache(), c.ttl)
}
return newIntCmd(resp)
}
Expand Down
36 changes: 36 additions & 0 deletions rueidiscompat/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,24 @@ func testAdapter(resp3 bool) {
})
Expect(bitCount.Err()).NotTo(HaveOccurred())
Expect(bitCount.Val()).To(Equal(int64(6)))

if resp3 {
bitCount = adapter.BitCount(ctx, "key", &BitCount{
Start: 1,
End: 1,
Unit: "BYTE",
})
Expect(bitCount.Err()).NotTo(HaveOccurred())
Expect(bitCount.Val()).To(Equal(int64(6)))

bitCount = adapter.BitCount(ctx, "key", &BitCount{
Start: 1,
End: 1,
Unit: "BIT",
})
Expect(bitCount.Err()).NotTo(HaveOccurred())
Expect(bitCount.Val()).To(Equal(int64(1)))
}
})

It("should BitOpAnd", func() {
Expand Down Expand Up @@ -7064,6 +7082,24 @@ func testAdapterCache(resp3 bool) {
})
Expect(bitCount.Err()).NotTo(HaveOccurred())
Expect(bitCount.Val()).To(Equal(int64(6)))

if resp3 {
bitCount = adapter.Cache(time.Hour).BitCount(ctx, "key", &BitCount{
Start: 1,
End: 1,
Unit: "BIT",
})
Expect(bitCount.Err()).NotTo(HaveOccurred())
Expect(bitCount.Val()).To(Equal(int64(1)))

bitCount = adapter.Cache(time.Hour).BitCount(ctx, "key", &BitCount{
Start: 1,
End: 1,
Unit: "BYTE",
})
Expect(bitCount.Err()).NotTo(HaveOccurred())
Expect(bitCount.Val()).To(Equal(int64(6)))
}
})

It("should BitPos", func() {
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 // Stores BIT or BYTE
}

//type BitPos struct {
Expand Down