forked from bpaquet/aerodis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
657 lines (602 loc) · 15.5 KB
/
commands.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
package main
import (
"errors"
"io"
"strconv"
as "github.com/aerospike/aerospike-client-go"
ase "github.com/aerospike/aerospike-client-go/types"
)
func cmdDEL(wf io.Writer, ctx *context, args [][]byte) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
existed, err := ctx.client.Delete(ctx.writePolicy, key)
if err != nil {
return err
}
if existed {
return writeLine(wf, ":1")
}
return writeLine(wf, ":0")
}
func get(wf io.Writer, ctx *context, k []byte, binName string) error {
key, err := buildKey(ctx, k)
if err != nil {
return err
}
rec, err := ctx.client.Get(ctx.readPolicy, key, binName)
if err != nil {
return err
}
return writeBin(wf, rec, binName, "$-1")
}
func cmdGET(wf io.Writer, ctx *context, args [][]byte) error {
return get(wf, ctx, args[0], binName)
}
func cmdMGET(wf io.Writer, ctx *context, args [][]byte) error {
res := make([]*as.Record, len(args))
for i := 0; i < len(args); i++ {
key, err := buildKey(ctx, args[i])
if err != nil {
return err
}
rec, err := ctx.client.Get(ctx.readPolicy, key, binName)
if err != nil {
return err
}
res[i] = rec
}
return writeArrayBin(wf, res, binName, "")
}
func cmdHGET(wf io.Writer, ctx *context, args [][]byte) error {
return get(wf, ctx, args[0], string(args[1]))
}
func setex(wf io.Writer, ctx *context, k []byte, binName string, content []byte, ttl int, createOnly bool) error {
key, err := buildKey(ctx, k)
if err != nil {
return err
}
err = ctx.client.PutBins(createWritePolicyEx(ttl, createOnly), key, as.NewBin(binName, encode(ctx, content)))
if err != nil {
if createOnly && errResultCode(err) == ase.KEY_EXISTS_ERROR {
return writeLine(wf, ":0")
}
return err
}
if createOnly {
return writeLine(wf, ":1")
}
return writeLine(wf, "+OK")
}
func cmdSET(wf io.Writer, ctx *context, args [][]byte) error {
return setex(wf, ctx, args[0], binName, args[1], -2, false)
}
func cmdSETEX(wf io.Writer, ctx *context, args [][]byte) error {
ttl, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
return setex(wf, ctx, args[0], binName, args[2], ttl, false)
}
func cmdMSET(wf io.Writer, ctx *context, args [][]byte) error {
for i := 0; i+1 < len(args); i += 2 {
key, err := buildKey(ctx, args[i])
if err != nil {
return err
}
err = ctx.client.PutBins(ctx.writePolicy, key, as.NewBin(binName, encode(ctx, args[i+1])))
if err != nil {
return err
}
}
return writeLine(wf, "+OK")
}
func cmdSETNX(wf io.Writer, ctx *context, args [][]byte) error {
return setex(wf, ctx, args[0], binName, args[1], -1, true)
}
func cmdSETNXEX(wf io.Writer, ctx *context, args [][]byte) error {
ttl, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
return setex(wf, ctx, args[0], binName, args[2], ttl, true)
}
func tryHSet(ctx *context, key *as.Key, field string, value interface{}, ttl int) (bool, error) {
policy := as.NewPolicy()
policy.ReplicaPolicy = as.MASTER
rec, err := ctx.client.Get(policy, key, field)
if err != nil {
return false, err
}
var generation uint32
if rec != nil {
generation = rec.Generation
} else {
generation = 0
}
err = ctx.client.PutBins(createWritePolicyGeneration(generation, ttl), key, as.NewBin(field, value))
if err != nil {
return false, err
}
if rec == nil {
return false, nil
}
if rec.Bins[field] == nil {
return false, nil
}
return true, nil
}
func hset(wf io.Writer, ctx *context, k []byte, kk []byte, v interface{}, ttl int, set bool) error {
key, err := buildKey(ctx, k)
if err != nil {
return err
}
field := string(kk)
for i := 0; i < ctx.generationRetries; i++ {
existed, err := tryHSet(ctx, key, field, v, ttl)
if err == nil {
if !set {
existed = !existed
}
if existed {
return writeLine(wf, ":0")
}
return writeLine(wf, ":1")
}
if errResultCode(err) != ase.GENERATION_ERROR {
return err
}
}
return errors.New("Too many retry for hset")
}
func cmdHSET(wf io.Writer, ctx *context, args [][]byte) error {
return hset(wf, ctx, args[0], args[1], args[2], -1, true)
}
func cmdHSETEX(wf io.Writer, ctx *context, args [][]byte) error {
ttl, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
return hset(wf, ctx, args[0], args[2], args[3], ttl, true)
}
func cmdHDEL(wf io.Writer, ctx *context, args [][]byte) error {
return hset(wf, ctx, args[0], args[1], nil, -1, false)
}
func listOpReturnSize(wf io.Writer, ctx *context, args [][]byte, ttl int, op *as.Operation) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
rec, err := ctx.client.Operate(createWritePolicyEx(ttl, false), key, op, as.AddOp(as.NewBin(sizeArrayField, 1)))
if err != nil {
return err
}
return writeBinInt(wf, rec, binName)
}
func arrayRPush(wf io.Writer, ctx *context, args [][]byte, ttl int) error {
// ListInsertOp does not like to be called on an empty list and -1
// ListAppendOp is ok
return listOpReturnSize(wf, ctx, args, ttl, as.ListAppendOp(binName, encode(ctx, args[1])))
}
func cmdRPUSH(wf io.Writer, ctx *context, args [][]byte) error {
return arrayRPush(wf, ctx, args, -1)
}
func cmdRPUSHEX(wf io.Writer, ctx *context, args [][]byte) error {
ttl, err := strconv.Atoi(string(args[2]))
if err != nil {
return err
}
return arrayRPush(wf, ctx, args, ttl)
}
func arrayLPush(wf io.Writer, ctx *context, args [][]byte, ttl int) error {
return listOpReturnSize(wf, ctx, args, ttl, as.ListInsertOp(binName, 0, encode(ctx, args[1])))
}
func cmdLPUSH(wf io.Writer, ctx *context, args [][]byte) error {
return arrayLPush(wf, ctx, args, -1)
}
func cmdLPUSHEX(wf io.Writer, ctx *context, args [][]byte) error {
ttl, err := strconv.Atoi(string(args[2]))
if err != nil {
return err
}
return arrayLPush(wf, ctx, args, ttl)
}
func arrayPop(wf io.Writer, ctx *context, args [][]byte, index int) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
size, err := ctx.client.Get(ctx.readPolicy, key, sizeArrayField)
if err != nil {
return err
}
if size == nil {
return writeLine(wf, "$-1")
}
if size.Bins[sizeArrayField] == nil {
return writeLine(wf, "$-1")
}
if size.Bins[sizeArrayField].(int) == 0 {
return writeLine(wf, "$-1")
}
rec, err := ctx.client.Operate(ctx.writePolicy, key, as.ListPopOp(binName, index), as.AddOp(as.NewBin(sizeArrayField, -1)))
code := errResultCode(err)
if code == ase.BIN_TYPE_ERROR || code == ase.PARAMETER_ERROR {
return writeLine(wf, "$-1")
}
if err != nil {
return err
}
result := rec.Bins[binName]
switch result.(type) {
case int:
return writeByteArray(wf, []byte(strconv.Itoa(result.(int))))
default:
return writeByteArray(wf, result.([]byte))
}
}
func cmdRPOP(wf io.Writer, ctx *context, args [][]byte) error {
return arrayPop(wf, ctx, args, -1)
}
func cmdLPOP(wf io.Writer, ctx *context, args [][]byte) error {
return arrayPop(wf, ctx, args, 0)
}
func cmdLLEN(wf io.Writer, ctx *context, args [][]byte) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
rec, err := ctx.client.Get(ctx.readPolicy, key, sizeArrayField)
if err != nil {
return err
}
return writeBinIntFull(wf, rec, sizeArrayField, ":0", "$-1")
}
func cmdLRANGE(wf io.Writer, ctx *context, args [][]byte) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
start, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
stop, err := strconv.Atoi(string(args[2]))
if err != nil {
return err
}
result, _, _, err := arrayRange(ctx, key, start, stop)
if err != nil {
return err
}
return writeArray(wf, result)
}
func arrayRange(ctx *context, key *as.Key, start int, stop int) ([]interface{}, uint32, bool, error) {
if ((stop > 0 && start > 0) || (stop < 0 && start < 0)) && start > stop {
return make([]interface{}, 0), 0, false, nil
}
count := stop - start + 1
if stop < start {
count -= 1
}
rec, err := ctx.client.Operate(ctx.writePolicy, key, as.ListGetRangeOp(binName, start, count), as.ListSizeOp(binName))
if errResultCode(err) == ase.PARAMETER_ERROR {
return make([]interface{}, 0), 0, false, nil
}
if err != nil {
return nil, 0, false, err
}
if rec == nil {
return make([]interface{}, 0), 0, true, nil
}
a := rec.Bins[binName].([]interface{})
size, result := a[len(a)-1], a[:len(a)-1]
if start < 0 && stop >= 0 {
end := size.(int) + start + 1
if end < len(result) {
result = result[0:end]
}
}
if start >= 0 && stop < 0 {
end := size.(int) + stop + 1
if end < len(result) {
result = result[0:end]
}
}
return result, rec.Generation, false, nil
}
func cmdLTRIM(wf io.Writer, ctx *context, args [][]byte) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
start, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
stop, err := strconv.Atoi(string(args[2]))
if err != nil {
return err
}
for i := 0; i < ctx.generationRetries; i++ {
err := tryLTRIM(wf, ctx, key, start, stop)
if err == nil {
return writeLine(wf, "+OK")
}
if errResultCode(err) != ase.GENERATION_ERROR {
return err
}
}
return errors.New("Too many retry for ltrim")
}
func tryLTRIM(wf io.Writer, ctx *context, key *as.Key, start int, stop int) error {
result, generation, nonExistent, err := arrayRange(ctx, key, start, stop)
if err != nil {
return err
}
ops := make([]*as.Operation, 1)
ops[0] = as.ListClearOp(binName)
if len(result) > 0 {
ops = append(ops, as.ListAppendOp(binName, result...))
}
ops = append(ops, as.PutOp(as.NewBin(sizeArrayField, len(result))))
policy := ctx.writePolicy
if nonExistent {
return nil
}
if generation > 0 {
policy = createWritePolicyGeneration(generation, -1)
}
_, err = ctx.client.Operate(policy, key, ops...)
if err != nil {
return err
}
return nil
}
func hIncrByEx(wf io.Writer, ctx *context, k []byte, field string, incr int, ttl int) error {
key, err := buildKey(ctx, k)
if err != nil {
return err
}
bin := as.NewBin(field, incr)
rec, err := ctx.client.Operate(createWritePolicyEx(ttl, false), key, as.AddOp(bin), as.GetOpForBin(field))
if err != nil {
if errResultCode(err) == ase.BIN_TYPE_ERROR {
return writeLine(wf, "$-1")
}
return err
}
return writeBinInt(wf, rec, field)
}
func cmdINCR(wf io.Writer, ctx *context, args [][]byte) error {
return hIncrByEx(wf, ctx, args[0], binName, 1, -1)
}
func cmdDECR(wf io.Writer, ctx *context, args [][]byte) error {
return hIncrByEx(wf, ctx, args[0], binName, -1, -1)
}
func cmdINCRBY(wf io.Writer, ctx *context, args [][]byte) error {
incr, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
return hIncrByEx(wf, ctx, args[0], binName, incr, -1)
}
func cmdINCRBYEX(wf io.Writer, ctx *context, args [][]byte) error {
ttl, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
incr, err := strconv.Atoi(string(args[2]))
if err != nil {
return err
}
return hIncrByEx(wf, ctx, args[0], binName, incr, ttl)
}
func cmdHINCRBY(wf io.Writer, ctx *context, args [][]byte) error {
incr, err := strconv.Atoi(string(args[2]))
if err != nil {
return err
}
return hIncrByEx(wf, ctx, args[0], string(args[1]), incr, -1)
}
func cmdHINCRBYEX(wf io.Writer, ctx *context, args [][]byte) error {
incr, err := strconv.Atoi(string(args[2]))
if err != nil {
return err
}
ttl, err := strconv.Atoi(string(args[3]))
if err != nil {
return err
}
return hIncrByEx(wf, ctx, args[0], string(args[1]), incr, ttl)
}
func cmdDECRBY(wf io.Writer, ctx *context, args [][]byte) error {
decr, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
return hIncrByEx(wf, ctx, args[0], binName, -decr, -1)
}
func cmdDECRBYEX(wf io.Writer, ctx *context, args [][]byte) error {
ttl, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
decr, err := strconv.Atoi(string(args[2]))
if err != nil {
return err
}
return hIncrByEx(wf, ctx, args[0], binName, -decr, ttl)
}
func cmdHMGET(wf io.Writer, ctx *context, args [][]byte) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
a := make([]string, len(args)-1)
for i, e := range args[1:] {
a[i] = string(e)
}
rec, err := ctx.client.Get(ctx.readPolicy, key, a...)
if err != nil {
return err
}
err = writeLine(wf, "*"+strconv.Itoa(len(a)))
if err != nil {
return err
}
for _, e := range a {
err = writeBin(wf, rec, e, "$-1")
if err != nil {
return err
}
}
return nil
}
func cmdHMSET(wf io.Writer, ctx *context, args [][]byte) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
bins := make([]*as.Bin, (len(args)-1)/2)
for i := 1; i+1 < len(args); i += 2 {
bins[i/2] = as.NewBin(string(args[i]), encode(ctx, args[i+1]))
}
err = ctx.client.PutBins(ctx.writePolicy, key, bins...)
if err != nil {
return err
}
return writeLine(wf, "+OK")
}
func cmdHGETALL(wf io.Writer, ctx *context, args [][]byte) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
rec, err := ctx.client.Get(ctx.readPolicy, key)
if err != nil {
return err
}
if rec == nil {
err = writeLine(wf, "*0")
if err != nil {
return err
}
} else {
err = writeLine(wf, "*"+strconv.Itoa(len(rec.Bins)*2))
if err != nil {
return err
}
for k, v := range rec.Bins {
err = writeByteArray(wf, []byte(k))
if err != nil {
return err
}
err = writeValue(wf, v)
if err != nil {
return err
}
}
}
return nil
}
func cmdEXPIRE(wf io.Writer, ctx *context, args [][]byte) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
ttl, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
err = ctx.client.Touch(createWritePolicyEx(ttl, false), key)
if err != nil {
if errResultCode(err) == ase.KEY_NOT_FOUND_ERROR {
return writeLine(wf, ":0")
}
return err
}
return writeLine(wf, ":1")
}
func cmdTTL(wf io.Writer, ctx *context, args [][]byte) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
rec, err := ctx.client.GetHeader(ctx.readPolicy, key)
if err != nil {
return err
}
if rec == nil {
return writeLine(wf, ":-2")
}
ttl := uint64(rec.Expiration)
if ttl >= 4294967295 {
return writeLine(wf, ":-1")
}
return writeLine(wf, ":"+strconv.FormatUint(ttl, 10))
}
func cmdEXISTS(wf io.Writer, ctx *context, args [][]byte) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
rec, err := ctx.client.GetHeader(ctx.readPolicy, key)
if err != nil {
return err
}
if rec == nil {
return writeLine(wf, ":0")
}
return writeLine(wf, ":1")
}
func cmdFLUSHDB(wf io.Writer, ctx *context, args [][]byte) error {
policy := as.NewScanPolicy()
recordset, err := ctx.client.ScanAll(policy, ctx.ns, ctx.set)
if err != nil {
return err
}
for res := range recordset.Results() {
if res.Err != nil {
return err
}
_, err := ctx.client.Delete(ctx.writePolicy, res.Record.Key)
if err != nil {
return err
}
}
return writeLine(wf, "+OK")
}
func cmdHMINCRBYEX(wf io.Writer, ctx *context, args [][]byte) error {
key, err := buildKey(ctx, args[0])
if err != nil {
return err
}
ttl, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
if len(args) == 2 {
err := ctx.client.Touch(createWritePolicyEx(ttl, false), key)
if err != nil {
if errResultCode(err) != ase.KEY_NOT_FOUND_ERROR {
return err
}
}
return writeLine(wf, "+OK")
}
ops := make([]*as.Operation, (len(args)-2)/2)
for i := 2; i+1 < len(args); i += 2 {
incr, err := strconv.Atoi(string(args[i+1]))
if err != nil {
return err
}
ops[(i/2)-1] = as.AddOp(as.NewBin(string(args[i]), incr))
}
_, err = ctx.client.Operate(createWritePolicyEx(ttl, false), key, ops...)
if err != nil {
return err
}
return writeLine(wf, "+OK")
}