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 all 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
78 changes: 78 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4168,3 +4168,81 @@ 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
}

cmd.val = make([]KeyFlags, n)

for i := 0; i < len(cmd.val); i++ {

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
}
22 changes: 22 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, commands ...interface{}) *StringSliceCmd
CommandGetKeysAndFlags(ctx context.Context, commands ...interface{}) *KeyFlagsCmd
ClientGetName(ctx context.Context) *StringCmd
Echo(ctx context.Context, message interface{}) *StringCmd
Ping(ctx context.Context) *StatusCmd
Expand Down Expand Up @@ -568,6 +570,26 @@ func (c cmdable) CommandList(ctx context.Context, filter *FilterBy) *StringSlice
return cmd
}

func (c cmdable) CommandGetKeys(ctx context.Context, commands ...interface{}) *StringSliceCmd {
args := make([]interface{}, 2+len(commands))
args[0] = "command"
args[1] = "getkeys"
copy(args[2:], commands)
cmd := NewStringSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}

func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, commands ...interface{}) *KeyFlagsCmd {
args := make([]interface{}, 2+len(commands))
args[0] = "command"
args[1] = "getkeysandflags"
copy(args[2:], commands)
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
37 changes: 37 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,43 @@ 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"}))

_, err = client.CommandGetKeys(ctx, "FAKECOMMAND", "arg1", "arg2").Result()
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("ERR Invalid command specified"))
})

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"},
},
}))

_, err = client.CommandGetKeysAndFlags(ctx, "FAKECOMMAND", "arg1", "arg2").Result()
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("ERR Invalid command specified"))
})

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