module SortedSets
Definitions
def bzpopmin(*keys, timeout: 0)
Remove and return the member with the lowest score from one or more sorted sets, or block until one is available. O(log(N)) with N being the number of elements in the sorted set.
Implementation
def bzpopmin(*keys, timeout: 0)
call("BZPOPMIN", *keys, timeout)
end
def bzpopmax(*keys, timeout: 0)
Remove and return the member with the highest score from one or more sorted sets, or block until one is available. O(log(N)) with N being the number of elements in the sorted set.
Implementation
def bzpopmax(*keys, timeout: 0)
call("BZPOPMAX", *keys, timeout: 0)
end
def zadd(key, score, member, *others, update: nil, change: false, increment: false)
Add one or more members to a sorted set, or update its score if it already exists. O(log(N)) for each item added, where N is the number of elements in the sorted set. to the total number of elements changed; changed elements are new elements added and elements already existing for which the score was updated. only one score-element pair can be specified in this mode.
Implementation
def zadd(key, score, member, *others, update: nil, change: false, increment: false)
arguments = ["ZADD", key]
if update == true
arguments.push("XX")
elsif update == false
arguments.push("NX")
end
arguments.push("CH") if change
arguments.push("INCR") if increment
arguments.push(score, member)
arguments.push(*others)
call(*arguments)
end
def zcard(key)
Get the number of members in a sorted set. O(1).
Implementation
def zcard(key)
call("ZCARD", key)
end
def zcount(key, min, max)
Count the members in a sorted set with scores within the given values. O(log(N)) with N being the number of elements in the sorted set.
Implementation
def zcount(key, min, max)
call("ZCOUNT", key, min, max)
end
def zincrby(key, increment, member)
Increment the score of a member in a sorted set. O(log(N)) where N is the number of elements in the sorted set.
Implementation
def zincrby(key, increment, member)
call("ZINCRBY", key, amount, member)
end
def zinterstore(destination, keys, weights = nil, aggregate: nil)
Intersect multiple sorted sets and store the resulting sorted set in a new key. O(NK)+O(Mlog(M)) worst case with N being the smallest input sorted set, K being the number of input sorted sets and M being the number of elements in the resulting sorted set.
Implementation
def zinterstore(destination, keys, weights = nil, aggregate: nil)
arguments = []
if weights
if weights.size != keys.size
raise ArgumentError, "#{weights.size} weights given for #{keys.size} keys!"
end
arguments.push("WEIGHTS")
arguments.concat(weights)
end
if aggregate
arguments.push("AGGREGATE", aggregate)
end
call("ZINTERSTORE", destination, keys.size, *keys, *arguments)
end
def zlexcount(key, min, max)
Count the number of members in a sorted set between a given lexicographical range. O(log(N)) with N being the number of elements in the sorted set.
Implementation
def zlexcount(key, min, max)
call("ZLEXCOUNT", key, min, max)
end
def zpopmax(key, count = 1)
Remove and return members with the highest scores in a sorted set. O(log(N)*M) with N being the number of elements in the sorted set, and M being the number of elements popped.
Implementation
def zpopmax(key, count = 1)
call("ZPOPMAX", key, count)
end
def zpopmin(key, count = 1)
Remove and return members with the lowest scores in a sorted set. O(log(N)*M) with N being the number of elements in the sorted set, and M being the number of elements popped.
Implementation
def zpopmin(key, count = 1)
call("ZPOPMIN", key, count = 1)
end
def zrange(key, start, stop, with_scores: false)
Return a range of members in a sorted set, by index. O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.
Implementation
def zrange(key, start, stop, with_scores: false)
arguments = [start, stop]
arguments.push("WITHSCORES") if with_scores
call("ZRANGE", key, *arguments)
end
def zrangebylex(key, min, max, limit: nil)
Return a range of members in a sorted set, by lexicographical range. O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).
Implementation
def zrangebylex(key, min, max, limit: nil)
if limit
arguments = ["LIMIT", *limit]
end
call("ZRANGEBYLEX", key, min, max, *arguments)
end
def zrevrangebylex(key, min, max, limit: nil)
Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings. O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).
Implementation
def zrevrangebylex(key, min, max, limit: nil)
if limit
arguments = ["LIMIT", *limit]
end
call("ZREVRANGEBYLEX", key, min, max, *arguments)
end
def zrangebyscore(key, min, max, with_scores: false, limit: nil)
Return a range of members in a sorted set, by score. O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).
redis.zrangebyscore("zset", "0", "100", limit: [0, 10])
Implementation
def zrangebyscore(key, min, max, with_scores: false, limit: nil)
arguments = [min, max]
arguments.push('WITHSCORES') if with_scores
arguments.push('LIMIT', *limit) if limit
call('ZRANGEBYSCORE', key, *arguments)
end
def zrank(key, member)
Determine the index of a member in a sorted set. O(log(N)).
Implementation
def zrank(key, member)
call("ZRANK", key, member)
end
def zrem(key, member)
Remove one or more members from a sorted set. O(M*log(N)) with N being the number of elements in the sorted set and M the number of elements to be removed.
Implementation
def zrem(key, member)
call("ZREM", key, member)
end
def zremrangebylex(key, min, max)
Remove all members in a sorted set between the given lexicographical range. O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements removed by the operation.
Implementation
def zremrangebylex(key, min, max)
call("ZREMRANGEBYLEX", key, min, max)
end
def zremrangebyrank(key, start, stop)
Remove all members in a sorted set within the given indexes. O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements removed by the operation.
Implementation
def zremrangebyrank(key, start, stop)
call("ZREMRANGEBYRANK", key, start, stop)
end
def zremrangebyscore(key, min, max)
Remove all members in a sorted set within the given scores. O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements removed by the operation.
Implementation
def zremrangebyscore(key, min, max)
call("ZREMRANGEBYSCORE", key, min, max)
end
def zrevrange(key, min, max, with_scores: false)
Return a range of members in a sorted set, by index, with scores ordered from high to low. O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.
Implementation
def zrevrange(key, min, max, with_scores: false)
arguments = [min, max]
arguments.push('WITHSCORES') if with_scores
call("ZREVRANGE", key, *arguments)
end
def zrevrangebyscore(key, min, max, with_scores: false, limit: nil)
Return a range of members in a sorted set, by score, with scores ordered from high to low. O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).
Implementation
def zrevrangebyscore(key, min, max, with_scores: false, limit: nil)
arguments = [min, max]
arguments.push('WITHSCORES') if with_scores
arguments.push('LIMIT', *limit) if limit
call("ZREVRANGEBYSCORE", key, *arguments)
end
def zrevrank(key, member)
Determine the index of a member in a sorted set, with scores ordered from high to low. O(log(N)).
Implementation
def zrevrank(key, member)
call("ZREVRANK", key, member)
end
def zscore(key, member)
Get the score associated with the given member in a sorted set. O(1).
Implementation
def zscore(key, member)
call("ZSCORE", key, member)
end
def zunionstore(*arguments)
Add multiple sorted sets and store the resulting sorted set in a new key. O(N)+O(M log(M)) with N being the sum of the sizes of the input sorted sets, and M being the number of elements in the resulting sorted set.
Implementation
def zunionstore(*arguments)
call("ZUNIONSTORE", *arguments)
end
def zscan(key, cursor = 0, match: nil, count: nil)
Incrementally iterate sorted sets elements and associated scores. O(1) for every call. O(N) for a complete iteration, including enough command calls for the cursor to return back to 0. N is the number of elements inside the collection..
Implementation
def zscan(key, cursor = 0, match: nil, count: nil)
arguments = [key, cursor]
if match
arguments.push("MATCH", match)
end
if count
arguments.push("COUNT", count)
end
call("ZSCAN", *arguments)
end