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

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

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.

Parameters:

  • key (Key)
  • timeout (Integer) (defaults to: 0)

See Also:



40
41
42
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 40

def bzpopmax(*keys, timeout: 0)
	call("BZPOPMAX", *keys, timeout: 0)
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.

Parameters:

  • key (Key)
  • timeout (Integer) (defaults to: 0)

See Also:



32
33
34
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 32

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.

Parameters:

  • key (Key)
  • score (Double)
  • member (String)
  • others (Array)

    an array of ‘score`, `member` elements.

  • update (Boolean, nil) (defaults to: nil)

    If true, only update elements that already exist (never add elements). If false, don’t update existing elements (only add new elements).

  • change (Boolean) (defaults to: false)

    Modify the return value from the number of new elements added, to the total number of elements changed; changed elements are new elements added and elements already existing for which the score was updated.

  • increment (Boolean) (defaults to: false)

    When this option is specified ZADD acts like ZINCRBY; only one score-element pair can be specified in this mode.

See Also:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 56

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).

Parameters:

  • key (Key)

See Also:



77
78
79
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 77

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.

Parameters:

  • key (Key)
  • min (Double)
  • max (Double)

See Also:



86
87
88
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 86

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.

Parameters:

  • key (Key)
  • increment (Integer)
  • member (String)

See Also:



95
96
97
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 95

def zincrby(key, increment, member)
	call("ZINCRBY", key, amount, 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.

Parameters:

  • destination (Key)
  • keys (Array<Key>)
  • weights (Array<Integer>) (defaults to: nil)
  • aggregate (Enum) (defaults to: nil)

    one of sum, min, max.

See Also:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 105

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.

Parameters:

  • key (Key)
  • min (String)
  • max (String)

See Also:



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

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.

Parameters:

  • key (Key)
  • count (Integer) (defaults to: 1)

See Also:



137
138
139
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 137

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.

Parameters:

  • key (Key)
  • count (Integer) (defaults to: 1)

See Also:



145
146
147
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 145

def zpopmin(key, count = 1)
	call("ZPOPMIN", key, count = 1)
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.

Parameters:

  • key (Key)
  • start (Integer)
  • stop (Integer)
  • with_scores (Boolean) (defaults to: false)

    Return the scores of the elements together with the elements.

See Also:



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

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)).

Parameters:

  • key (Key)
  • min (String)
  • max (String)
  • limit (Tuple<offset, count>) (defaults to: nil)

    Limit the results to the specified ‘offset` and `count` items.

See Also:



169
170
171
172
173
174
175
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 169

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)).

Examples:

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

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

Parameters:

  • key (Key)
  • min (Integer)
  • max (Integer)
  • with_scores (Boolean) (defaults to: false)

    Return the scores of the elements together with the elements.

  • limit (Tuple<offset, count>) (defaults to: nil)

    Limit the results to the specified ‘offset` and `count` items.

See Also:



200
201
202
203
204
205
206
207
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 200

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)).

Parameters:

  • key (Key)
  • member (String)

See Also:



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

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.

Parameters:

  • key (Key)
  • member (String)

See Also:



221
222
223
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 221

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.

Parameters:

  • key (Key)
  • min (String)
  • max (String)

See Also:



230
231
232
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 230

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.

Parameters:

  • key (Key)
  • start (Integer)
  • stop (Integer)

See Also:



239
240
241
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 239

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.

Parameters:

  • key (Key)
  • min (Double)
  • max (Double)

See Also:



248
249
250
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 248

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.

Parameters:

  • key (Key)
  • start (Integer)
  • stop (Integer)
  • withscores (Enum)

See Also:



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

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)).

Parameters:

  • key (Key)
  • max (String)
  • min (String)

See Also:



182
183
184
185
186
187
188
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 182

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)).

Parameters:

  • key (Key)
  • max (Double)
  • min (Double)
  • withscores (Enum)

See Also:



272
273
274
275
276
277
278
279
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 272

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)).

Parameters:

  • key (Key)
  • member (String)

See Also:



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

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..

Parameters:

  • key (Key)
  • cursor (Integer) (defaults to: 0)

See Also:



310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 310

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).

Parameters:

  • key (Key)
  • member (String)

See Also:



293
294
295
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 293

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.

Parameters:

  • destination (Key)
  • numkeys (Integer)
  • key (Key)

See Also:



302
303
304
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 302

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