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 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
use crate::{
resp::{
cmd, ArgsOrCollection, BulkString, CommandArgs, FromValue, IntoArgs, SingleArgOrCollection,
},
CommandResult, PrepareCommand,
};
/// 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<T>: PrepareCommand<T> {
/// This command is the blocking variant of [`zmpop`](crate::SortedSetCommands::zmpop).
///
/// # 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/bzmpop/>](https://redis.io/commands/bzmpop/)
#[must_use]
fn bzmpop<K, C, E>(
&self,
timeout: f64,
keys: C,
where_: ZWhere,
count: usize,
) -> CommandResult<T, Option<ZMPopResult<E>>>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
E: FromValue,
{
self.prepare_command(
cmd("BZMPOP")
.arg(timeout)
.arg(keys.num_args())
.arg(keys)
.arg(where_)
.arg("COUNT")
.arg(count),
)
}
/// This command is the blocking variant of [`zpopmax`](crate::SortedSetCommands::zpopmax).
///
/// # Return
/// * `None` when no element could be popped and the timeout expired.
/// * The list of tuple with
/// * the first element being the name of the key where a member was popped,
/// * the second element is the popped member itself,
/// * and the third element is the score of the popped element.
///
/// # See Also
/// [<https://redis.io/commands/bzpopmax/>](https://redis.io/commands/bzpopmax/)
#[must_use]
fn bzpopmax<K, KK, E, K1>(&self, keys: KK, timeout: f64) -> CommandResult<T, BZpopMinMaxResult<K1, E>>
where
K: Into<BulkString>,
KK: SingleArgOrCollection<K>,
K1: FromValue,
E: FromValue,
{
self.prepare_command(cmd("BZPOPMAX").arg(keys).arg(timeout))
}
/// This command is the blocking variant of [`zpopmin`](crate::SortedSetCommands::zpopmin).
///
/// # Return
/// * `None` when no element could be popped and the timeout expired.
/// * The list of tuple with
/// * the first element being the name of the key where a member was popped,
/// * the second element is the popped member itself,
/// * and the third element is the score of the popped element.
///
/// # See Also
/// [<https://redis.io/commands/bzpopmin/>](https://redis.io/commands/bzpopmin/)
#[must_use]
fn bzpopmin<K, KK, E, K1>(&self, keys: KK, timeout: f64) -> CommandResult<T, BZpopMinMaxResult<K1, E>>
where
K: Into<BulkString>,
KK: SingleArgOrCollection<K>,
K1: FromValue,
E: FromValue,
{
self.prepare_command(cmd("BZPOPMIN").arg(keys).arg(timeout))
}
/// 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) -> CommandResult<T, usize>
where
K: Into<BulkString>,
M: Into<BulkString>,
I: ArgsOrCollection<(f64, M)>,
{
self.prepare_command(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,
) -> CommandResult<T, Option<f64>>
where
K: Into<BulkString>,
M: Into<BulkString>,
{
self.prepare_command(
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) -> CommandResult<T, usize>
where
K: Into<BulkString>,
{
self.prepare_command(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) -> CommandResult<T, usize>
where
K: Into<BulkString>,
M1: Into<BulkString>,
M2: Into<BulkString>,
{
self.prepare_command(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 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) -> CommandResult<T, Vec<E>>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
E: FromValue,
{
self.prepare_command(cmd("ZDIFF").arg(keys.num_args()).arg(keys))
}
/// 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 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) -> CommandResult<T, Vec<(E, f64)>>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
E: FromValue,
{
self.prepare_command(
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) -> CommandResult<T, usize>
where
D: Into<BulkString>,
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
self.prepare_command(
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) -> CommandResult<T, f64>
where
K: Into<BulkString>,
M: Into<BulkString>,
{
self.prepare_command(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.
///
/// # 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,
) -> CommandResult<T, Vec<E>>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
W: SingleArgOrCollection<f64>,
E: FromValue,
{
self.prepare_command(
cmd("ZINTER")
.arg(keys.num_args())
.arg(keys)
.arg(weights.map(|w| ("WEIGHTS", w)))
.arg(aggregate),
)
}
/// This command is similar to [zinterstore](crate::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,
) -> CommandResult<T, Vec<(E, f64)>>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
W: SingleArgOrCollection<f64>,
E: FromValue,
{
self.prepare_command(
cmd("ZINTER")
.arg(keys.num_args())
.arg(keys)
.arg(weights.map(|w| ("WEIGHTS", w)))
.arg(aggregate)
.arg("WITHSCORES"),
)
}
/// 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/)
#[must_use]
fn zintercard<K, C>(&self, keys: C, limit: usize) -> CommandResult<T, usize>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
self.prepare_command(
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,
) -> CommandResult<T, usize>
where
D: Into<BulkString>,
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
W: SingleArgOrCollection<f64>,
{
self.prepare_command(
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) -> CommandResult<T, usize>
where
K: Into<BulkString>,
M1: Into<BulkString>,
M2: Into<BulkString>,
{
self.prepare_command(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,
) -> CommandResult<T, Option<ZMPopResult<E>>>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
E: FromValue,
{
self.prepare_command(
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) -> CommandResult<T, Vec<Option<f64>>>
where
K: Into<BulkString>,
M: Into<BulkString>,
C: SingleArgOrCollection<M>,
{
self.prepare_command(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) -> CommandResult<T, Vec<(M, f64)>>
where
K: Into<BulkString>,
M: FromValue,
{
self.prepare_command(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) -> CommandResult<T, Vec<(M, f64)>>
where
K: Into<BulkString>,
M: FromValue,
{
self.prepare_command(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) -> CommandResult<T, E>
where
K: Into<BulkString>,
E: FromValue,
{
self.prepare_command(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) -> CommandResult<T, Vec<E>>
where
K: Into<BulkString>,
E: FromValue,
{
self.prepare_command(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) -> CommandResult<T, Vec<E>>
where
K: Into<BulkString>,
E: FromValue,
{
self.prepare_command(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,
) -> CommandResult<T, Vec<E>>
where
K: Into<BulkString>,
S: Into<BulkString>,
E: FromValue,
{
self.prepare_command(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,
) -> CommandResult<T, Vec<(E, f64)>>
where
K: Into<BulkString>,
S: Into<BulkString>,
E: FromValue,
{
self.prepare_command(
cmd("ZRANGE")
.arg(key)
.arg(start)
.arg(stop)
.arg(options)
.arg("WITHSCORES"),
)
}
/// This command is like [zrange](crate::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,
) -> CommandResult<T, usize>
where
D: Into<BulkString>,
S: Into<BulkString>,
SS: Into<BulkString>,
{
self.prepare_command(
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) -> CommandResult<T, Option<usize>>
where
K: Into<BulkString>,
M: Into<BulkString>,
{
self.prepare_command(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) -> CommandResult<T, usize>
where
K: Into<BulkString>,
M: Into<BulkString>,
C: SingleArgOrCollection<M>,
{
self.prepare_command(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) -> CommandResult<T, usize>
where
K: Into<BulkString>,
S: Into<BulkString>,
{
self.prepare_command(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) -> CommandResult<T, usize>
where
K: Into<BulkString>,
{
self.prepare_command(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) -> CommandResult<T, usize>
where
K: Into<BulkString>,
S: Into<BulkString>,
{
self.prepare_command(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) -> CommandResult<T, Option<usize>>
where
K: Into<BulkString>,
M: Into<BulkString>,
{
self.prepare_command(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,
) -> CommandResult<T, (u64, Vec<(M, f64)>)>
where
K: Into<BulkString>,
M: FromValue,
{
self.prepare_command(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) -> CommandResult<T, Option<f64>>
where
K: Into<BulkString>,
M: Into<BulkString>,
{
self.prepare_command(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.
///
/// # 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,
) -> CommandResult<T, Vec<E>>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
W: SingleArgOrCollection<f64>,
E: FromValue,
{
self.prepare_command(
cmd("ZUNION")
.arg(keys.num_args())
.arg(keys)
.arg(weights.map(|w| ("WEIGHTS", w)))
.arg(aggregate),
)
}
/// This command is similar to [zunionstore](crate::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,
) -> CommandResult<T, Vec<(E, f64)>>
where
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
W: SingleArgOrCollection<f64>,
E: FromValue,
{
self.prepare_command(
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,
) -> CommandResult<T, usize>
where
D: Into<BulkString>,
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
W: SingleArgOrCollection<f64>,
{
self.prepare_command(
cmd("ZUNIONSTORE")
.arg(destination)
.arg(keys.num_args())
.arg(keys)
.arg(weights.map(|w| ("WEIGHTS", w)))
.arg(aggregate),
)
}
}
type BZpopMinMaxResult<K, E> = Option<Vec<(K, E, f64)>>;
/// Condition option for the [zadd](crate::SortedSetCommands::zadd) command
pub enum ZAddCondition {
/// No condition
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 Default for ZAddCondition {
fn default() -> Self {
ZAddCondition::None
}
}
impl IntoArgs for ZAddCondition {
fn into_args(self, args: CommandArgs) -> CommandArgs {
match self {
ZAddCondition::None => args,
ZAddCondition::NX => args.arg("NX"),
ZAddCondition::XX => args.arg("XX"),
}
}
}
/// Comparison option for the [zadd](crate::SortedSetCommands::zadd) command
pub enum ZAddComparison {
/// No comparison
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 Default for ZAddComparison {
fn default() -> Self {
ZAddComparison::None
}
}
impl IntoArgs for ZAddComparison {
fn into_args(self, args: CommandArgs) -> CommandArgs {
match self {
ZAddComparison::None => args,
ZAddComparison::GT => args.arg("GT"),
ZAddComparison::LT => args.arg("LT"),
}
}
}
/// sort by option of the [zrange](crate::SortedSetCommands::zrange) command
pub enum ZRangeSortBy {
/// No sort by
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 Default for ZRangeSortBy {
fn default() -> Self {
ZRangeSortBy::None
}
}
impl IntoArgs for ZRangeSortBy {
fn into_args(self, args: CommandArgs) -> CommandArgs {
match self {
ZRangeSortBy::None => args,
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](crate::SortedSetCommands::zinter)
/// [zinterstore](crate::SortedSetCommands::zinterstore)
/// [zunion](crate::SortedSetCommands::zunion)
/// [zunionstore](crate::SortedSetCommands::zunionstore)
pub enum ZAggregate {
/// No aggregation
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 Default for ZAggregate {
fn default() -> Self {
ZAggregate::None
}
}
impl IntoArgs for ZAggregate {
fn into_args(self, args: CommandArgs) -> CommandArgs {
match self {
ZAggregate::None => args,
ZAggregate::Sum => args.arg("SUM"),
ZAggregate::Min => args.arg("MIN"),
ZAggregate::Max => args.arg("MAX"),
}
}
}
/// Where option of the [zmpop](crate::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 IntoArgs for ZWhere {
fn into_args(self, args: CommandArgs) -> CommandArgs {
match self {
ZWhere::Min => args.arg("MIN"),
ZWhere::Max => args.arg("MAX"),
}
}
}
/// Options for the command [zadd](crate::SortedSetCommands::zadd)
#[derive(Default)]
pub struct ZAddOptions {
command_args: CommandArgs,
}
impl ZAddOptions {
#[must_use]
pub fn condition(self, condition: ZAddCondition) -> Self {
Self {
command_args: self.command_args.arg(condition),
}
}
#[must_use]
pub fn comparison(self, comparison: ZAddComparison) -> Self {
Self {
command_args: self.command_args.arg(comparison),
}
}
#[must_use]
pub fn change(self) -> Self {
Self {
command_args: self.command_args.arg("CH"),
}
}
}
impl IntoArgs for ZAddOptions {
fn into_args(self, args: CommandArgs) -> CommandArgs {
args.arg(self.command_args)
}
}
pub type ZMPopResult<E> = (String, Vec<(E, f64)>);
/// Options for the [`zrange`](crate::SortedSetCommands::zrange)
/// and [`zrangestore`](crate::SortedSetCommands::zrangestore) commands
#[derive(Default)]
pub struct ZRangeOptions {
command_args: CommandArgs,
}
impl ZRangeOptions {
#[must_use]
pub fn sort_by(self, sort_by: ZRangeSortBy) -> Self {
Self {
command_args: self.command_args.arg(sort_by),
}
}
#[must_use]
pub fn reverse(self) -> Self {
Self {
command_args: self.command_args.arg("REV"),
}
}
#[must_use]
pub fn limit(self, offset: usize, count: isize) -> Self {
Self {
command_args: self.command_args.arg("LIMIT").arg(offset).arg(count),
}
}
}
impl IntoArgs for ZRangeOptions {
fn into_args(self, args: CommandArgs) -> CommandArgs {
args.arg(self.command_args)
}
}
/// Options for the [`zscan`](crate::SortedSetCommands::zscan) command
#[derive(Default)]
pub struct ZScanOptions {
command_args: CommandArgs,
}
impl ZScanOptions {
#[must_use]
pub fn match_pattern<P: Into<BulkString>>(self, match_pattern: P) -> Self {
Self {
command_args: self.command_args.arg("MATCH").arg(match_pattern),
}
}
#[must_use]
pub fn count(self, count: usize) -> Self {
Self {
command_args: self.command_args.arg("COUNT").arg(count),
}
}
}
impl IntoArgs for ZScanOptions {
fn into_args(self, args: CommandArgs) -> CommandArgs {
args.arg(self.command_args)
}
}