Skip to main content

cmd_get.go

cmd_get.go - Overview

This file defines the GET command, which retrieves the value associated with a given key from the store.

Detailed Documentation

cGET

var cGET = &CommandMeta{
Name: "GET",
Syntax: "GET key",
HelpShort: "GET returns the value for the key",
HelpLong: `
GET returns the value for the key in args.

The command returns (nil) if the key does not exist.
`,
Examples: `
localhost:7379> SET k1 v1
OK OK
localhost:7379> GET k1
OK v1
localhost:7379> GET k2
(nil)
`,
Eval: evalGET,
Execute: executeGET,
}
  • Purpose: Defines the metadata for the GET command, including its name, syntax, help text, examples, and the functions to evaluate and execute the command.
  • Members:
    • Name: The name of the command ("GET").
    • Syntax: The syntax of the command ("GET 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 (evalGET) used to evaluate the command.
    • Execute: The function (executeGET) used to execute the command.

init

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

evalGET

func evalGET(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("GET")
}
key := c.C.Args[0]
obj := s.Get(key)

return cmdResFromObject(obj)
}
  • Purpose: Evaluates the GET command by retrieving the object associated with the given key from the store.
  • Parameters:
    • c: A pointer to the Cmd struct representing the command.
    • s: A pointer to the dstore.Store struct representing the data store.
  • Returns:
    • A pointer to a CmdRes struct representing the command result.
    • An error if the number of arguments is incorrect.

executeGET

func executeGET(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("GET")
}
shard := sm.GetShardForKey(c.C.Args[0])
return evalGET(c, shard.Thread.Store())
}
  • Purpose: Executes the GET command by retrieving the appropriate shard based on the key, and then evaluating the command against that shard's store.
  • Parameters:
    • c: A pointer to the Cmd struct representing the command.
    • sm: A pointer to the shardmanager.ShardManager struct for managing shards.
  • Returns:
    • A pointer to a CmdRes struct representing the command result.
    • An error if the number of arguments is incorrect.

cmdResFromObject

func cmdResFromObject(obj *object.Obj) (*CmdRes, error) {
if obj == nil {
return GetNilRes(), nil
}

switch obj.Type {
case object.ObjTypeInt:
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VInt{VInt: obj.Value.(int64)},
}}, nil
case object.ObjTypeString:
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VStr{VStr: obj.Value.(string)},
}}, nil
case object.ObjTypeByteArray, object.ObjTypeHLL:
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VBytes{VBytes: obj.Value.([]byte)},
}}, nil
default:
slog.Error("unknown object type", "type", obj.Type)
return cmdResNil, errors.ErrUnknownObjectType
}
}
  • Purpose: Converts an object.Obj to a CmdRes based on the object's type.
  • Parameters:
    • obj: A pointer to the object.Obj to convert.
  • Returns:
    • A pointer to a CmdRes struct representing the command result.
    • An error if the object type is unknown.

Code Examples

None.

Getting Started Relevance