Skip to content

Commit 30e598c

Browse files
authored
feat: add BitFieldRO, HScanNoValues, and ClientInfo to rueidiscompat (#707)
Signed-off-by: Rueian <[email protected]>
1 parent f55f3aa commit 30e598c

File tree

5 files changed

+374
-5
lines changed

5 files changed

+374
-5
lines changed

rueidiscompat/adapter.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ type CoreCmdable interface {
138138
BitPos(ctx context.Context, key string, bit int64, pos ...int64) *IntCmd
139139
BitPosSpan(ctx context.Context, key string, bit int64, start, end int64, span string) *IntCmd
140140
BitField(ctx context.Context, key string, args ...any) *IntSliceCmd
141-
// TODO BitFieldRO(ctx context.Context, key string, values ...interface{}) *IntSliceCmd
141+
BitFieldRO(ctx context.Context, key string, values ...any) *IntSliceCmd
142142

143143
Scan(ctx context.Context, cursor uint64, match string, count int64) *ScanCmd
144144
ScanType(ctx context.Context, cursor uint64, match string, count int64, keyType string) *ScanCmd
145145
SScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
146146
HScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
147-
// TODO HScanNoValues(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
147+
HScanNoValues(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
148148
ZScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
149149

150150
HDel(ctx context.Context, key string, fields ...string) *IntCmd
@@ -320,7 +320,7 @@ type CoreCmdable interface {
320320
ClientKill(ctx context.Context, ipPort string) *StatusCmd
321321
ClientKillByFilter(ctx context.Context, keys ...string) *IntCmd
322322
ClientList(ctx context.Context) *StringCmd
323-
// TODO ClientInfo(ctx context.Context) *ClientInfoCmd
323+
ClientInfo(ctx context.Context) *ClientInfoCmd
324324
ClientPause(ctx context.Context, dur time.Duration) *BoolCmd
325325
ClientUnpause(ctx context.Context) *BoolCmd
326326
ClientID(ctx context.Context) *IntCmd
@@ -1245,6 +1245,15 @@ func (c *Compat) BitField(ctx context.Context, key string, args ...any) *IntSlic
12451245
return newIntSliceCmd(resp)
12461246
}
12471247

1248+
func (c *Compat) BitFieldRO(ctx context.Context, key string, args ...any) *IntSliceCmd {
1249+
cmd := c.client.B().Arbitrary("BITFIELD_RO").Keys(key)
1250+
for i := 0; i < len(args); i += 2 {
1251+
cmd = cmd.Args("GET", str(args[i]), str(args[i+1]))
1252+
}
1253+
resp := c.client.Do(ctx, cmd.ReadOnly())
1254+
return newIntSliceCmd(resp)
1255+
}
1256+
12481257
func (c *Compat) Scan(ctx context.Context, cursor uint64, match string, count int64) *ScanCmd {
12491258
cmd := c.client.B().Arbitrary("SCAN", strconv.FormatInt(int64(cursor), 10))
12501259
if match != "" {
@@ -1293,6 +1302,19 @@ func (c *Compat) HScan(ctx context.Context, key string, cursor uint64, match str
12931302
return newScanCmd(resp)
12941303
}
12951304

1305+
func (c *Compat) HScanNoValues(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd {
1306+
cmd := c.client.B().Arbitrary("HSCAN").Keys(key).Args(strconv.FormatInt(int64(cursor), 10))
1307+
if match != "" {
1308+
cmd = cmd.Args("MATCH", match)
1309+
}
1310+
if count > 0 {
1311+
cmd = cmd.Args("COUNT", strconv.FormatInt(count, 10))
1312+
}
1313+
cmd = cmd.Args("NOVALUES")
1314+
resp := c.client.Do(ctx, cmd.ReadOnly())
1315+
return newScanCmd(resp)
1316+
}
1317+
12961318
func (c *Compat) ZScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd {
12971319
cmd := c.client.B().Arbitrary("ZSCAN").Keys(key).Args(strconv.FormatInt(int64(cursor), 10))
12981320
if match != "" {
@@ -2617,6 +2639,10 @@ func (c *Compat) ClientUnblockWithError(ctx context.Context, id int64) *IntCmd {
26172639
return newIntCmd(c.client.Do(ctx, c.client.B().ClientUnblock().ClientId(id).Error().Build()))
26182640
}
26192641

2642+
func (c *Compat) ClientInfo(ctx context.Context) *ClientInfoCmd {
2643+
return newClientInfoCmd(c.client.Do(ctx, c.client.B().ClientInfo().Build()))
2644+
}
2645+
26202646
func (c *Compat) ConfigGet(ctx context.Context, parameter string) *StringStringMapCmd {
26212647
cmd := c.client.B().ConfigGet().Parameter(parameter).Build()
26222648
resp := c.client.Do(ctx, cmd)
@@ -5660,6 +5686,15 @@ func (c CacheCompat) BitPosSpan(ctx context.Context, key string, bit, start, end
56605686
return newIntCmd(resp)
56615687
}
56625688

5689+
func (c CacheCompat) BitFieldRO(ctx context.Context, key string, args ...any) *IntSliceCmd {
5690+
cmd := c.client.B().Arbitrary("BITFIELD_RO").Keys(key)
5691+
for i := 0; i < len(args); i += 2 {
5692+
cmd = cmd.Args("GET", str(args[i]), str(args[i+1]))
5693+
}
5694+
resp := c.client.DoCache(ctx, rueidis.Cacheable(cmd.ReadOnly()), c.ttl)
5695+
return newIntSliceCmd(resp)
5696+
}
5697+
56635698
func (c CacheCompat) EvalRO(ctx context.Context, script string, keys []string, args ...any) *Cmd {
56645699
cmd := c.client.B().EvalRo().Script(script).Numkeys(int64(len(keys))).Key(keys...).Arg(argsToSlice(args)...).Cache()
56655700
return newCmd(c.client.DoCache(ctx, cmd, c.ttl))

rueidiscompat/adapter_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,25 @@ func testAdapter(resp3 bool) {
11141114
Expect(cursor).NotTo(BeZero())
11151115
})
11161116

1117+
if resp3 {
1118+
It("should HScan without values", func() {
1119+
for i := 0; i < 1000; i++ {
1120+
sadd := adapter.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello")
1121+
Expect(sadd.Err()).NotTo(HaveOccurred())
1122+
}
1123+
1124+
keys, cursor, err := adapter.HScanNoValues(ctx, "myhash", 0, "", 0).Result()
1125+
Expect(err).NotTo(HaveOccurred())
1126+
// If we don't get at least two items back, it's really strange.
1127+
Expect(cursor).To(BeNumerically(">=", 2))
1128+
Expect(len(keys)).To(BeNumerically(">=", 2))
1129+
Expect(keys[0]).To(HavePrefix("key"))
1130+
Expect(keys[1]).To(HavePrefix("key"))
1131+
Expect(keys).NotTo(BeEmpty())
1132+
Expect(cursor).NotTo(BeZero())
1133+
})
1134+
}
1135+
11171136
It("should ZScan", func() {
11181137
for i := 0; i < 1000; i++ {
11191138
err := adapter.ZAdd(ctx, "myset", Z{
@@ -1327,6 +1346,22 @@ func testAdapter(resp3 bool) {
13271346
Expect(nn).To(Equal([]int64{1, 0}))
13281347
})
13291348

1349+
if resp3 {
1350+
It("should BitFieldRO", func() {
1351+
nn, err := adapter.BitField(ctx, "mykey", "SET", "u8", 8, 255).Result()
1352+
Expect(err).NotTo(HaveOccurred())
1353+
Expect(nn).To(Equal([]int64{0}))
1354+
1355+
nn, err = adapter.BitFieldRO(ctx, "mykey", "u8", 0).Result()
1356+
Expect(err).NotTo(HaveOccurred())
1357+
Expect(nn).To(Equal([]int64{0}))
1358+
1359+
nn, err = adapter.BitFieldRO(ctx, "mykey", "u8", 0, "u4", 8, "u4", 12, "u4", 13).Result()
1360+
Expect(err).NotTo(HaveOccurred())
1361+
Expect(nn).To(Equal([]int64{0, 15, 15, 14}))
1362+
})
1363+
}
1364+
13301365
It("should Decr", func() {
13311366
set := adapter.Set(ctx, "key", "10", 0)
13321367
Expect(set.Err()).NotTo(HaveOccurred())
@@ -6746,6 +6781,12 @@ func testAdapterCache(resp3 bool) {
67466781
Expect(r).To(Equal(int64(0)))
67476782
})
67486783

6784+
It("should ClientInfo", func() {
6785+
info, err := adapter.ClientInfo(ctx).Result()
6786+
Expect(err).NotTo(HaveOccurred())
6787+
Expect(info).NotTo(BeNil())
6788+
})
6789+
67496790
It("ClientPause", func() {
67506791
Expect(adapter.ClientPause(ctx, time.Second).Err()).NotTo(HaveOccurred())
67516792
})
@@ -7360,6 +7401,20 @@ func testAdapterCache(resp3 bool) {
73607401
Expect(pos).To(Equal(int64(1)))
73617402
})
73627403

7404+
It("should BitFieldRO", func() {
7405+
nn, err := adapter.BitField(ctx, "mykey", "SET", "u8", 8, 255).Result()
7406+
Expect(err).NotTo(HaveOccurred())
7407+
Expect(nn).To(Equal([]int64{0}))
7408+
7409+
nn, err = adapter.Cache(time.Hour).BitFieldRO(ctx, "mykey", "u8", 0).Result()
7410+
Expect(err).NotTo(HaveOccurred())
7411+
Expect(nn).To(Equal([]int64{0}))
7412+
7413+
nn, err = adapter.Cache(time.Hour).BitFieldRO(ctx, "mykey", "u8", 0, "u4", 8, "u4", 12, "u4", 13).Result()
7414+
Expect(err).NotTo(HaveOccurred())
7415+
Expect(nn).To(Equal([]int64{0, 15, 15, 14}))
7416+
})
7417+
73637418
Describe("EvalRO", func() {
73647419
It("returns keys and values", func() {
73657420
vals, err := adapter.Cache(time.Hour).EvalRO(

0 commit comments

Comments
 (0)