cmd_getset.go
cmd_getset.go - Overview
This file defines the GETSET
command, which sets the value of a key and returns its old value.
Detailed Documentation
cGETSET
var cGETSET = &CommandMeta{
Name: "GETSET",
Syntax: "GETSET key value",
HelpShort: "GETSET sets the value for the key and returns the old value",
HelpLong: `
GETSET sets the value for the key and returns the old value.
The command returns (nil) if the key does not exist.
`,
Examples: `
localhost:7379> SET k1 v1
OK OK
localhost:7379> GETSET k1 v2
OK v1
localhost:7379> GET k1
OK v2
`,
Eval: evalGETSET,
Execute: executeGETSET,
}
- Purpose: Defines the metadata for the
GETSET
command, including its name, syntax, help text, examples, and the functions to evaluate and execute the command. - Fields:
Name
: The name of the command ("GETSET").Syntax
: The command's syntax ("GETSET key value").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 (evalGETSET
) responsible for evaluating the command.Execute
: The function (executeGETSET
) responsible for executing the command.
init
func init() {
CommandRegistry.AddCommand(cGETSET)
}
- Purpose: Registers the
GETSET
command with theCommandRegistry
. - Details: This function is automatically called when the package is initialized. It adds the
cGETSET
command to the globalCommandRegistry
, making it available for use.
evalGETSET
func evalGETSET(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) != 2 {
return cmdResNil, errors.ErrWrongArgumentCount("GETSET")
}
key, value := c.C.Args[0], c.C.Args[1]
obj := s.Get(key)
// Put the new value in the store
s.Put(key, CreateObjectFromValue(s, value, -1))
// Return the old value, if the key does not exist, return nil
if obj == nil {
return cmdResNil, nil
}
// Return the old value
return cmdResFromObject(obj)
}
- Purpose: Evaluates the
GETSET
command, setting the value for the key and returning the old value. - Parameters:
c
: A pointer to theCmd
struct representing the command.s
: A pointer to thedstore.Store
struct representing the data store.
- Returns:
*CmdRes
: A pointer to theCmdRes
struct representing the command result.error
: An error if one occurred, ornil
if the command was successful.
- Details:
- It checks if the number of arguments is correct (2).
- It retrieves the key and value from the command arguments.
- It gets the old object from the store using the key.
- It puts the new value into the store.
- It returns the old value (or nil if the key did not exist).
executeGETSET
func executeGETSET(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) != 2 {
return cmdResNil, errors.ErrWrongArgumentCount("GETSET")
}
shard := sm.GetShardForKey(c.C.Args[0])
return evalGETSET(c, shard.Thread.Store())
}
- Purpose: Executes the
GETSET
command on the appropriate shard. - Parameters:
c
: A pointer to theCmd
struct representing the command.sm
: A pointer to theshardmanager.ShardManager
struct.
- Returns:
*CmdRes
: A pointer to theCmdRes
struct representing the command result.error
: An error if one occurred, ornil
if the command was successful.
- Details:
- It checks if the number of arguments is correct (2).
- It gets the shard for the key from the
ShardManager
. - It calls
evalGETSET
with the command and the shard's store.