-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathspecification.go
296 lines (263 loc) · 9.5 KB
/
specification.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2009-2012 Joubin Houshyar
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redis
//import "fmt"
// ----------------------------------------------------------------------------
// PROTOCOL SPEC
//
// Various other elements of the package use the artifacts of this file to
// negotiate the Redis protocol.
//
// Redis version: 1.n
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Types
// ----------------------------------------------------------------------------
// Redis 'KeyType'
//
type KeyType byte
// Known Redis key types
//
const (
RT_NONE KeyType = iota
RT_STRING
RT_SET
RT_LIST
RT_ZSET
)
// Returns KeyType by name
//
func GetKeyType(typename string) (keytype KeyType) {
switch {
case typename == "none":
keytype = RT_NONE
case typename == "string":
keytype = RT_STRING
case typename == "list":
keytype = RT_LIST
case typename == "set":
keytype = RT_SET
case typename == "zset":
keytype = RT_ZSET
default:
panic("BUG - unknown type: " + string(keytype))
}
return
}
// Not yet used -- TODO: decide if returning status (say for Set) for non error cases
// really buys us anything beyond (useless) consistency.
//
type Status bool
const (
OK = true
PONG = true
ERR = false
)
// Request type defines the characteristic pattern of a cmd request
//
type RequestType int
const (
_ RequestType = iota
NO_ARG
KEY
KEY_KEY
KEY_NUM
KEY_SPEC
KEY_NUM_NUM
KEY_VALUE
KEY_IDX_VALUE
KEY_KEY_VALUE
KEY_CNT_VALUE
MULTI_KEY
)
// Response type defines the various flavors of responses from Redis
// per its specification.
type ResponseType int
const (
VIRTUAL ResponseType = iota
BOOLEAN
NUMBER
STRING // REVU - rename to TYPE
STATUS
BULK
MULTI_BULK
)
// Describes a given Redis command
//
type Command struct {
Code string
ReqType RequestType
RespType ResponseType
}
// The supported Command set, with one to one mapping to eponymous Redis command.
//
var (
AUTH Command = Command{"AUTH", KEY, STATUS}
PING Command = Command{"PING", NO_ARG, STATUS}
QUIT Command = Command{"QUIT", NO_ARG, VIRTUAL}
SET Command = Command{"SET", KEY_VALUE, STATUS}
GET Command = Command{"GET", KEY, BULK}
GETSET Command = Command{"GETSET", KEY_VALUE, BULK}
MGET Command = Command{"MGET", MULTI_KEY, MULTI_BULK}
SETNX Command = Command{"SETNX", KEY_VALUE, BOOLEAN}
INCR Command = Command{"INCR", KEY, NUMBER}
INCRBY Command = Command{"INCRBY", KEY_NUM, NUMBER}
DECR Command = Command{"DECR", KEY, NUMBER}
DECRBY Command = Command{"DECRBY", KEY_NUM, NUMBER}
EXISTS Command = Command{"EXISTS", KEY, BOOLEAN}
DEL Command = Command{"DEL", KEY, BOOLEAN}
TYPE Command = Command{"TYPE", KEY, STRING}
KEYS Command = Command{"KEYS", KEY, MULTI_BULK}
RANDOMKEY Command = Command{"RANDOMKEY", NO_ARG, BULK}
RENAME Command = Command{"RENAME", KEY_KEY, STATUS}
RENAMENX Command = Command{"RENAMENX", KEY_KEY, BOOLEAN}
DBSIZE Command = Command{"DBSIZE", NO_ARG, NUMBER}
EXPIRE Command = Command{"EXPIRE", KEY_NUM, BOOLEAN}
TTL Command = Command{"TTL", KEY, NUMBER}
RPUSH Command = Command{"RPUSH", KEY_VALUE, STATUS}
LPUSH Command = Command{"LPUSH", KEY_VALUE, STATUS}
LLEN Command = Command{"LLEN", KEY, NUMBER}
LRANGE Command = Command{"LRANGE", KEY_NUM_NUM, MULTI_BULK}
LTRIM Command = Command{"LTRIM", KEY_NUM_NUM, STATUS}
LINDEX Command = Command{"LINDEX", KEY_NUM, BULK}
LSET Command = Command{"LSET", KEY_IDX_VALUE, STATUS}
LREM Command = Command{"LREM", KEY_CNT_VALUE, NUMBER}
LPOP Command = Command{"LPOP", KEY, BULK}
BLPOP Command = Command{"BLPOP", KEY, MULTI_BULK}
RPOP Command = Command{"RPOP", KEY, BULK}
BRPOP Command = Command{"BRPOP", KEY, MULTI_BULK}
RPOPLPUSH Command = Command{"RPOPLPUSH", KEY_VALUE, BULK}
BRPOPLPUSH Command = Command{"BRPOPLPUSH", KEY_VALUE, MULTI_BULK}
SADD Command = Command{"SADD", KEY_VALUE, BOOLEAN}
SREM Command = Command{"SREM", KEY_VALUE, BOOLEAN}
SCARD Command = Command{"SCARD", KEY, NUMBER}
SISMEMBER Command = Command{"SISMEMBER", KEY_VALUE, BOOLEAN}
SINTER Command = Command{"SINTER", MULTI_KEY, MULTI_BULK}
SINTERSTORE Command = Command{"SINTERSTORE", MULTI_KEY, STATUS}
SUNION Command = Command{"SUNION", MULTI_KEY, MULTI_BULK}
SUNIONSTORE Command = Command{"SUNIONSTORE", MULTI_KEY, STATUS}
SDIFF Command = Command{"SDIFF", MULTI_KEY, MULTI_BULK}
SDIFFSTORE Command = Command{"SDIFFSTORE", MULTI_KEY, STATUS}
SMEMBERS Command = Command{"SMEMBERS", KEY, MULTI_BULK}
SMOVE Command = Command{"SMOVE", KEY_KEY_VALUE, BOOLEAN}
SRANDMEMBER Command = Command{"SRANDMEMBER", KEY, BULK}
HGET Command = Command{"HGET", KEY_KEY, BULK}
HSET Command = Command{"HSET", KEY_KEY_VALUE, STATUS}
HGETALL Command = Command{"HGETALL", KEY, MULTI_BULK}
ZADD Command = Command{"ZADD", KEY_IDX_VALUE, BOOLEAN}
ZREM Command = Command{"ZREM", KEY_VALUE, BOOLEAN}
ZCARD Command = Command{"ZCARD", KEY, NUMBER}
ZSCORE Command = Command{"ZSCORE", KEY_VALUE, BULK}
ZRANGE Command = Command{"ZRANGE", KEY_NUM_NUM, MULTI_BULK}
ZREVRANGE Command = Command{"ZREVRANGE", KEY_NUM_NUM, MULTI_BULK}
ZRANGEBYSCORE Command = Command{"ZRANGEBYSCORE", KEY_NUM_NUM, MULTI_BULK}
SELECT Command = Command{"SELECT", KEY, STATUS}
FLUSHDB Command = Command{"FLUSHDB", NO_ARG, STATUS}
FLUSHALL Command = Command{"FLUSHALL", NO_ARG, STATUS}
MOVE Command = Command{"MOVE", KEY_NUM, BOOLEAN}
SORT Command = Command{"SORT", KEY_SPEC, MULTI_BULK}
SAVE Command = Command{"SAVE", NO_ARG, STATUS}
BGSAVE Command = Command{"BGSAVE", NO_ARG, STATUS}
LASTSAVE Command = Command{"LASTSAVE", NO_ARG, NUMBER}
SHUTDOWN Command = Command{"SHUTDOWN", NO_ARG, VIRTUAL}
INFO Command = Command{"INFO", NO_ARG, BULK}
MONITOR Command = Command{"MONITOR", NO_ARG, VIRTUAL}
// TODO SORT (RequestType.MULTI_KEY, ResponseType.MULTI_BULK),
PUBLISH Command = Command{"PUBLISH", KEY_VALUE, NUMBER}
SUBSCRIBE Command = Command{"SUBSCRIBE", MULTI_KEY, MULTI_BULK}
UNSUBSCRIBE Command = Command{"UNSUBSCRIBE", MULTI_KEY, MULTI_BULK}
PSUBSCRIBE Command = Command{"PSUBSCRIBE", MULTI_KEY, MULTI_BULK}
PUNSUBSCRIBE Command = Command{"PUNSUBSCRIBE", MULTI_KEY, MULTI_BULK}
)
// ----------------------------------------------------------------------
// BlackBox testing contract (WIP)
// ----------------------------------------------------------------------
// For use by the generated tests.
type MethodSpec struct {
NoPanics bool
NoRedisErr bool
NoNilResultValue bool
NoNilFuture bool
}
// For use by the generated tests.
//
// Get MethodSpec for the given client type (name) and Method.
//
func GetMethodSpec(client, method string) (spec *MethodSpec) {
// Default spec for all clients and methods
//
// - Never panic
// - Never return redisError
// - Never return nil/zerovalue for any non-error result
defspec := &MethodSpec{
NoPanics: true,
NoRedisErr: true,
NoNilResultValue: true,
NoNilFuture: true,
}
// Allow client specific mods to the default spec, as required.
//
switch client {
case "Client":
spec = syncClientMethodSpec(defspec, method)
case "AsyncClient":
spec = asyncClientMethodSpec(defspec, method)
case "PubSubClient":
spec = pubsubClientMethodSpec(defspec, method)
}
return
}
// spec.NoRedisErr
// Specs MethodSpec.NoRedisErr - client type invariant
func anyClientRedisErrSpec(spec *MethodSpec, method string) *MethodSpec {
// Commands that can return ERR given random input and state
switch method {
// Redis-Spec: Background save may be running already and can raise -ERR
case "Bgsave":
spec.NoRedisErr = false
}
return spec
}
// spec.NoNilResultValue
// Specs MethodSpec.NoNilResultValue - client type invariant
func anyClientNilResultValueSpec(spec *MethodSpec, method string) *MethodSpec {
// Commands that can return nil/zero-value given random input and state
switch method {
// Redis-Spec: DB may be empty and AllKeys can return nil result
// REVU: check if this is actually conformant
case "AllKeys":
spec.NoNilResultValue = false
}
return spec
}
func syncClientMethodSpec(defspec *MethodSpec, method string) (spec *MethodSpec) {
spec = defspec
// spec.NoRedisErr
spec = anyClientRedisErrSpec(spec, method)
spec = anyClientNilResultValueSpec(spec, method)
return
}
func asyncClientMethodSpec(defspec *MethodSpec, method string) (spec *MethodSpec) {
spec = defspec
// spec.NoRedisErr
spec = anyClientRedisErrSpec(spec, method)
spec = anyClientNilResultValueSpec(spec, method)
return
}
func pubsubClientMethodSpec(defspec *MethodSpec, method string) (spec *MethodSpec) {
spec = defspec
switch method {
}
return
}