Skip to main content

cmd_zcount.go

cmd_zcount.go - Overview

This file implements the ZCOUNT command, which counts the number of members in a sorted set within a specified range.

Detailed Documentation

cZCOUNT

var cZCOUNT = &CommandMeta{
Name: "ZCOUNT",
Syntax: "ZCOUNT key min max",
HelpShort: "Counts the number of members in a sorted set between min and max (inclusive)",
HelpLong: `
Counts the number of members in a sorted set between min and max (inclusive)
Use -inf and +inf for unbounded ranges
Returns the count of members in a sorted set between min and max
Returns 0 if the key does not exist
`,
Examples: `
localhost:7379> ZCOUNT myzset 11 15
OK 0

localhost:7379> ZCOUNT myzset 5 10
OK 3

localhost:7379> ZCOUNT myzset 11
ERR wrong number of arguments for 'ZCOUNT' command
`,
Eval: evalZCOUNT,
Execute: executeZCOUNT,
}
  • Purpose: Defines the metadata for the ZCOUNT command, including its name, syntax, help messages, examples, and the functions to evaluate and execute the command.
  • Properties:
    • Name: The name of the command ("ZCOUNT").
    • Syntax: The command's syntax ("ZCOUNT key min max").
    • HelpShort: A short description of the command.
    • HelpLong: A detailed description of the command, including usage with -inf and +inf.
    • Examples: Examples of how to use the command.
    • Eval: The evalZCOUNT function, which evaluates the command.
    • Execute: The executeZCOUNT function, which executes the command.

init

func init() {
CommandRegistry.AddCommand(cZCOUNT)
}
  • Purpose: Registers the ZCOUNT command with the CommandRegistry.

evalZCOUNT

func evalZCOUNT(c *Cmd, s *dstore.Store) (*CmdRes, error) {
// check number of arguments
if len(c.C.Args) != 3 {
return cmdResNil, errors.ErrWrongArgumentCount("ZCOUNT")
}

key := c.C.Args[0]
minArg := c.C.Args[1]
maxArg := c.C.Args[2]

// parse minVal and maxVal scores
minVal, errMin := strconv.ParseFloat(minArg, 64)
maxVal, errMax := strconv.ParseFloat(maxArg, 64)
if errMin != nil || errMax != nil {
return cmdResNil, errors.ErrInvalidNumberFormat
}

// retrieve object from store
obj := s.Get(key)
if obj == nil {
return cmdResInt0, nil
}

// ensure object is a valid sorted set
var sortedSet *sortedset.Set
sortedSet, err := sortedset.FromObject(obj)
if err != nil {
return cmdResNil, errors.ErrWrongTypeOperation
}

// get count of members within range from sorted set
count := sortedSet.CountInRange(minVal, maxVal)

return &CmdRes{R: &wire.Response{
Value: &wire.Response_VInt{VInt: int64(count)},
}}, nil
}
  • Purpose: Evaluates the ZCOUNT command, retrieves the sorted set from the store, and counts the number of members within the specified range.
  • Parameters:
    • c: A pointer to the Cmd struct, which contains the command arguments.
    • s: A pointer to the dstore.Store struct, which represents the data store.
  • Returns:
    • A pointer to a CmdRes struct, which contains the result of the command.
    • An error, if any occurred.

executeZCOUNT

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

shard := sm.GetShardForKey(c.C.Args[0])
return evalZCOUNT(c, shard.Thread.Store())
}
  • Purpose: Executes the ZCOUNT command by retrieving the appropriate shard and calling evalZCOUNT on that shard's store.
  • Parameters:
    • c: A pointer to the Cmd struct, which contains the command arguments.
    • sm: A pointer to the shardmanager.ShardManager struct, which manages the shards.
  • Returns:
    • A pointer to a CmdRes struct, which contains the result of the command.
    • An error, if any occurred.

Getting Started Relevance