Class: Redis::Reader

Inherits:
Array
  • Object
show all
Defined in:
lib/redis/reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Reader

Minimize the amount of memory copying. The primary performance trick is to String#split and work with that. This is almost as fast as hiredis/reader plus it supports servers



8
9
10
11
12
13
14
# File 'lib/redis/reader.rb', line 8

def initialize options={}
  super()
  @multibulk_limit = options[:multibulk_limit] || 1024*1024
  @bulk_limit = options[:bulk_limit] || 512*1024*1024
  @buffer_overflow = false
  flush
end

Instance Method Details

#feed(data) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/redis/reader.rb', line 16

def feed data
  return if @buffer_overflow
  if reduce(0){|t,e|t+e.bytesize} > @bulk_limit + 2
    @buffer_overflow = true
    flush
    return
  end
  push data
end

#getsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/redis/reader.rb', line 26

def gets
  raise 'buffer overflow' if @buffer_overflow
  if @completed.empty?
    unshift_split if @split
    frame do |str|
      @elements << str
      if @remaining != 0
        if @remaining < 0
          @remaining = -@remaining
          @elements = str
        end
        @remaining -= 1
        if @remaining == 0 and !@stack.empty?
          elements = @elements
          @elements, @remaining = @stack.pop
          @elements << elements
          @remaining -= 1
        end
        next unless @remaining == 0
        @completed << @elements
      elsif !@elements.empty?
        @completed << @elements[0]
      end
      @elements = []
      @remaining = 0
    end
  end
  return false if @completed.empty?
  @completed.shift
end