Skip to main content

cmd_incr.go

cmd_incr.go - Overview

This file defines the INCR command, which increments the integer value of a specified key by 1 within a DiceDB instance. It includes the command's metadata, evaluation, and execution logic.

Detailed Documentation

cINCR

var cINCR = &CommandMeta{
Name: "INCR",
Syntax: "INCR key",
HelpShort: "INCR increments the value of the specified key in args by 1",
HelpLong: `
INCR command increments 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> INCR k
OK 44
`,
Eval: evalINCR,
Execute: executeINCR,
}
  • Purpose: Defines the metadata for the INCR command, including its name, syntax, help text, examples, and the functions to evaluate and execute it.
  • Fields:
    • Name: The name of the command ("INCR").
    • Syntax: The command's syntax ("INCR 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 (evalINCR).
    • Execute: The function to execute the command (executeINCR).

init

func init() {
CommandRegistry.AddCommand(cINCR)
}
  • Purpose: Registers the INCR command with the CommandRegistry during package initialization.

evalINCR

func evalINCR(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("INCR")
}
return doIncr(c, s, 1)
}
  • Purpose: Evaluates the INCR command by incrementing the value at the given key in the store. It checks for the correct number of arguments and calls doIncr to perform the actual increment operation.
  • Parameters:
    • c *Cmd: The command context.
    • s *dstore.Store: The data store.
  • Returns:
    • *CmdRes: The command result containing the incremented value.
    • error: An error if there's an issue with the arguments or the increment operation.

executeINCR

func executeINCR(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("INCR")
}
shard := sm.GetShardForKey(c.C.Args[0])
return evalINCR(c, shard.Thread.Store())
}
  • Purpose: Executes the INCR command. It retrieves the appropriate shard based on the key, then calls evalINCR on the store associated with that shard.
  • Parameters:
    • c *Cmd: The command context.
    • sm *shardmanager.ShardManager: The shard manager.
  • Returns:
    • *CmdRes: The command result containing the incremented value.
    • error: An error if there's an issue with the arguments or the increment operation.

Getting Started Relevance