forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
probabilistic.go
1429 lines (1218 loc) · 46.1 KB
/
probabilistic.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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package redis
import (
"context"
"fmt"
"github.com/redis/go-redis/v9/internal/proto"
)
type ProbabilisticCmdable interface {
BFAdd(ctx context.Context, key string, element interface{}) *BoolCmd
BFCard(ctx context.Context, key string) *IntCmd
BFExists(ctx context.Context, key string, element interface{}) *BoolCmd
BFInfo(ctx context.Context, key string) *BFInfoCmd
BFInfoArg(ctx context.Context, key, option string) *BFInfoCmd
BFInfoCapacity(ctx context.Context, key string) *BFInfoCmd
BFInfoSize(ctx context.Context, key string) *BFInfoCmd
BFInfoFilters(ctx context.Context, key string) *BFInfoCmd
BFInfoItems(ctx context.Context, key string) *BFInfoCmd
BFInfoExpansion(ctx context.Context, key string) *BFInfoCmd
BFInsert(ctx context.Context, key string, options *BFInsertOptions, elements ...interface{}) *BoolSliceCmd
BFMAdd(ctx context.Context, key string, elements ...interface{}) *BoolSliceCmd
BFMExists(ctx context.Context, key string, elements ...interface{}) *BoolSliceCmd
BFReserve(ctx context.Context, key string, errorRate float64, capacity int64) *StatusCmd
BFReserveExpansion(ctx context.Context, key string, errorRate float64, capacity, expansion int64) *StatusCmd
BFReserveNonScaling(ctx context.Context, key string, errorRate float64, capacity int64) *StatusCmd
BFReserveWithArgs(ctx context.Context, key string, options *BFReserveOptions) *StatusCmd
BFScanDump(ctx context.Context, key string, iterator int64) *ScanDumpCmd
BFLoadChunk(ctx context.Context, key string, iterator int64, data interface{}) *StatusCmd
CFAdd(ctx context.Context, key string, element interface{}) *BoolCmd
CFAddNX(ctx context.Context, key string, element interface{}) *BoolCmd
CFCount(ctx context.Context, key string, element interface{}) *IntCmd
CFDel(ctx context.Context, key string, element interface{}) *BoolCmd
CFExists(ctx context.Context, key string, element interface{}) *BoolCmd
CFInfo(ctx context.Context, key string) *CFInfoCmd
CFInsert(ctx context.Context, key string, options *CFInsertOptions, elements ...interface{}) *BoolSliceCmd
CFInsertNX(ctx context.Context, key string, options *CFInsertOptions, elements ...interface{}) *IntSliceCmd
CFMExists(ctx context.Context, key string, elements ...interface{}) *BoolSliceCmd
CFReserve(ctx context.Context, key string, capacity int64) *StatusCmd
CFReserveWithArgs(ctx context.Context, key string, options *CFReserveOptions) *StatusCmd
CFReserveExpansion(ctx context.Context, key string, capacity int64, expansion int64) *StatusCmd
CFReserveBucketSize(ctx context.Context, key string, capacity int64, bucketsize int64) *StatusCmd
CFReserveMaxIterations(ctx context.Context, key string, capacity int64, maxiterations int64) *StatusCmd
CFScanDump(ctx context.Context, key string, iterator int64) *ScanDumpCmd
CFLoadChunk(ctx context.Context, key string, iterator int64, data interface{}) *StatusCmd
CMSIncrBy(ctx context.Context, key string, elements ...interface{}) *IntSliceCmd
CMSInfo(ctx context.Context, key string) *CMSInfoCmd
CMSInitByDim(ctx context.Context, key string, width, height int64) *StatusCmd
CMSInitByProb(ctx context.Context, key string, errorRate, probability float64) *StatusCmd
CMSMerge(ctx context.Context, destKey string, sourceKeys ...string) *StatusCmd
CMSMergeWithWeight(ctx context.Context, destKey string, sourceKeys map[string]int64) *StatusCmd
CMSQuery(ctx context.Context, key string, elements ...interface{}) *IntSliceCmd
TopKAdd(ctx context.Context, key string, elements ...interface{}) *StringSliceCmd
TopKCount(ctx context.Context, key string, elements ...interface{}) *IntSliceCmd
TopKIncrBy(ctx context.Context, key string, elements ...interface{}) *StringSliceCmd
TopKInfo(ctx context.Context, key string) *TopKInfoCmd
TopKList(ctx context.Context, key string) *StringSliceCmd
TopKListWithCount(ctx context.Context, key string) *MapStringIntCmd
TopKQuery(ctx context.Context, key string, elements ...interface{}) *BoolSliceCmd
TopKReserve(ctx context.Context, key string, k int64) *StatusCmd
TopKReserveWithOptions(ctx context.Context, key string, k int64, width, depth int64, decay float64) *StatusCmd
TDigestAdd(ctx context.Context, key string, elements ...float64) *StatusCmd
TDigestByRank(ctx context.Context, key string, rank ...uint64) *FloatSliceCmd
TDigestByRevRank(ctx context.Context, key string, rank ...uint64) *FloatSliceCmd
TDigestCDF(ctx context.Context, key string, elements ...float64) *FloatSliceCmd
TDigestCreate(ctx context.Context, key string) *StatusCmd
TDigestCreateWithCompression(ctx context.Context, key string, compression int64) *StatusCmd
TDigestInfo(ctx context.Context, key string) *TDigestInfoCmd
TDigestMax(ctx context.Context, key string) *FloatCmd
TDigestMin(ctx context.Context, key string) *FloatCmd
TDigestMerge(ctx context.Context, destKey string, options *TDigestMergeOptions, sourceKeys ...string) *StatusCmd
TDigestQuantile(ctx context.Context, key string, elements ...float64) *FloatSliceCmd
TDigestRank(ctx context.Context, key string, values ...float64) *IntSliceCmd
TDigestReset(ctx context.Context, key string) *StatusCmd
TDigestRevRank(ctx context.Context, key string, values ...float64) *IntSliceCmd
TDigestTrimmedMean(ctx context.Context, key string, lowCutQuantile, highCutQuantile float64) *FloatCmd
}
type BFInsertOptions struct {
Capacity int64
Error float64
Expansion int64
NonScaling bool
NoCreate bool
}
type BFReserveOptions struct {
Capacity int64
Error float64
Expansion int64
NonScaling bool
}
type CFReserveOptions struct {
Capacity int64
BucketSize int64
MaxIterations int64
Expansion int64
}
type CFInsertOptions struct {
Capacity int64
NoCreate bool
}
// -------------------------------------------
// Bloom filter commands
//-------------------------------------------
// BFReserve creates an empty Bloom filter with a single sub-filter
// for the initial specified capacity and with an upper bound error_rate.
// For more information - https://redis.io/commands/bf.reserve/
func (c cmdable) BFReserve(ctx context.Context, key string, errorRate float64, capacity int64) *StatusCmd {
args := []interface{}{"BF.RESERVE", key, errorRate, capacity}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// BFReserveExpansion creates an empty Bloom filter with a single sub-filter
// for the initial specified capacity and with an upper bound error_rate.
// This function also allows for specifying an expansion rate for the filter.
// For more information - https://redis.io/commands/bf.reserve/
func (c cmdable) BFReserveExpansion(ctx context.Context, key string, errorRate float64, capacity, expansion int64) *StatusCmd {
args := []interface{}{"BF.RESERVE", key, errorRate, capacity, "EXPANSION", expansion}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// BFReserveNonScaling creates an empty Bloom filter with a single sub-filter
// for the initial specified capacity and with an upper bound error_rate.
// This function also allows for specifying that the filter should not scale.
// For more information - https://redis.io/commands/bf.reserve/
func (c cmdable) BFReserveNonScaling(ctx context.Context, key string, errorRate float64, capacity int64) *StatusCmd {
args := []interface{}{"BF.RESERVE", key, errorRate, capacity, "NONSCALING"}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// BFReserveWithArgs creates an empty Bloom filter with a single sub-filter
// for the initial specified capacity and with an upper bound error_rate.
// This function also allows for specifying additional options such as expansion rate and non-scaling behavior.
// For more information - https://redis.io/commands/bf.reserve/
func (c cmdable) BFReserveWithArgs(ctx context.Context, key string, options *BFReserveOptions) *StatusCmd {
args := []interface{}{"BF.RESERVE", key}
if options != nil {
args = append(args, options.Error, options.Capacity)
if options.Expansion != 0 {
args = append(args, "EXPANSION", options.Expansion)
}
if options.NonScaling {
args = append(args, "NONSCALING")
}
}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// BFAdd adds an item to a Bloom filter.
// For more information - https://redis.io/commands/bf.add/
func (c cmdable) BFAdd(ctx context.Context, key string, element interface{}) *BoolCmd {
args := []interface{}{"BF.ADD", key, element}
cmd := NewBoolCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// BFCard returns the cardinality of a Bloom filter -
// number of items that were added to a Bloom filter and detected as unique
// (items that caused at least one bit to be set in at least one sub-filter).
// For more information - https://redis.io/commands/bf.card/
func (c cmdable) BFCard(ctx context.Context, key string) *IntCmd {
args := []interface{}{"BF.CARD", key}
cmd := NewIntCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// BFExists determines whether a given item was added to a Bloom filter.
// For more information - https://redis.io/commands/bf.exists/
func (c cmdable) BFExists(ctx context.Context, key string, element interface{}) *BoolCmd {
args := []interface{}{"BF.EXISTS", key, element}
cmd := NewBoolCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// BFLoadChunk restores a Bloom filter previously saved using BF.SCANDUMP.
// For more information - https://redis.io/commands/bf.loadchunk/
func (c cmdable) BFLoadChunk(ctx context.Context, key string, iterator int64, data interface{}) *StatusCmd {
args := []interface{}{"BF.LOADCHUNK", key, iterator, data}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// Begins an incremental save of the Bloom filter.
// This command is useful for large Bloom filters that cannot fit into the DUMP and RESTORE model.
// For more information - https://redis.io/commands/bf.scandump/
func (c cmdable) BFScanDump(ctx context.Context, key string, iterator int64) *ScanDumpCmd {
args := []interface{}{"BF.SCANDUMP", key, iterator}
cmd := newScanDumpCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
type ScanDump struct {
Iter int64
Data string
}
type ScanDumpCmd struct {
baseCmd
val ScanDump
}
func newScanDumpCmd(ctx context.Context, args ...interface{}) *ScanDumpCmd {
return &ScanDumpCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}
func (cmd *ScanDumpCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *ScanDumpCmd) SetVal(val ScanDump) {
cmd.val = val
}
func (cmd *ScanDumpCmd) Result() (ScanDump, error) {
return cmd.val, cmd.err
}
func (cmd *ScanDumpCmd) Val() ScanDump {
return cmd.val
}
func (cmd *ScanDumpCmd) readReply(rd *proto.Reader) (err error) {
n, err := rd.ReadMapLen()
if err != nil {
return err
}
cmd.val = ScanDump{}
for i := 0; i < n; i++ {
iter, err := rd.ReadInt()
if err != nil {
return err
}
data, err := rd.ReadString()
if err != nil {
return err
}
cmd.val.Data = data
cmd.val.Iter = iter
}
return nil
}
// Returns information about a Bloom filter.
// For more information - https://redis.io/commands/bf.info/
func (c cmdable) BFInfo(ctx context.Context, key string) *BFInfoCmd {
args := []interface{}{"BF.INFO", key}
cmd := NewBFInfoCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
type BFInfo struct {
Capacity int64
Size int64
Filters int64
ItemsInserted int64
ExpansionRate int64
}
type BFInfoCmd struct {
baseCmd
val BFInfo
}
func NewBFInfoCmd(ctx context.Context, args ...interface{}) *BFInfoCmd {
return &BFInfoCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}
func (cmd *BFInfoCmd) SetVal(val BFInfo) {
cmd.val = val
}
func (cmd *BFInfoCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *BFInfoCmd) Val() BFInfo {
return cmd.val
}
func (cmd *BFInfoCmd) Result() (BFInfo, error) {
return cmd.val, cmd.err
}
func (cmd *BFInfoCmd) readReply(rd *proto.Reader) (err error) {
n, err := rd.ReadMapLen()
if err != nil {
return err
}
var key string
var result BFInfo
for f := 0; f < n; f++ {
key, err = rd.ReadString()
if err != nil {
return err
}
switch key {
case "Capacity":
result.Capacity, err = rd.ReadInt()
case "Size":
result.Size, err = rd.ReadInt()
case "Number of filters":
result.Filters, err = rd.ReadInt()
case "Number of items inserted":
result.ItemsInserted, err = rd.ReadInt()
case "Expansion rate":
result.ExpansionRate, err = rd.ReadInt()
default:
return fmt.Errorf("redis: BLOOM.INFO unexpected key %s", key)
}
if err != nil {
return err
}
}
cmd.val = result
return nil
}
// BFInfoCapacity returns information about the capacity of a Bloom filter.
// For more information - https://redis.io/commands/bf.info/
func (c cmdable) BFInfoCapacity(ctx context.Context, key string) *BFInfoCmd {
return c.BFInfoArg(ctx, key, "CAPACITY")
}
// BFInfoSize returns information about the size of a Bloom filter.
// For more information - https://redis.io/commands/bf.info/
func (c cmdable) BFInfoSize(ctx context.Context, key string) *BFInfoCmd {
return c.BFInfoArg(ctx, key, "SIZE")
}
// BFInfoFilters returns information about the filters of a Bloom filter.
// For more information - https://redis.io/commands/bf.info/
func (c cmdable) BFInfoFilters(ctx context.Context, key string) *BFInfoCmd {
return c.BFInfoArg(ctx, key, "FILTERS")
}
// BFInfoItems returns information about the items of a Bloom filter.
// For more information - https://redis.io/commands/bf.info/
func (c cmdable) BFInfoItems(ctx context.Context, key string) *BFInfoCmd {
return c.BFInfoArg(ctx, key, "ITEMS")
}
// BFInfoExpansion returns information about the expansion rate of a Bloom filter.
// For more information - https://redis.io/commands/bf.info/
func (c cmdable) BFInfoExpansion(ctx context.Context, key string) *BFInfoCmd {
return c.BFInfoArg(ctx, key, "EXPANSION")
}
// BFInfoArg returns information about a specific option of a Bloom filter.
// For more information - https://redis.io/commands/bf.info/
func (c cmdable) BFInfoArg(ctx context.Context, key, option string) *BFInfoCmd {
args := []interface{}{"BF.INFO", key, option}
cmd := NewBFInfoCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// BFInsert inserts elements into a Bloom filter.
// This function also allows for specifying additional options such as:
// capacity, error rate, expansion rate, and non-scaling behavior.
// For more information - https://redis.io/commands/bf.insert/
func (c cmdable) BFInsert(ctx context.Context, key string, options *BFInsertOptions, elements ...interface{}) *BoolSliceCmd {
args := []interface{}{"BF.INSERT", key}
if options != nil {
if options.Capacity != 0 {
args = append(args, "CAPACITY", options.Capacity)
}
if options.Error != 0 {
args = append(args, "ERROR", options.Error)
}
if options.Expansion != 0 {
args = append(args, "EXPANSION", options.Expansion)
}
if options.NoCreate {
args = append(args, "NOCREATE")
}
if options.NonScaling {
args = append(args, "NONSCALING")
}
}
args = append(args, "ITEMS")
args = append(args, elements...)
cmd := NewBoolSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// BFMAdd adds multiple elements to a Bloom filter.
// Returns an array of booleans indicating whether each element was added to the filter or not.
// For more information - https://redis.io/commands/bf.madd/
func (c cmdable) BFMAdd(ctx context.Context, key string, elements ...interface{}) *BoolSliceCmd {
args := []interface{}{"BF.MADD", key}
args = append(args, elements...)
cmd := NewBoolSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// BFMExists check if multiple elements exist in a Bloom filter.
// Returns an array of booleans indicating whether each element exists in the filter or not.
// For more information - https://redis.io/commands/bf.mexists/
func (c cmdable) BFMExists(ctx context.Context, key string, elements ...interface{}) *BoolSliceCmd {
args := []interface{}{"BF.MEXISTS", key}
args = append(args, elements...)
cmd := NewBoolSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// -------------------------------------------
// Cuckoo filter commands
//-------------------------------------------
// CFReserve creates an empty Cuckoo filter with the specified capacity.
// For more information - https://redis.io/commands/cf.reserve/
func (c cmdable) CFReserve(ctx context.Context, key string, capacity int64) *StatusCmd {
args := []interface{}{"CF.RESERVE", key, capacity}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFReserveExpansion creates an empty Cuckoo filter with the specified capacity and expansion rate.
// For more information - https://redis.io/commands/cf.reserve/
func (c cmdable) CFReserveExpansion(ctx context.Context, key string, capacity int64, expansion int64) *StatusCmd {
args := []interface{}{"CF.RESERVE", key, capacity, "EXPANSION", expansion}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFReserveBucketSize creates an empty Cuckoo filter with the specified capacity and bucket size.
// For more information - https://redis.io/commands/cf.reserve/
func (c cmdable) CFReserveBucketSize(ctx context.Context, key string, capacity int64, bucketsize int64) *StatusCmd {
args := []interface{}{"CF.RESERVE", key, capacity, "BUCKETSIZE", bucketsize}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFReserveMaxIterations creates an empty Cuckoo filter with the specified capacity and maximum number of iterations.
// For more information - https://redis.io/commands/cf.reserve/
func (c cmdable) CFReserveMaxIterations(ctx context.Context, key string, capacity int64, maxiterations int64) *StatusCmd {
args := []interface{}{"CF.RESERVE", key, capacity, "MAXITERATIONS", maxiterations}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFReserveWithArgs creates an empty Cuckoo filter with the specified options.
// This function allows for specifying additional options such as bucket size and maximum number of iterations.
// For more information - https://redis.io/commands/cf.reserve/
func (c cmdable) CFReserveWithArgs(ctx context.Context, key string, options *CFReserveOptions) *StatusCmd {
args := []interface{}{"CF.RESERVE", key, options.Capacity}
if options.BucketSize != 0 {
args = append(args, "BUCKETSIZE", options.BucketSize)
}
if options.MaxIterations != 0 {
args = append(args, "MAXITERATIONS", options.MaxIterations)
}
if options.Expansion != 0 {
args = append(args, "EXPANSION", options.Expansion)
}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFAdd adds an element to a Cuckoo filter.
// Returns true if the element was added to the filter or false if it already exists in the filter.
// For more information - https://redis.io/commands/cf.add/
func (c cmdable) CFAdd(ctx context.Context, key string, element interface{}) *BoolCmd {
args := []interface{}{"CF.ADD", key, element}
cmd := NewBoolCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFAddNX adds an element to a Cuckoo filter only if it does not already exist in the filter.
// Returns true if the element was added to the filter or false if it already exists in the filter.
// For more information - https://redis.io/commands/cf.addnx/
func (c cmdable) CFAddNX(ctx context.Context, key string, element interface{}) *BoolCmd {
args := []interface{}{"CF.ADDNX", key, element}
cmd := NewBoolCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFCount returns an estimate of the number of times an element may be in a Cuckoo Filter.
// For more information - https://redis.io/commands/cf.count/
func (c cmdable) CFCount(ctx context.Context, key string, element interface{}) *IntCmd {
args := []interface{}{"CF.COUNT", key, element}
cmd := NewIntCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFDel deletes an item once from the cuckoo filter.
// For more information - https://redis.io/commands/cf.del/
func (c cmdable) CFDel(ctx context.Context, key string, element interface{}) *BoolCmd {
args := []interface{}{"CF.DEL", key, element}
cmd := NewBoolCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFExists determines whether an item may exist in the Cuckoo Filter or not.
// For more information - https://redis.io/commands/cf.exists/
func (c cmdable) CFExists(ctx context.Context, key string, element interface{}) *BoolCmd {
args := []interface{}{"CF.EXISTS", key, element}
cmd := NewBoolCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFLoadChunk restores a filter previously saved using SCANDUMP.
// For more information - https://redis.io/commands/cf.loadchunk/
func (c cmdable) CFLoadChunk(ctx context.Context, key string, iterator int64, data interface{}) *StatusCmd {
args := []interface{}{"CF.LOADCHUNK", key, iterator, data}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFScanDump begins an incremental save of the cuckoo filter.
// For more information - https://redis.io/commands/cf.scandump/
func (c cmdable) CFScanDump(ctx context.Context, key string, iterator int64) *ScanDumpCmd {
args := []interface{}{"CF.SCANDUMP", key, iterator}
cmd := newScanDumpCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
type CFInfo struct {
Size int64
NumBuckets int64
NumFilters int64
NumItemsInserted int64
NumItemsDeleted int64
BucketSize int64
ExpansionRate int64
MaxIteration int64
}
type CFInfoCmd struct {
baseCmd
val CFInfo
}
func NewCFInfoCmd(ctx context.Context, args ...interface{}) *CFInfoCmd {
return &CFInfoCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}
func (cmd *CFInfoCmd) SetVal(val CFInfo) {
cmd.val = val
}
func (cmd *CFInfoCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *CFInfoCmd) Val() CFInfo {
return cmd.val
}
func (cmd *CFInfoCmd) Result() (CFInfo, error) {
return cmd.val, cmd.err
}
func (cmd *CFInfoCmd) readReply(rd *proto.Reader) (err error) {
n, err := rd.ReadMapLen()
if err != nil {
return err
}
var key string
var result CFInfo
for f := 0; f < n; f++ {
key, err = rd.ReadString()
if err != nil {
return err
}
switch key {
case "Size":
result.Size, err = rd.ReadInt()
case "Number of buckets":
result.NumBuckets, err = rd.ReadInt()
case "Number of filters":
result.NumFilters, err = rd.ReadInt()
case "Number of items inserted":
result.NumItemsInserted, err = rd.ReadInt()
case "Number of items deleted":
result.NumItemsDeleted, err = rd.ReadInt()
case "Bucket size":
result.BucketSize, err = rd.ReadInt()
case "Expansion rate":
result.ExpansionRate, err = rd.ReadInt()
case "Max iterations":
result.MaxIteration, err = rd.ReadInt()
default:
return fmt.Errorf("redis: CF.INFO unexpected key %s", key)
}
if err != nil {
return err
}
}
cmd.val = result
return nil
}
// CFInfo returns information about a Cuckoo filter.
// For more information - https://redis.io/commands/cf.info/
func (c cmdable) CFInfo(ctx context.Context, key string) *CFInfoCmd {
args := []interface{}{"CF.INFO", key}
cmd := NewCFInfoCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFInsert inserts elements into a Cuckoo filter.
// This function also allows for specifying additional options such as capacity, error rate, expansion rate, and non-scaling behavior.
// Returns an array of booleans indicating whether each element was added to the filter or not.
// For more information - https://redis.io/commands/cf.insert/
func (c cmdable) CFInsert(ctx context.Context, key string, options *CFInsertOptions, elements ...interface{}) *BoolSliceCmd {
args := []interface{}{"CF.INSERT", key}
args = c.getCfInsertWithArgs(args, options, elements...)
cmd := NewBoolSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CFInsertNX inserts elements into a Cuckoo filter only if they do not already exist in the filter.
// This function also allows for specifying additional options such as:
// capacity, error rate, expansion rate, and non-scaling behavior.
// Returns an array of integers indicating whether each element was added to the filter or not.
// For more information - https://redis.io/commands/cf.insertnx/
func (c cmdable) CFInsertNX(ctx context.Context, key string, options *CFInsertOptions, elements ...interface{}) *IntSliceCmd {
args := []interface{}{"CF.INSERTNX", key}
args = c.getCfInsertWithArgs(args, options, elements...)
cmd := NewIntSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
func (c cmdable) getCfInsertWithArgs(args []interface{}, options *CFInsertOptions, elements ...interface{}) []interface{} {
if options != nil {
if options.Capacity != 0 {
args = append(args, "CAPACITY", options.Capacity)
}
if options.NoCreate {
args = append(args, "NOCREATE")
}
}
args = append(args, "ITEMS")
args = append(args, elements...)
return args
}
// CFMExists check if multiple elements exist in a Cuckoo filter.
// Returns an array of booleans indicating whether each element exists in the filter or not.
// For more information - https://redis.io/commands/cf.mexists/
func (c cmdable) CFMExists(ctx context.Context, key string, elements ...interface{}) *BoolSliceCmd {
args := []interface{}{"CF.MEXISTS", key}
args = append(args, elements...)
cmd := NewBoolSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// -------------------------------------------
// CMS commands
//-------------------------------------------
// CMSIncrBy increments the count of one or more items in a Count-Min Sketch filter.
// Returns an array of integers representing the updated count of each item.
// For more information - https://redis.io/commands/cms.incrby/
func (c cmdable) CMSIncrBy(ctx context.Context, key string, elements ...interface{}) *IntSliceCmd {
args := make([]interface{}, 2, 2+len(elements))
args[0] = "CMS.INCRBY"
args[1] = key
args = appendArgs(args, elements)
cmd := NewIntSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
type CMSInfo struct {
Width int64
Depth int64
Count int64
}
type CMSInfoCmd struct {
baseCmd
val CMSInfo
}
func NewCMSInfoCmd(ctx context.Context, args ...interface{}) *CMSInfoCmd {
return &CMSInfoCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}
func (cmd *CMSInfoCmd) SetVal(val CMSInfo) {
cmd.val = val
}
func (cmd *CMSInfoCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *CMSInfoCmd) Val() CMSInfo {
return cmd.val
}
func (cmd *CMSInfoCmd) Result() (CMSInfo, error) {
return cmd.val, cmd.err
}
func (cmd *CMSInfoCmd) readReply(rd *proto.Reader) (err error) {
n, err := rd.ReadMapLen()
if err != nil {
return err
}
var key string
var result CMSInfo
for f := 0; f < n; f++ {
key, err = rd.ReadString()
if err != nil {
return err
}
switch key {
case "width":
result.Width, err = rd.ReadInt()
case "depth":
result.Depth, err = rd.ReadInt()
case "count":
result.Count, err = rd.ReadInt()
default:
return fmt.Errorf("redis: CMS.INFO unexpected key %s", key)
}
if err != nil {
return err
}
}
cmd.val = result
return nil
}
// CMSInfo returns information about a Count-Min Sketch filter.
// For more information - https://redis.io/commands/cms.info/
func (c cmdable) CMSInfo(ctx context.Context, key string) *CMSInfoCmd {
args := []interface{}{"CMS.INFO", key}
cmd := NewCMSInfoCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CMSInitByDim creates an empty Count-Min Sketch filter with the specified dimensions.
// For more information - https://redis.io/commands/cms.initbydim/
func (c cmdable) CMSInitByDim(ctx context.Context, key string, width, depth int64) *StatusCmd {
args := []interface{}{"CMS.INITBYDIM", key, width, depth}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CMSInitByProb creates an empty Count-Min Sketch filter with the specified error rate and probability.
// For more information - https://redis.io/commands/cms.initbyprob/
func (c cmdable) CMSInitByProb(ctx context.Context, key string, errorRate, probability float64) *StatusCmd {
args := []interface{}{"CMS.INITBYPROB", key, errorRate, probability}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CMSMerge merges multiple Count-Min Sketch filters into a single filter.
// The destination filter must not exist and will be created with the dimensions of the first source filter.
// The number of items in each source filter must be equal.
// Returns OK on success or an error if the filters could not be merged.
// For more information - https://redis.io/commands/cms.merge/
func (c cmdable) CMSMerge(ctx context.Context, destKey string, sourceKeys ...string) *StatusCmd {
args := []interface{}{"CMS.MERGE", destKey, len(sourceKeys)}
for _, s := range sourceKeys {
args = append(args, s)
}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CMSMergeWithWeight merges multiple Count-Min Sketch filters into a single filter with weights for each source filter.
// The destination filter must not exist and will be created with the dimensions of the first source filter.
// The number of items in each source filter must be equal.
// Returns OK on success or an error if the filters could not be merged.
// For more information - https://redis.io/commands/cms.merge/
func (c cmdable) CMSMergeWithWeight(ctx context.Context, destKey string, sourceKeys map[string]int64) *StatusCmd {
args := make([]interface{}, 0, 4+(len(sourceKeys)*2+1))
args = append(args, "CMS.MERGE", destKey, len(sourceKeys))
if len(sourceKeys) > 0 {
sk := make([]interface{}, len(sourceKeys))
sw := make([]interface{}, len(sourceKeys))
i := 0
for k, w := range sourceKeys {
sk[i] = k
sw[i] = w
i++
}
args = append(args, sk...)
args = append(args, "WEIGHTS")
args = append(args, sw...)
}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// CMSQuery returns count for item(s).
// For more information - https://redis.io/commands/cms.query/
func (c cmdable) CMSQuery(ctx context.Context, key string, elements ...interface{}) *IntSliceCmd {
args := []interface{}{"CMS.QUERY", key}
args = append(args, elements...)
cmd := NewIntSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// -------------------------------------------
// TopK commands
//--------------------------------------------
// TopKAdd adds one or more elements to a Top-K filter.
// Returns an array of strings representing the items that were removed from the filter, if any.
// For more information - https://redis.io/commands/topk.add/
func (c cmdable) TopKAdd(ctx context.Context, key string, elements ...interface{}) *StringSliceCmd {
args := make([]interface{}, 2, 2+len(elements))
args[0] = "TOPK.ADD"
args[1] = key
args = appendArgs(args, elements)
cmd := NewStringSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// TopKReserve creates an empty Top-K filter with the specified number of top items to keep.
// For more information - https://redis.io/commands/topk.reserve/
func (c cmdable) TopKReserve(ctx context.Context, key string, k int64) *StatusCmd {
args := []interface{}{"TOPK.RESERVE", key, k}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
// TopKReserveWithOptions creates an empty Top-K filter with the specified number of top items to keep and additional options.
// This function allows for specifying additional options such as width, depth and decay.
// For more information - https://redis.io/commands/topk.reserve/
func (c cmdable) TopKReserveWithOptions(ctx context.Context, key string, k int64, width, depth int64, decay float64) *StatusCmd {
args := []interface{}{"TOPK.RESERVE", key, k, width, depth, decay}
cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
type TopKInfo struct {
K int64
Width int64
Depth int64
Decay float64
}
type TopKInfoCmd struct {
baseCmd
val TopKInfo
}
func NewTopKInfoCmd(ctx context.Context, args ...interface{}) *TopKInfoCmd {
return &TopKInfoCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}
func (cmd *TopKInfoCmd) SetVal(val TopKInfo) {
cmd.val = val
}
func (cmd *TopKInfoCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *TopKInfoCmd) Val() TopKInfo {
return cmd.val
}
func (cmd *TopKInfoCmd) Result() (TopKInfo, error) {
return cmd.val, cmd.err
}
func (cmd *TopKInfoCmd) readReply(rd *proto.Reader) (err error) {
n, err := rd.ReadMapLen()
if err != nil {
return err
}
var key string
var result TopKInfo
for f := 0; f < n; f++ {
key, err = rd.ReadString()
if err != nil {
return err
}
switch key {
case "k":
result.K, err = rd.ReadInt()
case "width":
result.Width, err = rd.ReadInt()
case "depth":
result.Depth, err = rd.ReadInt()
case "decay":
result.Decay, err = rd.ReadFloat()
default:
return fmt.Errorf("redis: topk.info unexpected key %s", key)
}
if err != nil {