forked from IceFireDB/IceFireDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.go
382 lines (317 loc) · 8.44 KB
/
lists.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* @Author: gitsrc
* @Date: 2021-03-08 17:57:04
* @LastEditors: gitsrc
* @LastEditTime: 2021-03-11 18:31:35
* @FilePath: /IceFireDB/lists.go
*/
package main
import (
"bytes"
"strconv"
"time"
"github.com/gitsrc/IceFireDB/rafthub"
"github.com/ledisdb/ledisdb/ledis"
"github.com/siddontang/go/hack"
"github.com/tidwall/redcon"
)
func init() {
//队列的block类型指令均需要避免,危险操作
//conf.AddReadCommand("BLPOP", cmdBLPOP) //此处危险:如果是raft写指令,则raft会进行指令回滚=>卡住raft,如果是raft读指令,则会因为raft无法回滚队列消费日志,出现队列脏数据
conf.AddWriteCommand("RPUSH", cmdRPUSH)
conf.AddWriteCommand("LPOP", cmdLPOP)
conf.AddReadCommand("LINDEX", cmdLINDEX)
conf.AddWriteCommand("LPUSH", cmdLPUSH)
conf.AddWriteCommand("RPOP", cmdRPOP)
conf.AddReadCommand("LRANGE", cmdLRANGE)
conf.AddWriteCommand("LSET", cmdLSET)
conf.AddReadCommand("LLEN", cmdLLEN)
conf.AddWriteCommand("RPOPLPUSH", cmdRPOPLPUSH)
//IceFireDB special command
conf.AddWriteCommand("LCLEAR", cmdLCLEAR)
conf.AddWriteCommand("LMCLEAR", cmdLMCLEAR)
//conf.AddWriteCommand("LEXPIRE", cmdLEXPIRE) //超时时间指令:谨慎,raft日志回滚,造成脏数据:超时时间 LEXPIRE => LEXPIREAT
conf.AddWriteCommand("LEXPIREAT", cmdLEXPIREAT)
conf.AddReadCommand("LTTL", cmdLTTL)
// conf.AddWriteCommand("LPERSIST", cmdLPERSIST)
conf.AddReadCommand("LKEYEXISTS", cmdLKEYEXISTS)
conf.AddWriteCommand("LTRIM", cmdLTRIM)
}
func cmdLTRIM(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 4 {
return nil, rafthub.ErrWrongNumArgs
}
var start int64
var stop int64
var err error
start, err = ledis.StrInt64([]byte(args[2]), nil)
if err != nil {
return nil, err
}
stop, err = ledis.StrInt64([]byte(args[3]), nil)
if err != nil {
return nil, err
}
if err := ldb.LTrim([]byte(args[1]), start, stop); err != nil {
return nil, err
}
return redcon.SimpleString("OK"), nil
}
func cmdLKEYEXISTS(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 2 {
return nil, rafthub.ErrWrongNumArgs
}
n, err := ldb.LKeyExists([]byte(args[1]))
if err != nil {
return nil, err
}
return redcon.SimpleInt(n), nil
}
// func cmdLPERSIST(m rafthub.Machine, args []string) (interface{}, error) {
// if len(args) != 2 {
// return nil, rafthub.ErrWrongNumArgs
// }
// n, err := ldb.LPersist([]byte(args[1]))
// if err != nil {
// return nil, err
// }
// return redcon.SimpleInt(n), nil
// }
func cmdLTTL(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 2 {
return nil, rafthub.ErrWrongNumArgs
}
v, err := ldb.LTTL([]byte(args[1]))
if err != nil {
return nil, err
}
return redcon.SimpleInt(v), nil
}
func cmdLEXPIREAT(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 3 {
return nil, rafthub.ErrWrongNumArgs
}
when, err := ledis.StrInt64([]byte(args[2]), nil)
if err != nil {
return nil, err
}
//如果时间戳小于当前时间,则进行删除操作 :此处有边界条件:因为是队列,raft 日志回滚是顺序的
if when < time.Now().Unix() {
_, err := ldb.LClear([]byte(args[1]))
if err != nil {
return nil, err
}
return redcon.SimpleInt(0), nil
}
v, err := ldb.LExpireAt([]byte(args[1]), when)
if err != nil {
return nil, err
}
return redcon.SimpleInt(v), nil
}
func cmdLEXPIRE(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 3 {
return nil, rafthub.ErrWrongNumArgs
}
duration, err := ledis.StrInt64([]byte(args[2]), nil)
if err != nil {
return nil, err
}
v, err := ldb.LExpire([]byte(args[1]), duration)
if err != nil {
return nil, err
}
return redcon.SimpleInt(v), nil
}
func cmdLMCLEAR(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) < 2 {
return nil, rafthub.ErrWrongNumArgs
}
keys := make([][]byte, len(args)-1)
for i := 1; i < len(args); i++ {
keys[i-1] = []byte(args[i])
}
n, err := ldb.LMclear(keys...)
if err != nil {
return nil, err
}
return redcon.SimpleInt(n), nil
}
func cmdLCLEAR(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 2 {
return nil, rafthub.ErrWrongNumArgs
}
n, err := ldb.LClear([]byte(args[1]))
if err != nil {
return nil, err
}
return redcon.SimpleInt(n), nil
}
func cmdRPOPLPUSH(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 3 {
return nil, rafthub.ErrWrongNumArgs
}
source, dest := []byte(args[1]), []byte(args[2])
var ttl int64 = -1
if bytes.Compare(source, dest) == 0 {
var err error
ttl, err = ldb.LTTL(source)
if err != nil {
return nil, err
}
}
data, err := ldb.RPop(source)
if err != nil {
return nil, err
}
if data == nil {
return nil, nil
}
if _, err := ldb.LPush(dest, data); err != nil {
ldb.RPush(source, data) //revert pop
return nil, err
}
//reset ttl
if ttl != -1 {
ldb.LExpire(source, ttl)
}
return data, nil
}
func cmdLLEN(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 2 {
return nil, rafthub.ErrWrongNumArgs
}
n, err := ldb.LLen([]byte(args[1]))
if err != nil {
return nil, err
}
return redcon.SimpleInt(n), nil
}
func cmdLSET(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 4 {
return nil, rafthub.ErrWrongNumArgs
}
index, err := ledis.StrInt64([]byte(args[2]), nil)
if err != nil {
return nil, err
}
if err := ldb.LSet([]byte(args[1]), int32(index), []byte(args[3])); err != nil {
return nil, err
}
return redcon.SimpleString("OK"), nil
}
func cmdLRANGE(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 4 {
return nil, rafthub.ErrWrongNumArgs
}
var start int64
var stop int64
var err error
start, err = ledis.StrInt64([]byte(args[2]), nil)
if err != nil {
return nil, err
}
stop, err = ledis.StrInt64([]byte(args[3]), nil)
if err != nil {
return nil, err
}
v, err := ldb.LRange([]byte(args[1]), int32(start), int32(stop))
if err != nil {
return nil, err
}
return v, nil
}
func cmdRPOP(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 2 {
return nil, rafthub.ErrWrongNumArgs
}
v, err := ldb.RPop([]byte(args[1]))
if err != nil {
return nil, err
}
return v, nil
}
func cmdLPUSH(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) < 3 {
return nil, rafthub.ErrWrongNumArgs
}
argList := make([][]byte, len(args)-2)
for i := 2; i < len(args); i++ {
argList[i-2] = []byte(args[i])
}
n, err := ldb.LPush([]byte(args[1]), argList...)
if err != nil {
return nil, err
}
return redcon.SimpleInt(n), nil
}
func cmdLINDEX(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 3 {
return nil, rafthub.ErrWrongNumArgs
}
index, err := ledis.StrInt64([]byte(args[2]), nil)
if err != nil {
return nil, err
}
v, err := ldb.LIndex([]byte(args[1]), int32(index))
if err != nil {
return nil, err
}
return v, nil
}
func cmdLPOP(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) != 2 {
return nil, rafthub.ErrWrongNumArgs
}
v, err := ldb.LPop([]byte(args[1]))
if err != nil {
return nil, err
}
return v, nil
}
func cmdRPUSH(m rafthub.Machine, args []string) (interface{}, error) {
if len(args) < 3 {
return nil, rafthub.ErrWrongNumArgs
}
argList := make([][]byte, len(args)-2)
for i := 2; i < len(args); i++ {
argList[i-2] = []byte(args[i])
}
n, err := ldb.RPush([]byte(args[1]), argList...)
if err != nil {
return nil, err
}
return redcon.SimpleInt(n), nil
}
//此处危险:如果是raft写指令,则raft会进行指令回滚=>卡住raft,如果是raft读指令,则会因为raft无法回滚队列消费日志,出现队列脏数据
// func cmdBLPOP(m rafthub.Machine, args []string) (interface{}, error) {
// if len(args) < 3 {
// return nil, rafthub.ErrWrongNumArgs
// }
// keys, timeout, err := lParseBPopArgs(args)
// if err != nil {
// return nil, err
// }
// ay, err := ldb.BLPop(keys, timeout)
// if err != nil {
// return nil, err
// }
// return ay, nil
// }
func lParseBPopArgs(argsOrigin []string) (keys [][]byte, timeout time.Duration, err error) {
if len(argsOrigin) < 3 {
err = rafthub.ErrWrongNumArgs
return
}
args := make([][]byte, len(argsOrigin)-1)
for i := 1; i < len(argsOrigin); i++ {
args[i-1] = []byte(argsOrigin[i])
}
var t float64
if t, err = strconv.ParseFloat(hack.String(args[len(args)-1]), 64); err != nil {
return
}
timeout = time.Duration(t * float64(time.Second))
keys = args[0 : len(args)-1]
return
}