Skip to content

Commit

Permalink
Add initial LCS implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
arbha1erao committed Feb 17, 2025
1 parent 5648609 commit df80059
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
24 changes: 23 additions & 1 deletion rueidiscompat/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ type CoreCmdable interface {
BLMPop(ctx context.Context, timeout time.Duration, direction string, count int64, keys ...string) *KeyValuesCmd
BRPop(ctx context.Context, timeout time.Duration, keys ...string) *StringSliceCmd
BRPopLPush(ctx context.Context, source, destination string, timeout time.Duration) *StringCmd
// TODO LCS(ctx context.Context, q *LCSQuery) *LCSCmd
LCS(ctx context.Context, q *LCSQuery) *LCSCmd
LIndex(ctx context.Context, key string, index int64) *StringCmd
LInsert(ctx context.Context, key, op string, pivot, value any) *IntCmd
LInsertBefore(ctx context.Context, key string, pivot, value any) *IntCmd
Expand Down Expand Up @@ -1581,6 +1581,28 @@ func (c *Compat) BRPopLPush(ctx context.Context, source, destination string, tim
return newStringCmd(resp)
}

func (c *Compat) LCS(ctx context.Context, q *LCSQuery) *LCSCmd {
var cmd cmds.Completed
_cmd := cmds.Incomplete(c.client.B().Lcs().Key1(q.Key1).Key2(q.Key2))

if q.Len {
cmd = cmds.LcsKey2(_cmd).Len().Build()
return newLCSCmd(c.client.Do(ctx, cmd))
}

if q.MinMatchLen > 0 && q.WithMatchLen {
cmd = cmds.LcsKey2(_cmd).Idx().Minmatchlen(int64(q.MinMatchLen)).Withmatchlen().Build()
} else if q.MinMatchLen > 0 {
cmd = cmds.LcsKey2(_cmd).Idx().Minmatchlen(int64(q.MinMatchLen)).Build()
} else if q.WithMatchLen {
cmd = cmds.LcsKey2(_cmd).Idx().Withmatchlen().Build()
} else {
cmd = cmds.LcsKey2(_cmd).Idx().Build()
}

return newLCSCmd(c.client.Do(ctx, cmd))
}

func (c *Compat) LIndex(ctx context.Context, key string, index int64) *StringCmd {
cmd := c.client.B().Lindex().Key(key).Index(index).Build()
resp := c.client.Do(ctx, cmd)
Expand Down
48 changes: 48 additions & 0 deletions rueidiscompat/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5107,3 +5107,51 @@ func newSlowLogCmd(res rueidis.RedisResult) *SlowLogCmd {
cmd.from(res)
return cmd
}

type LCSQuery struct {
Key1 string
Key2 string
Len bool
Idx bool
MinMatchLen int
WithMatchLen bool
}

type LCSCmd struct {
baseCmd[any]
}

func newLCSCmd(res rueidis.RedisResult) *LCSCmd {
cmd := &LCSCmd{}
cmd.from(res)
return cmd
}

func (cmd *LCSCmd) from(res rueidis.RedisResult) {
if err := res.Error(); err != nil {
cmd.SetErr(err)
return
}

if val, err := res.AsInt64(); err == nil {
cmd.SetVal(val)
return
}

if val, err := res.ToString(); err == nil {
cmd.SetVal(val)
return
}

if val, err := res.AsStrMap(); err == nil {
cmd.SetVal(val)
return
}

if val, err := res.ToArray(); err == nil {
cmd.SetVal(val)
return
}

cmd.SetErr(errors.New("unexpected LCS response type"))
}
6 changes: 6 additions & 0 deletions rueidiscompat/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,12 @@ func (c *Pipeline) BRPopLPush(ctx context.Context, source, destination string, t
return ret
}

func (c *Pipeline) LCS(ctx context.Context, q *LCSQuery) *LCSCmd {
ret := c.comp.LCS(ctx, q)
c.rets = append(c.rets, ret)
return ret
}

func (c *Pipeline) LIndex(ctx context.Context, key string, index int64) *StringCmd {
ret := c.comp.LIndex(ctx, key, index)
c.rets = append(c.rets, ret)
Expand Down

0 comments on commit df80059

Please sign in to comment.