cmd_exists.go
cmd_exists.go - Overview
This file defines the EXISTS
command, which returns the number of keys that exist among the provided arguments.
Detailed Documentation
cEXISTS
var cEXISTS = &CommandMeta{
Name: "EXISTS",
Syntax: "EXISTS key [key ...]",
HelpShort: "EXISTS returns the count of keys that exist among the given",
HelpLong: `EXISTS command returns the count of keys that exist among the given arguments without modifying them.`,
Examples: `
localhost:7379> SET k1 v1
OK
localhost:7379> SET k2 v2
OK
localhost:7379> EXISTS k1 k2 k3
OK 2
`,
Eval: evalEXISTS,
Execute: executeEXISTS,
}
- Purpose: Defines the metadata for the
EXISTS
command, including its name, syntax, help text, examples, and the functions to evaluate and execute the command. - Properties:
Name
: The name of the command ("EXISTS").Syntax
: The command's syntax ("EXISTS 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 (evalEXISTS
).Execute
: The function to execute the command (executeEXISTS
).
init
func init() {
CommandRegistry.AddCommand(cEXISTS)
}
- Purpose: Registers the
EXISTS
command with theCommandRegistry
.
evalEXISTS
func evalEXISTS(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) < 1 {
return cmdResNil, errors.ErrWrongArgumentCount("EXISTS")
}
var count int64
for _, key := range c.C.Args {
// GetNoTouch is used to check if a key exists in the store
// without updating its last access time.
if s.GetNoTouch(key) != nil {
count++
}
}
// Return the count as a response
return cmdResInt(count), nil
}
- Purpose: Evaluates the
EXISTS
command against a single store. - 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 command result.error
: An error if one occurred, otherwisenil
.
executeEXISTS
func executeEXISTS(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) < 1 {
return cmdResNil, errors.ErrWrongArgumentCount("EXISTS")
}
var count int64
var shardMap = make(map[*shard.Shard][]string)
for _, key := range c.C.Args {
shard := sm.GetShardForKey(key)
shardMap[shard] = append(shardMap[shard], key)
}
for shard, keys := range shardMap {
c.C.Args = keys
r, err := evalEXISTS(c, shard.Thread.Store())
if err != nil {
return nil, err
}
count += r.R.GetVInt()
}
return cmdResInt(count), nil
}
- Purpose: Executes the
EXISTS
command across multiple shards. - Parameters:
c
: A pointer to theCmd
struct representing the command.sm
: A pointer to theshardmanager.ShardManager
struct representing the shard manager.
- Returns:
*CmdRes
: A pointer to theCmdRes
struct representing the command result.error
: An error if one occurred, otherwisenil
.