Skip to content

Commit

Permalink
feat: add BitFieldRO, HScanNoValues, and ClientInfo to rueidiscompat (#…
Browse files Browse the repository at this point in the history
…707)

Signed-off-by: Rueian <[email protected]>
  • Loading branch information
rueian authored Dec 21, 2024
1 parent f55f3aa commit 30e598c
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 5 deletions.
41 changes: 38 additions & 3 deletions rueidiscompat/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ type CoreCmdable interface {
BitPos(ctx context.Context, key string, bit int64, pos ...int64) *IntCmd
BitPosSpan(ctx context.Context, key string, bit int64, start, end int64, span string) *IntCmd
BitField(ctx context.Context, key string, args ...any) *IntSliceCmd
// TODO BitFieldRO(ctx context.Context, key string, values ...interface{}) *IntSliceCmd
BitFieldRO(ctx context.Context, key string, values ...any) *IntSliceCmd

Scan(ctx context.Context, cursor uint64, match string, count int64) *ScanCmd
ScanType(ctx context.Context, cursor uint64, match string, count int64, keyType string) *ScanCmd
SScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
HScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
// TODO HScanNoValues(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
HScanNoValues(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
ZScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd

HDel(ctx context.Context, key string, fields ...string) *IntCmd
Expand Down Expand Up @@ -320,7 +320,7 @@ type CoreCmdable interface {
ClientKill(ctx context.Context, ipPort string) *StatusCmd
ClientKillByFilter(ctx context.Context, keys ...string) *IntCmd
ClientList(ctx context.Context) *StringCmd
// TODO ClientInfo(ctx context.Context) *ClientInfoCmd
ClientInfo(ctx context.Context) *ClientInfoCmd
ClientPause(ctx context.Context, dur time.Duration) *BoolCmd
ClientUnpause(ctx context.Context) *BoolCmd
ClientID(ctx context.Context) *IntCmd
Expand Down Expand Up @@ -1245,6 +1245,15 @@ func (c *Compat) BitField(ctx context.Context, key string, args ...any) *IntSlic
return newIntSliceCmd(resp)
}

func (c *Compat) BitFieldRO(ctx context.Context, key string, args ...any) *IntSliceCmd {
cmd := c.client.B().Arbitrary("BITFIELD_RO").Keys(key)
for i := 0; i < len(args); i += 2 {
cmd = cmd.Args("GET", str(args[i]), str(args[i+1]))
}
resp := c.client.Do(ctx, cmd.ReadOnly())
return newIntSliceCmd(resp)
}

func (c *Compat) Scan(ctx context.Context, cursor uint64, match string, count int64) *ScanCmd {
cmd := c.client.B().Arbitrary("SCAN", strconv.FormatInt(int64(cursor), 10))
if match != "" {
Expand Down Expand Up @@ -1293,6 +1302,19 @@ func (c *Compat) HScan(ctx context.Context, key string, cursor uint64, match str
return newScanCmd(resp)
}

func (c *Compat) HScanNoValues(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd {
cmd := c.client.B().Arbitrary("HSCAN").Keys(key).Args(strconv.FormatInt(int64(cursor), 10))
if match != "" {
cmd = cmd.Args("MATCH", match)
}
if count > 0 {
cmd = cmd.Args("COUNT", strconv.FormatInt(count, 10))
}
cmd = cmd.Args("NOVALUES")
resp := c.client.Do(ctx, cmd.ReadOnly())
return newScanCmd(resp)
}

func (c *Compat) ZScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd {
cmd := c.client.B().Arbitrary("ZSCAN").Keys(key).Args(strconv.FormatInt(int64(cursor), 10))
if match != "" {
Expand Down Expand Up @@ -2617,6 +2639,10 @@ func (c *Compat) ClientUnblockWithError(ctx context.Context, id int64) *IntCmd {
return newIntCmd(c.client.Do(ctx, c.client.B().ClientUnblock().ClientId(id).Error().Build()))
}

func (c *Compat) ClientInfo(ctx context.Context) *ClientInfoCmd {
return newClientInfoCmd(c.client.Do(ctx, c.client.B().ClientInfo().Build()))
}

func (c *Compat) ConfigGet(ctx context.Context, parameter string) *StringStringMapCmd {
cmd := c.client.B().ConfigGet().Parameter(parameter).Build()
resp := c.client.Do(ctx, cmd)
Expand Down Expand Up @@ -5660,6 +5686,15 @@ func (c CacheCompat) BitPosSpan(ctx context.Context, key string, bit, start, end
return newIntCmd(resp)
}

func (c CacheCompat) BitFieldRO(ctx context.Context, key string, args ...any) *IntSliceCmd {
cmd := c.client.B().Arbitrary("BITFIELD_RO").Keys(key)
for i := 0; i < len(args); i += 2 {
cmd = cmd.Args("GET", str(args[i]), str(args[i+1]))
}
resp := c.client.DoCache(ctx, rueidis.Cacheable(cmd.ReadOnly()), c.ttl)
return newIntSliceCmd(resp)
}

func (c CacheCompat) EvalRO(ctx context.Context, script string, keys []string, args ...any) *Cmd {
cmd := c.client.B().EvalRo().Script(script).Numkeys(int64(len(keys))).Key(keys...).Arg(argsToSlice(args)...).Cache()
return newCmd(c.client.DoCache(ctx, cmd, c.ttl))
Expand Down
55 changes: 55 additions & 0 deletions rueidiscompat/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,25 @@ func testAdapter(resp3 bool) {
Expect(cursor).NotTo(BeZero())
})

if resp3 {
It("should HScan without values", func() {
for i := 0; i < 1000; i++ {
sadd := adapter.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello")
Expect(sadd.Err()).NotTo(HaveOccurred())
}

keys, cursor, err := adapter.HScanNoValues(ctx, "myhash", 0, "", 0).Result()
Expect(err).NotTo(HaveOccurred())
// If we don't get at least two items back, it's really strange.
Expect(cursor).To(BeNumerically(">=", 2))
Expect(len(keys)).To(BeNumerically(">=", 2))
Expect(keys[0]).To(HavePrefix("key"))
Expect(keys[1]).To(HavePrefix("key"))
Expect(keys).NotTo(BeEmpty())
Expect(cursor).NotTo(BeZero())
})
}

It("should ZScan", func() {
for i := 0; i < 1000; i++ {
err := adapter.ZAdd(ctx, "myset", Z{
Expand Down Expand Up @@ -1327,6 +1346,22 @@ func testAdapter(resp3 bool) {
Expect(nn).To(Equal([]int64{1, 0}))
})

if resp3 {
It("should BitFieldRO", func() {
nn, err := adapter.BitField(ctx, "mykey", "SET", "u8", 8, 255).Result()
Expect(err).NotTo(HaveOccurred())
Expect(nn).To(Equal([]int64{0}))

nn, err = adapter.BitFieldRO(ctx, "mykey", "u8", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(nn).To(Equal([]int64{0}))

nn, err = adapter.BitFieldRO(ctx, "mykey", "u8", 0, "u4", 8, "u4", 12, "u4", 13).Result()
Expect(err).NotTo(HaveOccurred())
Expect(nn).To(Equal([]int64{0, 15, 15, 14}))
})
}

It("should Decr", func() {
set := adapter.Set(ctx, "key", "10", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expand Down Expand Up @@ -6746,6 +6781,12 @@ func testAdapterCache(resp3 bool) {
Expect(r).To(Equal(int64(0)))
})

It("should ClientInfo", func() {
info, err := adapter.ClientInfo(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(info).NotTo(BeNil())
})

It("ClientPause", func() {
Expect(adapter.ClientPause(ctx, time.Second).Err()).NotTo(HaveOccurred())
})
Expand Down Expand Up @@ -7360,6 +7401,20 @@ func testAdapterCache(resp3 bool) {
Expect(pos).To(Equal(int64(1)))
})

It("should BitFieldRO", func() {
nn, err := adapter.BitField(ctx, "mykey", "SET", "u8", 8, 255).Result()
Expect(err).NotTo(HaveOccurred())
Expect(nn).To(Equal([]int64{0}))

nn, err = adapter.Cache(time.Hour).BitFieldRO(ctx, "mykey", "u8", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(nn).To(Equal([]int64{0}))

nn, err = adapter.Cache(time.Hour).BitFieldRO(ctx, "mykey", "u8", 0, "u4", 8, "u4", 12, "u4", 13).Result()
Expect(err).NotTo(HaveOccurred())
Expect(nn).To(Equal([]int64{0, 15, 15, 14}))
})

Describe("EvalRO", func() {
It("returns keys and values", func() {
vals, err := adapter.Cache(time.Hour).EvalRO(
Expand Down
Loading

0 comments on commit 30e598c

Please sign in to comment.