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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
package commands
import (
"errors"
"net"
"redisClone/pkg/core"
"redisClone/pkg/pubsub"
)
var dbInstance core.RedisDB
func SetDB(db core.RedisDB) {
dbInstance = db
}
func SingleKey(args []string) []string {
return []string{args[1]}
}
func AllSubsequentKeys(args []string) []string {
return args[1:]
}
func NoKeys(args []string) []string {
return nil
}
func RenameKeys(args []string) []string {
return []string{args[1], args[2]}
}
type BaseCommand struct{
MinArgs int
ExtractKeys func(args []string)[]string
Execute func(args []string, getShard func(k string) *core.Shard) interface{}
}
func DispatchBaseCommand(db core.RedisDB ,cmd BaseCommand, args []string) interface{} {
if len(args) < cmd.MinArgs {
return errors.New("wrong number of arguments")
}
keys := cmd.ExtractKeys(args)
db.Lock(keys)
defer db.Unlock(keys)
return cmd.Execute(args, db.GetShard)
}
type PubSubCommand struct{
MinArgs int
Execute func(hub *pubsub.Hub, conn net.Conn, args []string) interface{}
}
func DispatchPubSub(conn net.Conn, h *pubsub.Hub, cmd PubSubCommand, args []string) {
if len(args) < cmd.MinArgs {
conn.Write(core.SerializeRESP(errors.New("ERR wrong number of arguments")))
return
}
if res := cmd.Execute(h, conn, args); res != nil {
conn.Write(core.SerializeRESP(res))
}
}
var BaseRegistry = map[string]BaseCommand{
"GET": {
MinArgs: 2,
ExtractKeys: SingleKey,
Execute: Get,
},
"SET": {
MinArgs: 3,
ExtractKeys: SingleKey,
Execute: Set,
},
"DEL": {
MinArgs: 2,
ExtractKeys: AllSubsequentKeys,
Execute: Del,
},
"EXISTS": {
MinArgs: 2,
ExtractKeys: SingleKey,
Execute: Exists,
},
"INCR": {
MinArgs: 2,
ExtractKeys: SingleKey,
Execute: Incr,
},
"DECR": {
MinArgs: 2,
ExtractKeys: SingleKey,
Execute: Decr,
},
"RENAME": {
MinArgs: 3,
ExtractKeys: RenameKeys,
Execute: Rename,
},
"RPUSH": {
MinArgs: 3,
ExtractKeys: SingleKey,
Execute: Rpush,
},
"LPUSH": {
MinArgs: 3,
ExtractKeys: SingleKey,
Execute: Lpush,
},
"RPOP": {
MinArgs: 2,
ExtractKeys: SingleKey,
Execute: Rpop,
},
"LPOP": {
MinArgs: 2,
ExtractKeys: SingleKey,
Execute: Lpop,
},
"LRANGE": {
MinArgs: 4,
ExtractKeys: SingleKey,
Execute: Lrange,
},
"FLUSHALL": {
MinArgs: 1,
ExtractKeys: NoKeys,
Execute: Flushall,
},
"EXPIRE": {
MinArgs: 3,
ExtractKeys: SingleKey,
Execute: Expire,
},
"HSET": {
MinArgs: 4,
ExtractKeys: SingleKey,
Execute: Hset,
},
"HGET": {
MinArgs: 3,
ExtractKeys: SingleKey,
Execute: Hget,
},
"HGETALL": {
MinArgs: 2,
ExtractKeys: SingleKey,
Execute: Hgetall,
},
"HKEYS": {
MinArgs: 2,
ExtractKeys: SingleKey,
Execute: Hkeys,
},
"HVALUES": {
MinArgs: 2,
ExtractKeys: SingleKey,
Execute: Hvalues,
},
"TYPE": {
MinArgs: 2,
ExtractKeys: SingleKey,
Execute: Type,
},
"DBSIZE": {
MinArgs: 1,
ExtractKeys: NoKeys,
Execute: Dbsize,
},
"KEYS": {
MinArgs: 2,
ExtractKeys: NoKeys,
Execute: Keys,
},
"LREM": {
MinArgs: 4,
ExtractKeys: SingleKey,
Execute: Lrem,
},
"PING": {
MinArgs: 1,
ExtractKeys: NoKeys,
Execute: Ping,
},
}
var PubSubRegistry = map[string]PubSubCommand{
"SUBSCRIBE": {MinArgs: 2, Execute: Subscribe},
"UNSUBSCRIBE": {MinArgs: 1, Execute: Unsubscribe},
"PUBLISH": {MinArgs: 3, Execute: Publish},
}
|