-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsorted_set_commands.rs
1098 lines (1038 loc) · 32.8 KB
/
sorted_set_commands.rs
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
use crate::{
client::{prepare_command, PreparedCommand},
resp::{
cmd, deserialize_vec_of_pairs, CommandArgs, MultipleArgsCollection, PrimitiveResponse,
SingleArg, SingleArgCollection, ToArgs,
},
};
use serde::{de::DeserializeOwned, Deserialize};
/// A group of Redis commands related to [`Sorted Sets`](https://redis.io/docs/data-types/sorted-sets/)
///
/// # See Also
/// [Redis Sorted Set Commands](https://redis.io/commands/?group=sorted-set)
pub trait SortedSetCommands<'a> {
/// Adds all the specified members with the specified scores
/// to the sorted set stored at key.
///
/// # Return
/// * When used without optional arguments, the number of elements added to the sorted set (excluding score updates).
/// * If the `change` option is specified, the number of elements that were changed (added or updated).
///
/// # See Also
/// [<https://redis.io/commands/zadd/>](https://redis.io/commands/zadd/)
#[must_use]
fn zadd<K, M, I>(
self,
key: K,
items: I,
options: ZAddOptions,
) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
I: MultipleArgsCollection<(f64, M)>,
{
prepare_command(self, cmd("ZADD").arg(key).arg(options).arg(items))
}
/// In this mode ZADD acts like ZINCRBY.
/// Only one score-element pair can be specified in this mode.
///
/// # Return
/// The new score of member (a double precision floating point number),
/// or nil if the operation was aborted (when called with either the XX or the NX option).
///
/// # See Also
/// [<https://redis.io/commands/zadd/>](https://redis.io/commands/zadd/)
#[must_use]
fn zadd_incr<K, M>(
self,
key: K,
condition: ZAddCondition,
comparison: ZAddComparison,
change: bool,
score: f64,
member: M,
) -> PreparedCommand<'a, Self, Option<f64>>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
{
prepare_command(
self,
cmd("ZADD")
.arg(key)
.arg(condition)
.arg(comparison)
.arg_if(change, "CH")
.arg(score)
.arg(member),
)
}
/// Returns the sorted set cardinality (number of elements)
/// of the sorted set stored at key.
///
/// # Return
/// The cardinality (number of elements) of the sorted set, or 0 if key does not exist.
///
/// # See Also
/// [<https://redis.io/commands/zcard/>](https://redis.io/commands/zcard/)
#[must_use]
fn zcard<K>(self, key: K) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
{
prepare_command(self, cmd("ZCARD").arg(key))
}
/// Returns the number of elements in the sorted set at key with a score between min and max.
///
/// # Return
/// The number of elements in the specified score range.
///
/// # See Also
/// [<https://redis.io/commands/zcount/>](https://redis.io/commands/zcount/)
#[must_use]
fn zcount<K, M1, M2>(self, key: K, min: M1, max: M2) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
M1: SingleArg,
M2: SingleArg,
{
prepare_command(self, cmd("ZCOUNT").arg(key).arg(min).arg(max))
}
/// This command is similar to [zdiffstore](SortedSetCommands::zdiffstore), but instead of storing the resulting sorted set,
/// it is returned to the client.
///
/// # Return
/// The result of the difference
///
/// # See Also
/// [<https://redis.io/commands/zdiff/>](https://redis.io/commands/zdiff/)
#[must_use]
fn zdiff<K, C, E>(self, keys: C) -> PreparedCommand<'a, Self, Vec<E>>
where
Self: Sized,
K: SingleArg,
C: SingleArgCollection<K>,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(self, cmd("ZDIFF").arg(keys.num_args()).arg(keys))
}
/// This command is similar to [zdiffstore](SortedSetCommands::zdiffstore), but instead of storing the resulting sorted set,
/// it is returned to the client.
///
/// # Return
/// The result of the difference with their scores
///
/// # See Also
/// [<https://redis.io/commands/zdiff/>](https://redis.io/commands/zdiff/)
#[must_use]
fn zdiff_with_scores<K, C, E>(self, keys: C) -> PreparedCommand<'a, Self, Vec<(E, f64)>>
where
Self: Sized,
K: SingleArg,
C: SingleArgCollection<K>,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(
self,
cmd("ZDIFF")
.arg(keys.num_args())
.arg(keys)
.arg("WITHSCORES"),
)
}
/// Computes the difference between the first and all successive
/// input sorted sets and stores the result in destination.
///
/// # Return
/// The number of elements in the resulting sorted set at destination.
///
/// # See Also
/// [<https://redis.io/commands/zdiffstore/>](https://redis.io/commands/zdiffstore/)
#[must_use]
fn zdiffstore<D, K, C>(self, destination: D, keys: C) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
D: SingleArg,
K: SingleArg,
C: SingleArgCollection<K>,
{
prepare_command(
self,
cmd("ZDIFFSTORE")
.arg(destination)
.arg(keys.num_args())
.arg(keys),
)
}
/// Increments the score of member in the sorted set stored at key by increment.
///
/// # Return
/// the new score of member
///
/// # See Also
/// [<https://redis.io/commands/zincrby/>](https://redis.io/commands/zincrby/)
#[must_use]
fn zincrby<K, M>(self, key: K, increment: f64, member: M) -> PreparedCommand<'a, Self, f64>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
{
prepare_command(self, cmd("ZINCRBY").arg(key).arg(increment).arg(member))
}
/// This command is similar to [zinterstore](SortedSetCommands::zinterstore),
/// but instead of storing the resulting sorted set, it is returned to the client.
///
/// # Return
/// The result of the intersection as an array of members
///
/// # See Also
/// [<https://redis.io/commands/zinter/>](https://redis.io/commands/zinter/)
#[must_use]
fn zinter<K, C, W, E>(
self,
keys: C,
weights: Option<W>,
aggregate: ZAggregate,
) -> PreparedCommand<'a, Self, Vec<E>>
where
Self: Sized,
K: SingleArg,
C: SingleArgCollection<K>,
W: SingleArgCollection<f64>,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(
self,
cmd("ZINTER")
.arg(keys.num_args())
.arg(keys)
.arg(weights.map(|w| ("WEIGHTS", w)))
.arg(aggregate),
)
}
/// This command is similar to [zinterstore](SortedSetCommands::zinterstore),
/// but instead of storing the resulting sorted set, it is returned to the client.
///
/// # Return
/// The result of the intersection as an array of members with their scores
///
/// # See Also
/// [<https://redis.io/commands/zinter/>](https://redis.io/commands/zinter/)
#[must_use]
fn zinter_with_scores<K, C, W, E>(
self,
keys: C,
weights: Option<W>,
aggregate: ZAggregate,
) -> PreparedCommand<'a, Self, Vec<(E, f64)>>
where
Self: Sized,
K: SingleArg,
C: SingleArgCollection<K>,
W: SingleArgCollection<f64>,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(
self,
cmd("ZINTER")
.arg(keys.num_args())
.arg(keys)
.arg(weights.map(|w| ("WEIGHTS", w)))
.arg(aggregate)
.arg("WITHSCORES"),
)
}
/// This command is similar to [zinter](SortedSetCommands::zinter),
/// but instead of returning the result set, it returns just the cardinality of the result.
///
//// limit: if the intersection cardinality reaches limit partway through the computation,
/// the algorithm will exit and yield limit as the cardinality. 0 means unlimited
///
/// # See Also
/// [<https://redis.io/commands/zintercard/>](https://redis.io/commands/zintercard/)
#[must_use]
fn zintercard<K, C>(self, keys: C, limit: usize) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
C: SingleArgCollection<K>,
{
prepare_command(
self,
cmd("ZINTERCARD")
.arg(keys.num_args())
.arg(keys)
.arg("LIMIT")
.arg(limit),
)
}
/// Computes the intersection of numkeys sorted sets given by the specified keys,
/// and stores the result in destination.
///
/// # Return
/// The number of elements in the resulting sorted set at destination.
///
/// # See Also
/// [<https://redis.io/commands/zinterstore/>](https://redis.io/commands/zinterstore/)
#[must_use]
fn zinterstore<D, K, C, W>(
self,
destination: D,
keys: C,
weights: Option<W>,
aggregate: ZAggregate,
) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
D: SingleArg,
K: SingleArg,
C: SingleArgCollection<K>,
W: SingleArgCollection<f64>,
{
prepare_command(
self,
cmd("ZINTERSTORE")
.arg(destination)
.arg(keys.num_args())
.arg(keys)
.arg(weights.map(|w| ("WEIGHTS", w)))
.arg(aggregate),
)
}
/// When all the elements in a sorted set are inserted with the same score,
/// in order to force lexicographical ordering, this command returns the number
/// of elements in the sorted set at key with a value between min and max.
///
/// # Return
/// the number of elements in the specified score range.
///
/// # See Also
/// [<https://redis.io/commands/zlexcount/>](https://redis.io/commands/zlexcount/)
#[must_use]
fn zlexcount<K, M1, M2>(self, key: K, min: M1, max: M2) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
M1: SingleArg,
M2: SingleArg,
{
prepare_command(self, cmd("ZLEXCOUNT").arg(key).arg(min).arg(max))
}
/// Pops one or more elements, that are member-score pairs,
/// from the first non-empty sorted set in the provided list of key names.
///
/// # Return
/// * None if no element could be popped
/// * A tuple made up of
/// * The name of the key from which elements were popped
/// * An array of tuples with all the popped members and their scores
///
/// # See Also
/// [<https://redis.io/commands/zmpop/>](https://redis.io/commands/zmpop/)
#[must_use]
fn zmpop<K, C, E>(
self,
keys: C,
where_: ZWhere,
count: usize,
) -> PreparedCommand<'a, Self, Option<ZMPopResult<E>>>
where
Self: Sized,
K: SingleArg,
C: SingleArgCollection<K>,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(
self,
cmd("ZMPOP")
.arg(keys.num_args())
.arg(keys)
.arg(where_)
.arg("COUNT")
.arg(count),
)
}
/// Returns the scores associated with the specified members in the sorted set stored at key.
///
/// For every member that does not exist in the sorted set, a nil value is returned.
///
/// # Return
/// The list of scores or nil associated with the specified member value
///
/// # See Also
/// [<https://redis.io/commands/zmscore/>](https://redis.io/commands/zmscore/)
#[must_use]
fn zmscore<K, M, C>(self, key: K, members: C) -> PreparedCommand<'a, Self, Vec<Option<f64>>>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
C: SingleArgCollection<M>,
{
prepare_command(self, cmd("ZMSCORE").arg(key).arg(members))
}
/// Removes and returns up to count members with the highest scores in the sorted set stored at key.
///
/// # Return
/// The list of popped elements and scores.
///
/// # See Also
/// [<https://redis.io/commands/zpopmax/>](https://redis.io/commands/zpopmax/)
#[must_use]
fn zpopmax<K, M>(self, key: K, count: usize) -> PreparedCommand<'a, Self, Vec<(M, f64)>>
where
Self: Sized,
K: SingleArg,
M: PrimitiveResponse + DeserializeOwned,
{
prepare_command(self, cmd("ZPOPMAX").arg(key).arg(count))
}
/// Removes and returns up to count members with the lowest scores in the sorted set stored at key.
///
/// # Return
/// The list of popped elements and scores.
///
/// # See Also
/// [<https://redis.io/commands/zpopmin/>](https://redis.io/commands/zpopmin/)
#[must_use]
fn zpopmin<K, M>(self, key: K, count: usize) -> PreparedCommand<'a, Self, Vec<(M, f64)>>
where
Self: Sized,
K: SingleArg,
M: PrimitiveResponse + DeserializeOwned,
{
prepare_command(self, cmd("ZPOPMIN").arg(key).arg(count))
}
/// Return a random element from the sorted set value stored at key.
///
/// # Return
/// The randomly selected element, or nil when key does not exist.
///
/// # See Also
/// [<https://redis.io/commands/zrandmember/>](https://redis.io/commands/zrandmember/)
#[must_use]
fn zrandmember<K, E>(self, key: K) -> PreparedCommand<'a, Self, E>
where
Self: Sized,
K: SingleArg,
E: PrimitiveResponse,
{
prepare_command(self, cmd("ZRANDMEMBER").arg(key))
}
/// Return random elements from the sorted set value stored at key.
///
/// # Return
/// * If the provided count argument is positive, return an array of distinct elements.
/// The array's length is either count or the sorted set's cardinality (ZCARD), whichever is lower.
/// * If called with a negative count, the behavior changes and the command is allowed
/// to return the same element multiple times. In this case, the number of returned elements
/// is the absolute value of the specified count.
///
/// # See Also
/// [<https://redis.io/commands/zrandmember/>](https://redis.io/commands/zrandmember/)
#[must_use]
fn zrandmembers<K, E>(self, key: K, count: isize) -> PreparedCommand<'a, Self, Vec<E>>
where
Self: Sized,
K: SingleArg,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(self, cmd("ZRANDMEMBER").arg(key).arg(count))
}
/// Return random elements with their scores from the sorted set value stored at key.
///
/// # Return
/// * If the provided count argument is positive, return an array of distinct elements with their scores.
/// The array's length is either count or the sorted set's cardinality (ZCARD), whichever is lower.
/// * If called with a negative count, the behavior changes and the command is allowed
/// to return the same element multiple times. In this case, the number of returned elements
/// is the absolute value of the specified count.
///
/// # See Also
/// [<https://redis.io/commands/zrandmember/>](https://redis.io/commands/zrandmember/)
#[must_use]
fn zrandmembers_with_scores<K, E>(
self,
key: K,
count: isize,
) -> PreparedCommand<'a, Self, Vec<E>>
where
Self: Sized,
K: SingleArg,
E: DeserializeOwned,
{
prepare_command(
self,
cmd("ZRANDMEMBER").arg(key).arg(count).arg("WITHSCORES"),
)
}
/// Returns the specified range of elements in the sorted set stored at `key`.
///
/// # Return
/// A collection of elements in the specified range
///
/// # See Also
/// [<https://redis.io/commands/zrange/>](https://redis.io/commands/zrange/)
#[must_use]
fn zrange<K, S, E>(
self,
key: K,
start: S,
stop: S,
options: ZRangeOptions,
) -> PreparedCommand<'a, Self, Vec<E>>
where
Self: Sized,
K: SingleArg,
S: SingleArg,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(
self,
cmd("ZRANGE").arg(key).arg(start).arg(stop).arg(options),
)
}
/// Returns the specified range of elements in the sorted set stored at `key`.
///
/// # Return
/// A collection of elements and their scores in the specified range
///
/// # See Also
/// [<https://redis.io/commands/zrange/>](https://redis.io/commands/zrange/)
#[must_use]
fn zrange_with_scores<K, S, E>(
self,
key: K,
start: S,
stop: S,
options: ZRangeOptions,
) -> PreparedCommand<'a, Self, Vec<(E, f64)>>
where
Self: Sized,
K: SingleArg,
S: SingleArg,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(
self,
cmd("ZRANGE")
.arg(key)
.arg(start)
.arg(stop)
.arg(options)
.arg("WITHSCORES"),
)
}
/// This command is like [zrange](SortedSetCommands::zrange),
/// but stores the result in the `dst` destination key.
///
/// # Return
/// The number of elements in the resulting sorted set.
///
/// # See Also
/// [<https://redis.io/commands/zrangestore/>](https://redis.io/commands/zrangestore/)
#[must_use]
fn zrangestore<D, S, SS>(
self,
dst: D,
src: S,
start: SS,
stop: SS,
options: ZRangeOptions,
) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
D: SingleArg,
S: SingleArg,
SS: SingleArg,
{
prepare_command(
self,
cmd("ZRANGESTORE")
.arg(dst)
.arg(src)
.arg(start)
.arg(stop)
.arg(options),
)
}
/// Returns the rank of member in the sorted set stored at key,
/// with the scores ordered from low to high.
///
/// # Return
/// * If member exists in the sorted set, the rank of member.
/// * If member does not exist in the sorted set or key does not exist, None.
///
/// # See Also
/// [<https://redis.io/commands/zrank/>](https://redis.io/commands/zrank/)
#[must_use]
fn zrank<K, M>(self, key: K, member: M) -> PreparedCommand<'a, Self, Option<usize>>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
{
prepare_command(self, cmd("ZRANK").arg(key).arg(member))
}
/// Removes the specified members from the sorted set stored at key.
///
/// # Return
/// The number of members removed from the sorted set, not including non existing members.
///
/// # See Also
/// [<https://redis.io/commands/zrem/>](https://redis.io/commands/zrem/)
#[must_use]
fn zrem<K, M, C>(self, key: K, members: C) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
C: SingleArgCollection<M>,
{
prepare_command(self, cmd("ZREM").arg(key).arg(members))
}
/// When all the elements in a sorted set are inserted with the same score,
/// in order to force lexicographical ordering,
/// this command removes all elements in the sorted set stored at key
/// between the lexicographical range specified by min and max.
///
/// # Return
/// the number of elements removed.
///
/// # See Also
/// [<https://redis.io/commands/zremrangebylex/>](https://redis.io/commands/zremrangebylex/)
#[must_use]
fn zremrangebylex<K, S>(self, key: K, start: S, stop: S) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
S: SingleArg,
{
prepare_command(self, cmd("ZREMRANGEBYLEX").arg(key).arg(start).arg(stop))
}
/// Removes all elements in the sorted set stored at key with rank between start and stop.
///
/// # Return
/// the number of elements removed.
///
/// # See Also
/// [<https://redis.io/commands/zremrangebyrank/>](https://redis.io/commands/zremrangebyrank/)
#[must_use]
fn zremrangebyrank<K>(
self,
key: K,
start: isize,
stop: isize,
) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
{
prepare_command(self, cmd("ZREMRANGEBYRANK").arg(key).arg(start).arg(stop))
}
/// Removes all elements in the sorted set stored at key with a score between min and max (inclusive).
///
/// # Return
/// the number of elements removed.
///
/// # See Also
/// [<https://redis.io/commands/zremrangebyscore/>](https://redis.io/commands/zremrangebyscore/)
#[must_use]
fn zremrangebyscore<K, S>(self, key: K, start: S, stop: S) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
S: SingleArg,
{
prepare_command(self, cmd("ZREMRANGEBYSCORE").arg(key).arg(start).arg(stop))
}
/// Returns the rank of member in the sorted set stored at key, with the scores ordered from high to low.
///
/// # Return
/// * If member exists in the sorted set, the rank of member.
/// * If member does not exist in the sorted set or key does not exist, None.
///
/// # See Also
/// [<https://redis.io/commands/zrevrank/>](https://redis.io/commands/zrevrank/)
#[must_use]
fn zrevrank<K, M>(self, key: K, member: M) -> PreparedCommand<'a, Self, Option<usize>>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
{
prepare_command(self, cmd("ZREVRANK").arg(key).arg(member))
}
/// Iterates elements of Sorted Set types and their associated scores.
///
/// # Returns
/// A tuple where
/// * The first value is the cursor as an unsigned 64 bit number
/// * The second value is a list of members and their scores in a Vec of Tuples
///
/// # See Also
/// [<https://redis.io/commands/zscan/>](https://redis.io/commands/zscan/)
#[must_use]
fn zscan<K, M>(
self,
key: K,
cursor: usize,
options: ZScanOptions,
) -> PreparedCommand<'a, Self, ZScanResult<M>>
where
Self: Sized,
K: SingleArg,
M: PrimitiveResponse + DeserializeOwned,
{
prepare_command(self, cmd("ZSCAN").arg(key).arg(cursor).arg(options))
}
/// Returns the score of member in the sorted set at key.
///
/// # Return
/// The score of `member` or nil if `key`does not exist
///
/// # See Also
/// [<https://redis.io/commands/zscore/>](https://redis.io/commands/zscore/)
#[must_use]
fn zscore<K, M>(self, key: K, member: M) -> PreparedCommand<'a, Self, Option<f64>>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
{
prepare_command(self, cmd("ZSCORE").arg(key).arg(member))
}
/// This command is similar to [zunionstore](SortedSetCommands::zunionstore),
/// but instead of storing the resulting sorted set, it is returned to the client.
///
/// # Return
/// The result of the unionsection as an array of members
///
/// # See Also
/// [<https://redis.io/commands/zunion/>](https://redis.io/commands/zunion/)
#[must_use]
fn zunion<K, C, W, E>(
self,
keys: C,
weights: Option<W>,
aggregate: ZAggregate,
) -> PreparedCommand<'a, Self, Vec<E>>
where
Self: Sized,
K: SingleArg,
C: SingleArgCollection<K>,
W: SingleArgCollection<f64>,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(
self,
cmd("ZUNION")
.arg(keys.num_args())
.arg(keys)
.arg(weights.map(|w| ("WEIGHTS", w)))
.arg(aggregate),
)
}
/// This command is similar to [zunionstore](SortedSetCommands::zunionstore),
/// but instead of storing the resulting sorted set, it is returned to the client.
///
/// # Return
/// The result of the unionsection as an array of members with their scores
///
/// # See Also
/// [<https://redis.io/commands/zunion/>](https://redis.io/commands/zunion/)
#[must_use]
fn zunion_with_scores<K, C, W, E>(
self,
keys: C,
weights: Option<W>,
aggregate: ZAggregate,
) -> PreparedCommand<'a, Self, Vec<(E, f64)>>
where
Self: Sized,
K: SingleArg,
C: SingleArgCollection<K>,
W: SingleArgCollection<f64>,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(
self,
cmd("ZUNION")
.arg(keys.num_args())
.arg(keys)
.arg(weights.map(|w| ("WEIGHTS", w)))
.arg(aggregate)
.arg("WITHSCORES"),
)
}
/// Computes the unionsection of numkeys sorted sets given by the specified keys,
/// and stores the result in destination.
///
/// # Return
/// The number of elements in the resulting sorted set at destination.
///
/// # See Also
/// [<https://redis.io/commands/zunionstore/>](https://redis.io/commands/zunionstore/)
#[must_use]
fn zunionstore<D, K, C, W>(
self,
destination: D,
keys: C,
weights: Option<W>,
aggregate: ZAggregate,
) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
D: SingleArg,
K: SingleArg,
C: SingleArgCollection<K>,
W: SingleArgCollection<f64>,
{
prepare_command(
self,
cmd("ZUNIONSTORE")
.arg(destination)
.arg(keys.num_args())
.arg(keys)
.arg(weights.map(|w| ("WEIGHTS", w)))
.arg(aggregate),
)
}
}
/// Condition option for the [`zadd`](SortedSetCommands::zadd) command
#[derive(Default)]
pub enum ZAddCondition {
/// No condition
#[default]
None,
/// Only update elements that already exist. Don't add new elements.
NX,
/// Only add new elements. Don't update already existing elements.
XX,
}
impl ToArgs for ZAddCondition {
fn write_args(&self, args: &mut CommandArgs) {
match self {
ZAddCondition::None => {}
ZAddCondition::NX => {
args.arg("NX");
}
ZAddCondition::XX => {
args.arg("XX");
}
}
}
}
/// Comparison option for the [`zadd`](SortedSetCommands::zadd) command
#[derive(Default)]
pub enum ZAddComparison {
/// No comparison
#[default]
None,
/// Only update existing elements if the new score is greater than the current score.
///
/// This flag doesn't prevent adding new elements.
GT,
/// Only update existing elements if the new score is less than the current score.
///
/// This flag doesn't prevent adding new elements.
LT,
}
impl ToArgs for ZAddComparison {
fn write_args(&self, args: &mut CommandArgs) {
match self {
ZAddComparison::None => {}
ZAddComparison::GT => {
args.arg("GT");
}
ZAddComparison::LT => {
args.arg("LT");
}
}
}
}
/// sort by option of the [`zrange`](SortedSetCommands::zrange) command
#[derive(Default)]
pub enum ZRangeSortBy {
/// No sort by
#[default]
None,
/// When the `ByScore` option is provided, the command behaves like `ZRANGEBYSCORE` and returns
/// the range of elements from the sorted set having scores equal or between `start` and `stop`.
ByScore,
/// When the `ByLex` option is used, the command behaves like `ZRANGEBYLEX` and returns the range
/// of elements from the sorted set between the `start` and `stop` lexicographical closed range intervals.
ByLex,
}
impl ToArgs for ZRangeSortBy {
fn write_args(&self, args: &mut CommandArgs) {
match self {
ZRangeSortBy::None => {}
ZRangeSortBy::ByScore => {
args.arg("BYSCORE");
}
ZRangeSortBy::ByLex => {
args.arg("BYLEX");
}
}
}
}
/// Option that specify how results of an union or intersection are aggregated
///
/// # See Also
/// [zinter](SortedSetCommands::zinter)
/// [zinterstore](SortedSetCommands::zinterstore)
/// [zunion](SortedSetCommands::zunion)
/// [zunionstore](SortedSetCommands::zunionstore)
#[derive(Default)]
pub enum ZAggregate {
/// No aggregation
#[default]
None,
/// The score of an element is summed across the inputs where it exists.
Sum,
/// The minimum score of an element across the inputs where it exists.
Min,
/// The maximum score of an element across the inputs where it exists.
Max,
}
impl ToArgs for ZAggregate {
fn write_args(&self, args: &mut CommandArgs) {
match self {
ZAggregate::None => {}
ZAggregate::Sum => {
args.arg("SUM");
}
ZAggregate::Min => {
args.arg("MIN");
}
ZAggregate::Max => {
args.arg("MAX");
}
}
}
}
/// Where option of the [`zmpop`](SortedSetCommands::zmpop) command
pub enum ZWhere {
/// When the MIN modifier is used, the elements popped are those
/// with the lowest scores from the first non-empty sorted set.
Min,
/// The MAX modifier causes elements with the highest scores to be popped.
Max,
}
impl ToArgs for ZWhere {
fn write_args(&self, args: &mut CommandArgs) {
match self {
ZWhere::Min => args.arg("MIN"),
ZWhere::Max => args.arg("MAX"),
};
}
}
/// Options for the [`zadd`](SortedSetCommands::zadd) command.
#[derive(Default)]
pub struct ZAddOptions {
command_args: CommandArgs,
}
impl ZAddOptions {
#[must_use]
pub fn condition(mut self, condition: ZAddCondition) -> Self {
Self {
command_args: self.command_args.arg(condition).build(),
}
}
#[must_use]
pub fn comparison(mut self, comparison: ZAddComparison) -> Self {
Self {
command_args: self.command_args.arg(comparison).build(),
}