Class: NusaDB::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/nusadb/protocol.rb

Overview

Reads the protocol's primitive encodings from a payload buffer.

Instance Method Summary collapse

Constructor Details

#initialize(buf) ⇒ Reader

Returns a new instance of Reader.



158
159
160
161
# File 'lib/nusadb/protocol.rb', line 158

def initialize(buf)
  @buf = buf
  @pos = 0
end

Instance Method Details

#fieldsObject

Decode a Fields list; a nil entry is SQL NULL.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/nusadb/protocol.rb', line 195

def fields
  n = u16
  out = []
  n.times do
    if u8.zero?
      out << nil
    else
      len = u32
      out << @buf.byteslice(@pos, len)
      @pos += len
    end
  end
  out
end

#restObject



188
189
190
191
192
# File 'lib/nusadb/protocol.rb', line 188

def rest
  s = @buf.byteslice(@pos, @buf.bytesize - @pos)
  @pos = @buf.bytesize
  s
end

#strObject



181
182
183
184
185
186
# File 'lib/nusadb/protocol.rb', line 181

def str
  n = u32
  s = @buf.byteslice(@pos, n)
  @pos += n
  s.force_encoding(Encoding::UTF_8)
end

#u16Object



169
170
171
172
173
# File 'lib/nusadb/protocol.rb', line 169

def u16
  v = @buf.byteslice(@pos, 2).unpack1('n')
  @pos += 2
  v
end

#u32Object



175
176
177
178
179
# File 'lib/nusadb/protocol.rb', line 175

def u32
  v = @buf.byteslice(@pos, 4).unpack1('N')
  @pos += 4
  v
end

#u8Object



163
164
165
166
167
# File 'lib/nusadb/protocol.rb', line 163

def u8
  v = @buf.getbyte(@pos)
  @pos += 1
  v
end