Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for COMMAND GETKEYS & COMMAND GETKEYSANDFLAGS #2500

Merged
Merged
Show file tree
Hide file tree
Changes from 10 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
88 changes: 88 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4168,3 +4168,91 @@ func (cmd *LCSCmd) readPosition(rd *proto.Reader) (pos LCSPosition, err error) {

return pos, nil
}

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

type KeyFlags struct {
Key string
Flags []string
}

type KeyFlagsCmd struct {
baseCmd

val []KeyFlags
}

var _ Cmder = (*KeyFlagsCmd)(nil)

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

func (cmd *KeyFlagsCmd) SetVal(val []KeyFlags) {
cmd.val = val
}

func (cmd *KeyFlagsCmd) Val() []KeyFlags {
return cmd.val
}

func (cmd *KeyFlagsCmd) Result() ([]KeyFlags, error) {
return cmd.val, cmd.err
}

func (cmd *KeyFlagsCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}

if n == 0 {
cmd.val = make([]KeyFlags, 0)
return nil
}

typ, err := rd.PeekReplyType()
if err != nil {
return err
}
array := typ == proto.RespArray

if array {
cmd.val = make([]KeyFlags, n)
} else {
cmd.val = make([]KeyFlags, n/2)
}
SoulPancake marked this conversation as resolved.
Show resolved Hide resolved

for i := 0; i < len(cmd.val); i++ {
if array {
if err = rd.ReadFixedArrayLen(2); err != nil {
return err
}
}
if cmd.val[i].Key, err = rd.ReadString(); err != nil {
return err
}
flagsLen, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val[i].Flags = make([]string, flagsLen)

for j := 0; j < flagsLen; j++ {
if cmd.val[i].Flags[j], err = rd.ReadString(); err != nil {
return err
}
}
}

return nil
}
28 changes: 28 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ type Cmdable interface {

Command(ctx context.Context) *CommandsInfoCmd
CommandList(ctx context.Context, filter *FilterBy) *StringSliceCmd
CommandGetKeys(ctx context.Context, command string, args ...string) *StringSliceCmd
CommandGetKeysAndFlags(ctx context.Context, command string, args ...string) *KeyFlagsCmd
SoulPancake marked this conversation as resolved.
Show resolved Hide resolved
ClientGetName(ctx context.Context) *StringCmd
Echo(ctx context.Context, message interface{}) *StringCmd
Ping(ctx context.Context) *StatusCmd
Expand Down Expand Up @@ -568,6 +570,32 @@ func (c cmdable) CommandList(ctx context.Context, filter *FilterBy) *StringSlice
return cmd
}

func (c cmdable) CommandGetKeys(ctx context.Context, command string, cmdArgs ...string) *StringSliceCmd {
args := make([]interface{}, 3+len(cmdArgs))
args[0] = "command"
args[1] = "getkeys"
args[2] = command
for i, cmdArg := range cmdArgs {
SoulPancake marked this conversation as resolved.
Show resolved Hide resolved
args[3+i] = cmdArg
}
cmd := NewStringSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}

func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, command string, cmdArgs ...string) *KeyFlagsCmd {
args := make([]interface{}, 3+len(cmdArgs))
args[0] = "command"
args[1] = "getkeysandflags"
args[2] = command
for i, cmdArg := range cmdArgs {
args[3+i] = cmdArg
}
cmd := NewKeyFlagsCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}

// ClientGetName returns the name of the connection.
func (c cmdable) ClientGetName(ctx context.Context) *StringCmd {
cmd := NewStringCmd(ctx, "client", "getname")
Expand Down
29 changes: 29 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,35 @@ var _ = Describe("Commands", func() {
}, "30s").Should(Equal("Background saving started"))
})

It("Should CommandGetKeys", func() {
keys, err := client.CommandGetKeys(ctx, "MSET", "a", "b", "c", "d", "e", "f").Result()
Expect(err).NotTo(HaveOccurred())
Expect(keys).To(Equal([]string{"a", "c", "e"}))

keys, err = client.CommandGetKeys(ctx, "EVAL", "not consulted", "3", "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN").Result()
Expect(err).NotTo(HaveOccurred())
Expect(keys).To(Equal([]string{"key1", "key2", "key3"}))

keys, err = client.CommandGetKeys(ctx, "SORT", "mylist", "ALPHA", "STORE", "outlist").Result()
Expect(err).NotTo(HaveOccurred())
Expect(keys).To(Equal([]string{"mylist", "outlist"}))
})

It("should CommandGetKeysAndFlags", func() {
keysAndFlags, err := client.CommandGetKeysAndFlags(ctx, "LMOVE", "mylist1", "mylist2", "left", "left").Result()
Expect(err).NotTo(HaveOccurred())
Expect(keysAndFlags).To(Equal([]redis.KeyFlags{
{
Key: "mylist1",
Flags: []string{"RW", "access", "delete"},
},
{
Key: "mylist2",
Flags: []string{"RW", "insert"},
},
}))
})

It("should ClientKill", func() {
r := client.ClientKill(ctx, "1.1.1.1:1111")
Expect(r.Err()).To(MatchError("ERR No such client"))
Expand Down