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 singledstore.Store
. - Parameters:
c
: A pointer to theCmd
struct, containing the command's arguments.s
: A pointer to thedstore.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.
- A pointer to a
- Logic:
- Retrieves the key and field from the command arguments.
- Attempts to retrieve the object associated with the key from the store. If the object doesn't exist, returns
nil
. - Checks if the object's value is of type
SSMap
. If not, returns anErrWrongTypeOperation
error. - Attempts to retrieve the value associated with the field from the
SSMap
. If the field doesn't exist, returnsnil
. - 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 theCmd
struct, containing the command's arguments.sm
: A pointer to theshardmanager.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.
- A pointer to a
- Logic:
- Checks if the number of arguments is correct (2: key and field). If not, returns an
ErrWrongArgumentCount
error. - Gets the appropriate shard based on the key.
- Calls
evalHGET
to evaluate the command on the selected shard's store.
- Checks if the number of arguments is correct (2: key and field). If not, returns an