Skip to content

Commit 3b9473a

Browse files
authored
feat: add ModuleLoadex, SlaveOf, ClusterMyShardID to rueidiscompat (#708)
Signed-off-by: Rueian <[email protected]>
1 parent 30e598c commit 3b9473a

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

rueidiscompat/adapter.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ type CoreCmdable interface {
341341
Shutdown(ctx context.Context) *StatusCmd
342342
ShutdownSave(ctx context.Context) *StatusCmd
343343
ShutdownNoSave(ctx context.Context) *StatusCmd
344-
// TODO SlaveOf(ctx context.Context, host, port string) *StatusCmd
344+
SlaveOf(ctx context.Context, host, port string) *StatusCmd
345345
// TODO SlowLogGet(ctx context.Context, num int64) *SlowLogCmd
346346
Time(ctx context.Context) *TimeCmd
347347
DebugObject(ctx context.Context, key string) *StringCmd
@@ -379,7 +379,7 @@ type CoreCmdable interface {
379379
PubSubShardChannels(ctx context.Context, pattern string) *StringSliceCmd
380380
PubSubShardNumSub(ctx context.Context, channels ...string) *StringIntMapCmd
381381

382-
// TODO ClusterMyShardID(ctx context.Context) *StringCmd
382+
ClusterMyShardID(ctx context.Context) *StringCmd
383383
ClusterSlots(ctx context.Context) *ClusterSlotsCmd
384384
ClusterShards(ctx context.Context) *ClusterShardsCmd
385385
// TODO ClusterLinks(ctx context.Context) *ClusterLinksCmd
@@ -401,8 +401,6 @@ type CoreCmdable interface {
401401
ClusterFailover(ctx context.Context) *StatusCmd
402402
ClusterAddSlots(ctx context.Context, slots ...int64) *StatusCmd
403403
ClusterAddSlotsRange(ctx context.Context, min, max int64) *StatusCmd
404-
// TODO ReadOnly(ctx context.Context) *StatusCmd
405-
// TODO ReadWrite(ctx context.Context) *StatusCmd
406404

407405
GeoAdd(ctx context.Context, key string, geoLocation ...GeoLocation) *IntCmd
408406
GeoPos(ctx context.Context, key string, members ...string) *GeoPosCmd
@@ -420,7 +418,7 @@ type CoreCmdable interface {
420418
// TODO ACLLog(ctx context.Context, count int64) *ACLLogCmd
421419
// TODO ACLLogReset(ctx context.Context) *StatusCmd
422420

423-
// TODO ModuleLoadex(ctx context.Context, conf *ModuleLoadexConfig) *StringCmd
421+
ModuleLoadex(ctx context.Context, conf *ModuleLoadexConfig) *StringCmd
424422
GearsCmdable
425423
ProbabilisticCmdable
426424
TimeseriesCmdable
@@ -2733,6 +2731,12 @@ func (c *Compat) ShutdownNoSave(ctx context.Context) *StatusCmd {
27332731
})
27342732
}
27352733

2734+
func (c *Compat) SlaveOf(ctx context.Context, host, port string) *StatusCmd {
2735+
cmd := c.client.B().Arbitrary("SLAVEOF").Args(host, port).Build()
2736+
resp := c.client.Do(ctx, cmd)
2737+
return newStatusCmd(resp)
2738+
}
2739+
27362740
func (c *Compat) Time(ctx context.Context) *TimeCmd {
27372741
cmd := c.client.B().Time().Build()
27382742
resp := c.client.Do(ctx, cmd)
@@ -2940,6 +2944,12 @@ func (c *Compat) PubSubShardNumSub(ctx context.Context, channels ...string) *Str
29402944
return newStringIntMapCmd(resp)
29412945
}
29422946

2947+
func (c *Compat) ClusterMyShardID(ctx context.Context) *StringCmd {
2948+
cmd := c.client.B().ClusterMyshardid().Build()
2949+
resp := c.client.Do(ctx, cmd)
2950+
return newStringCmd(resp)
2951+
}
2952+
29432953
func (c *Compat) ClusterSlots(ctx context.Context) *ClusterSlotsCmd {
29442954
cmd := c.client.B().ClusterSlots().Build()
29452955
resp := c.client.Do(ctx, cmd)
@@ -5640,6 +5650,20 @@ func (c *Compat) FTTagVals(ctx context.Context, index string, field string) *Str
56405650
return newStringSliceCmd(c.client.Do(ctx, cmd))
56415651
}
56425652

5653+
func (c *Compat) ModuleLoadex(ctx context.Context, conf *ModuleLoadexConfig) *StringCmd {
5654+
cmd := c.client.B().ModuleLoadex().Path(conf.Path).Config()
5655+
for k, v := range conf.Conf {
5656+
cmd = cmd.Config(k, str(v))
5657+
}
5658+
var resp rueidis.RedisResult
5659+
if len(conf.Args) > 0 {
5660+
resp = c.client.Do(ctx, cmd.Args(argsToSlice(conf.Args)...).Build())
5661+
} else {
5662+
resp = c.client.Do(ctx, cmd.Build())
5663+
}
5664+
return newStringCmd(resp)
5665+
}
5666+
56435667
func (c CacheCompat) BitCount(ctx context.Context, key string, bitCount *BitCount) *IntCmd {
56445668
var resp rueidis.RedisResult
56455669
if bitCount == nil {

rueidiscompat/command.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4833,3 +4833,11 @@ func (cmd *ClientInfoCmd) from(res rueidis.RedisResult) {
48334833

48344834
cmd.SetVal(info)
48354835
}
4836+
4837+
// ModuleLoadexConfig struct is used to specify the arguments for the MODULE LOADEX command of redis.
4838+
// `MODULE LOADEX path [CONFIG name value [CONFIG name value ...]] [ARGS args [args ...]]`
4839+
type ModuleLoadexConfig struct {
4840+
Path string
4841+
Conf map[string]interface{}
4842+
Args []interface{}
4843+
}

rueidiscompat/pipeline.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,12 @@ func (c *Pipeline) ShutdownNoSave(ctx context.Context) *StatusCmd {
16991699
return ret
17001700
}
17011701

1702+
func (c *Pipeline) SlaveOf(ctx context.Context, host, port string) *StatusCmd {
1703+
ret := c.comp.SlaveOf(ctx, host, port)
1704+
c.rets = append(c.rets, ret)
1705+
return ret
1706+
}
1707+
17021708
func (c *Pipeline) Time(ctx context.Context) *TimeCmd {
17031709
ret := c.comp.Time(ctx)
17041710
c.rets = append(c.rets, ret)
@@ -1885,6 +1891,12 @@ func (c *Pipeline) PubSubShardNumSub(ctx context.Context, channels ...string) *S
18851891
return ret
18861892
}
18871893

1894+
func (c *Pipeline) ClusterMyShardID(ctx context.Context) *StringCmd {
1895+
ret := c.comp.ClusterMyShardID(ctx)
1896+
c.rets = append(c.rets, ret)
1897+
return ret
1898+
}
1899+
18881900
func (c *Pipeline) ClusterSlots(ctx context.Context) *ClusterSlotsCmd {
18891901
ret := c.comp.ClusterSlots(ctx)
18901902
c.rets = append(c.rets, ret)
@@ -3030,6 +3042,12 @@ func (c *Pipeline) FTTagVals(ctx context.Context, index string, field string) *S
30303042
return ret
30313043
}
30323044

3045+
func (c *Pipeline) ModuleLoadex(ctx context.Context, conf *ModuleLoadexConfig) *StringCmd {
3046+
ret := c.comp.ModuleLoadex(ctx, conf)
3047+
c.rets = append(c.rets, ret)
3048+
return ret
3049+
}
3050+
30333051
// Len returns the number of queued commands.
30343052
func (c *Pipeline) Len() int {
30353053
return len(c.comp.client.(*proxy).cmds)

rueidiscompat/pipeline_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,16 +600,23 @@ func TestPipeliner(t *testing.T) {
600600
p.JSONStrLen(ctx, "1", "1")
601601
p.JSONToggle(ctx, "1", "1")
602602
p.JSONType(ctx, "1", "1")
603+
p.SlaveOf(ctx, "NO", "ONE")
604+
p.ClusterMyShardID(ctx)
605+
p.ModuleLoadex(ctx, &ModuleLoadexConfig{
606+
Path: "/",
607+
Conf: map[string]any{"k": "v"},
608+
Args: []any{"1", "2"},
609+
})
603610

604-
if n := len(p.rets); n != 477 {
611+
if n := len(p.rets); n != 480 {
605612
t.Fatalf("unexpected pipeline calls: %v", n)
606613
}
607614
for i, cmd := range p.rets {
608615
if err := cmd.Err(); !errors.Is(err, placeholder.err) {
609616
t.Fatalf("unexpected pipeline placeholder err(%d): %v", i, err)
610617
}
611618
}
612-
if n := len(p.comp.client.(*proxy).cmds); n != 477 {
619+
if n := len(p.comp.client.(*proxy).cmds); n != 480 {
613620
t.Fatalf("unexpected pipeline commands: %v", n)
614621
}
615622
var pipeline [][]string
@@ -1117,5 +1124,8 @@ var golden = `[
11171124
["JSON.STRAPPEND","1","1","1"],
11181125
["JSON.STRLEN","1","1"],
11191126
["JSON.TOGGLE","1","1"],
1120-
["JSON.TYPE","1","1"]
1127+
["JSON.TYPE","1","1"],
1128+
["SLAVEOF","NO","ONE"],
1129+
["CLUSTER","MYSHARDID"],
1130+
["MODULE","LOADEX","/","CONFIG","k","v","ARGS","1","2"]
11211131
]`

0 commit comments

Comments
 (0)