mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-05 00:42:43 +08:00
add dbsize command
This commit is contained in:
@@ -155,6 +155,12 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
|
|||||||
return protocol.MakeErrReply("NOAUTH Authentication required")
|
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 cmdName == "multi" {
|
||||||
if len(cmdLine) != 1 {
|
if len(cmdLine) != 1 {
|
||||||
return protocol.MakeArgNumErrReply(cmdName)
|
return protocol.MakeArgNumErrReply(cmdName)
|
||||||
|
@@ -19,6 +19,7 @@
|
|||||||
- keys
|
- keys
|
||||||
- bgrewriteaof
|
- bgrewriteaof
|
||||||
- copy
|
- copy
|
||||||
|
- dbsize
|
||||||
- String
|
- String
|
||||||
- set
|
- set
|
||||||
- setnx
|
- setnx
|
||||||
|
@@ -117,6 +117,9 @@ func (server *Server) Exec(c redis.Connection, cmdLine [][]byte) (result redis.R
|
|||||||
if cmdName == "info" {
|
if cmdName == "info" {
|
||||||
return Info(server, cmdLine[1:])
|
return Info(server, cmdLine[1:])
|
||||||
}
|
}
|
||||||
|
if cmdName == "dbsize" {
|
||||||
|
return DbSize(c, server)
|
||||||
|
}
|
||||||
if cmdName == "slaveof" {
|
if cmdName == "slaveof" {
|
||||||
if c != nil && c.InMultiState() {
|
if c != nil && c.InMultiState() {
|
||||||
return protocol.MakeErrReply("cannot use slave of database within multi")
|
return protocol.MakeErrReply("cannot use slave of database within multi")
|
||||||
|
@@ -74,6 +74,11 @@ func isAuthenticated(c redis.Connection) bool {
|
|||||||
return c.GetPassword() == config.Properties.RequirePass
|
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 {
|
func GenGodisInfoString(section string, db *Server) []byte {
|
||||||
startUpTimeFromNow := getGodisRuninngTime()
|
startUpTimeFromNow := getGodisRuninngTime()
|
||||||
switch section {
|
switch section {
|
||||||
|
@@ -5,7 +5,9 @@ import (
|
|||||||
"github.com/hdt3213/godis/lib/utils"
|
"github.com/hdt3213/godis/lib/utils"
|
||||||
"github.com/hdt3213/godis/redis/connection"
|
"github.com/hdt3213/godis/redis/connection"
|
||||||
"github.com/hdt3213/godis/redis/protocol/asserts"
|
"github.com/hdt3213/godis/redis/protocol/asserts"
|
||||||
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPing(t *testing.T) {
|
func TestPing(t *testing.T) {
|
||||||
@@ -59,3 +61,16 @@ func TestInfo(t *testing.T) {
|
|||||||
ret = testServer.Exec(c, utils.ToCmdLine("INFO", "abc"))
|
ret = testServer.Exec(c, utils.ToCmdLine("INFO", "abc"))
|
||||||
asserts.AssertErrReply(t, ret, "Invalid section for 'info' command")
|
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)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user