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
40
41
# 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
  rescue Redis::CommandError => e
    raise Error.command_error(e.message, self)
  end
end

#xlen(key) ⇒ Object



49
50
51
52
53
# File 'lib/mock_redis/stream_methods.rb', line 49

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

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



55
56
57
58
59
60
61
62
63
# File 'lib/mock_redis/stream_methods.rb', line 55

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)
  rescue Redis::CommandError => e
    raise Error.command_error(e.message, self)
  end
end

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mock_redis/stream_methods.rb', line 75

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



65
66
67
68
69
70
71
72
73
# File 'lib/mock_redis/stream_methods.rb', line 65

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)
  rescue Redis::CommandError => e
    raise Error.command_error(e.message, self)
  end
end

#xtrim(key, count) ⇒ Object



43
44
45
46
47
# File 'lib/mock_redis/stream_methods.rb', line 43

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