Class: Monga::Connections::Buffer
- Inherits:
-
Object
- Object
- Monga::Connections::Buffer
- Includes:
- Enumerable
- Defined in:
- lib/monga/connections/buffer.rb
Constant Summary collapse
- EMPTY =
""
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#buffer_size ⇒ Object
readonly
Returns the value of attribute buffer_size.
Instance Method Summary collapse
- #append(data) ⇒ Object
- #each ⇒ Object
-
#initialize ⇒ Buffer
constructor
A new instance of Buffer.
- #parse_buffer ⇒ Object
- #parse_doc ⇒ Object
- #parse_meta ⇒ Object
Constructor Details
#initialize ⇒ Buffer
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
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
6 7 8 |
# File 'lib/monga/connections/buffer.rb', line 6 def buffer @buffer end |
#buffer_size ⇒ Object (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 |
#each ⇒ Object
20 21 22 23 24 |
# File 'lib/monga/connections/buffer.rb', line 20 def each while doc = parse_buffer yield doc end end |
#parse_buffer ⇒ Object
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 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_doc ⇒ Object
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_meta ⇒ Object
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 @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 |