Skip to main content

cmd_hgetall.go

cmd_hgetall.go - Overview

This file implements the HGETALL command, which retrieves all field-value pairs from a string-string map (SSMap) stored under a given key in the database.

Detailed Documentation

cHGETALL

var cHGETALL = &CommandMeta{
Name: "HGETALL",
Syntax: "HGETALL key",
HelpShort: "HGETALL returns all the field-value pairs for the key from the string-string map",
HelpLong: `
HGETALL returns all the field-value pairs for the key from the string-string map.

The command returns (nil) if the key does not exist or the map is empty.
`,
Examples: `
localhost:7379> HSET k1 f1 v1 f2 v2 f3 v3
OK 3
localhost:7379> HGETALL k1
OK
f1=v1
f2=v2
f3=v3
localhost:7379> HGETALL k2
OK (nil)
`,
Eval: evalHGETALL,
Execute: executeHGETALL,
}
  • Purpose: Defines the metadata for the HGETALL command, including its name, syntax, help messages, examples, and the functions to evaluate and execute it.
  • Properties:
    • Name: The name of the command ("HGETALL").
    • Syntax: The command's syntax ("HGETALL key").
    • HelpShort: A short description of the command.
    • HelpLong: A longer, more detailed description of the command.
    • Examples: Example usages of the command.
    • Eval: The function (evalHGETALL) responsible for evaluating the command.
    • Execute: The function (executeHGETALL) responsible for executing the command.

init

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

evalHGETALL

func evalHGETALL(c *Cmd, s *dstore.Store) (*CmdRes, error) {
key := c.C.Args[0]
var m SSMap

obj := s.Get(key)
if obj != nil {
if err := object.AssertType(obj.Type, object.ObjTypeSSMap); err != nil {
return cmdResNil, errors.ErrWrongTypeOperation
}
m = obj.Value.(SSMap)
}

if len(m) == 0 {
return cmdResNil, nil
}

return &CmdRes{R: &wire.Response{
VSsMap: m,
}}, nil
}
  • Purpose: Evaluates the HGETALL command. It retrieves the string-string map (SSMap) associated with the given key from the store and returns all field-value pairs.
  • Parameters:
    • c: A pointer to the Cmd struct representing the command.
    • s: A pointer to the dstore.Store struct representing the data store.
  • Returns:
    • *CmdRes: A pointer to the CmdRes struct, which contains the response data (a map of string-string pairs) if successful. Returns cmdResNil if the key does not exist or the map is empty.
    • error: An error object, or nil if the command was evaluated successfully. Returns errors.ErrWrongTypeOperation if the object stored at the key is not of type object.ObjTypeSSMap.

executeHGETALL

func executeHGETALL(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("HGETALL")
}
shard := sm.GetShardForKey(c.C.Args[0])
return evalHGETALL(c, shard.Thread.Store())
}
  • Purpose: Executes the HGETALL command. It retrieves the appropriate shard based on the key, then calls evalHGETALL to perform the actual retrieval of the map.
  • Parameters:
    • c: A pointer to the Cmd struct representing the command.
    • sm: A pointer to the shardmanager.ShardManager struct, used to manage data shards.
  • Returns:
    • *CmdRes: A pointer to the CmdRes struct, containing the response from evalHGETALL.
    • error: An error object, or nil if execution was successful. Returns errors.ErrWrongArgumentCount if the number of arguments is incorrect.

Getting Started Relevance