Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#145 : Added support for COMMAND COUNT command #153

Merged
merged 7 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ var (
Info: "Quit the server",
Eval: nil,
}
commandCmdMeta = DiceCmdMeta{
Name: "COMMAND <subcommand>",
Info: "Evaluates COMMAND <subcommand> command based on subcommand",
Eval: evalCommand,
}
)

func init() {
Expand Down Expand Up @@ -346,4 +351,5 @@ func init() {
diceCmds["EXEC"] = execCmdMeta
diceCmds["DISCARD"] = discardCmdMeta
diceCmds["ABORT"] = abortCmdMeta
diceCmds["COMMAND"] = commandCmdMeta
}
25 changes: 23 additions & 2 deletions core/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"bytes"
"errors"
"fmt"
"github.com/dicedb/dice/config"
"log"
"strconv"
"strings"
"syscall"
"time"

"github.com/dicedb/dice/config"
)

var RESP_NIL []byte = []byte("$-1\r\n")
Expand All @@ -24,8 +23,10 @@ var RESP_EMPTY_ARRAY []byte = []byte("*0\r\n")

var txnCommands map[string]bool
var serverID string
var diceCommandsCount int

func init() {
diceCommandsCount = len(diceCmds)
txnCommands = map[string]bool{"EXEC": true, "DISCARD": true}
serverID = fmt.Sprintf("%s:%d", config.Host, config.Port)
}
Expand Down Expand Up @@ -857,6 +858,26 @@ func evalQWATCH(args []string, c *Client) []byte {
return RESP_OK
}

// evalCommand evaluates COMMAND <subcommand> command based on subcommand
// COUNT: return total count of commands in Dice.
func evalCommand(args []string) []byte {
if len(args) == 0 {
return Encode(errors.New("(error) ERR wrong number of arguments for 'command' command"), false)
}
subcommand := strings.ToUpper(args[0])
switch subcommand {
case "COUNT":
return evalCommandCount(nil)
default:
return Encode(fmt.Errorf("ERR unknown subcommand '%s'. Try COMMAND HELP", subcommand), false)
}
}

// evalCommandCount returns an number of commands supported by DiceDB
func evalCommandCount(args []string) []byte {
return Encode(diceCommandsCount, false)
}

func executeCommand(cmd *RedisCmd, c *Client) []byte {
diceCmd, ok := diceCmds[cmd.Cmd]
if !ok {
Expand Down
36 changes: 36 additions & 0 deletions tests/command_count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tests

import (
"fmt"
"gotest.tools/v3/assert"
"net"
"testing"
)

func TestCommandCount(t *testing.T) {
connection := getLocalConnection()
commandCount := getCommandCount(connection)
if commandCount <= 0 {
t.Fail()
}
assert.Assert(t, commandCount > 0,
fmt.Sprintf("Unexpected number of CLI commands found. expected greater than 0, %d found", commandCount))
}

func getCommandCount(connection net.Conn) int64 {
responseValue := fireCommand(connection, "COMMAND COUNT")
if responseValue == nil {
return -1
}
return responseValue.(int64)
}

func BenchmarkCountCommand(b *testing.B) {
connection := getLocalConnection()
for n := 0; n < b.N; n++ {
commandCount := getCommandCount(connection)
if commandCount <= 0 {
b.Fail()
}
}
}