Class: Monga::Connections::Buffer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/monga/connections/buffer.rb

Constant Summary collapse

EMPTY =
""

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuffer

Returns a new instance of Buffer.



8
9
10
11
12
13
# File 'lib/monga/connections/buffer.rb', line 8

def initialize
  @buffer = ""
  @position = 0
  @buffer_size = 0
  @response = []
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



6
7
8
# File 'lib/monga/connections/buffer.rb', line 6

def buffer
  @buffer
end

#buffer_sizeObject (readonly)

Returns the value of attribute buffer_size.



6
7
8
# File 'lib/monga/connections/buffer.rb', line 6

def buffer_size
  @buffer_size
end

Instance Method Details

#append(data) ⇒ Object



15
16
17
18
# File 'lib/monga/connections/buffer.rb', line 15

def append(data)
  @buffer << data
  @buffer_size += data.bytesize
end

#eachObject



20
21
22
23
24
# File 'lib/monga/connections/buffer.rb', line 20

def each
  while doc = parse_buffer
    yield doc
  end
end

#parse_bufferObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/monga/connections/buffer.rb', line 26

def parse_buffer
  return if @buffer_size == 0

  if @position == 0
    parse_doc if parse_meta
  else
    parse_doc 
  end

  if @number_returned == 0
    if @buffer_size == @position
      @buffer.clear
      @buffer_size = 0
    else
      @buffer = @buffer[@position, @buffer_size-@position]
      @buffer_size -= @position
    end
    @position = 0

    @response  unless @response.empty?
  end
end

#parse_docObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/monga/connections/buffer.rb', line 66

def parse_doc
  if @number_returned == @response[7]
    @current_pos = @position
    @str_io = nil
  end

  while @number_returned > 0
    break if @buffer_size - @position < 4
    doc_length = ::BinUtils.get_int32_le(@buffer, @position)
    break if @buffer_size - @position < doc_length
    @number_returned -= 1
    @position += doc_length
  end

  if @number_returned == 0
    @str_io = StringIO.new @buffer[@current_pos..@position]
    @str_io.rewind
    @response[7].times do
      @response[-1] << BSON::Document.from_bson(@str_io)
    end
  end
end

#parse_metaObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/monga/connections/buffer.rb', line 49

def parse_meta
  @response.clear
  return  if @buffer_size < 36
  @response << ::BinUtils.get_int32_le(@buffer, @position)
  @response << ::BinUtils.get_int32_le(@buffer, @position += 4)
  @response << ::BinUtils.get_int32_le(@buffer, @position += 4)
  @response << ::BinUtils.get_int32_le(@buffer, @position += 4)
  @response << ::BinUtils.get_int32_le(@buffer, @position += 4)
  @response << ::BinUtils.get_int64_le(@buffer, @position += 4)
  @response << ::BinUtils.get_int32_le(@buffer, @position += 8)
  @number_returned = ::BinUtils.get_int32_le(@buffer, @position += 4)
  @response << @number_returned
  @position += 4
  @response << []

end