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

add dbsize command #217

Closed
wants to merge 2 commits into from
Closed
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: 5 additions & 1 deletion cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
if !isAuthenticated(c) {
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
3 changes: 1 addition & 2 deletions database/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,8 +831,7 @@ func getRandomKey(db *DB, args [][]byte) redis.Reply {
if len(k) == 0 {
return &protocol.NullBulkReply{}
}
var key []byte
return protocol.MakeBulkReply(strconv.AppendQuote(key, k[0]))
return protocol.MakeBulkReply([]byte(k[0]))
}

func init() {
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)
}
Loading