forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
782 lines (614 loc) · 14.5 KB
/
command.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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
package redis
import (
"bytes"
"fmt"
"strconv"
"strings"
"time"
"gopkg.in/bufio.v1"
)
var (
_ Cmder = (*Cmd)(nil)
_ Cmder = (*SliceCmd)(nil)
_ Cmder = (*StatusCmd)(nil)
_ Cmder = (*IntCmd)(nil)
_ Cmder = (*DurationCmd)(nil)
_ Cmder = (*BoolCmd)(nil)
_ Cmder = (*StringCmd)(nil)
_ Cmder = (*FloatCmd)(nil)
_ Cmder = (*StringSliceCmd)(nil)
_ Cmder = (*BoolSliceCmd)(nil)
_ Cmder = (*StringStringMapCmd)(nil)
_ Cmder = (*StringIntMapCmd)(nil)
_ Cmder = (*ZSliceCmd)(nil)
_ Cmder = (*ScanCmd)(nil)
_ Cmder = (*ClusterSlotCmd)(nil)
)
type Cmder interface {
args() []interface{}
parseReply(*bufio.Reader) error
setErr(error)
reset()
writeTimeout() *time.Duration
readTimeout() *time.Duration
clusterKey() string
Err() error
fmt.Stringer
}
func setCmdsErr(cmds []Cmder, e error) {
for _, cmd := range cmds {
cmd.setErr(e)
}
}
func resetCmds(cmds []Cmder) {
for _, cmd := range cmds {
cmd.reset()
}
}
func cmdString(cmd Cmder, val interface{}) string {
var ss []string
for _, arg := range cmd.args() {
ss = append(ss, fmt.Sprint(arg))
}
s := strings.Join(ss, " ")
if err := cmd.Err(); err != nil {
return s + ": " + err.Error()
}
if val != nil {
switch vv := val.(type) {
case []byte:
return s + ": " + string(vv)
default:
return s + ": " + fmt.Sprint(val)
}
}
return s
}
//------------------------------------------------------------------------------
type baseCmd struct {
_args []interface{}
err error
_clusterKeyPos int
_writeTimeout, _readTimeout *time.Duration
}
func (cmd *baseCmd) Err() error {
if cmd.err != nil {
return cmd.err
}
return nil
}
func (cmd *baseCmd) args() []interface{} {
return cmd._args
}
func (cmd *baseCmd) readTimeout() *time.Duration {
return cmd._readTimeout
}
func (cmd *baseCmd) setReadTimeout(d time.Duration) {
cmd._readTimeout = &d
}
func (cmd *baseCmd) writeTimeout() *time.Duration {
return cmd._writeTimeout
}
func (cmd *baseCmd) clusterKey() string {
if cmd._clusterKeyPos > 0 && cmd._clusterKeyPos < len(cmd._args) {
return fmt.Sprint(cmd._args[cmd._clusterKeyPos])
}
return ""
}
func (cmd *baseCmd) setWriteTimeout(d time.Duration) {
cmd._writeTimeout = &d
}
func (cmd *baseCmd) setErr(e error) {
cmd.err = e
}
//------------------------------------------------------------------------------
type Cmd struct {
baseCmd
val interface{}
}
func NewCmd(args ...interface{}) *Cmd {
return &Cmd{baseCmd: baseCmd{_args: args}}
}
func (cmd *Cmd) reset() {
cmd.val = nil
cmd.err = nil
}
func (cmd *Cmd) Val() interface{} {
return cmd.val
}
func (cmd *Cmd) Result() (interface{}, error) {
return cmd.val, cmd.err
}
func (cmd *Cmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *Cmd) parseReply(rd *bufio.Reader) error {
cmd.val, cmd.err = parseReply(rd, parseSlice)
// Convert to string to preserve old behaviour.
// TODO: remove in v4
if v, ok := cmd.val.([]byte); ok {
cmd.val = string(v)
}
return cmd.err
}
//------------------------------------------------------------------------------
type SliceCmd struct {
baseCmd
val []interface{}
}
func NewSliceCmd(args ...interface{}) *SliceCmd {
return &SliceCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *SliceCmd) reset() {
cmd.val = nil
cmd.err = nil
}
func (cmd *SliceCmd) Val() []interface{} {
return cmd.val
}
func (cmd *SliceCmd) Result() ([]interface{}, error) {
return cmd.val, cmd.err
}
func (cmd *SliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *SliceCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseSlice)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.([]interface{})
return nil
}
//------------------------------------------------------------------------------
type StatusCmd struct {
baseCmd
val string
}
func NewStatusCmd(args ...interface{}) *StatusCmd {
return &StatusCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func newKeylessStatusCmd(args ...interface{}) *StatusCmd {
return &StatusCmd{baseCmd: baseCmd{_args: args}}
}
func (cmd *StatusCmd) reset() {
cmd.val = ""
cmd.err = nil
}
func (cmd *StatusCmd) Val() string {
return cmd.val
}
func (cmd *StatusCmd) Result() (string, error) {
return cmd.val, cmd.err
}
func (cmd *StatusCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *StatusCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
if err != nil {
cmd.err = err
return err
}
cmd.val = string(v.([]byte))
return nil
}
//------------------------------------------------------------------------------
type IntCmd struct {
baseCmd
val int64
}
func NewIntCmd(args ...interface{}) *IntCmd {
return &IntCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *IntCmd) reset() {
cmd.val = 0
cmd.err = nil
}
func (cmd *IntCmd) Val() int64 {
return cmd.val
}
func (cmd *IntCmd) Result() (int64, error) {
return cmd.val, cmd.err
}
func (cmd *IntCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *IntCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(int64)
return nil
}
//------------------------------------------------------------------------------
type DurationCmd struct {
baseCmd
val time.Duration
precision time.Duration
}
func NewDurationCmd(precision time.Duration, args ...interface{}) *DurationCmd {
return &DurationCmd{
precision: precision,
baseCmd: baseCmd{_args: args, _clusterKeyPos: 1},
}
}
func (cmd *DurationCmd) reset() {
cmd.val = 0
cmd.err = nil
}
func (cmd *DurationCmd) Val() time.Duration {
return cmd.val
}
func (cmd *DurationCmd) Result() (time.Duration, error) {
return cmd.val, cmd.err
}
func (cmd *DurationCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *DurationCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
if err != nil {
cmd.err = err
return err
}
cmd.val = time.Duration(v.(int64)) * cmd.precision
return nil
}
//------------------------------------------------------------------------------
type BoolCmd struct {
baseCmd
val bool
}
func NewBoolCmd(args ...interface{}) *BoolCmd {
return &BoolCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *BoolCmd) reset() {
cmd.val = false
cmd.err = nil
}
func (cmd *BoolCmd) Val() bool {
return cmd.val
}
func (cmd *BoolCmd) Result() (bool, error) {
return cmd.val, cmd.err
}
func (cmd *BoolCmd) String() string {
return cmdString(cmd, cmd.val)
}
var ok = []byte("OK")
func (cmd *BoolCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
// `SET key value NX` returns nil when key already exists.
if err == Nil {
cmd.val = false
return nil
}
if err != nil {
cmd.err = err
return err
}
switch vv := v.(type) {
case int64:
cmd.val = vv == 1
return nil
case []byte:
cmd.val = bytes.Equal(vv, ok)
return nil
default:
return fmt.Errorf("got %T, wanted int64 or string")
}
}
//------------------------------------------------------------------------------
type StringCmd struct {
baseCmd
val []byte
}
func NewStringCmd(args ...interface{}) *StringCmd {
return &StringCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *StringCmd) reset() {
cmd.val = nil
cmd.err = nil
}
func (cmd *StringCmd) Val() string {
return string(cmd.val)
}
func (cmd *StringCmd) Result() (string, error) {
return cmd.Val(), cmd.err
}
func (cmd *StringCmd) Bytes() ([]byte, error) {
return cmd.val, cmd.err
}
func (cmd *StringCmd) Int64() (int64, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.ParseInt(cmd.Val(), 10, 64)
}
func (cmd *StringCmd) Uint64() (uint64, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.ParseUint(cmd.Val(), 10, 64)
}
func (cmd *StringCmd) Float64() (float64, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.ParseFloat(cmd.Val(), 64)
}
func (cmd *StringCmd) Scan(val interface{}) error {
if cmd.err != nil {
return cmd.err
}
return scan(cmd.val, val)
}
func (cmd *StringCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *StringCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
if err != nil {
cmd.err = err
return err
}
b := v.([]byte)
cmd.val = make([]byte, len(b))
copy(cmd.val, b)
return nil
}
//------------------------------------------------------------------------------
type FloatCmd struct {
baseCmd
val float64
}
func NewFloatCmd(args ...interface{}) *FloatCmd {
return &FloatCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *FloatCmd) reset() {
cmd.val = 0
cmd.err = nil
}
func (cmd *FloatCmd) Val() float64 {
return cmd.val
}
func (cmd *FloatCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *FloatCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
if err != nil {
cmd.err = err
return err
}
cmd.val, cmd.err = strconv.ParseFloat(string(v.([]byte)), 64)
return cmd.err
}
//------------------------------------------------------------------------------
type StringSliceCmd struct {
baseCmd
val []string
}
func NewStringSliceCmd(args ...interface{}) *StringSliceCmd {
return &StringSliceCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *StringSliceCmd) reset() {
cmd.val = nil
cmd.err = nil
}
func (cmd *StringSliceCmd) Val() []string {
return cmd.val
}
func (cmd *StringSliceCmd) Result() ([]string, error) {
return cmd.Val(), cmd.Err()
}
func (cmd *StringSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *StringSliceCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseStringSlice)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.([]string)
return nil
}
//------------------------------------------------------------------------------
type BoolSliceCmd struct {
baseCmd
val []bool
}
func NewBoolSliceCmd(args ...interface{}) *BoolSliceCmd {
return &BoolSliceCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *BoolSliceCmd) reset() {
cmd.val = nil
cmd.err = nil
}
func (cmd *BoolSliceCmd) Val() []bool {
return cmd.val
}
func (cmd *BoolSliceCmd) Result() ([]bool, error) {
return cmd.val, cmd.err
}
func (cmd *BoolSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *BoolSliceCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseBoolSlice)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.([]bool)
return nil
}
//------------------------------------------------------------------------------
type StringStringMapCmd struct {
baseCmd
val map[string]string
}
func NewStringStringMapCmd(args ...interface{}) *StringStringMapCmd {
return &StringStringMapCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *StringStringMapCmd) reset() {
cmd.val = nil
cmd.err = nil
}
func (cmd *StringStringMapCmd) Val() map[string]string {
return cmd.val
}
func (cmd *StringStringMapCmd) Result() (map[string]string, error) {
return cmd.val, cmd.err
}
func (cmd *StringStringMapCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *StringStringMapCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseStringStringMap)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(map[string]string)
return nil
}
//------------------------------------------------------------------------------
type StringIntMapCmd struct {
baseCmd
val map[string]int64
}
func NewStringIntMapCmd(args ...interface{}) *StringIntMapCmd {
return &StringIntMapCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *StringIntMapCmd) Val() map[string]int64 {
return cmd.val
}
func (cmd *StringIntMapCmd) Result() (map[string]int64, error) {
return cmd.val, cmd.err
}
func (cmd *StringIntMapCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *StringIntMapCmd) reset() {
cmd.val = nil
cmd.err = nil
}
func (cmd *StringIntMapCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseStringIntMap)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(map[string]int64)
return nil
}
//------------------------------------------------------------------------------
type ZSliceCmd struct {
baseCmd
val []Z
}
func NewZSliceCmd(args ...interface{}) *ZSliceCmd {
return &ZSliceCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *ZSliceCmd) reset() {
cmd.val = nil
cmd.err = nil
}
func (cmd *ZSliceCmd) Val() []Z {
return cmd.val
}
func (cmd *ZSliceCmd) Result() ([]Z, error) {
return cmd.val, cmd.err
}
func (cmd *ZSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *ZSliceCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseZSlice)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.([]Z)
return nil
}
//------------------------------------------------------------------------------
type ScanCmd struct {
baseCmd
cursor int64
keys []string
}
func NewScanCmd(args ...interface{}) *ScanCmd {
return &ScanCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *ScanCmd) reset() {
cmd.cursor = 0
cmd.keys = nil
cmd.err = nil
}
func (cmd *ScanCmd) Val() (int64, []string) {
return cmd.cursor, cmd.keys
}
func (cmd *ScanCmd) Result() (int64, []string, error) {
return cmd.cursor, cmd.keys, cmd.err
}
func (cmd *ScanCmd) String() string {
return cmdString(cmd, cmd.keys)
}
func (cmd *ScanCmd) parseReply(rd *bufio.Reader) error {
vi, err := parseReply(rd, parseSlice)
if err != nil {
cmd.err = err
return cmd.err
}
v := vi.([]interface{})
cmd.cursor, cmd.err = strconv.ParseInt(v[0].(string), 10, 64)
if cmd.err != nil {
return cmd.err
}
keys := v[1].([]interface{})
for _, keyi := range keys {
cmd.keys = append(cmd.keys, keyi.(string))
}
return nil
}
//------------------------------------------------------------------------------
type ClusterSlotInfo struct {
Start, End int
Addrs []string
}
type ClusterSlotCmd struct {
baseCmd
val []ClusterSlotInfo
}
func NewClusterSlotCmd(args ...interface{}) *ClusterSlotCmd {
return &ClusterSlotCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *ClusterSlotCmd) Val() []ClusterSlotInfo {
return cmd.val
}
func (cmd *ClusterSlotCmd) Result() ([]ClusterSlotInfo, error) {
return cmd.Val(), cmd.Err()
}
func (cmd *ClusterSlotCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *ClusterSlotCmd) reset() {
cmd.val = nil
cmd.err = nil
}
func (cmd *ClusterSlotCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseClusterSlotInfoSlice)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.([]ClusterSlotInfo)
return nil
}