fred/commands/interfaces/
sorted_sets.rs

1use crate::{
2  commands,
3  error::Error,
4  interfaces::{ClientLike, FredResult},
5  types::{
6    sorted_sets::{AggregateOptions, MultipleWeights, MultipleZaddValues, Ordering, ZCmp, ZRange, ZSort},
7    FromValue,
8    Key,
9    Limit,
10    MultipleKeys,
11    MultipleValues,
12    SetOptions,
13    Value,
14  },
15};
16use fred_macros::rm_send_if;
17use futures::Future;
18use std::convert::TryInto;
19
20/// Functions that implement the [sorted sets](https://redis.io/commands#sorted_set) interface.
21#[rm_send_if(feature = "glommio")]
22pub trait SortedSetsInterface: ClientLike + Sized {
23  /// The blocking variant of [Self::zmpop].
24  ///
25  /// <https://redis.io/commands/bzmpop/>
26  fn bzmpop<R, K>(
27    &self,
28    timeout: f64,
29    keys: K,
30    sort: ZCmp,
31    count: Option<i64>,
32  ) -> impl Future<Output = FredResult<R>> + Send
33  where
34    R: FromValue,
35    K: Into<MultipleKeys> + Send,
36  {
37    async move {
38      into!(keys);
39      commands::sorted_sets::bzmpop(self, timeout, keys, sort, count)
40        .await?
41        .convert()
42    }
43  }
44
45  /// The blocking variant of [Self::zpopmin].
46  ///
47  /// <https://redis.io/commands/bzpopmin>
48  fn bzpopmin<R, K>(&self, keys: K, timeout: f64) -> impl Future<Output = FredResult<R>> + Send
49  where
50    R: FromValue,
51    K: Into<MultipleKeys> + Send,
52  {
53    async move {
54      into!(keys);
55      commands::sorted_sets::bzpopmin(self, keys, timeout).await?.convert()
56    }
57  }
58
59  /// The blocking variant of [Self::zpopmax].
60  ///
61  /// <https://redis.io/commands/bzpopmax>
62  fn bzpopmax<R, K>(&self, keys: K, timeout: f64) -> impl Future<Output = FredResult<R>> + Send
63  where
64    R: FromValue,
65    K: Into<MultipleKeys> + Send,
66  {
67    async move {
68      into!(keys);
69      commands::sorted_sets::bzpopmax(self, keys, timeout).await?.convert()
70    }
71  }
72
73  /// Adds all the specified members with the specified scores to the sorted set stored at `key`.
74  ///
75  /// <https://redis.io/commands/zadd>
76  fn zadd<R, K, V>(
77    &self,
78    key: K,
79    options: Option<SetOptions>,
80    ordering: Option<Ordering>,
81    changed: bool,
82    incr: bool,
83    values: V,
84  ) -> impl Future<Output = FredResult<R>> + Send
85  where
86    R: FromValue,
87    K: Into<Key> + Send,
88    V: TryInto<MultipleZaddValues> + Send,
89    V::Error: Into<Error> + Send,
90  {
91    async move {
92      into!(key);
93      try_into!(values);
94      commands::sorted_sets::zadd(self, key, options, ordering, changed, incr, values)
95        .await?
96        .convert()
97    }
98  }
99
100  /// Returns the sorted set cardinality (number of elements) of the sorted set stored at `key`.
101  ///
102  /// <https://redis.io/commands/zcard>
103  fn zcard<R, K>(&self, key: K) -> impl Future<Output = FredResult<R>> + Send
104  where
105    R: FromValue,
106    K: Into<Key> + Send,
107  {
108    async move {
109      into!(key);
110      commands::sorted_sets::zcard(self, key).await?.convert()
111    }
112  }
113
114  /// Returns the number of elements in the sorted set at `key` with a score between `min` and `max`.
115  ///
116  /// <https://redis.io/commands/zcount>
117  fn zcount<R, K>(&self, key: K, min: f64, max: f64) -> impl Future<Output = FredResult<R>> + Send
118  where
119    R: FromValue,
120    K: Into<Key> + Send,
121  {
122    async move {
123      into!(key);
124      commands::sorted_sets::zcount(self, key, min, max).await?.convert()
125    }
126  }
127
128  /// This command is similar to ZDIFFSTORE, but instead of storing the resulting sorted set, it is returned to the
129  /// client.
130  ///
131  /// <https://redis.io/commands/zdiff>
132  fn zdiff<R, K>(&self, keys: K, withscores: bool) -> impl Future<Output = FredResult<R>> + Send
133  where
134    R: FromValue,
135    K: Into<MultipleKeys> + Send,
136  {
137    async move {
138      into!(keys);
139      commands::sorted_sets::zdiff(self, keys, withscores).await?.convert()
140    }
141  }
142
143  /// Computes the difference between the first and all successive input sorted sets and stores the result in
144  /// `destination`.
145  ///
146  /// <https://redis.io/commands/zdiffstore>
147  fn zdiffstore<R, D, K>(&self, dest: D, keys: K) -> impl Future<Output = FredResult<R>> + Send
148  where
149    R: FromValue,
150    D: Into<Key> + Send,
151    K: Into<MultipleKeys> + Send,
152  {
153    async move {
154      into!(dest, keys);
155      commands::sorted_sets::zdiffstore(self, dest, keys).await?.convert()
156    }
157  }
158
159  /// Increments the score of `member` in the sorted set stored at `key` by `increment`.
160  ///
161  /// <https://redis.io/commands/zincrby>
162  fn zincrby<R, K, V>(&self, key: K, increment: f64, member: V) -> impl Future<Output = FredResult<R>> + Send
163  where
164    R: FromValue,
165    K: Into<Key> + Send,
166    V: TryInto<Value> + Send,
167    V::Error: Into<Error> + Send,
168  {
169    async move {
170      into!(key);
171      try_into!(member);
172      commands::sorted_sets::zincrby(self, key, increment, member)
173        .await?
174        .convert()
175    }
176  }
177
178  /// This command is similar to ZINTERSTORE, but instead of storing the resulting sorted set, it is returned to the
179  /// client.
180  ///
181  /// <https://redis.io/commands/zinter>
182  fn zinter<R, K, W>(
183    &self,
184    keys: K,
185    weights: W,
186    aggregate: Option<AggregateOptions>,
187    withscores: bool,
188  ) -> impl Future<Output = FredResult<R>> + Send
189  where
190    R: FromValue,
191    K: Into<MultipleKeys> + Send,
192    W: Into<MultipleWeights> + Send,
193  {
194    async move {
195      into!(keys, weights);
196      commands::sorted_sets::zinter(self, keys, weights, aggregate, withscores)
197        .await?
198        .convert()
199    }
200  }
201
202  /// Computes the intersection of the sorted sets given by the specified keys, and stores the result in
203  /// `destination`.
204  ///
205  /// <https://redis.io/commands/zinterstore>
206  fn zinterstore<R, D, K, W>(
207    &self,
208    dest: D,
209    keys: K,
210    weights: W,
211    aggregate: Option<AggregateOptions>,
212  ) -> impl Future<Output = FredResult<R>> + Send
213  where
214    R: FromValue,
215    D: Into<Key> + Send,
216    K: Into<MultipleKeys> + Send,
217    W: Into<MultipleWeights> + Send,
218  {
219    async move {
220      into!(dest, keys, weights);
221      commands::sorted_sets::zinterstore(self, dest, keys, weights, aggregate)
222        .await?
223        .convert()
224    }
225  }
226
227  /// When all the elements in a sorted set are inserted with the same score, in order to force lexicographical
228  /// ordering, this command returns the number of elements in the sorted set at key with a value between min and
229  /// max.
230  ///
231  /// <https://redis.io/commands/zlexcount>
232  fn zlexcount<R, K, M, N>(&self, key: K, min: M, max: N) -> impl Future<Output = FredResult<R>> + Send
233  where
234    R: FromValue,
235    K: Into<Key> + Send,
236    M: TryInto<ZRange> + Send,
237    M::Error: Into<Error> + Send,
238    N: TryInto<ZRange> + Send,
239    N::Error: Into<Error> + Send,
240  {
241    async move {
242      into!(key);
243      try_into!(min, max);
244      commands::sorted_sets::zlexcount(self, key, min, max).await?.convert()
245    }
246  }
247
248  /// Removes and returns up to count members with the highest scores in the sorted set stored at `key`.
249  ///
250  /// <https://redis.io/commands/zpopmax>
251  fn zpopmax<R, K>(&self, key: K, count: Option<usize>) -> impl Future<Output = FredResult<R>> + Send
252  where
253    R: FromValue,
254    K: Into<Key> + Send,
255  {
256    async move {
257      into!(key);
258      commands::sorted_sets::zpopmax(self, key, count).await?.convert()
259    }
260  }
261
262  /// Removes and returns up to count members with the lowest scores in the sorted set stored at `key`.
263  ///
264  /// <https://redis.io/commands/zpopmin>
265  fn zpopmin<R, K>(&self, key: K, count: Option<usize>) -> impl Future<Output = FredResult<R>> + Send
266  where
267    R: FromValue,
268    K: Into<Key> + Send,
269  {
270    async move {
271      into!(key);
272      commands::sorted_sets::zpopmin(self, key, count).await?.convert()
273    }
274  }
275
276  /// Pops one or more elements, that are member-score pairs, from the first non-empty sorted set in the provided list
277  /// of key names.
278  ///
279  /// <https://redis.io/commands/zmpop/>
280  fn zmpop<R, K>(&self, keys: K, sort: ZCmp, count: Option<i64>) -> impl Future<Output = FredResult<R>> + Send
281  where
282    R: FromValue,
283    K: Into<MultipleKeys> + Send,
284  {
285    async move {
286      into!(keys);
287      commands::sorted_sets::zmpop(self, keys, sort, count).await?.convert()
288    }
289  }
290
291  /// When called with just the key argument, return a random element from the sorted set value stored at `key`.
292  ///
293  /// <https://redis.io/commands/zrandmember>
294  fn zrandmember<R, K>(&self, key: K, count: Option<(i64, bool)>) -> impl Future<Output = FredResult<R>> + Send
295  where
296    R: FromValue,
297    K: Into<Key> + Send,
298  {
299    async move {
300      into!(key);
301      commands::sorted_sets::zrandmember(self, key, count).await?.convert()
302    }
303  }
304
305  /// This command is like ZRANGE, but stores the result in the `destination` key.
306  ///
307  /// <https://redis.io/commands/zrangestore>
308  fn zrangestore<R, D, S, M, N>(
309    &self,
310    dest: D,
311    source: S,
312    min: M,
313    max: N,
314    sort: Option<ZSort>,
315    rev: bool,
316    limit: Option<Limit>,
317  ) -> impl Future<Output = FredResult<R>> + Send
318  where
319    R: FromValue,
320    D: Into<Key> + Send,
321    S: Into<Key> + Send,
322    M: TryInto<ZRange> + Send,
323    M::Error: Into<Error> + Send,
324    N: TryInto<ZRange> + Send,
325    N::Error: Into<Error> + Send,
326  {
327    async move {
328      into!(dest, source);
329      try_into!(min, max);
330      commands::sorted_sets::zrangestore(self, dest, source, min, max, sort, rev, limit)
331        .await?
332        .convert()
333    }
334  }
335
336  /// Returns the specified range of elements in the sorted set stored at `key`.
337  ///
338  /// <https://redis.io/commands/zrange>
339  fn zrange<R, K, M, N>(
340    &self,
341    key: K,
342    min: M,
343    max: N,
344    sort: Option<ZSort>,
345    rev: bool,
346    limit: Option<Limit>,
347    withscores: bool,
348  ) -> impl Future<Output = FredResult<R>> + Send
349  where
350    R: FromValue,
351    K: Into<Key> + Send,
352    M: TryInto<ZRange> + Send,
353    M::Error: Into<Error> + Send,
354    N: TryInto<ZRange> + Send,
355    N::Error: Into<Error> + Send,
356  {
357    async move {
358      into!(key);
359      try_into!(min, max);
360      commands::sorted_sets::zrange(self, key, min, max, sort, rev, limit, withscores)
361        .await?
362        .convert()
363    }
364  }
365
366  /// When all the elements in a sorted set are inserted with the same score, in order to force lexicographical
367  /// ordering, this command returns all the elements in the sorted set at `key` with a value between `min` and `max`.
368  ///
369  /// <https://redis.io/commands/zrangebylex>
370  fn zrangebylex<R, K, M, N>(
371    &self,
372    key: K,
373    min: M,
374    max: N,
375    limit: Option<Limit>,
376  ) -> impl Future<Output = FredResult<R>> + Send
377  where
378    R: FromValue,
379    K: Into<Key> + Send,
380    M: TryInto<ZRange> + Send,
381    M::Error: Into<Error> + Send,
382    N: TryInto<ZRange> + Send,
383    N::Error: Into<Error> + Send,
384  {
385    async move {
386      into!(key);
387      try_into!(min, max);
388      commands::sorted_sets::zrangebylex(self, key, min, max, limit)
389        .await?
390        .convert()
391    }
392  }
393
394  /// When all the elements in a sorted set are inserted with the same score, in order to force lexicographical
395  /// ordering, this command returns all the elements in the sorted set at `key` with a value between `max` and `min`.
396  ///
397  /// <https://redis.io/commands/zrevrangebylex>
398  fn zrevrangebylex<R, K, M, N>(
399    &self,
400    key: K,
401    max: M,
402    min: N,
403    limit: Option<Limit>,
404  ) -> impl Future<Output = FredResult<R>> + Send
405  where
406    R: FromValue,
407    K: Into<Key> + Send,
408    M: TryInto<ZRange> + Send,
409    M::Error: Into<Error> + Send,
410    N: TryInto<ZRange> + Send,
411    N::Error: Into<Error> + Send,
412  {
413    async move {
414      into!(key);
415      try_into!(max, min);
416      commands::sorted_sets::zrevrangebylex(self, key, max, min, limit)
417        .await?
418        .convert()
419    }
420  }
421
422  /// Returns all the elements in the sorted set at key with a score between `min` and `max` (including elements
423  /// with score equal to `min` or `max`).
424  ///
425  /// <https://redis.io/commands/zrangebyscore>
426  fn zrangebyscore<R, K, M, N>(
427    &self,
428    key: K,
429    min: M,
430    max: N,
431    withscores: bool,
432    limit: Option<Limit>,
433  ) -> impl Future<Output = FredResult<R>> + Send
434  where
435    R: FromValue,
436    K: Into<Key> + Send,
437    M: TryInto<ZRange> + Send,
438    M::Error: Into<Error> + Send,
439    N: TryInto<ZRange> + Send,
440    N::Error: Into<Error> + Send,
441  {
442    async move {
443      into!(key);
444      try_into!(min, max);
445      commands::sorted_sets::zrangebyscore(self, key, min, max, withscores, limit)
446        .await?
447        .convert()
448    }
449  }
450
451  /// Returns all the elements in the sorted set at `key` with a score between `max` and `min` (including
452  /// elements with score equal to `max` or `min`).
453  ///
454  /// <https://redis.io/commands/zrevrangebyscore>
455  fn zrevrangebyscore<R, K, M, N>(
456    &self,
457    key: K,
458    max: M,
459    min: N,
460    withscores: bool,
461    limit: Option<Limit>,
462  ) -> impl Future<Output = FredResult<R>> + Send
463  where
464    R: FromValue,
465    K: Into<Key> + Send,
466    M: TryInto<ZRange> + Send,
467    M::Error: Into<Error> + Send,
468    N: TryInto<ZRange> + Send,
469    N::Error: Into<Error> + Send,
470  {
471    async move {
472      into!(key);
473      try_into!(max, min);
474      commands::sorted_sets::zrevrangebyscore(self, key, max, min, withscores, limit)
475        .await?
476        .convert()
477    }
478  }
479
480  /// Returns the rank of member in the sorted set stored at `key`, with the scores ordered from low to high.
481  ///
482  /// <https://redis.io/commands/zrank>
483  fn zrank<R, K, V>(&self, key: K, member: V, withscore: bool) -> impl Future<Output = FredResult<R>> + Send
484  where
485    R: FromValue,
486    K: Into<Key> + Send,
487    V: TryInto<Value> + Send,
488    V::Error: Into<Error> + Send,
489  {
490    async move {
491      into!(key);
492      try_into!(member);
493      commands::sorted_sets::zrank(self, key, member, withscore)
494        .await?
495        .convert()
496    }
497  }
498
499  /// Removes the specified members from the sorted set stored at `key`. Non existing members are ignored.
500  ///
501  /// <https://redis.io/commands/zrem>
502  fn zrem<R, K, V>(&self, key: K, members: V) -> impl Future<Output = FredResult<R>> + Send
503  where
504    R: FromValue,
505    K: Into<Key> + Send,
506    V: TryInto<MultipleValues> + Send,
507    V::Error: Into<Error> + Send,
508  {
509    async move {
510      into!(key);
511      try_into!(members);
512      commands::sorted_sets::zrem(self, key, members).await?.convert()
513    }
514  }
515
516  /// When all the elements in a sorted set are inserted with the same score, in order to force lexicographical
517  /// ordering, this command removes all elements in the sorted set stored at `key` between the lexicographical range
518  /// specified by `min` and `max`.
519  ///
520  /// <https://redis.io/commands/zremrangebylex>
521  fn zremrangebylex<R, K, M, N>(&self, key: K, min: M, max: N) -> impl Future<Output = FredResult<R>> + Send
522  where
523    R: FromValue,
524    K: Into<Key> + Send,
525    M: TryInto<ZRange> + Send,
526    M::Error: Into<Error> + Send,
527    N: TryInto<ZRange> + Send,
528    N::Error: Into<Error> + Send,
529  {
530    async move {
531      into!(key);
532      try_into!(min, max);
533      commands::sorted_sets::zremrangebylex(self, key, min, max)
534        .await?
535        .convert()
536    }
537  }
538
539  /// Removes all elements in the sorted set stored at `key` with rank between `start` and `stop`.
540  ///
541  /// <https://redis.io/commands/zremrangebyrank>
542  fn zremrangebyrank<R, K>(&self, key: K, start: i64, stop: i64) -> impl Future<Output = FredResult<R>> + Send
543  where
544    R: FromValue,
545    K: Into<Key> + Send,
546  {
547    async move {
548      into!(key);
549      commands::sorted_sets::zremrangebyrank(self, key, start, stop)
550        .await?
551        .convert()
552    }
553  }
554
555  /// Removes all elements in the sorted set stored at `key` with a score between `min` and `max`.
556  ///
557  /// <https://redis.io/commands/zremrangebyscore>
558  fn zremrangebyscore<R, K, M, N>(&self, key: K, min: M, max: N) -> impl Future<Output = FredResult<R>> + Send
559  where
560    R: FromValue,
561    K: Into<Key> + Send,
562    M: TryInto<ZRange> + Send,
563    M::Error: Into<Error> + Send,
564    N: TryInto<ZRange> + Send,
565    N::Error: Into<Error> + Send,
566  {
567    async move {
568      into!(key);
569      try_into!(min, max);
570      commands::sorted_sets::zremrangebyscore(self, key, min, max)
571        .await?
572        .convert()
573    }
574  }
575
576  /// Returns the specified range of elements in the sorted set stored at `key`.
577  ///
578  /// <https://redis.io/commands/zrevrange>
579  fn zrevrange<R, K>(
580    &self,
581    key: K,
582    start: i64,
583    stop: i64,
584    withscores: bool,
585  ) -> impl Future<Output = FredResult<R>> + Send
586  where
587    R: FromValue,
588    K: Into<Key> + Send,
589  {
590    async move {
591      into!(key);
592      commands::sorted_sets::zrevrange(self, key, start, stop, withscores)
593        .await?
594        .convert()
595    }
596  }
597
598  /// Returns the rank of `member` in the sorted set stored at `key`, with the scores ordered from high to low.
599  ///
600  /// <https://redis.io/commands/zrevrank>
601  fn zrevrank<R, K, V>(&self, key: K, member: V, withscore: bool) -> impl Future<Output = FredResult<R>> + Send
602  where
603    R: FromValue,
604    K: Into<Key> + Send,
605    V: TryInto<Value> + Send,
606    V::Error: Into<Error> + Send,
607  {
608    async move {
609      into!(key);
610      try_into!(member);
611      commands::sorted_sets::zrevrank(self, key, member, withscore)
612        .await?
613        .convert()
614    }
615  }
616
617  /// Returns the score of `member` in the sorted set at `key`.
618  ///
619  /// <https://redis.io/commands/zscore>
620  fn zscore<R, K, V>(&self, key: K, member: V) -> impl Future<Output = FredResult<R>> + Send
621  where
622    R: FromValue,
623    K: Into<Key> + Send,
624    V: TryInto<Value> + Send,
625    V::Error: Into<Error> + Send,
626  {
627    async move {
628      into!(key);
629      try_into!(member);
630      commands::sorted_sets::zscore(self, key, member).await?.convert()
631    }
632  }
633
634  /// This command is similar to ZUNIONSTORE, but instead of storing the resulting sorted set, it is returned to the
635  /// client.
636  ///
637  /// <https://redis.io/commands/zunion>
638  fn zunion<R, K, W>(
639    &self,
640    keys: K,
641    weights: W,
642    aggregate: Option<AggregateOptions>,
643    withscores: bool,
644  ) -> impl Future<Output = FredResult<R>> + Send
645  where
646    R: FromValue,
647    K: Into<MultipleKeys> + Send,
648    W: Into<MultipleWeights> + Send,
649  {
650    async move {
651      into!(keys, weights);
652      commands::sorted_sets::zunion(self, keys, weights, aggregate, withscores)
653        .await?
654        .convert()
655    }
656  }
657
658  /// Computes the union of the sorted sets given by the specified keys, and stores the result in `destination`.
659  ///
660  /// <https://redis.io/commands/zunionstore>
661  fn zunionstore<R, D, K, W>(
662    &self,
663    dest: D,
664    keys: K,
665    weights: W,
666    aggregate: Option<AggregateOptions>,
667  ) -> impl Future<Output = FredResult<R>> + Send
668  where
669    R: FromValue,
670    D: Into<Key> + Send,
671    K: Into<MultipleKeys> + Send,
672    W: Into<MultipleWeights> + Send,
673  {
674    async move {
675      into!(dest, keys, weights);
676      commands::sorted_sets::zunionstore(self, dest, keys, weights, aggregate)
677        .await?
678        .convert()
679    }
680  }
681
682  /// Returns the scores associated with the specified members in the sorted set stored at `key`.
683  ///
684  /// <https://redis.io/commands/zmscore>
685  fn zmscore<R, K, V>(&self, key: K, members: V) -> impl Future<Output = FredResult<R>> + Send
686  where
687    R: FromValue,
688    K: Into<Key> + Send,
689    V: TryInto<MultipleValues> + Send,
690    V::Error: Into<Error> + Send,
691  {
692    async move {
693      into!(key);
694      try_into!(members);
695      commands::sorted_sets::zmscore(self, key, members).await?.convert()
696    }
697  }
698}