Module: MockRedis::StreamMethods

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

Instance Method Summary collapse

Instance Method Details

#xadd(key, entry, opts = {}) ⇒ Object


32
33
34
35
36
37
38
39
# File 'lib/mock_redis/stream_methods.rb', line 32

def xadd(key, entry, opts = {})
  id = opts[:id] || '*'
  with_stream_at(key) do |stream|
    stream.add id, entry
    stream.trim opts[:maxlen] if opts[:maxlen]
    return stream.last_id
  end
end

#xlen(key) ⇒ Object


47
48
49
50
51
# File 'lib/mock_redis/stream_methods.rb', line 47

def xlen(key)
  with_stream_at(key) do |stream|
    return stream.count
  end
end

#xrange(key, first = '-', last = '+', count: nil) ⇒ Object


53
54
55
56
57
58
59
# File 'lib/mock_redis/stream_methods.rb', line 53

def xrange(key, first = '-', last = '+', count: nil)
  args = [first, last, false]
  args += ['COUNT', count] if count
  with_stream_at(key) do |stream|
    return stream.range(*args)
  end
end

#xread(keys, ids, count: nil, block: nil) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mock_redis/stream_methods.rb', line 69

def xread(keys, ids, count: nil, block: nil)
  args = []
  args += ['COUNT', count] if count
  args += ['BLOCK', block.to_i] if block
  result = {}
  keys = keys.is_a?(Array) ? keys : [keys]
  ids = ids.is_a?(Array) ? ids : [ids]
  keys.each_with_index do |key, index|
    with_stream_at(key) do |stream|
      data = stream.read(ids[index], *args)
      result[key] = data unless data.empty?
    end
  end
  result
end

#xrevrange(key, last = '+', first = '-', count: nil) ⇒ Object


61
62
63
64
65
66
67
# File 'lib/mock_redis/stream_methods.rb', line 61

def xrevrange(key, last = '+', first = '-', count: nil)
  args = [first, last, true]
  args += ['COUNT', count] if count
  with_stream_at(key) do |stream|
    return stream.range(*args)
  end
end

#xtrim(key, count) ⇒ Object


41
42
43
44
45
# File 'lib/mock_redis/stream_methods.rb', line 41

def xtrim(key, count)
  with_stream_at(key) do |stream|
    stream.trim count
  end
end