Module: MockRedis::ZsetMethods

Includes:
Assertions, UtilityMethods
Included in:
Database
Defined in:
lib/mock_redis/zset_methods.rb

Instance Method Summary collapse

Instance Method Details

#bzmpop(timeout, *keys, **options) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/mock_redis/zset_methods.rb', line 363

def bzmpop(timeout, *keys, **options)
  timeout = assert_valid_timeout(timeout)

  keys.each do |key|
    assert_zsety(key)
  end

  modifier = options.is_a?(Hash) && options[:modifier]&.to_s&.downcase || 'min'
  count = (options.is_a?(Hash) && options[:count]) || 1

  unless %w[min max].include?(modifier)
    raise ArgumentError, 'Pick either MIN or MAX'
  end

  keys.each do |key|
    record_count = zcard(key)
    next if record_count.zero?

    values = [count, record_count].min.times.map do
      modifier == 'min' ? zpopmin(key) : zpopmax(key)
    end

    return [key, values]
  end

  if timeout > 0
    nil
  else
    raise MockRedis::WouldBlock, "Can't block forever"
  end
end

#bzpopmax(*args) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/mock_redis/zset_methods.rb', line 323

def bzpopmax(*args)
  keys, timeout = extract_timeout(args)
  nonempty_zset = first_nonempty_zset(keys)

  if nonempty_zset
    member, score = zpopmax(nonempty_zset)
    [nonempty_zset, member, score]
  elsif timeout > 0
    nil
  else
    raise MockRedis::WouldBlock, "Can't block forever"
  end
end

#bzpopmin(*args) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/mock_redis/zset_methods.rb', line 309

def bzpopmin(*args)
  keys, timeout = extract_timeout(args)
  nonempty_zset = first_nonempty_zset(keys)

  if nonempty_zset
    member, score = zpopmin(nonempty_zset)
    [nonempty_zset, member, score]
  elsif timeout > 0
    nil
  else
    raise MockRedis::WouldBlock, "Can't block forever"
  end
end

#zadd(key, *args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mock_redis/zset_methods.rb', line 10

def zadd(key, *args)
  zadd_options = {}
  zadd_options = args.pop if args.last.is_a?(Hash)

  if zadd_options&.include?(:nx) && zadd_options&.include?(:xx)
    raise Error.command_error(
      'ERR XX and NX options at the same time are not compatible',
      self
    )
  end

  if zadd_options&.include?(:gt) && zadd_options&.include?(:nx)
    raise Error.command_error(
      'ERR GT, LT, and/or NX options at the same time are not compatible',
      self
    )
  end

  if args.size == 1 && args[0].is_a?(Array)
    zadd_multiple_members(key, args.first, zadd_options)
  elsif args.size == 2
    score, member = args
    zadd_one_member(key, score, member, zadd_options)
  else
    raise ArgumentError, 'wrong number of arguments'
  end
end

#zcard(key) ⇒ Object



146
147
148
# File 'lib/mock_redis/zset_methods.rb', line 146

def zcard(key)
  with_zset_at(key, &:size)
end

#zcount(key, min, max) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/mock_redis/zset_methods.rb', line 150

def zcount(key, min, max)
  assert_range_args(min, max)

  with_zset_at(key) do |zset|
    zset.in_range(min, max).size
  end
end

#zincrby(key, increment, member) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/mock_redis/zset_methods.rb', line 158

def zincrby(key, increment, member)
  assert_scorey(increment)
  member = member.to_s
  with_zset_at(key) do |z|
    old_score = z.include?(member) ? z.score(member) : 0
    new_score = old_score + increment
    z.add(new_score, member)
    new_score.to_f
  end
end

#zinterstore(destination, keys, options = {}) ⇒ Object



169
170
171
172
173
174
# File 'lib/mock_redis/zset_methods.rb', line 169

def zinterstore(destination, keys, options = {})
  assert_has_args(keys, 'zinterstore')

  data[destination] = combine_weighted_zsets(keys, options, :intersection)
  zcard(destination)
end

#zmpop(*keys, **options) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/mock_redis/zset_methods.rb', line 337

def zmpop(*keys, **options)
  keys.each do |key|
    assert_zsety(key)
  end

  modifier = options.is_a?(Hash) && options[:modifier]&.to_s&.downcase || 'min'
  count = (options.is_a?(Hash) && options[:count]) || 1

  unless %w[min max].include?(modifier)
    raise ArgumentError, 'Pick either MIN or MAX'
  end

  keys.each do |key|
    record_count = zcard(key)
    next if record_count.zero?

    values = [count, record_count].min.times.map do
      modifier == 'min' ? zpopmin(key) : zpopmax(key)
    end

    return [key, values]
  end

  nil
end

#zmscore(key, *members) ⇒ Object



293
294
295
296
297
298
299
300
# File 'lib/mock_redis/zset_methods.rb', line 293

def zmscore(key, *members)
  with_zset_at(key) do |z|
    members.map do |member|
      score = z.score(member.to_s)
      score&.to_f
    end
  end
end

#zpopmax(key, count = 1) ⇒ Object



