Skip to content

Commit

Permalink
add dbsize command
Browse files Browse the repository at this point in the history
  • Loading branch information
NaNShaner authored and HDT3213 committed May 6, 2024
1 parent dd073b0 commit 0c6b086
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
return protocol.MakeErrReply("NOAUTH Authentication required")
}

if cmdName == "dbsize" {
if ser, ok := cluster.db.(*database2.Server); ok {
return database2.DbSize(c, ser)
}
}

if cmdName == "multi" {
if len(cmdLine) != 1 {
return protocol.MakeArgNumErrReply(cmdName)
Expand Down
1 change: 1 addition & 0 deletions commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- keys
- bgrewriteaof
- copy
- dbsize
- String
- set
- setnx
Expand Down
3 changes: 3 additions & 0 deletions database/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ func (server *Server) Exec(c redis.Connection, cmdLine [][]byte) (result redis.R
if cmdName == "info" {
return Info(server, cmdLine[1:])
}
if cmdName == "dbsize" {
return DbSize(c, server)
}
if cmdName == "slaveof" {
if c != nil && c.InMultiState() {
return protocol.MakeErrReply("cannot use slave of database within multi")
Expand Down
5 changes: 5 additions & 0 deletions database/systemcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func isAuthenticated(c redis.Connection) bool {
return c.GetPassword() == config.Properties.RequirePass
}

func DbSize(c redis.Connection, db *Server) redis.Reply {
keys, _ := db.GetDBSize(c.GetDBIndex())
return protocol.MakeIntReply(int64(keys))
}

func GenGodisInfoString(section string, db *Server) []byte {
startUpTimeFromNow := getGodisRuninngTime()
switch section {
Expand Down
15 changes: 15 additions & 0 deletions database/systemcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/connection"
"github.com/hdt3213/godis/redis/protocol/asserts"
"math/rand"
"testing"
"time"
)

func TestPing(t *testing.T) {
Expand Down Expand Up @@ -59,3 +61,16 @@ func TestInfo(t *testing.T) {
ret = testServer.Exec(c, utils.ToCmdLine("INFO", "abc"))
asserts.AssertErrReply(t, ret, "Invalid section for 'info' command")
}

func TestDbSize(t *testing.T) {
c := connection.NewFakeConn()
rand.NewSource(time.Now().UnixNano())
randomNum := rand.Intn(10) + 1
for i := 0; i < randomNum; i++ {
key := utils.RandString(10)
value := utils.RandString(10)
testServer.Exec(c, utils.ToCmdLine("SET", key, value))
}
ret := testServer.Exec(c, utils.ToCmdLine("dbsize"))
asserts.AssertIntReply(t, ret, randomNum)
}

0 comments on commit 0c6b086

Please sign in to comment.