Module: Protocol::Redis::Methods::SortedSets

Defined in:
lib/protocol/redis/methods/sorted_sets.rb

Overview

Methods for managing Redis sorted sets.

Instance Method Summary collapse

Instance Method Details

#bzpopmax(*keys, timeout: 0) ⇒ Object

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. See <redis.io/commands/bzpopmax> for more details.



24
25
26
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 24

def bzpopmax(*keys, timeout: 0)
	call("BZPOPMAX", *keys, timeout)
end

#bzpopmin(*keys, timeout: 0) ⇒ Object

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. See <redis.io/commands/bzpopmin> for more details.



16
17
18
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 16

def bzpopmin(*keys, timeout: 0)
	call("BZPOPMIN", *keys, timeout)
end

#zadd(key, score, member, *others, update: nil, change: false, increment: false) ⇒ Object

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. See <redis.io/commands/zadd> for more details.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 40

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

#zcard(key) ⇒ Object

Get the number of members in a sorted set. O(1). See <redis.io/commands/zcard> for more details.



61
62
63
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 61

def zcard(key)
	call("ZCARD", key)
end

#zcount(key, min, max) ⇒ Object

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. See <redis.io/commands/zcount> for more details.



70
71
72
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 70

def zcount(key, min, max)
	call("ZCOUNT", key, min, max)
end

#zincrby(key, increment, member) ⇒ Object

Increment the score of a member in a sorted set. O(log(N)) where N is the number of elements in the sorted set. See <redis.io/commands/zincrby> for more details.



79
80
81
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 79

def zincrby(key, increment, member)
	call("ZINCRBY", key, increment, member)
end

#zinterstore(destination, keys, weights = nil, aggregate: nil) ⇒ Object

Intersect multiple sorted sets and store the resulting sorted set in a new key. O(N*K)+O(M*log(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. See <redis.io/commands/zinterstore> for more details.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 89

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

#zlexcount(key, min, max) ⇒ Object

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. See <redis.io/commands/zlexcount> for more details.



113
114
115
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 113

def zlexcount(key, min, max)
	call("ZLEXCOUNT", key, min, max)
end

#zpopmax(key, count = 1) ⇒ Object

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. See <redis.io/commands/zpopmax> for more details.



121
122
123
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 121

def zpopmax(key, count = 1)
	call("ZPOPMAX", key, count)
end

#zpopmin(key, count = 1) ⇒ Object

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. See <redis.io/commands/zpopmin> for more details.



129
130
131
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 129

def zpopmin(key, count = 1)
	call("ZPOPMIN", key, count)
end

#zrange(key, start, stop, with_scores: false) ⇒ Object

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. See <redis.io/commands/zrange> for more details.



139
140
141
142
143
144
145
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 139

def zrange(key, start, stop, with_scores: false)
	arguments = [start, stop]
	
	arguments.push("WITHSCORES") if with_scores
	
	call("ZRANGE", key, *arguments)
end

#zrangebylex(key, min, max, limit: nil) ⇒ Object

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)). See <redis.io/commands/zrangebylex> for more details.



153
154
155
156
157
158
159
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 153

def zrangebylex(key, min, max, limit: nil)
	if limit
		arguments = ["LIMIT", *limit]
	end
	
	call("ZRANGEBYLEX", key, min, max, *arguments)
end

#zrangebyscore(key, min, max, with_scores: false, limit: nil) ⇒ Object

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)). See <redis.io/commands/zrangebyscore> for more details.

Examples:

Retrieve the first 10 members with score ‘>= 0` and `<= 100`

redis.zrangebyscore("zset", "0", "100", limit: [0, 10])


184
185
186
187
188
189
190
191
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 184

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

#zrank(key, member) ⇒ Object

Determine the index of a member in a sorted set. O(log(N)). See <redis.io/commands/zrank> for more details.



197
198
199
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 197

def zrank(key, member)
	call("ZRANK", key, member)
end

#zrem(key, member) ⇒ Object

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. See <redis.io/commands/zrem> for more details.



205
206
207
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 205

def zrem(key, member)
	call("ZREM", key, member)
end

#zremrangebylex(key, min, max) ⇒ Object

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. See <redis.io/commands/zremrangebylex> for more details.



214
215
216
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 214

def zremrangebylex(key, min, max)
	call("ZREMRANGEBYLEX", key, min, max)
end

#zremrangebyrank(key, start, stop) ⇒ Object

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. See <redis.io/commands/zremrangebyrank> for more details.



223
224
225
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 223

def zremrangebyrank(key, start, stop)
	call("ZREMRANGEBYRANK", key, start, stop)
end

#zremrangebyscore(key, min, max) ⇒ Object

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. See <redis.io/commands/zremrangebyscore> for more details.



232
233
234
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 232

def zremrangebyscore(key, min, max)
	call("ZREMRANGEBYSCORE", key, min, max)
end

#zrevrange(key, min, max, with_scores: false) ⇒ Object

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. See <redis.io/commands/zrevrange> for more details.



242
243
244
245
246
247
248
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 242

def zrevrange(key, min, max, with_scores: false)
	arguments = [min, max]
	
	arguments.push("WITHSCORES") if with_scores
	
	call("ZREVRANGE", key, *arguments)
end

#zrevrangebylex(key, min, max, limit: nil) ⇒ Object

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)). See <redis.io/commands/zrevrangebylex> for more details.



166
167
168
169
170
171
172
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 166

def zrevrangebylex(key, min, max, limit: nil)
	if limit
		arguments = ["LIMIT", *limit]
	end
	
	call("ZREVRANGEBYLEX", key, min, max, *arguments)
end

#zrevrangebyscore(key, min, max, with_scores: false, limit: nil) ⇒ Object

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)). See <redis.io/commands/zrevrangebyscore> for more details.



256
257
258
259
260
261
262
263
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 256

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

#zrevrank(key, member) ⇒ Object

Determine the index of a member in a sorted set, with scores ordered from high to low. O(log(N)). See <redis.io/commands/zrevrank> for more details.



269
270
271
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 269

def zrevrank(key, member)
	call("ZREVRANK", key, member)
end

#zscan(key, cursor = 0, match: nil, count: nil) ⇒ Object

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.. See <redis.io/commands/zscan> for more details.



294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 294

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

#zscore(key, member) ⇒ Object

Get the score associated with the given member in a sorted set. O(1). See <redis.io/commands/zscore> for more details.



277
278
279
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 277

def zscore(key, member)
	call("ZSCORE", key, member)
end

#zunionstore(*arguments) ⇒ Object

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. See <redis.io/commands/zunionstore> for more details.



286
287
288
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 286

def zunionstore(*arguments)
	call("ZUNIONSTORE", *arguments)
end