Skip to main content

cmd_hget.go

cmd_hget.go - Overview

This file implements the HGET command for DiceDB. It retrieves the value of a specified field from a string-string map stored at a given key.

Detailed Documentation

cHGET

var cHGET = &CommandMeta{
Name: "HGET",
Syntax: "HGET key field",
HelpShort: "HGET returns the value of field present in the string-string map held at key.",
HelpLong: `
HGET returns the value of field present in the string-string map held at key.

The command returns (nil) if the key or field does not exist.
`,
Examples: `
localhost:7379> HSET k1 f1 v1
OK 1
localhost:7379> HGET k1 f1
OK v1
localhost:7379> HGET k2 f1
OK (nil)
localhost:7379> HGET k1 f2
OK (nil)
`,
Eval: evalHGET,
Execute: executeHGET,
}
  • Purpose: Defines the metadata for the HGET command, including its name, syntax, help text, examples, and the functions to evaluate and execute it.
  • Members:
    • Name: The name of the command ("HGET").
    • Syntax: The command's syntax ("HGET key field").
    • 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 (evalHGET) responsible for evaluating the command's logic against a single store.
    • Execute: The function (executeHGET) responsible for executing the command across the sharded database.

evalHGET

func evalHGET(c *Cmd, s *dstore.Store) (*CmdRes, error) {
key, field := c.C.Args[0], c.C.Args[1]

obj := s.Get(key)
if obj == nil {
return cmdResNil, nil
}

m, ok := obj.Value.(SSMap)
if !ok {
return cmdResNil, errors.ErrWrongTypeOperation
}

val, ok := m.Get(field)
if !ok {
return cmdResNil, nil
}

return &CmdRes{R: &wire.Response{
Value: &wire.Response_VStr{VStr: val},
}}, nil
}
  • Purpose: Evaluates the HGET command against a single dstore.Store.
  • Parameters:
    • c: A pointer to the Cmd struct, containing the command's arguments.
    • s: A pointer to the dstore.Store struct, representing the data store.
  • Returns:
    • A pointer to a CmdRes struct, containing the command's result.
    • An error, if any occurred during the evaluation.
  • Logic:
    1. Retrieves the key and field from the command arguments.
    2. Attempts to retrieve the object associated with the key from the store. If the object doesn't exist, returns nil.
    3. Checks if the object's value is of type SSMap. If not, returns an ErrWrongTypeOperation error.
    4. Attempts to retrieve the value associated with the field from the SSMap. If the field doesn't exist, returns nil.
    5. If the value is found, constructs a CmdRes containing the value as a string.

executeHGET

func executeHGET(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) != 2 {
return cmdResNil, errors.ErrWrongArgumentCount("HGET")
}
shard := sm.GetShardForKey(c.C.Args[0])
return evalHGET(c, shard.Thread.Store())
}
  • Purpose: Executes the HGET command in a sharded environment.
  • Parameters:
    • c: A pointer to the Cmd struct, containing the command's arguments.
    • sm: A pointer to the shardmanager.ShardManager struct, responsible for managing shards.
  • Returns:
    • A pointer to a CmdRes struct, containing the command's result.
    • An error, if any occurred during the execution.
  • Logic:
    1. Checks if the number of arguments is correct (2: key and field). If not, returns an ErrWrongArgumentCount error.
    2. Gets the appropriate shard based on the key.
    3. Calls evalHGET to evaluate the command on the selected shard's store.

Getting Started Relevance