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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
use crate::{
cmd,
resp::{BulkString, FromValue, Value},
ArgsOrCollection, Command, CommandSend, Error, Result, SingleArgOrCollection,
};
use futures::Future;
use std::pin::Pin;
/// A group of Redis commands related to Sorted Sets
///
/// # See Also
/// [Redis Sorted Set Commands](https://redis.io/commands/?group=sorted-set)
pub trait SortedSetCommands: CommandSend {
/// Adds all the specified members with the specified scores
/// to the sorted set stored at key.
///
/// # See Also
/// [https://redis.io/commands/zadd/](https://redis.io/commands/zadd/)
fn zadd<K>(&self, key: K) -> ZAdd<Self>
where
K: Into<BulkString>,
{
ZAdd {
sorted_set_commands: &self,
cmd: cmd("ZADD").arg(key),
}
}
/// 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/)
fn zcard<K>(&self, key: K) -> Pin<Box<dyn Future<Output = Result<usize>> + '_>>
where
K: Into<BulkString>,
{
self.send_into(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/)
fn zcount<K, T, U>(
&self,
key: K,
min: T,
max: U,
) -> Pin<Box<dyn Future<Output = Result<usize>> + '_>>
where
K: Into<BulkString>,
T: Into<BulkString>,
U: Into<BulkString>,
{
self.send_into(cmd("ZCOUNT").arg(key).arg(min).arg(max))
}
/// This command is similar to [zdiffstore](crate::SortedSetCommands::zdiffstore), but instead of storing the resulting sorted set,
/// it is returned to the client.
///
/// # Return
/// The number of elements in the specified score range.
///
/// # See Also
/// [https://redis.io/commands/zdiff/](https://redis.io/commands/zdiff/)
fn zdiff<K, C>(&self, keys: C) -> ZDiff<Self>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
ZDiff {
sorted_set_commands: &self,
cmd: cmd("ZDIFF").arg(keys.num_args()).arg(keys),
}
}
/// 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/)
fn zdiffstore<D, K, C>(
&self,
destination: D,
keys: C,
) -> Pin<Box<dyn Future<Output = Result<usize>> + '_>>
where
D: Into<BulkString>,
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
self.send_into(
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/)
fn zincrby<K, M>(
&self,
key: K,
increment: f64,
member: M,
) -> Pin<Box<dyn Future<Output = Result<f64>> + '_>>
where
K: Into<BulkString>,
M: Into<BulkString>,
{
self.send_into(cmd("ZINCRBY").arg(key).arg(increment).arg(member))
}
/// This command is similar to [zinterstore](crate::SortedSetCommands::zinterstore),
/// but instead of storing the resulting sorted set, it is returned to the client.
///
/// # See Also
/// [https://redis.io/commands/zinter/](https://redis.io/commands/zinter/)
fn zinter<K, C>(&self, keys: C) -> ZInterUnion<Self>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
ZInterUnion {
sorted_set_commands: &self,
cmd: cmd("ZINTER").arg(keys.num_args()).arg(keys),
}
}
/// This command is similar to [zinter](crate::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/)
fn zintercard<K, C>(
&self,
keys: C,
limit: usize,
) -> Pin<Box<dyn Future<Output = Result<usize>> + '_>>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
self.send_into(
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/)
fn zinterstore<D, K, C>(&self, destination: D, keys: C) -> ZInterUnionStore<Self>
where
D: Into<BulkString>,
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
ZInterUnionStore {
sorted_set_commands: &self,
cmd: cmd("ZINTERSTORE")
.arg(destination)
.arg(keys.num_args())
.arg(keys),
}
}
/// 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/)
fn zlexcount<K, M1, M2>(
&self,
key: K,
min: M1,
max: M2,
) -> Pin<Box<dyn Future<Output = Result<usize>> + '_>>
where
K: Into<BulkString>,
M1: Into<BulkString>,
M2: Into<BulkString>,
{
self.send_into(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
/// the number of elements in the specified score range.
///
/// # See Also
/// [https://redis.io/commands/zmpop/](https://redis.io/commands/zmpop/)
fn zmpop<K, C, E>(
&self,
keys: C,
where_: ZWhere,
count: usize,
) -> Pin<Box<dyn Future<Output = Result<(String, Vec<(E, f64)>)>> + '_>>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
E: FromValue + Default,
{
self.send_into(
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/)
fn zmscore<K, M, C>(
&self,
key: K,
members: C,
) -> Pin<Box<dyn Future<Output = Result<Vec<Option<f64>>>> + '_>>
where
K: Into<BulkString>,
M: Into<BulkString>,
C: SingleArgOrCollection<M>,
{
self.send_into(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/)
fn zpopmax<K, M>(
&self,
key: K,
count: usize,
) -> Pin<Box<dyn Future<Output = Result<Vec<(M, f64)>>> + '_>>
where
K: Into<BulkString>,
M: FromValue,
{
self.send_into_tuple_vec(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/)
fn zpopmin<K, M>(
&self,
key: K,
count: usize,
) -> Pin<Box<dyn Future<Output = Result<Vec<(M, f64)>>> + '_>>
where
K: Into<BulkString>,
M: FromValue,
{
self.send_into_tuple_vec(cmd("ZPOPMIN").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/zrandmember/](https://redis.io/commands/zrandmember/)
fn zrandmember<K>(&self, key: K) -> ZRandMember<Self>
where
K: Into<BulkString>,
{
ZRandMember {
sorted_set_commands: &self,
cmd: cmd("ZRANDMEMBER").arg(key),
}
}
/// Returns the specified range of elements in the sorted set stored at `key`.
///
/// # See Also
/// [https://redis.io/commands/zrange/](https://redis.io/commands/zrange/)
fn zrange<K, S>(&self, key: K, start: S, stop: S) -> ZRange<Self>
where
K: Into<BulkString>,
S: Into<BulkString>,
{
ZRange {
sorted_set_commands: &self,
cmd: cmd("ZRANGE").arg(key).arg(start).arg(stop),
}
}
/// This command is like [zrange](crate::SortedSetCommands::zrange),
/// but stores the result in the `dst` destination key.
///
/// # See Also
/// [https://redis.io/commands/zrangestore/](https://redis.io/commands/zrangestore/)
fn zrangestore<D, S, T>(&self, dst: D, src: S, start: T, stop: T) -> ZRangeStore<Self>
where
D: Into<BulkString>,
S: Into<BulkString>,
T: Into<BulkString>,
{
ZRangeStore {
sorted_set_commands: &self,
cmd: cmd("ZRANGESTORE").arg(dst).arg(src).arg(start).arg(stop),
}
}
/// 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/)
fn zrank<K, M>(
&self,
key: K,
member: M,
) -> Pin<Box<dyn Future<Output = Result<Option<usize>>> + '_>>
where
K: Into<BulkString>,
M: Into<BulkString>,
{
self.send_into(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/)
fn zrem<K, M, C>(&self, key: K, members: C) -> Pin<Box<dyn Future<Output = Result<usize>> + '_>>
where
K: Into<BulkString>,
M: Into<BulkString>,
C: SingleArgOrCollection<M>,
{
self.send_into(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/)
fn zremrangebylex<K, S>(
&self,
key: K,
start: S,
stop: S,
) -> Pin<Box<dyn Future<Output = Result<usize>> + '_>>
where
K: Into<BulkString>,
S: Into<BulkString>,
{
self.send_into(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/)
fn zremrangebyrank<K>(
&self,
key: K,
start: isize,
stop: isize,
) -> Pin<Box<dyn Future<Output = Result<usize>> + '_>>
where
K: Into<BulkString>,
{
self.send_into(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/)
fn zremrangebyscore<K, S>(
&self,
key: K,
start: S,
stop: S,
) -> Pin<Box<dyn Future<Output = Result<usize>> + '_>>
where
K: Into<BulkString>,
S: Into<BulkString>,
{
self.send_into(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/)
fn zrevrank<K, M>(
&self,
key: K,
member: M,
) -> Pin<Box<dyn Future<Output = Result<Option<usize>>> + '_>>
where
K: Into<BulkString>,
M: Into<BulkString>,
{
self.send_into(cmd("ZREVRANK").arg(key).arg(member))
}
/// Iterates elements of Sorted Set types and their associated scores.
///
/// # Return
/// The list of members and their associated scores.
///
/// # See Also
/// [https://redis.io/commands/zscan/](https://redis.io/commands/zscan/)
fn zscan<K>(&self, key: K, cursor: usize) -> ZScan<Self>
where
K: Into<BulkString>,
{
ZScan {
sorted_set_commands: self,
cmd: cmd("ZSCAN").arg(key).arg(cursor),
}
}
/// 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/)
fn zscore<K, M>(
&self,
key: K,
member: M,
) -> Pin<Box<dyn Future<Output = Result<Option<f64>>> + '_>>
where
K: Into<BulkString>,
M: Into<BulkString>,
{
self.send_into(cmd("ZSCORE").arg(key).arg(member))
}
/// This command is similar to [zunionstore](crate::SortedSetCommands::zunionstore),
/// but instead of storing the resulting sorted set, it is returned to the client.
///
/// # See Also
/// [https://redis.io/commands/zunion/](https://redis.io/commands/zunion/)
fn zunion<K, C>(&self, keys: C) -> ZInterUnion<Self>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
ZInterUnion {
sorted_set_commands: &self,
cmd: cmd("ZUNION").arg(keys.num_args()).arg(keys),
}
}
/// Computes the union 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/)
fn zunionstore<D, K, C>(&self, destination: D, keys: C) -> ZInterUnionStore<Self>
where
D: Into<BulkString>,
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
ZInterUnionStore {
sorted_set_commands: &self,
cmd: cmd("ZUNIONSTORE")
.arg(destination)
.arg(keys.num_args())
.arg(keys),
}
}
}
pub struct ZAdd<'a, T>
where
T: SortedSetCommands + ?Sized,
{
sorted_set_commands: &'a T,
cmd: Command,
}
impl<'a, T> ZAdd<'a, T>
where
T: SortedSetCommands + ?Sized,
{
/// # Return
/// * When used without optional arguments, the number of elements added to the sorted set (excluding score updates).
/// * If the CH option is specified, the number of elements that were changed (added or updated).
pub fn execute<M, I>(self, items: I) -> Pin<Box<dyn Future<Output = Result<usize>> + 'a>>
where
M: Into<BulkString>,
I: ArgsOrCollection<(f64, M)>,
{
self.sorted_set_commands.send_into(self.cmd.arg(items))
}
/// Only update elements that already exist. Don't add new elements.
pub fn nx(self) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("NX"),
}
}
/// Only add new elements. Don't update already existing elements.
pub fn xx(self) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("XX"),
}
}
/// Only update existing elements if the new score is less than the current score.
///
/// This flag doesn't prevent adding new elements.
pub fn lt(self) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("LT"),
}
}
/// Only update existing elements if the new score is greater than the current score.
///
/// This flag doesn't prevent adding new elements.
pub fn gt(self) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("GT"),
}
}
/// Modify the return value from the number of new elements added,
/// to the total number of elements changed (CH is an abbreviation of changed).
pub fn ch(self) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("CH"),
}
}
/// When this option is specified 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).
pub fn incr<M>(
self,
score: f64,
member: M,
) -> Pin<Box<dyn Future<Output = Result<Option<f64>>> + 'a>>
where
M: Into<BulkString>,
{
self.sorted_set_commands
.send_into(self.cmd.arg("INCR").arg(score).arg(member))
}
}
/// Builder for the [zrange](crate::SortedSetCommands::zrange) command
pub struct ZRange<'a, T>
where
T: SortedSetCommands + ?Sized,
{
sorted_set_commands: &'a T,
cmd: Command,
}
impl<'a, T> ZRange<'a, T>
where
T: SortedSetCommands + ?Sized,
{
/// 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`.
///
/// 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.
pub fn sort_by(self, sort_by: ZRangeSortBy) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg(sort_by),
}
}
/// Using the REV option reverses the sorted set, with index 0 as the element with the highest score.
pub fn rev(self) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("REV"),
}
}
/// The optional LIMIT argument can be used to obtain a sub-range from the matching elements
/// (similar to SELECT LIMIT offset, count in SQL).
///
/// A negative `count` returns all elements from the `offset`.
///
/// Keep in mind that if `offset` is large, the sorted set needs to be traversed for `offset`
/// elements before getting to the elements to return, which can add up to O(N) time complexity.
pub fn limit(self, offset: usize, count: isize) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("LIMIT").arg(offset).arg(count),
}
}
/// # Return
/// list of elements in the specified range
pub fn execute<E>(self) -> Pin<Box<dyn Future<Output = Result<Vec<E>>> + 'a>>
where
E: FromValue,
{
self.sorted_set_commands.send_into(self.cmd)
}
/// The optional `WITHSCORES` argument supplements the command's reply with the scores of elements returned.
/// The returned list contains value1,score1,...,valueN,scoreN instead of value1,...,valueN.
///
/// # Return
/// list of elements and their scores in the specified range
pub fn with_scores<E>(self) -> Pin<Box<dyn Future<Output = Result<Vec<(E, f64)>>> + 'a>>
where
E: FromValue,
{
self.sorted_set_commands
.send_into_tuple_vec(self.cmd.arg("WITHSCORES"))
}
}
pub enum ZRangeSortBy {
/// 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 From<ZRangeSortBy> for BulkString {
fn from(s: ZRangeSortBy) -> Self {
match s {
ZRangeSortBy::ByScore => BulkString::Str("BYSCORE"),
ZRangeSortBy::ByLex => BulkString::Str("BYLEX"),
}
}
}
/// Builder for the [zdiff](crate::SortedSetCommands::zdiff) command
pub struct ZDiff<'a, T>
where
T: SortedSetCommands + ?Sized,
{
sorted_set_commands: &'a T,
cmd: Command,
}
impl<'a, T> ZDiff<'a, T>
where
T: SortedSetCommands + ?Sized,
{
/// The result of the difference
pub fn execute<E>(self) -> Pin<Box<dyn Future<Output = Result<Vec<E>>> + 'a>>
where
E: FromValue,
{
self.sorted_set_commands.send_into(self.cmd)
}
/// The result of the difference with scores
pub fn with_scores<E>(self) -> Pin<Box<dyn Future<Output = Result<Vec<(E, f64)>>> + 'a>>
where
E: FromValue,
{
self.sorted_set_commands
.send_into_tuple_vec(self.cmd.arg("WITHSCORES"))
}
}
pub enum ZAggregate {
Sum,
Min,
Max,
}
impl From<ZAggregate> for BulkString {
fn from(a: ZAggregate) -> Self {
match a {
ZAggregate::Sum => BulkString::Str("SUM"),
ZAggregate::Min => BulkString::Str("MIN"),
ZAggregate::Max => BulkString::Str("MAX"),
}
}
}
/// Builder for the [zinter](crate::SortedSetCommands::zinter) and [zunion](crate::SortedSetCommands::zunion) commands
pub struct ZInterUnion<'a, T>
where
T: SortedSetCommands + ?Sized,
{
sorted_set_commands: &'a T,
cmd: Command,
}
impl<'a, T> ZInterUnion<'a, T>
where
T: SortedSetCommands + ?Sized,
{
/// Using the WEIGHTS option, it is possible to specify a multiplication factor for each input sorted set.
///
/// This means that the score of every element in every input sorted set is multiplied by this factor
/// before being passed to the aggregation function.
/// When WEIGHTS is not given, the multiplication factors default to 1.
pub fn weights<W>(self, weights: W) -> Self
where
W: SingleArgOrCollection<f64>,
{
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("WEIGHT").arg(weights),
}
}
/// With the AGGREGATE option, it is possible to specify how the results of the union are aggregated.
///
/// This option defaults to SUM, where the score of an element is summed across the inputs where it exists.
/// When this option is set to either MIN or MAX, the resulting set will contain the minimum or maximum score
/// of an element across the inputs where it exists.
pub fn aggregate(self, aggregate: ZAggregate) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("AGGREGATE").arg(aggregate),
}
}
/// The result of the intersection
pub fn execute<E>(self) -> Pin<Box<dyn Future<Output = Result<Vec<E>>> + 'a>>
where
E: FromValue,
{
self.sorted_set_commands.send_into(self.cmd)
}
/// The result of the intersection with scores
pub fn with_scores<E>(self) -> Pin<Box<dyn Future<Output = Result<Vec<(E, f64)>>> + 'a>>
where
E: FromValue,
{
self.sorted_set_commands
.send_into_tuple_vec(self.cmd.arg("WITHSCORES"))
}
}
/// Builder for the [zinterstore](crate::SortedSetCommands::zinterstore) and [zunionstore](crate::SortedSetCommands::zunionstore) commands
pub struct ZInterUnionStore<'a, T>
where
T: SortedSetCommands + ?Sized,
{
sorted_set_commands: &'a T,
cmd: Command,
}
impl<'a, T> ZInterUnionStore<'a, T>
where
T: SortedSetCommands + ?Sized,
{
/// Using the WEIGHTS option, it is possible to specify a multiplication factor for each input sorted set.
///
/// This means that the score of every element in every input sorted set is multiplied by this factor
/// before being passed to the aggregation function.
/// When WEIGHTS is not given, the multiplication factors default to 1.
pub fn weights<W>(self, weights: W) -> Self
where
W: SingleArgOrCollection<f64>,
{
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("WEIGHTS").arg(weights),
}
}
/// With the AGGREGATE option, it is possible to specify how the results of the union are aggregated.
///
/// This option defaults to SUM, where the score of an element is summed across the inputs where it exists.
/// When this option is set to either MIN or MAX, the resulting set will contain the minimum or maximum score
/// of an element across the inputs where it exists.
pub fn aggregate(self, aggregate: ZAggregate) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("AGGREGATE").arg(aggregate),
}
}
/// The number of elements in the resulting sorted set at destination.
pub fn execute(self) -> Pin<Box<dyn Future<Output = Result<usize>> + 'a>> {
self.sorted_set_commands.send_into(self.cmd)
}
}
pub enum ZWhere {
Min,
Max,
}
impl From<ZWhere> for BulkString {
fn from(w: ZWhere) -> Self {
match w {
ZWhere::Min => BulkString::Str("MIN"),
ZWhere::Max => BulkString::Str("MAX"),
}
}
}
/// Builder for the [zrandmember](crate::SortedSetCommands::zrandmember) command
pub struct ZRandMember<'a, T>
where
T: SortedSetCommands + ?Sized,
{
sorted_set_commands: &'a T,
cmd: Command,
}
impl<'a, T> ZRandMember<'a, T>
where
T: SortedSetCommands + ?Sized,
{
/// The randomly selected element, or nil when key does not exist.
pub fn execute<E>(self) -> Pin<Box<dyn Future<Output = Result<E>> + 'a>>
where
E: FromValue,
{
self.sorted_set_commands.send_into(self.cmd)
}
/// 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.
pub fn count(self, count: isize) -> ZRandMemberCount<'a, T> {
ZRandMemberCount {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg(count),
}
}
}
/// Builder for the [zrandmember](crate::SortedSetCommands::zrandmember) command
pub struct ZRandMemberCount<'a, T>
where
T: SortedSetCommands + ?Sized,
{
sorted_set_commands: &'a T,
cmd: Command,
}
impl<'a, T> ZRandMemberCount<'a, T>
where
T: SortedSetCommands + ?Sized,
{
/// The result of the intersection
pub fn execute<E>(self) -> Pin<Box<dyn Future<Output = Result<Vec<E>>> + 'a>>
where
E: FromValue,
{
self.sorted_set_commands.send_into(self.cmd)
}
/// The result of the intersection with scores
pub fn with_scores<E>(self) -> Pin<Box<dyn Future<Output = Result<Vec<(E, f64)>>> + 'a>>
where
E: FromValue,
{
self.sorted_set_commands
.send_into_tuple_vec(self.cmd.arg("WITHSCORES"))
}
}
/// Builder for the [zrangestore](crate::SortedSetCommands::zrangestore) command
pub struct ZRangeStore<'a, T>
where
T: SortedSetCommands + ?Sized,
{
sorted_set_commands: &'a T,
cmd: Command,
}
impl<'a, T> ZRangeStore<'a, T>
where
T: SortedSetCommands + ?Sized,
{
/// 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`.
///
/// 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.
pub fn sort_by(self, sort_by: ZRangeSortBy) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg(sort_by),
}
}
/// Using the REV option reverses the sorted set, with index 0 as the element with the highest score.
pub fn rev(self) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("REV"),
}
}
/// The optional LIMIT argument can be used to obtain a sub-range from the matching elements
/// (similar to SELECT LIMIT offset, count in SQL).
///
/// A negative `count` returns all elements from the `offset`.
///
/// Keep in mind that if `offset` is large, the sorted set needs to be traversed for `offset`
/// elements before getting to the elements to return, which can add up to O(N) time complexity.
pub fn limit(self, offset: usize, count: isize) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("LIMIT").arg(offset).arg(count),
}
}
/// # Return
/// the number of elements in the resulting sorted set.
pub fn execute(self) -> Pin<Box<dyn Future<Output = Result<usize>> + 'a>> {
self.sorted_set_commands.send_into(self.cmd)
}
}
/// Builder for the [zscan](crate::SortedSetCommands::zscan) command
pub struct ZScan<'a, T: SortedSetCommands + ?Sized> {
sorted_set_commands: &'a T,
cmd: Command,
}
impl<'a, T: SortedSetCommands + ?Sized> ZScan<'a, T> {
/// # 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
pub fn execute<M>(self) -> Pin<Box<dyn Future<Output = Result<ZScanResult<M>>> + 'a>>
where
M: FromValue,
{
self.sorted_set_commands.send_into(self.cmd)
}
pub fn match_<P>(self, pattern: P) -> Self
where
P: Into<BulkString>,
{
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("MATCH").arg(pattern),
}
}
pub fn count(self, count: usize) -> Self {
Self {
sorted_set_commands: self.sorted_set_commands,
cmd: self.cmd.arg("COUNT").arg(count),
}
}
}
#[derive(Debug)]
pub struct ZScanResult<M>
where
M: FromValue,
{
pub cursor: u64,
pub elements: Vec<(M, f64)>,
}
impl<M> FromValue for ZScanResult<M>
where
M: FromValue,
{
fn from_value(value: Value) -> Result<Self> {
let mut values: Vec<Value> = value.into()?;
match (values.pop(), values.pop(), values.pop()) {
(Some(elements), Some(cursor), None) => Ok(ZScanResult {
cursor: cursor.into()?,
elements: elements.into_tuple_vec::<M, f64>()?,
}),
_ => Err(Error::Internal("unexpected hscan result".to_owned())),
}
}
}