Module: Valkey::Commands::SortedSetCommands
- Included in:
- Valkey::Commands
- Defined in:
- lib/valkey/commands/sorted_set_commands.rb
Instance Method Summary collapse
-
#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.
-
#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.
-
#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.
-
#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.
-
#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.
-
#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.
-
#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
#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 |
#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 |
#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 |
#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 |
#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 |
#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 |
#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 |