Skip to main content

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 the CommandRegistry 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 calls doIncr with the negative delta to perform the decrement operation.
  • Parameters:
    • c: A pointer to the Cmd struct representing the command.
    • s: A pointer to the dstore.Store struct representing the data store.
  • Returns:
    • *CmdRes: A pointer to the CmdRes struct representing the result of the command.
    • error: An error object, or nil 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 calls evalDECRBY to evaluate the command within that shard's store.
  • Parameters:
    • c: A pointer to the Cmd struct representing the command.
    • sm: A pointer to the shardmanager.ShardManager struct.
  • Returns:
    • *CmdRes: A pointer to the CmdRes struct representing the result of the command.
    • error: An error object, or nil if the command was successful.

Getting Started Relevance