Module: Valkey::Commands::SortedSetCommands
- Included in:
- Valkey::Commands
- Defined in:
- lib/valkey/commands/sorted_set_commands.rb
Instance Method Summary collapse
-
#bzmpop(timeout, *keys, modifier: "MIN", count: 1) ⇒ Array?
Remove and return members with scores from one or more sorted sets, blocking until available.
-
#bzpopmax(*keys, timeout:) ⇒ Array?
Remove and return the member with the highest score from a sorted set, blocking until available.
-
#bzpopmin(*keys, timeout:) ⇒ Array?
Remove and return the member with the lowest score from a sorted set, blocking until available.
-
#zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil) ⇒ Boolean, ...
Add one or more members to a sorted set, or update the score for members that already exist.
-
#zcard(key) ⇒ Integer
Get the number of members in a sorted set.
-
#zcount(key, min, max) ⇒ Integer
Count the members in a sorted set with scores within the given values.
-
#zdiff(*keys, with_scores: false) ⇒ Array<String>, Array<[String, Float]>
Return the difference between the first and all successive input sorted sets.
-
#zdiffstore(*args) ⇒ Integer
Compute the difference between the first and all successive input sorted sets and store the resulting sorted set in a new key.
-
#zincrby(key, increment, member) ⇒ Float
Increment the score of a member in a sorted set.
-
#zinter(*args) ⇒ Array<String>, Array<[String, Float]>
Return the intersection of multiple sorted sets.
-
#zintercard(*keys, limit: nil) ⇒ Integer
Return the number of elements in the intersection of sorted sets.
-
#zinterstore(*args) ⇒ Integer
Intersect multiple sorted sets and store the resulting sorted set in a new key.
-
#zlexcount(key, min, max) ⇒ Integer
Count the members, with the same score in a sorted set, within the given lexicographical range.
-
#zmpop(*keys, modifier: "MIN", count: 1) ⇒ Array?
Remove and return members with scores from one or more sorted sets.
-
#zmscore(key, *members) ⇒ Array<Float>
Get the scores associated with the given members in a sorted set.
-
#zpopmax(key, count = nil) ⇒ Array<String, Float>+
Removes and returns up to count members with the highest scores in the sorted set stored at key.
-
#zpopmin(key, count = nil) ⇒ Array<String, Float>+
Removes and returns up to count members with the lowest scores in the sorted set stored at key.
-
#zrandmember(key, count = nil, with_scores: false) ⇒ String, Array
Return random members from a sorted set.
-
#zrange(key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil, withscores: false, with_scores: withscores) ⇒ Array<String>, Array<[String, Float]>
Return a range of members in a sorted set, by index, score or lexicographical ordering.
-
#zrangebylex(key, min, max, limit: nil) ⇒ Array<String>
Return a range of members in a sorted set, by lexicographical ordering.
-
#zrangebyscore(key, min, max, withscores: false, with_scores: withscores, limit: nil) ⇒ Array<String>, Array<[String, Float]>
Return a range of members in a sorted set, by score, with scores ordered from low to high.
-
#zrangestore(dest_key, src_key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil) ⇒ Integer
Select a range of members in a sorted set, by index, score or lexicographical ordering and store the resulting sorted set in a new key.
-
#zrank(key, member, withscore: false, with_score: withscore) ⇒ Integer, [Integer, Float]
Determine the index of a member in a sorted set.
-
#zrem(key, member) ⇒ Boolean, Integer
Remove one or more members from a sorted set.
-
#zremrangebylex(key, min, max) ⇒ Integer
Remove members from a sorted set within a lexicographical range.
-
#zremrangebyrank(key, start, stop) ⇒ Integer
Remove all members in a sorted set within the given indexes.
-
#zremrangebyscore(key, min, max) ⇒ Integer
Remove all members in a sorted set within the given scores.
-
#zrevrange(key, start, stop, withscores: false, with_scores: withscores) ⇒ Array<String>, Array<[String, Float]>
Return a range of members in a sorted set, by index, with scores ordered from high to low.
-
#zrevrangebylex(key, max, min, limit: nil) ⇒ Array<String>
Return a range of members in a sorted set, by lexicographical ordering, ordered from high to low.
-
#zrevrangebyscore(key, max, min, withscores: false, with_scores: withscores, limit: nil) ⇒ Array<String>, Array<[String, Float]>
Return a range of members in a sorted set, by score, with scores ordered from high to low.
-
#zrevrank(key, member, withscore: false, with_score: withscore) ⇒ Integer, [Integer, Float]
Determine the index of a member in a sorted set, with scores ordered from high to low.
-
#zscan(key, cursor, **options) ⇒ String, Array<[String, Float]>
Scan a sorted set.
-
#zscore(key, member) ⇒ Float
Get the score associated with the given member in a sorted set.
-
#zunion(*args) ⇒ Array<String>, Array<[String, Float]>
Return the union of multiple sorted sets.
-
#zunionstore(*args) ⇒ Integer
Add multiple sorted sets and store the resulting sorted set in a new key.
Instance Method Details
#bzmpop(timeout, *keys, modifier: "MIN", count: 1) ⇒ Array?
Remove and return members with scores from one or more sorted sets, blocking until available.
574 575 576 577 578 579 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 574 def bzmpop(timeout, *keys, modifier: "MIN", count: 1) args = [timeout, keys.size] + keys + [modifier, "COUNT", count] send_command(RequestType::BZ_MPOP, args) do |reply| reply && [reply[0], Utils::FloatifyPairs.call(reply[1])] end end |
#bzpopmax(*keys, timeout:) ⇒ Array?
Remove and return the member with the highest score from a sorted set, blocking until available.
595 596 597 598 599 600 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 595 def bzpopmax(*keys, timeout:) args = keys + [timeout] send_command(RequestType::BZ_POP_MAX, args) do |reply| reply && [reply[0], reply[1], Utils::Floatify.call(reply[2])] end end |
#bzpopmin(*keys, timeout:) ⇒ Array?
Remove and return the member with the lowest score from a sorted set, blocking until available.
616 617 618 619 620 621 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 616 def bzpopmin(*keys, timeout:) args = keys + [timeout] send_command(RequestType::BZ_POP_MIN, args) do |reply| reply && [reply[0], reply[1], Utils::Floatify.call(reply[2])] end end |
#zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil) ⇒ Boolean, ...
Add one or more members to a sorted set, or update the score for members that already exist.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 53 def zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil) command_args = [key] command_args << "NX" if nx command_args << "XX" if xx command_args << "LT" if lt command_args << "GT" if gt command_args << "CH" if ch command_args << "INCR" if incr if args.size == 1 && args[0].is_a?(Array) members_to_add = args[0] return 0 if members_to_add.empty? # Variadic: return float if INCR, integer if !INCR send_command(RequestType::Z_ADD, command_args + members_to_add.flatten, &(incr ? Utils::Floatify : nil)) elsif args.size == 2 # Single pair: return float if INCR, boolean if !INCR send_command(RequestType::Z_ADD, command_args + args.flatten.flatten, &(incr ? Utils::Floatify : Utils::Boolify)) else raise ArgumentError, "wrong number of arguments" end end |
#zcard(key) ⇒ Integer
Get the number of members in a sorted set.
14 15 16 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 14 def zcard(key) send_command(RequestType::Z_CARD, [key]) end |
#zcount(key, min, max) ⇒ Integer
Count the members in a sorted set with scores within the given values.
411 412 413 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 411 def zcount(key, min, max) send_command(RequestType::Z_COUNT, [key, min, max]) end |
#zdiff(*keys, with_scores: false) ⇒ Array<String>, Array<[String, Float]>
Return the difference between the first and all successive input sorted sets
520 521 522 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 520 def zdiff(*keys, with_scores: false) _zsets_operation(RequestType::Z_DIFF, *keys, with_scores: with_scores) end |
#zdiffstore(*args) ⇒ Integer
Compute the difference between the first and all successive input sorted sets and store the resulting sorted set in a new key
536 537 538 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 536 def zdiffstore(*args) _zsets_operation_store(RequestType::Z_DIFF_STORE, *args) end |
#zincrby(key, increment, member) ⇒ Float
Increment the score of a member in a sorted set.
86 87 88 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 86 def zincrby(key, increment, member) send_command(RequestType::Z_INCR_BY, [key, increment, member], &Utils::Floatify) end |
#zinter(*args) ⇒ Array<String>, Array<[String, Float]>
Return the intersection of multiple sorted sets
434 435 436 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 434 def zinter(*args) _zsets_operation(RequestType::Z_INTER, *args) end |
#zintercard(*keys, limit: nil) ⇒ Integer
Return the number of elements in the intersection of sorted sets.
637 638 639 640 641 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 637 def zintercard(*keys, limit: nil) args = [keys.size] + keys args += ["LIMIT", limit] if limit send_command(RequestType::Z_INTER_CARD, args) end |
#zinterstore(*args) ⇒ Integer
Intersect multiple sorted sets and store the resulting sorted set in a new key.
453 454 455 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 453 def zinterstore(*args) _zsets_operation_store(RequestType::Z_INTER_STORE, *args) end |
#zlexcount(key, min, max) ⇒ Integer
Count the members, with the same score in a sorted set, within the given lexicographical range.
369 370 371 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 369 def zlexcount(key, min, max) send_command(RequestType::Z_LEX_COUNT, [key, min, max]) end |
#zmpop(*keys, modifier: "MIN", count: 1) ⇒ Array?
Remove and return members with scores from one or more sorted sets.
655 656 657 658 659 660 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 655 def zmpop(*keys, modifier: "MIN", count: 1) args = [keys.size] + keys + [modifier, "COUNT", count] send_command(RequestType::Z_MPOP, args) do |reply| reply && [reply[0], Utils::FloatifyPairs.call(reply[1])] end end |
#zmscore(key, *members) ⇒ Array<Float>
Get the scores associated with the given members in a sorted set.
191 192 193 194 195 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 191 def zmscore(key, *members) send_command(RequestType::Z_MSCORE, [key, *members]) do |reply| reply.map(&Utils::Floatify) end end |
#zpopmax(key, count = nil) ⇒ Array<String, Float>+
Removes and returns up to count members with the highest scores in the sorted set stored at key.
137 138 139 140 141 142 143 144 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 137 def zpopmax(key, count = nil) command_args = [key] command_args << Integer(count) if count send_command(RequestType::Z_POP_MAX, command_args) do |members| members = Utils::FloatifyPairs.call(members) count.to_i > 1 ? members : members.first end end |
#zpopmin(key, count = nil) ⇒ Array<String, Float>+
Removes and returns up to count members with the lowest scores in the sorted set stored at key.
160 161 162 163 164 165 166 167 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 160 def zpopmin(key, count = nil) command_args = [key] command_args << Integer(count) if count send_command(RequestType::Z_POP_MIN, command_args) do |members| members = Utils::FloatifyPairs.call(members) count.to_i > 1 ? members : members.first end end |
#zrandmember(key, count = nil, with_scores: false) ⇒ String, Array
Return random members from a sorted set.
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 680 def zrandmember(key, count = nil, with_scores: false) args = [key] if count args << count args << "WITHSCORES" if with_scores end send_command(RequestType::Z_RAND_MEMBER, args) do |reply| if with_scores # The reply is already in pairs format from the server # Just need to convert scores to floats reply.map { |pair| [pair[0], Utils::Floatify.call(pair[1])] } else reply end end end |
#zrange(key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil, withscores: false, with_scores: withscores) ⇒ Array<String>, Array<[String, Float]>
Return a range of members in a sorted set, by index, score or lexicographical ordering.
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 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 220 def zrange(key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil, withscores: false, with_scores: withscores) raise ArgumentError, "only one of :by_score or :by_lex can be specified" if by_score && by_lex args = [key, start, stop] if by_score args << "BYSCORE" elsif by_lex args << "BYLEX" end args << "REV" if rev if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end if with_scores args << "WITHSCORES" block = Utils::FloatifyPairs end send_command(RequestType::Z_RANGE, args, &block) end |
#zrangebylex(key, min, max, limit: nil) ⇒ Array<String>
This command is deprecated since Redis 6.2.0. Use #zrange with the ‘:by_lex` option instead.
Return a range of members in a sorted set, by lexicographical ordering.
833 834 835 836 837 838 839 840 841 842 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 833 def zrangebylex(key, min, max, limit: nil) args = [key, min, max] if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end send_command(RequestType::Z_RANGE_BY_LEX, args) end |
#zrangebyscore(key, min, max, withscores: false, with_scores: withscores, limit: nil) ⇒ Array<String>, Array<[String, Float]>
This command is deprecated since Redis 6.2.0. Use #zrange with the ‘:by_score` option instead.
Return a range of members in a sorted set, by score, with scores ordered from low to high.
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 788 def zrangebyscore(key, min, max, withscores: false, with_scores: withscores, limit: nil) args = [key, min, max] if with_scores args << "WITHSCORES" block = Utils::FloatifyPairs end if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end send_command(RequestType::Z_RANGE_BY_SCORE, args, &block) end |
#zrangestore(dest_key, src_key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil) ⇒ Integer
Select a range of members in a sorted set, by index, score or lexicographical ordering and store the resulting sorted set in a new key.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 259 def zrangestore(dest_key, src_key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil) raise ArgumentError, "only one of :by_score or :by_lex can be specified" if by_score && by_lex args = [dest_key, src_key, start, stop] if by_score args << "BYSCORE" elsif by_lex args << "BYLEX" end args << "REV" if rev if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end send_command(RequestType::Z_RANGE_STORE, args) end |
#zrank(key, member, withscore: false, with_score: withscore) ⇒ Integer, [Integer, Float]
Determine the index of a member in a sorted set.
296 297 298 299 300 301 302 303 304 305 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 296 def zrank(key, member, withscore: false, with_score: withscore) args = [key, member] if with_score args << "WITHSCORE" block = Utils::FloatifyPair end send_command(RequestType::Z_RANK, args, &block) end |
#zrem(key, member) ⇒ Boolean, Integer
Remove one or more members from a sorted set.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 107 def zrem(key, member) if member.is_a?(Array) members_to_remove = member return 0 if members_to_remove.empty? end send_command(RequestType::Z_REM, [key, member].flatten) do |reply| if member.is_a? Array # Variadic: return integer reply else # Single argument: return boolean Utils::Boolify.call(reply) end end end |
#zremrangebylex(key, min, max) ⇒ Integer
Remove members from a sorted set within a lexicographical range.
716 717 718 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 716 def zremrangebylex(key, min, max) send_command(RequestType::Z_REM_RANGE_BY_LEX, [key, min, max]) end |
#zremrangebyrank(key, start, stop) ⇒ Integer
Remove all members in a sorted set within the given indexes.
347 348 349 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 347 def zremrangebyrank(key, start, stop) send_command(RequestType::Z_REM_RANGE_BY_RANK, [key, start, stop]) end |
#zremrangebyscore(key, min, max) ⇒ Integer
Remove all members in a sorted set within the given scores.
390 391 392 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 390 def zremrangebyscore(key, min, max) send_command(RequestType::Z_REM_RANGE_BY_SCORE, [key, min, max]) end |
#zrevrange(key, start, stop, withscores: false, with_scores: withscores) ⇒ Array<String>, Array<[String, Float]>
This command is deprecated since Redis 6.2.0. Use #zrange with the ‘:rev` option instead.
Return a range of members in a sorted set, by index, with scores ordered from high to low.
742 743 744 745 746 747 748 749 750 751 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 742 def zrevrange(key, start, stop, withscores: false, with_scores: withscores) args = [key, start, stop] if with_scores args << "WITHSCORES" block = Utils::FloatifyPairs end send_command(RequestType::Z_REV_RANGE, args, &block) end |
#zrevrangebylex(key, max, min, limit: nil) ⇒ Array<String>
This command is deprecated since Redis 6.2.0. Use #zrange with the ‘:by_lex` and `:rev` options instead.
Return a range of members in a sorted set, by lexicographical ordering, ordered from high to low.
924 925 926 927 928 929 930 931 932 933 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 924 def zrevrangebylex(key, max, min, limit: nil) args = [key, max, min] if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end send_command(RequestType::Z_REV_RANGE_BY_LEX, args) end |
#zrevrangebyscore(key, max, min, withscores: false, with_scores: withscores, limit: nil) ⇒ Array<String>, Array<[String, Float]>
This command is deprecated since Redis 6.2.0. Use #zrange with ‘:by_score` and `:rev` instead.
Return a range of members in a sorted set, by score, with scores ordered from high to low.
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 879 def zrevrangebyscore(key, max, min, withscores: false, with_scores: withscores, limit: nil) args = [key, max, min] if with_scores args << "WITHSCORES" block = Utils::FloatifyPairs end if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end send_command(RequestType::Z_REV_RANGE_BY_SCORE, args, &block) end |
#zrevrank(key, member, withscore: false, with_score: withscore) ⇒ Integer, [Integer, Float]
Determine the index of a member in a sorted set, with scores ordered from high to low.
323 324 325 326 327 328 329 330 331 332 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 323 def zrevrank(key, member, withscore: false, with_score: withscore) args = [key, member] if with_score args << "WITHSCORE" block = Utils::FloatifyPair end send_command(RequestType::Z_REV_RANK, args, &block) end |
#zscan(key, cursor, **options) ⇒ String, Array<[String, Float]>
Scan a sorted set
See the [Valkey Server ZSCAN documentation](valkey.io/docs/latest/commands/zscan/) for further details
555 556 557 558 559 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 555 def zscan(key, cursor, **) _scan(RequestType::Z_SCAN, cursor, [key], **) do |reply| [reply[0], Utils::FloatifyPairs.call(reply[1])] end end |
#zscore(key, member) ⇒ Float
Get the score associated with the given member in a sorted set.
178 179 180 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 178 def zscore(key, member) send_command(RequestType::Z_SCORE, [key, member], &Utils::Floatify) end |
#zunion(*args) ⇒ Array<String>, Array<[String, Float]>
Return the union of multiple sorted sets
477 478 479 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 477 def zunion(*args) _zsets_operation(RequestType::Z_UNION, *args) end |
#zunionstore(*args) ⇒ Integer
Add multiple sorted sets and store the resulting sorted set in a new key.
495 496 497 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 495 def zunionstore(*args) _zsets_operation_store(RequestType::Z_UNION_STORE, *args) end |