Files
redis-go/exec_helper.go
2021-06-15 01:03:13 +08:00

68 lines
1.6 KiB
Go

package godis
import (
"github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/redis/reply"
"strings"
)
func execNormalCommand(db *DB, cmdArgs [][]byte) redis.Reply {
cmdName := strings.ToLower(string(cmdArgs[0]))
cmd, ok := cmdTable[cmdName]
if !ok {
return reply.MakeErrReply("ERR unknown command '" + cmdName + "'")
}
if !validateArity(cmd.arity, cmdArgs) {
return reply.MakeArgNumErrReply(cmdName)
}
prepare := cmd.prepare
write, read := prepare(cmdArgs[1:])
db.RWLocks(write, read)
defer db.RWUnLocks(write, read)
fun := cmd.executor
return fun(db, cmdArgs[1:])
}
// ExecWithLock executes normal commands, invoker should provide locks
func (db *DB) ExecWithLock(cmdLine [][]byte) redis.Reply {
cmdName := strings.ToLower(string(cmdLine[0]))
cmd, ok := cmdTable[cmdName]
if !ok {
return reply.MakeErrReply("ERR unknown command '" + cmdName + "'")
}
if !validateArity(cmd.arity, cmdLine) {
return reply.MakeArgNumErrReply(cmdName)
}
fun := cmd.executor
return fun(db, cmdLine[1:])
}
// GetRelatedKeys analysis related keys
func (db *DB) GetRelatedKeys(cmdLine [][]byte) ([]string, []string) {
cmdName := strings.ToLower(string(cmdLine[0]))
cmd, ok := cmdTable[cmdName]
if !ok {
return nil, nil
}
prepare := cmd.prepare
if prepare == nil {
return nil, nil
}
return prepare(cmdLine[1:])
}
// GetUndoLogs return rollback commands
func (db *DB) GetUndoLogs(cmdLine [][]byte) []CmdLine {
cmdName := strings.ToLower(string(cmdLine[0]))
cmd, ok := cmdTable[cmdName]
if !ok {
return nil
}
undo := cmd.undo
if undo == nil {
return nil
}
return undo(db, cmdLine[1:])
}