mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-05 08:46:56 +08:00
Add sscan, hscan, zscan
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/hdt3213/godis/lib/utils"
|
||||
"github.com/hdt3213/godis/redis/protocol"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (db *DB) getAsSet(key string) (*HashSet.Set, protocol.ErrorReply) {
|
||||
@@ -354,6 +355,53 @@ func execSRandMember(db *DB, args [][]byte) redis.Reply {
|
||||
return &protocol.EmptyMultiBulkReply{}
|
||||
}
|
||||
|
||||
func execSScan(db *DB, args [][]byte) redis.Reply {
|
||||
var count int = 10
|
||||
var pattern string = "*"
|
||||
if len(args) > 2 {
|
||||
for i := 2; i < len(args); i++ {
|
||||
arg := strings.ToLower(string(args[i]))
|
||||
if arg == "count" {
|
||||
count0, err := strconv.Atoi(string(args[i+1]))
|
||||
if err != nil {
|
||||
return &protocol.SyntaxErrReply{}
|
||||
}
|
||||
count = count0
|
||||
i++
|
||||
} else if arg == "match" {
|
||||
pattern = string(args[i+1])
|
||||
i++
|
||||
} else {
|
||||
return &protocol.SyntaxErrReply{}
|
||||
}
|
||||
}
|
||||
}
|
||||
key := string(args[0])
|
||||
// get entity
|
||||
set, errReply := db.getAsSet(key)
|
||||
if errReply != nil {
|
||||
return errReply
|
||||
}
|
||||
if set == nil {
|
||||
return &protocol.EmptyMultiBulkReply{}
|
||||
}
|
||||
cursor, err := strconv.Atoi(string(args[1]))
|
||||
if err != nil {
|
||||
return protocol.MakeErrReply("ERR invalid cursor")
|
||||
}
|
||||
|
||||
keysReply, nextCursor := set.SetScan(cursor, count, pattern)
|
||||
if nextCursor < 0 {
|
||||
return protocol.MakeErrReply("Invalid argument")
|
||||
}
|
||||
|
||||
result := make([]redis.Reply, 2)
|
||||
result[0] = protocol.MakeBulkReply([]byte(strconv.FormatInt(int64(nextCursor), 10)))
|
||||
result[1] = protocol.MakeMultiBulkReply(keysReply)
|
||||
|
||||
return protocol.MakeMultiRawReply(result)
|
||||
}
|
||||
|
||||
func init() {
|
||||
registerCommand("SAdd", execSAdd, writeFirstKey, undoSetChange, -3, flagWrite).
|
||||
attachCommandExtra([]string{redisFlagWrite, redisFlagDenyOOM, redisFlagFast}, 1, 1, 1)
|
||||
@@ -381,4 +429,6 @@ func init() {
|
||||
attachCommandExtra([]string{redisFlagWrite, redisFlagDenyOOM}, 1, 1, 1)
|
||||
registerCommand("SRandMember", execSRandMember, readFirstKey, nil, -2, flagReadOnly).
|
||||
attachCommandExtra([]string{redisFlagReadonly, redisFlagRandom}, 1, 1, 1)
|
||||
registerCommand("SScan", execSScan, readFirstKey, nil, -2, flagReadOnly).
|
||||
attachCommandExtra([]string{redisFlagReadonly, redisFlagSortForScript}, 1, 1, 1)
|
||||
}
|
||||
|
Reference in New Issue
Block a user