Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
51 changes: 51 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3840,3 +3840,54 @@ func (cmd *ZSliceWithKeyCmd) readReply(rd *proto.Reader) (err error) {

return nil
}

//------------------------------------------------------------------------------

type LCSMatch struct {
Positions1 []int
Positions2 []int
}

type LCSMatchLen struct {
Match LCSMatch
Len int64
}

type LCSCmd struct {
baseCmd

Val LCSMatchLen
}

var _ Cmder = (*LCSCmd)(nil)

func NewLCSCmd(ctx context.Context,args ...interface{}) *LCSCmd {
return &LCSCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}

func (cmd *LCSCmd) Result() (LCSMatchLen, error) {
return cmd.Val, cmd.err
}

func (cmd *LCSCmd) String() string {
return cmdString(cmd, cmd.Val)
}

func (cmd *LCSCmd) readReply(rd *proto.Reader) error {

length, err := rd.ReadArrayLen()
if err != nil {
return err
}

if length != 4 {
return fmt.Errorf("Unexpected number of items in array reply: %d", length)
}

return nil
}
19 changes: 19 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,25 @@ func (c cmdable) BRPopLPush(ctx context.Context, source, destination string, tim
return cmd
}

func (c cmdable) LCS(ctx context.Context, key1 string, key2 string) *StringCmd {
cmd := NewStringCmd(ctx, "lcs", key1, key2)
_ = c(ctx, cmd)
return cmd
}

func (c cmdable) LCSLen(ctx context.Context, key1 string, key2 string) *IntCmd {
cmd := NewIntCmd(ctx, "lcs", key1, key2, "len")
_ = c(ctx, cmd)
return cmd
}

func (c cmdable) LCSIdx(ctx context.Context, key1 string, key2 string, idx bool) *LCSCmd {
cmd := NewLCSCmd(ctx, "lcs", key1, key2, true)
_ = c(ctx,cmd)
return cmd
}


func (c cmdable) LIndex(ctx context.Context, key string, index int64) *StringCmd {
cmd := NewStringCmd(ctx, "lindex", key, index)
_ = c(ctx, cmd)
Expand Down
28 changes: 28 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,34 @@ var _ = Describe("Commands", func() {
Expect(v).To(Equal("c"))
})

It("should LCS", func() {
mSet := client.MSet(ctx, "key1", "ohmytext", "key2", "mynewtext")
Expect(mSet.Err()).NotTo(HaveOccurred())
Expect(mSet.Val()).To(Equal("OK"))

lcs := client.LCS(ctx, "key1", "key2")
Expect(lcs.Err()).NotTo(HaveOccurred())
Expect(lcs.Val()).To(Equal("mytext"))

lcs = client.LCS(ctx, "nonexistent_key1", "nonexistent_key2")
Expect(lcs.Err()).To(Equal(redis.Nil))
})

It("should LCSLen", func() {
mSet := client.MSet(ctx, "key1", "ohmytext", "key2", "mynewtext")
Expect(mSet.Err()).NotTo(HaveOccurred())
Expect(mSet.Val()).To(Equal("OK"))

lcsLen := client.LCSLen(ctx, "key1", "key2")
Expect(lcsLen.Err()).NotTo(HaveOccurred())
Expect(lcsLen.Val()).To(Equal(6))

lcsLen = client.LCSLen(ctx, "nonexistent_key1", "nonexistent_key2")
Expect(lcsLen.Err()).To(Equal(redis.Nil))

})


It("should LIndex", func() {
lPush := client.LPush(ctx, "list", "World")
Expect(lPush.Err()).NotTo(HaveOccurred())
Expand Down