cmd_decrby.go
Filename: cmd_decrby.go
Overview
This file implements the DECRBY
command for the DiceDB, which decrements the value of a key by a specified delta.
Detailed Documentation
cDECRBY
var cDECRBY = &CommandMeta{
Name: "DECRBY",
Syntax: "DECRBY key delta",
HelpShort: "DECRBY decrements the specified key by the specified delta",
HelpLong: `
DECRBY command decrements the integer at 'key' by the delta specified. Creates 'key' with value (-delta) if absent.
Errors on wrong type or non-integer string. Limited to 64-bit signed integers.
Returns the new value of 'key' on success.
`,
Examples: `
localhost:7379> SET k 43
OK OK
localhost:7379> DECRBY k 10
OK 33
`,
Eval: evalDECRBY,
Execute: executeDECRBY,
}
- Purpose: Defines the metadata for the
DECRBY
command, including its name, syntax, help text, examples, and the functions to evaluate and execute the command. - Properties:
Name
: The name of the command, "DECRBY".Syntax
: The command's syntax, "DECRBY key delta".HelpShort
: A short description of the command.HelpLong
: A detailed description of the command.Examples
: Example usage of the command.Eval
: The function to evaluate the command (evalDECRBY
).Execute
: The function to execute the command (executeDECRBY
).
init()
func init() {
CommandRegistry.AddCommand(cDECRBY)
}
- Purpose: Registers the
DECRBY
command with theCommandRegistry
during package initialization.
evalDECRBY(c *Cmd, s *dstore.Store) (*CmdRes, error)
func evalDECRBY(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) != 2 {
return cmdResNil, errors.ErrWrongArgumentCount("DECRBY")
}
delta, err := strconv.ParseInt(c.C.Args[1], 10, 64)
if err != nil {
return cmdResNil, errors.ErrIntegerOutOfRange
}
return doIncr(c, s, -delta)
}
- Purpose: Evaluates the
DECRBY
command. It parses the delta value from the command arguments and callsdoIncr
with the negative delta to perform the decrement operation. - 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 result of the command.error
: An error object, ornil
if the command was successful.
executeDECRBY(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error)
func executeDECRBY(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) != 2 {
return cmdResNil, errors.ErrWrongArgumentCount("DECRBY")
}
shard := sm.GetShardForKey(c.C.Args[0])
return evalDECRBY(c, shard.Thread.Store())
}
- Purpose: Executes the
DECRBY
command. It retrieves the appropriate shard based on the key, and then callsevalDECRBY
to evaluate the command within that shard's store. - 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 result of the command.error
: An error object, ornil
if the command was successful.