Skip to main content

cmd_decr.go

cmd_decr.go - Overview

This file defines the DECR command, which decrements the integer value of a specified key in the DiceDB store by 1.

Detailed Documentation

cDECR

var cDECR = &CommandMeta{
Name: "DECR",
Syntax: "DECR key",
HelpShort: "DECR decrements the value of the specified key in args by 1",
HelpLong: `
DECR command decrements the integer at 'key' by one. Creates 'key' as -1 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> DECR k
OK 42
`,
Eval: evalDECR,
Execute: executeDECR,
}
  • Purpose: Defines the metadata for the DECR command, including its name, syntax, help text, example usage, and associated functions for evaluation and execution.
  • Fields:
    • Name: The name of the command ("DECR").
    • Syntax: The syntax of the command ("DECR 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 (evalDECR).
    • Execute: The function to execute the command (executeDECR).

init

func init() {
CommandRegistry.AddCommand(cDECR)
}
  • Purpose: Registers the DECR command with the CommandRegistry. This function is automatically called when the package is initialized.

evalDECR

func evalDECR(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("DECR")
}

return doIncr(c, s, -1)
}
  • Purpose: Evaluates the DECR command. It retrieves the key from the arguments, decrements the integer value by 1, and returns the new value.
  • Parameters:
    • c (*Cmd): The command context containing the arguments.
    • s (*dstore.Store): The data store instance.
  • Returns:
    • *CmdRes: Response containing the new integer value after decrement.
    • error: Error if the wrong number of arguments is provided or if the value is of the wrong type.

executeDECR

func executeDECR(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("DECR")
}

shard := sm.GetShardForKey(c.C.Args[0])
return evalDECR(c, shard.Thread.Store())
}
  • Purpose: Executes the DECR command. It determines the shard responsible for the key, retrieves the store from that shard, and then calls evalDECR to perform the decrement operation.
  • Parameters:
    • c (*Cmd): The command context containing the arguments.
    • sm (*shardmanager.ShardManager): The shard manager instance.
  • Returns:
    • *CmdRes: Response containing the new integer value after decrement.
    • error: Error if the wrong number of arguments is provided or if evalDECR returns an error.

Getting Started Relevance