-
Notifications
You must be signed in to change notification settings - Fork 17
/
pipeline.go
795 lines (700 loc) · 18.5 KB
/
pipeline.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
783
784
785
786
787
788
789
790
791
792
793
794
795
package godis
import "sync"
//Response pipeline and transaction response,include replies from redis
type Response struct {
response interface{} //store replies
exception *DataError
building bool //whether response is building
built bool //whether response is build done
isSet bool //whether response is set with data
builder Builder //response data convert rule
data interface{} //real data
dependency *Response //response cycle dependency
}
func newResponse() *Response {
return &Response{
building: false,
built: false,
isSet: false,
}
}
func (r *Response) set(data interface{}) {
r.data = data
r.isSet = true
}
//Get get real content of response
func (r *Response) Get() (interface{}, error) {
if r.dependency != nil && r.dependency.isSet && !r.dependency.built {
err := r.dependency.build()
if err != nil {
return nil, err
}
}
if !r.isSet {
return nil, newDataError("please close pipeline or multi block before calling this method")
}
if !r.built {
err := r.build()
if err != nil {
return nil, err
}
}
if r.exception != nil {
return nil, r.exception
}
return r.response, nil
}
func (r *Response) setDependency(dependency *Response) {
r.dependency = dependency
}
func (r *Response) build() error {
if r.building {
return nil
}
r.building = true
defer func() {
r.building = false
r.built = true
}()
if r.data != nil {
switch r.data.(type) {
case *DataError:
r.exception = r.data.(*DataError)
return nil
}
result, err := r.builder.build(r.data)
if err != nil {
return err
}
r.response = result
}
r.data = nil
return nil
}
//Transaction redis transaction struct
type Transaction struct {
*multiKeyPipelineBase
inTransaction bool
}
func newTransaction(c *client) *Transaction {
base := newMultiKeyPipelineBase(c)
base.getClient = func(key string) *client {
return c
}
return &Transaction{multiKeyPipelineBase: base}
}
//Clear clear
func (t *Transaction) Clear() (string, error) {
if t.inTransaction {
return t.Discard()
}
return "", nil
}
//Exec execute transaction
func (t *Transaction) Exec() ([]interface{}, error) {
err := t.client.exec()
if err != nil {
return nil, err
}
_, err = t.client.getAll(1)
if err != nil {
return nil, err
}
t.inTransaction = false
reply, err := t.client.getObjectMultiBulkReply()
if err != nil {
return nil, err
}
result := make([]interface{}, 0)
for _, r := range reply {
result = append(result, t.generateResponse(r))
}
return result, nil
}
//ExecGetResponse ...
func (t *Transaction) ExecGetResponse() ([]*Response, error) {
err := t.client.exec()
if err != nil {
return nil, err
}
_, err = t.client.getAll(1)
if err != nil {
return nil, err
}
t.inTransaction = false
reply, err := t.client.getObjectMultiBulkReply()
if err != nil {
return nil, err
}
result := make([]*Response, 0)
for _, r := range reply {
result = append(result, t.generateResponse(r))
}
return result, nil
}
//Discard see redis command
func (t *Transaction) Discard() (string, error) {
err := t.client.discard()
if err != nil {
return "", err
}
_, err = t.client.getAll(1)
if err != nil {
return "", err
}
t.inTransaction = false
t.clean()
return t.client.getStatusCodeReply()
}
func (t *Transaction) clean() {
t.pipelinedResponses = make([]*Response, 0)
}
//Pipeline redis pipeline struct
type Pipeline struct {
*multiKeyPipelineBase
}
func newPipeline(c *client) *Pipeline {
base := newMultiKeyPipelineBase(c)
base.getClient = func(key string) *client {
return c
}
return &Pipeline{multiKeyPipelineBase: base}
}
//Sync see redis command
func (p *Pipeline) Sync() error {
if len(p.pipelinedResponses) == 0 {
return nil
}
all, err := p.client.connection.getAll()
if err != nil {
return err
}
for _, a := range all.([]interface{}) {
p.generateResponse(a)
}
return nil
}
type queue struct {
pipelinedResponses []*Response
mu sync.Mutex
}
func newQueue() *queue {
return &queue{pipelinedResponses: make([]*Response, 0)}
}
func (q *queue) clean() {
q.mu.Lock()
defer q.mu.Unlock()
q.pipelinedResponses = make([]*Response, 0)
}
func (q *queue) generateResponse(data interface{}) *Response {
q.mu.Lock()
defer q.mu.Unlock()
size := len(q.pipelinedResponses)
if size == 0 {
return nil
}
r := q.pipelinedResponses[0]
r.set(data)
if size == 1 {
q.pipelinedResponses = make([]*Response, 0)
} else {
q.pipelinedResponses = q.pipelinedResponses[1:]
}
return r
}
func (q *queue) getResponse(builder Builder) *Response {
q.mu.Lock()
defer q.mu.Unlock()
response := newResponse()
response.builder = builder
q.pipelinedResponses = append(q.pipelinedResponses, response)
return response
}
func (q *queue) hasPipelinedResponse() bool {
return q.getPipelinedResponseLength() > 0
}
func (q *queue) getPipelinedResponseLength() int {
q.mu.Lock()
defer q.mu.Unlock()
return len(q.pipelinedResponses)
}
type multiKeyPipelineBase struct {
*queue
client *client
getClient func(key string) *client
}
func newMultiKeyPipelineBase(client *client) *multiKeyPipelineBase {
return &multiKeyPipelineBase{queue: newQueue(), client: client}
}
//<editor-fold desc="basicpipeline">
//BgRewriteAof see redis command
func (p *multiKeyPipelineBase) BgRewriteAof() (*Response, error) {
err := p.client.bgrewriteaof()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//BgSave see redis command
func (p *multiKeyPipelineBase) BgSave() (*Response, error) {
err := p.client.bgsave()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//ConfigGet see redis command
func (p *multiKeyPipelineBase) ConfigGet(pattern string) (*Response, error) {
err := p.client.configGet(pattern)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//ConfigSet see redis command
func (p *multiKeyPipelineBase) ConfigSet(parameter, value string) (*Response, error) {
err := p.client.configSet(parameter, value)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//ConfigResetStat see redis command
func (p *multiKeyPipelineBase) ConfigResetStat() (*Response, error) {
err := p.client.configResetStat()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//Save see redis command
func (p *multiKeyPipelineBase) Save() (*Response, error) {
err := p.client.save()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//LastSave see redis command
func (p *multiKeyPipelineBase) LastSave() (*Response, error) {
err := p.client.lastsave()
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//FlushDB see redis command
func (p *multiKeyPipelineBase) FlushDB() (*Response, error) {
err := p.client.flushDB()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//FlushAll see redis command
func (p *multiKeyPipelineBase) FlushAll() (*Response, error) {
err := p.client.flushAll()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//Info see redis command
func (p *multiKeyPipelineBase) Info() (*Response, error) {
err := p.client.info()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//Time see redis command
func (p *multiKeyPipelineBase) Time() (*Response, error) {
err := p.client.time()
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//DbSize see redis command
func (p *multiKeyPipelineBase) DbSize() (*Response, error) {
err := p.client.dbSize()
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//Shutdown see redis command
func (p *multiKeyPipelineBase) Shutdown() (*Response, error) {
err := p.client.shutdown()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//Ping see redis command
func (p *multiKeyPipelineBase) Ping() (*Response, error) {
err := p.client.ping()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//Select see redis command
func (p *multiKeyPipelineBase) Select(index int) (*Response, error) {
err := p.client.selectDb(index)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//</editor-fold>
//<editor-fold desc="multikeypipeline">
//Del see redis command
func (p *multiKeyPipelineBase) Del(keys ...string) (*Response, error) {
err := p.client.del(keys...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//Exists see redis command
func (p *multiKeyPipelineBase) Exists(keys ...string) (*Response, error) {
err := p.client.exists(keys...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//BLPopTimeout see redis command
func (p *multiKeyPipelineBase) BLPopTimeout(timeout int, keys ...string) (*Response, error) {
err := p.client.blpopTimout(timeout, keys...)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//BRPopTimeout see redis command
func (p *multiKeyPipelineBase) BRPopTimeout(timeout int, keys ...string) (*Response, error) {
err := p.client.brpopTimout(timeout, keys...)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//BLPop see redis command
func (p *multiKeyPipelineBase) BLPop(args ...string) (*Response, error) {
err := p.client.blpop(args)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//BRPop see redis command
func (p *multiKeyPipelineBase) BRPop(args ...string) (*Response, error) {
err := p.client.brpop(args)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//Keys see redis command
func (p *multiKeyPipelineBase) Keys(pattern string) (*Response, error) {
err := p.client.keys(pattern)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//MGet see redis command
func (p *multiKeyPipelineBase) MGet(keys ...string) (*Response, error) {
err := p.client.mget(keys...)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//MSet see redis command
func (p *multiKeyPipelineBase) MSet(kvs ...string) (*Response, error) {
err := p.client.mset(kvs...)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//MSetNx see redis command
func (p *multiKeyPipelineBase) MSetNx(kvs ...string) (*Response, error) {
err := p.client.msetnx(kvs...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//Rename see redis command
func (p *multiKeyPipelineBase) Rename(oldkey, newkey string) (*Response, error) {
err := p.client.rename(oldkey, newkey)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//RenameNx see redis command
func (p *multiKeyPipelineBase) RenameNx(oldkey, newkey string) (*Response, error) {
err := p.client.renamenx(oldkey, newkey)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//RPopLPush see redis command
func (p *multiKeyPipelineBase) RPopLPush(srcKey, destKey string) (*Response, error) {
err := p.client.rpopLpush(srcKey, destKey)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//SDiff see redis command
func (p *multiKeyPipelineBase) SDiff(keys ...string) (*Response, error) {
err := p.client.sDiff(keys...)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//SDiffStore see redis command
func (p *multiKeyPipelineBase) SDiffStore(destKey string, keys ...string) (*Response, error) {
err := p.client.sDiffStore(destKey, keys...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//SInter see redis command
func (p *multiKeyPipelineBase) SInter(keys ...string) (*Response, error) {
err := p.client.sInter(keys...)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//SInterStore see redis command
func (p *multiKeyPipelineBase) SInterStore(destKey string, keys ...string) (*Response, error) {
err := p.client.sInterStore(destKey, keys...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//SMove see redis command
func (p *multiKeyPipelineBase) SMove(srcKey, destKey, member string) (*Response, error) {
err := p.client.smove(srcKey, destKey, member)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//SortMulti see redis command
func (p *multiKeyPipelineBase) SortStore(key string, destKey string, params ...*SortParams) (*Response, error) {
err := p.client.sortMulti(key, destKey, params...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//SUnion see redis command
func (p *multiKeyPipelineBase) SUnion(keys ...string) (*Response, error) {
err := p.client.sUnion(keys...)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//SUnionStore see redis command
func (p *multiKeyPipelineBase) SUnionStore(destKey string, keys ...string) (*Response, error) {
err := p.client.sUnionStore(destKey, keys...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//Watch see redis command
func (p *multiKeyPipelineBase) Watch(keys ...string) (*Response, error) {
err := p.client.watch(keys...)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//ZInterStore see redis command
func (p *multiKeyPipelineBase) ZInterStore(destKey string, sets ...string) (*Response, error) {
err := p.client.zinterstore(destKey, sets...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//ZInterStoreWithParams see redis command
func (p *multiKeyPipelineBase) ZInterStoreWithParams(destKey string, params *ZParams, sets ...string) (*Response, error) {
err := p.client.zinterstoreWithParams(destKey, params, sets...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//ZUnionStore see redis command
func (p *multiKeyPipelineBase) ZUnionStore(destKey string, sets ...string) (*Response, error) {
err := p.client.zunionstore(destKey, sets...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//ZUnionStoreWithParams see redis command
func (p *multiKeyPipelineBase) ZUnionStoreWithParams(destKey string, params *ZParams, sets ...string) (*Response, error) {
err := p.client.zunionstoreWithParams(destKey, params, sets...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//BRPopLPush see redis command
func (p *multiKeyPipelineBase) BRPopLPush(source, destination string, timeout int) (*Response, error) {
err := p.client.brpoplpush(source, destination, timeout)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//Publish see redis command
func (p *multiKeyPipelineBase) Publish(channel, message string) (*Response, error) {
err := p.client.publish(channel, message)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//RandomKey see redis command
func (p *multiKeyPipelineBase) RandomKey() (*Response, error) {
err := p.client.randomKey()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//BitOp see redis command
func (p *multiKeyPipelineBase) BitOp(op BitOP, destKey string, srcKeys ...string) (*Response, error) {
err := p.client.bitop(op, destKey, srcKeys...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//PfMerge see redis command
func (p *multiKeyPipelineBase) PfMerge(destKey string, srcKeys ...string) (*Response, error) {
err := p.client.pfmerge(destKey, srcKeys...)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//PfCount see redis command
func (p *multiKeyPipelineBase) PfCount(keys ...string) (*Response, error) {
err := p.client.pfcount(keys...)
if err != nil {
return nil, err
}
return p.getResponse(Int64Builder), nil
}
//</editor-fold>
//<editor-fold desc="cluster pipeline">
//ClusterNodes see redis command
func (p *multiKeyPipelineBase) ClusterNodes() (*Response, error) {
err := p.client.clusterNodes()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//ClusterMeet see redis command
func (p *multiKeyPipelineBase) ClusterMeet(ip string, port int) (*Response, error) {
err := p.client.clusterMeet(ip, port)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//ClusterAddSlots see redis command
func (p *multiKeyPipelineBase) ClusterAddSlots(slots ...int) (*Response, error) {
err := p.client.clusterAddSlots(slots...)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//ClusterDelSlots see redis command
func (p *multiKeyPipelineBase) ClusterDelSlots(slots ...int) (*Response, error) {
err := p.client.clusterDelSlots(slots...)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//ClusterInfo see redis command
func (p *multiKeyPipelineBase) ClusterInfo() (*Response, error) {
err := p.client.clusterInfo()
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//ClusterGetKeysInSlot see redis command
func (p *multiKeyPipelineBase) ClusterGetKeysInSlot(slot int, count int) (*Response, error) {
err := p.client.clusterGetKeysInSlot(slot, count)
if err != nil {
return nil, err
}
return p.getResponse(StrArrBuilder), nil
}
//ClusterSetSlotNode see redis command
func (p *multiKeyPipelineBase) ClusterSetSlotNode(slot int, nodeID string) (*Response, error) {
err := p.client.clusterSetSlotNode(slot, nodeID)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//ClusterSetSlotMigrating see redis command
func (p *multiKeyPipelineBase) ClusterSetSlotMigrating(slot int, nodeID string) (*Response, error) {
err := p.client.clusterSetSlotMigrating(slot, nodeID)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//ClusterSetSlotImporting see redis command
func (p *multiKeyPipelineBase) ClusterSetSlotImporting(slot int, nodeID string) (*Response, error) {
err := p.client.clusterSetSlotImporting(slot, nodeID)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//</editor-fold>
//<editor-fold desc="scripting pipeline">
//Eval see redis command
func (p *multiKeyPipelineBase) Eval(script string, keyCount int, params ...string) (*Response, error) {
err := p.getClient(script).eval(script, keyCount, params...)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//EvalSha see redis command
func (p *multiKeyPipelineBase) EvalSha(sha1 string, keyCount int, params ...string) (*Response, error) {
err := p.getClient(sha1).evalsha(sha1, keyCount, params...)
if err != nil {
return nil, err
}
return p.getResponse(StrBuilder), nil
}
//</editor-fold>