Skip to main content

cmd_del.go

cmd_del.go - Overview

This file defines the DEL command, which deletes specified keys from the data store and returns the number of keys deleted.

Detailed Documentation

cDEL

var cDEL = &CommandMeta{
Name: "DEL",
Syntax: "DEL key [key ...]",
HelpShort: "DEL deletes all the specified keys",
HelpLong: `DEL command deletes all the specified keys and returns the number of keys deleted on success.`,
Examples: `
localhost:7379> SET k1 v1
OK OK
localhost:7379> SET k2 v2
OK OK
localhost:7379> DEL k1 k2 k3
OK 2`,
Eval: evalDEL,
Execute: executeDEL,
}
  • Purpose: Defines the metadata for the DEL command, including its name, syntax, help messages, examples, and the functions to evaluate and execute the command.
  • Fields:
    • Name: The name of the command ("DEL").
    • Syntax: The syntax of the command ("DEL key [key ...]").
    • HelpShort: A short description of the command.
    • HelpLong: A longer description of the command.
    • Examples: Example usage of the command.
    • Eval: The function to evaluate the command (evalDEL).
    • Execute: The function to execute the command (executeDEL).

evalDEL

func evalDEL(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) < 1 {
return cmdResNil, errors.ErrWrongArgumentCount("DEL")
}

var count int
for _, key := range c.C.Args {
if ok := s.Del(key); ok {
count++
}
}

return cmdResInt(int64(count)), nil
}
  • Purpose: Evaluates the DEL command by deleting the specified keys from the provided data store.
  • Parameters:
    • c (*Cmd): The command context, containing the arguments for the DEL command.
    • s (*dstore.Store): The data store instance to delete keys from.
  • Returns:
    • *CmdRes: A command result containing the number of keys deleted.
    • error: An error if the number of arguments is incorrect.

executeDEL

func executeDEL(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) < 1 {
return cmdResNil, errors.ErrWrongArgumentCount("DEL")
}

var count int64
for _, key := range c.C.Args {
shard := sm.GetShardForKey(key)
r, err := evalDEL(c, shard.Thread.Store())
if err != nil {
return nil, err
}
count += r.R.GetVInt()
}
return cmdResInt(count), nil
}
  • Purpose: Executes the DEL command by deleting the specified keys across shards managed by the shard manager.
  • Parameters:
    • c (*Cmd): The command context, containing the arguments for the DEL command.
    • sm (*shardmanager.ShardManager): The shard manager instance to retrieve shards from.
  • Returns:
    • *CmdRes: A command result containing the total number of keys deleted across all shards.
    • error: An error if the number of arguments is incorrect or if an error occurs during the evaluation of the command on a shard.

Code Examples

None.

Getting Started Relevance