224
225
226
227
228
229
230
231
# File 'lib/mock_redis/zset_methods.rb', line 224

def zpopmax(key, count = 1)
  with_zset_at(key) do |z|
    pairs = z.sorted.reverse.first(count)
    pairs.each { |pair| z.delete?(pair.last) }
    retval = to_response(pairs, with_scores: true)
    count == 1 ? retval.first : retval
  end
end

#zpopmin(key, count = 1) ⇒ Object



215
216
217
218
219
220
221
222
# File 'lib/mock_redis/zset_methods.rb', line 215

def zpopmin(key, count = 1)
  with_zset_at(key) do |z|
    pairs = z.sorted.first(count)
    pairs.each { |pair| z.delete?(pair.last) }
    retval = to_response(pairs, with_scores: true)
    count == 1 ? retval.first : retval
  end
end

#zrange(key, start, stop, options = {}) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/mock_redis/zset_methods.rb', line 176

def zrange(key, start, stop, options = {})
  with_zset_at(key) do |z|
    start = [start.to_i, -z.sorted.size].max
    stop = stop.to_i
    to_response(z.sorted[start..stop] || [], options)
  end
end

#zrangebyscore(key, min, max, options = {}) ⇒ Object



184
185
186
187
188
189
190
191
# File 'lib/mock_redis/zset_methods.rb', line 184

def zrangebyscore(key, min, max, options = {})
  assert_range_args(min, max)

  with_zset_at(key) do |zset|
    all_results = zset.in_range(min, max)
    to_response(apply_limit(all_results, options[:limit]), options)
  end
end

#zrank(key, member) ⇒ Object



193
194
195
# File 'lib/mock_redis/zset_methods.rb', line 193

def zrank(key, member)
  with_zset_at(key) { |z| z.sorted_members.index(member.to_s) }
end

#zrem(key, *args) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/mock_redis/zset_methods.rb', line 197

def zrem(key, *args)
  if !args.first.is_a?(Array)
    retval = with_zset_at(key) { |z| !!z.delete?(args.first.to_s) }
  else
    args = args.first
    if args.empty?
      retval = 0
    else
      retval = args.map { |member| !!zscore(key, member.to_s) }.count(true)
      with_zset_at(key) do |z|
        args.each { |member| z.delete?(member.to_s) }
      end
    end
  end

  retval
end

#zremrangebyrank(key, start, stop) ⇒ Object



239
240
241
242
243
# File 'lib/mock_redis/zset_methods.rb', line 239

def zremrangebyrank(key, start, stop)
  zrange(key, start, stop).
    each { |member| zrem(key, member) }.
    size
end

#zremrangebyscore(key, min, max) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/mock_redis/zset_methods.rb', line 245

def zremrangebyscore(key, min, max)
  assert_range_args(min, max)

  zrangebyscore(key, min, max).
    each { |member| zrem(key, member) }.
    size
end

#zrevrange(key, start, stop, options = {}) ⇒ Object



233
234
235
236
237
# File 'lib/mock_redis/zset_methods.rb', line 233

def zrevrange(key, start, stop, options = {})
  with_zset_at(key) do |z|
    to_response(z.sorted.reverse[start..stop] || [], options)
  end
end

#zrevrangebyscore(key, max, min, options = {}) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/mock_redis/zset_methods.rb', line 253

def zrevrangebyscore(key, max, min, options = {})
  assert_range_args(min, max)

  with_zset_at(key) do |zset|
    to_response(
      apply_limit(
        zset.in_range(min, max).reverse,
        options[:limit]
      ),
      options
    )
  end
end

#zrevrank(key, member) ⇒ Object



267
268
269
# File 'lib/mock_redis/zset_methods.rb', line 267

def zrevrank(key, member)
  with_zset_at(key) { |z| z.sorted_members.reverse.index(member.to_s) }
end

#zscan(key, cursor, opts = {}) ⇒ Object



271
272
273
274
# File 'lib/mock_redis/zset_methods.rb', line 271

def zscan(key, cursor, opts = {})
  opts = opts.merge(key: lambda { |x| x[0] })
  common_scan(zrange(key, 0, -1, withscores: true), cursor, opts)
end

#zscan_each(key, opts = {}, &block) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/mock_redis/zset_methods.rb', line 276

def zscan_each(key, opts = {}, &block)
  return to_enum(:zscan_each, key, opts) unless block_given?
  cursor = 0
  loop do
    cursor, values = zscan(key, cursor, opts)
    values.each(&block)
    break if cursor == '0'
  end
end

#zscore(key, member) ⇒ Object



286
287
288
289
290
291
# File 'lib/mock_redis/zset_methods.rb', line 286

def zscore(key, member)
  with_zset_at(key) do |z|
    score = z.score(member.to_s)
    score&.to_f
  end
end

#zunionstore(destination, keys, options = {}) ⇒ Object



302
303
304
305
306
307
# File 'lib/mock_redis/zset_methods.rb', line 302

def zunionstore(destination, keys, options = {})
  assert_has_args(keys, 'zunionstore')

  data[destination] = combine_weighted_zsets(keys, options, :union)
  zcard(destination)
end