forked from HDT3213/godis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
81 lines (75 loc) · 2.25 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package godis
import (
"fmt"
"github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/lib/logger"
"github.com/hdt3213/godis/pubsub"
"github.com/hdt3213/godis/redis/reply"
"runtime/debug"
"strings"
)
// Exec executes command
// parameter `cmdLine` contains command and its arguments, for example: "set key value"
func (db *DB) Exec(c redis.Connection, cmdLine [][]byte) (result redis.Reply) {
defer func() {
if err := recover(); err != nil {
logger.Warn(fmt.Sprintf("error occurs: %v\n%s", err, string(debug.Stack())))
result = &reply.UnknownErrReply{}
}
}()
cmdName := strings.ToLower(string(cmdLine[0]))
// authenticate
if cmdName == "auth" {
return Auth(db, c, cmdLine[1:])
}
if !isAuthenticated(c) {
return reply.MakeErrReply("NOAUTH Authentication required")
}
// special commands
done := false
result, done = execSpecialCmd(c, cmdLine, cmdName, db)
if done {
return result
}
if c != nil && c.InMultiState() {
return EnqueueCmd(db, c, cmdLine)
}
// normal commands
return execNormalCommand(db, cmdLine)
}
func execSpecialCmd(c redis.Connection, cmdLine [][]byte, cmdName string, db *DB) (redis.Reply, bool) {
if cmdName == "subscribe" {
if len(cmdLine) < 2 {
return reply.MakeArgNumErrReply("subscribe"), true
}
return pubsub.Subscribe(db.hub, c, cmdLine[1:]), true
} else if cmdName == "publish" {
return pubsub.Publish(db.hub, cmdLine[1:]), true
} else if cmdName == "unsubscribe" {
return pubsub.UnSubscribe(db.hub, c, cmdLine[1:]), true
} else if cmdName == "bgrewriteaof" {
// aof.go imports router.go, router.go cannot import BGRewriteAOF from aof.go
return BGRewriteAOF(db, cmdLine[1:]), true
} else if cmdName == "multi" {
if len(cmdLine) != 1 {
return reply.MakeArgNumErrReply(cmdName), true
}
return StartMulti(db, c), true
} else if cmdName == "discard" {
if len(cmdLine) != 1 {
return reply.MakeArgNumErrReply(cmdName), true
}
return DiscardMulti(db, c), true
} else if cmdName == "exec" {
if len(cmdLine) != 1 {
return reply.MakeArgNumErrReply(cmdName), true
}
return execMulti(db, c), true
} else if cmdName == "watch" {
if !validateArity(-2, cmdLine) {
return reply.MakeArgNumErrReply(cmdName), true
}
return Watch(db, c, cmdLine[1:]), true
}
return nil, false
}