-
Notifications
You must be signed in to change notification settings - Fork 23
/
service_redis.go
330 lines (270 loc) · 8.67 KB
/
service_redis.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package basalt
import (
"strconv"
"strings"
"github.com/tidwall/redcon"
)
// RedisService is a redis service only supports bitmap commands.
type RedisService struct {
s *Server
confChangeCallback ConfChange
}
func (rs *RedisService) redisAccept(conn redcon.Conn) bool {
return true
}
func (rs *RedisService) redisClose(conn redcon.Conn, err error) {
}
// redisHandler handles redis commands.
func (rs *RedisService) redisHandler(conn redcon.Conn, cmd redcon.Command) {
switch strings.ToLower(string(cmd.Args[0])) {
default:
conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
case "ping":
conn.WriteString("PONG")
case "quit":
conn.WriteString("OK")
conn.Close()
case "bmadd": // bitmap add
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
v, err := byte2uint32(cmd.Args[2])
if err != nil {
conn.WriteError("ERR wrong value for '" + string(cmd.Args[0]) + "' command because of " + err.Error())
return
}
rs.s.bitmaps.Add(string(cmd.Args[1]), v, true)
conn.WriteInt(1)
case "bmaddmany": // bitmap addmany
if len(cmd.Args) < 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
values, err := bytes2uint32(cmd.Args[2:])
if err != nil {
conn.WriteError("ERR wrong value for '" + string(cmd.Args[0]) + "' command because of " + err.Error())
return
}
rs.s.bitmaps.AddMany(string(cmd.Args[1]), values, true)
conn.WriteInt(len(values))
case "bmdel": // bitmap remove
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
v, err := byte2uint32(cmd.Args[2])
if err != nil {
conn.WriteError("ERR wrong value for '" + string(cmd.Args[0]) + "' command because of " + err.Error())
return
}
rs.s.bitmaps.Remove(string(cmd.Args[1]), v, true)
conn.WriteInt(1)
case "bmdrop": // bitmap remove_bitmap
if len(cmd.Args) != 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
rs.s.bitmaps.RemoveBitmap(string(cmd.Args[1]), true)
conn.WriteString("OK")
case "bmclear": // bitmap clear_bitmap
if len(cmd.Args) != 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
rs.s.bitmaps.ClearBitmap(string(cmd.Args[1]), true)
conn.WriteString("OK")
case "bmcard": // bitmap clear_bitmap
if len(cmd.Args) != 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
count := rs.s.bitmaps.Card(string(cmd.Args[1]))
conn.WriteInt64(int64(count))
case "bmexists": // bitmap exists
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
v, err := byte2uint32(cmd.Args[2])
if err != nil {
conn.WriteError("ERR wrong value for '" + string(cmd.Args[0]) + "' command because of " + err.Error())
return
}
existed := rs.s.bitmaps.Exists(string(cmd.Args[1]), v)
if existed {
conn.WriteInt(1)
} else {
conn.WriteInt(0)
}
case "bminter": // bitmap intersect
if len(cmd.Args) < 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
names := bytes2string(cmd.Args[1:])
rt := rs.s.bitmaps.Inter(names...)
conn.WriteArray(len(rt))
for _, v := range rt {
conn.WriteInt64(int64(v))
}
case "bminterstore": // bitmap intersect store
if len(cmd.Args) < 4 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
names := bytes2string(cmd.Args[1:])
count := rs.s.bitmaps.InterStore(names[0], names[1:]...)
conn.WriteInt64(int64(count))
case "bmunion": // bitmap union
if len(cmd.Args) < 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
names := bytes2string(cmd.Args[1:])
rt := rs.s.bitmaps.Union(names...)
conn.WriteArray(len(rt))
for _, v := range rt {
conn.WriteInt64(int64(v))
}
case "bmunionstore": // bitmap union store
if len(cmd.Args) < 4 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
names := bytes2string(cmd.Args[1:])
count := rs.s.bitmaps.UnionStore(names[0], names[1:]...)
conn.WriteInt64(int64(count))
case "bmxor": // bitmap xor
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
rt := rs.s.bitmaps.Xor(string(cmd.Args[1]), string(cmd.Args[2]))
conn.WriteArray(len(rt))
for _, v := range rt {
conn.WriteInt64(int64(v))
}
case "bmxorstore": // bitmap xor store
if len(cmd.Args) != 4 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
count := rs.s.bitmaps.XorStore(string(cmd.Args[1]), string(cmd.Args[2]), string(cmd.Args[3]))
conn.WriteInt64(int64(count))
case "bmdiff": // bitmap diff
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
rt := rs.s.bitmaps.Diff(string(cmd.Args[1]), string(cmd.Args[2]))
conn.WriteArray(len(rt))
for _, v := range rt {
conn.WriteInt64(int64(v))
}
case "bmdiffstore": // bitmap diff store
if len(cmd.Args) != 4 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
count := rs.s.bitmaps.DiffStore(string(cmd.Args[1]), string(cmd.Args[2]), string(cmd.Args[3]))
conn.WriteInt64(int64(count))
case "bmstats": // bitmap diff store
if len(cmd.Args) != 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
stats := rs.s.bitmaps.Stats(string(cmd.Args[1]))
var sb strings.Builder
appendMetric(&sb, "cardinality", stats.Cardinality)
appendMetric(&sb, "Containers", stats.Containers)
appendMetric(&sb, "ArrayContainers", stats.ArrayContainers)
appendMetric(&sb, "ArrayContainerBytes", stats.ArrayContainerBytes)
appendMetric(&sb, "ArrayContainerValues", stats.ArrayContainerValues)
appendMetric(&sb, "BitmapContainers", stats.BitmapContainers)
appendMetric(&sb, "BitmapContainerBytes", stats.BitmapContainerBytes)
appendMetric(&sb, "BitmapContainerValues", stats.BitmapContainerValues)
appendMetric(&sb, "RunContainers", stats.RunContainers)
appendMetric(&sb, "RunContainerBytes", stats.RunContainerBytes)
appendMetric(&sb, "RunContainerValues", stats.RunContainerValues)
conn.WriteBulkString(sb.String())
case "bmsave": // bitmap persist
if len(cmd.Args) != 1 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
err := rs.s.Save()
if err != nil {
conn.WriteError("ERR save because of " + err.Error())
return
}
conn.WriteInt(1)
case "addnode": // add raft node
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
if rs.confChangeCallback != nil {
nodeID := string(cmd.Args[1])
url := cmd.Args[2]
id, err := strconv.ParseUint(nodeID, 0, 64)
if err != nil {
conn.WriteError("ERR parse id because of " + err.Error())
return
}
err = rs.confChangeCallback.AddNode(id, url)
if err != nil {
conn.WriteError("ERR failed to add node because of " + err.Error())
return
}
}
conn.WriteInt(1)
case "removenode": // remove raft node
if len(cmd.Args) != 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
if rs.confChangeCallback != nil {
nodeID := string(cmd.Args[1])
id, err := strconv.ParseUint(nodeID, 0, 64)
if err != nil {
conn.WriteError("ERR parse id because of " + err.Error())
return
}
err = rs.confChangeCallback.RemoveNode(id)
if err != nil {
conn.WriteError("ERR failed to remove node because of " + err.Error())
return
}
}
conn.WriteInt(1)
}
}
func appendMetric(sb *strings.Builder, name string, v uint64) {
sb.WriteString(name)
sb.WriteString(":")
sb.WriteString(strconv.FormatUint(v, 10))
sb.WriteString("\r\n")
}
func byte2uint32(b []byte) (uint32, error) {
i, err := strconv.ParseUint(string(b), 10, 32)
return uint32(i), err
}
func bytes2uint32(b [][]byte) ([]uint32, error) {
var rt []uint32
for _, bt := range b {
i, err := strconv.ParseUint(string(bt), 10, 32)
if err != nil {
return nil, err
}
rt = append(rt, uint32(i))
}
return rt, nil
}
func bytes2string(b [][]byte) []string {
var rt []string
for _, bt := range b {
rt = append(rt, string(bt))
}
return rt
}