Skip to main content

cmd_keys.go

cmd_keys.go - Overview

This file implements the KEYS command for the DiceDB, which retrieves all keys matching a given pattern.

Detailed Documentation

cKEYS

var cKEYS = &CommandMeta{
Name: "KEYS",
Syntax: "KEYS pattern",
HelpShort: "KEYS returns all keys matching the pattern",
HelpLong: `
KEYS returns all keys matching the pattern.

The pattern can contain the following special characters to match multiple keys.
Supports glob-style patterns:
- *: matches any sequence of characters
- ?: matches any single character`,
Examples: `
localhost:7379> SET k1 v1
OK OK
localhost:7379> SET k2 v2
OK OK
localhost:7379> SET k33 v33
OK OK
localhost:7379> KEYS k?
OK
1) "k1"
2) "k2"
localhost:7379> KEYS k*
OK
1) "k1"
2) "k2"
3) "k33"
localhost:7379> KEYS *
OK
1) "k1"
2) "k2"
3) "k33"
`,
Eval: evalKEYS,
Execute: executeKEYS,
}
  • Purpose: Defines the metadata for the KEYS command, including its name, syntax, help text, examples, and the functions to evaluate and execute it.
  • Members:
    • Name: The name of the command ("KEYS").
    • Syntax: The command's syntax ("KEYS pattern").
    • HelpShort: A short description of the command.
    • HelpLong: A longer, more detailed description of the command, including supported patterns.
    • Examples: Example usages of the command.
    • Eval: The function to evaluate the command on a single store (evalKEYS).
    • Execute: The function to execute the command across shards (executeKEYS).

init()

func init() {
CommandRegistry.AddCommand(cKEYS)
}
  • Purpose: Registers the KEYS command with the CommandRegistry during package initialization.

evalKEYS(c *Cmd, s *dstore.Store) (*CmdRes, error)

func evalKEYS(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("KEYS")
}
pattern := c.C.Args[0]
keys, err := s.Keys(pattern)
if err != nil {
return nil, err
}
return createResponseFromArray(keys), nil
}
  • Purpose: Evaluates the KEYS command on a single store.
  • Parameters:
    • c: A pointer to the Cmd struct representing the command.
    • s: A pointer to the dstore.Store struct representing the store.
  • Returns:
    • *CmdRes: A pointer to the CmdRes struct representing the command result.
    • error: An error if one occurred, or nil if the command was successful.
  • Functionality:
    • Checks if the number of arguments is correct (1).
    • Retrieves the pattern from the command arguments.
    • Calls the Keys method on the store to get the matching keys.
    • Creates a response from the array of keys using createResponseFromArray.

executeKEYS(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error)

func executeKEYS(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("KEYS")
}
var keys []string
for _, shard := range sm.Shards() {
res, err := evalKEYS(c, shard.Thread.Store())
if err != nil {
return nil, err
}
for _, v := range res.R.GetVList() {
keys = append(keys, v.GetStringValue())
}
}
finalRes := createResponseFromArray(keys)
return finalRes, nil
}
  • Purpose: Executes the KEYS command across multiple shards.
  • Parameters:
    • c: A pointer to the Cmd struct representing the command.
    • sm: A pointer to the shardmanager.ShardManager struct representing the shard manager.
  • Returns:
    • *CmdRes: A pointer to the CmdRes struct representing the command result.
    • error: An error if one occurred, or nil if the command was successful.
  • Functionality:
    • Checks if the number of arguments is correct (1).
    • Iterates over each shard in the shard manager.
    • Evaluates the KEYS command on each shard using evalKEYS.
    • Appends the keys from each shard's response to a final list of keys.
    • Creates a response from the final list of keys using createResponseFromArray.

createResponseFromArray(arr []string) *CmdRes

func createResponseFromArray(arr []string) *CmdRes {
if len(arr) == 0 {
return cmdResNil
}
var res []*structpb.Value
for _, v := range arr {
val := structpb.NewStringValue(v)
res = append(res, val)
}
return &CmdRes{R: &wire.Response{
VList: res}}
}
  • Purpose: Creates a CmdRes struct from an array of strings.
  • Parameters:
    • arr: An array of strings representing the keys.
  • Returns:
    • *CmdRes: A pointer to the CmdRes struct representing the command result.
  • Functionality:
    • Creates a structpb.Value for each string in the input array.
    • Appends each structpb.Value to a list.
    • Creates a wire.Response with the list of structpb.Values.
    • Creates a CmdRes with the wire.Response.

Getting Started Relevance