Class: Neo4jBolt::BoltBuffer
- Inherits:
-
Object
- Object
- Neo4jBolt::BoltBuffer
- Defined in:
- lib/neo4j_bolt.rb
Instance Attribute Summary collapse
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
Instance Method Summary collapse
- #dump ⇒ Object
- #eof? ⇒ Boolean
- #flush ⇒ Object
-
#initialize(socket) ⇒ BoltBuffer
constructor
A new instance of BoltBuffer.
- #next ⇒ Object
- #next_float ⇒ Object
- #next_int16 ⇒ Object
- #next_int32 ⇒ Object
- #next_int64 ⇒ Object
- #next_int8 ⇒ Object
- #next_s(length) ⇒ Object
- #next_uint16 ⇒ Object
- #next_uint32 ⇒ Object
- #next_uint64 ⇒ Object
- #next_uint8 ⇒ Object
- #peek ⇒ Object
-
#request(n) ⇒ Object
make sure we have at least n bytes in the buffer.
Constructor Details
#initialize(socket) ⇒ BoltBuffer
Returns a new instance of BoltBuffer.
98 99 100 101 102 103 |
# File 'lib/neo4j_bolt.rb', line 98 def initialize(socket) @socket = socket @stream_ended = false @data = [] @offset = 0 end |
Instance Attribute Details
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
105 106 107 |
# File 'lib/neo4j_bolt.rb', line 105 def offset @offset end |
Instance Method Details
#dump ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/neo4j_bolt.rb', line 228 def dump offset = 0 last_offset = 0 while offset < @data.size if offset % 16 == 0 STDERR.write sprintf('%04x | ', offset) end STDERR.write sprintf("%02x ", @data[offset]) offset += 1 if offset % 16 == 0 STDERR.write ' ' * 4 (last_offset...offset).each do |i| b = @data[i] STDERR.write (b >= 32 && b < 128) ? b.chr : '.' end STDERR.puts last_offset = offset end end (16 - offset + last_offset).times { STDERR.write ' ' } STDERR.write ' ' * 4 (last_offset...offset).each do |i| b = @data[i] STDERR.write (b >= 32 && b < 128) ? b.chr : '.' end STDERR.puts end |
#eof? ⇒ Boolean
224 225 226 |
# File 'lib/neo4j_bolt.rb', line 224 def eof? @stream_ended end |
#flush ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/neo4j_bolt.rb', line 128 def flush() # STDERR.write "Flushing buffer: " loop do length = @socket.read(2).unpack('n').first # TODO: length should be 0, otherwise we're out of protocol # or we encountered features not yet implemented here # STDERR.write "#{length} " break if length == 0 @socket.read(length) end # STDERR.puts end |
#next ⇒ Object
141 142 143 144 145 146 |
# File 'lib/neo4j_bolt.rb', line 141 def next request(1) v = @data[@offset] @offset += 1 v end |
#next_float ⇒ Object
212 213 214 215 216 217 |
# File 'lib/neo4j_bolt.rb', line 212 def next_float() request(8) f = @data[@offset, 8].pack('C*').unpack('G').first @offset += 8 f end |
#next_int16 ⇒ Object
191 192 193 194 195 196 |
# File 'lib/neo4j_bolt.rb', line 191 def next_int16() request(2) i = @data[@offset, 2].pack('C*').unpack('s>').first @offset += 2 i end |
#next_int32 ⇒ Object
198 199 200 201 202 203 |
# File 'lib/neo4j_bolt.rb', line 198 def next_int32() request(4) i = @data[@offset, 4].pack('C*').unpack('l>').first @offset += 4 i end |
#next_int64 ⇒ Object
205 206 207 208 209 210 |
# File 'lib/neo4j_bolt.rb', line 205 def next_int64() request(8) i = @data[@offset, 8].pack('C*').unpack('q>').first @offset += 8 i end |
#next_int8 ⇒ Object
184 185 186 187 188 189 |
# File 'lib/neo4j_bolt.rb', line 184 def next_int8() request(1) i = @data[@offset, 1].pack('C*').unpack('c').first @offset += 1 i end |
#next_s(length) ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/neo4j_bolt.rb', line 148 def next_s(length) request(length) s = @data[@offset, length].pack('C*') s.force_encoding('UTF-8') @offset += length s end |
#next_uint16 ⇒ Object
163 164 165 166 167 168 |
# File 'lib/neo4j_bolt.rb', line 163 def next_uint16() request(2) i = @data[@offset, 2].pack('C*').unpack('S>').first @offset += 2 i end |
#next_uint32 ⇒ Object
170 171 172 173 174 175 |
# File 'lib/neo4j_bolt.rb', line 170 def next_uint32() request(4) i = @data[@offset, 4].pack('C*').unpack('L>').first @offset += 4 i end |
#next_uint64 ⇒ Object
177 178 179 180 181 182 |
# File 'lib/neo4j_bolt.rb', line 177 def next_uint64() request(8) i = @data[@offset, 8].pack('C*').unpack('Q>').first @offset += 8 i end |
#next_uint8 ⇒ Object
156 157 158 159 160 161 |
# File 'lib/neo4j_bolt.rb', line 156 def next_uint8() request(1) i = @data[@offset] @offset += 1 i end |
#peek ⇒ Object
219 220 221 222 |
# File 'lib/neo4j_bolt.rb', line 219 def peek request(1) @data[@offset] end |
#request(n) ⇒ Object
make sure we have at least n bytes in the buffer
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/neo4j_bolt.rb', line 108 def request(n) while @offset + n > @data.length length = @socket.read(2).unpack('n').first # STDERR.puts "Reading next chunk at offset #{@offset}, got #{length} bytes (requested #{n} bytes)" if length == 0 @stream_ended = true else if @offset > 0 @data = @data[@offset, @data.size - @offset] @offset = 0 end chunk = @socket.read(length).unpack('C*') @data += chunk if Neo4jBolt.bolt_verbosity >= 3 dump() end end end end |