cmd_getdel.go
cmd_getdel.go - Overview
This file defines the GETDEL
command, which retrieves the value of a key and then deletes it from the store.
Detailed Documentation
cGETDEL
var cGETDEL = &CommandMeta{
Name: "GETDEL",
Syntax: "GETDEL key",
HelpShort: "GETDEL returns the value of the key and then deletes the key.",
HelpLong: `
GETDEL returns the value of the key and then deletes the key.
The command returns (nil) if the key does not exist.
`,
Examples: `
localhost:7379> SET k v
OK OK
localhost:7379> GETDEL k
OK v
localhost:7379> GET k
(nil)
`,
Eval: evalGETDEL,
Execute: executeGETDEL,
}
- Purpose: Defines the metadata for the
GETDEL
command, including its name, syntax, help text, examples, and the functions to evaluate and execute the command. - Properties:
Name
: The name of the command ("GETDEL").Syntax
: The command's syntax ("GETDEL key").HelpShort
: A short description of the command.HelpLong
: A longer, more detailed description of the command.Examples
: Example usage of the command.Eval
: The function to evaluate the command (evalGETDEL
).Execute
: The function to execute the command (executeGETDEL
).
init
func init() {
CommandRegistry.AddCommand(cGETDEL)
}
- Purpose: Registers the
GETDEL
command with theCommandRegistry
during package initialization.
evalGETDEL
func evalGETDEL(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("GETDEL")
}
key := c.C.Args[0]
// Getting the key based on previous touch value
obj := s.GetNoTouch(key)
if obj == nil {
return cmdResNil, nil
}
// Get the key from the hash table
// TODO: Evaluate the need for having GetDel
// implemented in the store. It might be better if we can
// keep the business logic untangled from the store.
objVal := s.GetDel(key)
// Decode and return the value based on its encoding
switch oType := objVal.Type; oType {
case object.ObjTypeInt:
// Value is stored as an int64, so use type assertion
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VInt{VInt: objVal.Value.(int64)},
}}, nil
case object.ObjTypeString:
// Value is stored as a string, use type assertion
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VStr{VStr: objVal.Value.(string)},
}}, nil
default:
return cmdResNil, errors.ErrWrongTypeOperation
}
}
- Purpose: Evaluates the
GETDEL
command, retrieving the value associated with the given key from the store and deleting the key. - Parameters:
c
(*Cmd): The command context, containing the arguments.s
(*dstore.Store): The data store instance.
- Returns:
*CmdRes
: A command result containing the value of the key, orcmdResNil
if the key does not exist or is expired.error
: An error if the number of arguments is incorrect or if the value type is unexpected.
executeGETDEL
func executeGETDEL(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("GETDEL")
}
shard := sm.GetShardForKey(c.C.Args[0])
return evalGETDEL(c, shard.Thread.Store())
}
- Purpose: Executes the
GETDEL
command by retrieving the appropriate shard based on the key, and then callingevalGETDEL
on that shard's store. - Parameters:
c
(*Cmd): The command context, containing the arguments.sm
(*shardmanager.ShardManager): The shard manager instance.
- Returns:
*CmdRes
: The result fromevalGETDEL
.error
: An error if the number of arguments is incorrect.