class Cassandra::Protocol::CqlByteBuffer
Constants
- DECIMAL_POINT
@private
- FLOAT_STRING_FORMAT
@private
- MINUS
@private
- NO_CHAR
@private
- ZERO
@private
Public Instance Methods
append_bytes(bytes)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 356 def append_bytes(bytes) 357 if bytes 358 append_int(bytes.bytesize) 359 append(bytes) 360 else 361 append_int(-1) 362 end 363 end
append_bytes_map(map)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 389 def append_bytes_map(map) 390 append_short(map.size) 391 map.each do |key, value| 392 append_string(key) 393 append_bytes(value) 394 end 395 self 396 end
append_consistency(consistency)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 374 def append_consistency(consistency) 375 index = CONSISTENCIES.index(consistency) 376 raise Errors::EncodingError, %(Unknown consistency "#{consistency}") if index.nil? || CONSISTENCIES[index].nil? 377 append_short(index) 378 end
append_decimal(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 416 def append_decimal(n) 417 str = n.to_s(FLOAT_STRING_FORMAT) 418 size = str.index(DECIMAL_POINT) 419 number_string = str.gsub(DECIMAL_POINT, NO_CHAR) 420 421 num = number_string.to_i 422 raw = self.class.new.append_varint(num) 423 append_int(number_string.length - size) 424 append(raw) 425 end
append_double(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 427 def append_double(n) 428 append([n].pack(Formats::DOUBLE_FORMAT)) 429 end
append_float(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 431 def append_float(n) 432 append([n].pack(Formats::FLOAT_FORMAT)) 433 end
append_int(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 321 def append_int(n) 322 append([n].pack(Formats::INT_FORMAT)) 323 end
append_long(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 398 def append_long(n) 399 top = n >> 32 400 bottom = n & 0xffffffff 401 append_int(top) 402 append_int(bottom) 403 end
append_long_string(str)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 335 def append_long_string(str) 336 append_int(str.bytesize) 337 append(str) 338 end
append_short(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 325 def append_short(n) 326 append([n].pack(Formats::SHORT_FORMAT)) 327 end
append_short_bytes(bytes)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 365 def append_short_bytes(bytes) 366 if bytes 367 append_short(bytes.bytesize) 368 append(bytes) 369 else 370 append_short(-1) 371 end 372 end
append_signed_vint32(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 446 def append_signed_vint32(n) 447 append_vint(Util.encode_zigzag32(n)) 448 end
append_signed_vint64(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 450 def append_signed_vint64(n) 451 append_vint(Util.encode_zigzag64(n)) 452 end
append_smallint(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 317 def append_smallint(n) 318 append_short(n) 319 end
append_string(str)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 329 def append_string(str) 330 str = str.to_s 331 append_short(str.bytesize) 332 append(str) 333 end
append_string_list(strs)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 348 def append_string_list(strs) 349 append_short(strs.size) 350 strs.each do |str| 351 append_string(str) 352 end 353 self 354 end
append_string_map(map)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 380 def append_string_map(map) 381 append_short(map.size) 382 map.each do |key, value| 383 append_string(key) 384 append_string(value) 385 end 386 self 387 end
append_tinyint(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 313 def append_tinyint(n) 314 append([n].pack(Formats::CHAR_FORMAT)) 315 end
append_uuid(uuid)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 340 def append_uuid(uuid) 341 v = uuid.value 342 append_int((v >> 96) & 0xffffffff) 343 append_int((v >> 64) & 0xffffffff) 344 append_int((v >> 32) & 0xffffffff) 345 append_int((v >> 0) & 0xffffffff) 346 end
append_varint(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 405 def append_varint(n) 406 num = n 407 bytes = [] 408 loop do 409 bytes << (num & 0xff) 410 num >>= 8 411 break if (num == 0 || num == -1) && (bytes.last[7] == num[7]) 412 end 413 append(bytes.reverse.pack(Formats::BYTES_FORMAT)) 414 end
append_vint(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 435 def append_vint(n) 436 send_bytes = Util.to_min_byte_array(n) 437 send_cnt = send_bytes.length 438 439 raise Errors::EncodingError, "Too many bytes (#{bytes_to_send.length}) to send!" if send_cnt > 8 440 441 send_cnt_byte = (0xff << (8 - send_cnt)) & 0xff 442 append([send_cnt_byte].pack(Formats::BYTES_FORMAT)) 443 append(send_bytes.pack(Formats::BYTES_FORMAT)) 444 end
eql?(other)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 454 def eql?(other) 455 other.eql?(to_str) 456 end
Also aliased as: ==
inspect()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 33 def inspect 34 "#<#{self.class.name}:0x#{object_id.to_s(16)} #{to_str.inspect}>" 35 end
read_bytes()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 178 def read_bytes 179 size = read_signed_int 180 return nil if size & 0x80000000 == 0x80000000 181 read(size) 182 rescue RangeError => e 183 raise Errors::DecodingError, 184 "Not enough bytes available to decode a bytes: #{e.message}", e.backtrace 185 end
read_bytes_map()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 240 def read_bytes_map 241 map = {} 242 map_size = read_unsigned_short 243 map_size.times do 244 key = read_string 245 map[key] = read_bytes 246 end 247 map 248 end
read_consistency()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 222 def read_consistency 223 index = read_unsigned_short 224 if index >= CONSISTENCIES.size || CONSISTENCIES[index].nil? 225 raise Errors::DecodingError, "Unknown consistency index #{index}" 226 end 227 CONSISTENCIES[index] 228 end
read_decimal(len = bytesize)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 55 def read_decimal(len = bytesize) 56 scale = read_signed_int 57 number_string = read_varint(len - 4).to_s 58 if scale <= 0 59 # Special case where the actual scale is positive; scale in the protocol is actually negative of 60 # reality. 61 BigDecimal(number_string + '0' * -scale) 62 else 63 if number_string.length <= scale 64 if number_string.start_with?(MINUS) 65 number_string = number_string[1, number_string.length - 1] 66 fraction_string = MINUS + ZERO << DECIMAL_POINT 67 else 68 fraction_string = ZERO + DECIMAL_POINT 69 end 70 (scale - number_string.length).times { fraction_string << ZERO } 71 fraction_string << number_string 72 else 73 fraction_string = number_string[0, number_string.length - scale] 74 fraction_string << DECIMAL_POINT 75 fraction_string << 76 number_string[number_string.length - scale, number_string.length] 77 end 78 BigDecimal(fraction_string) 79 end 80 rescue Errors::DecodingError => e 81 raise Errors::DecodingError, e.message, e.backtrace 82 end
read_double()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 94 def read_double 95 read(8).unpack(Formats::DOUBLE_FORMAT).first 96 rescue RangeError => e 97 raise Errors::DecodingError, 98 "Not enough bytes available to decode a double: #{e.message}", e.backtrace 99 end
read_double_le()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 101 def read_double_le 102 read(8).unpack(Formats::DOUBLE_FORMAT_LE).first 103 rescue RangeError => e 104 raise Errors::DecodingError, 105 "Not enough bytes available to decode a double: #{e.message}", e.backtrace 106 end
read_float()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 108 def read_float 109 read(4).unpack(Formats::FLOAT_FORMAT).first 110 rescue RangeError => e 111 raise Errors::DecodingError, 112 "Not enough bytes available to decode a float: #{e.message}", e.backtrace 113 end
read_inet()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 202 def read_inet 203 size = read_byte 204 ip_addr = IPAddr.new_ntoh(read(size)) 205 port = read_int 206 [ip_addr, port] 207 rescue RangeError => e 208 raise Errors::DecodingError, 209 "Not enough bytes available to decode an INET: #{e.message}", 210 e.backtrace 211 end
read_inet_addr()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 213 def read_inet_addr 214 size = read_byte 215 IPAddr.new_ntoh(read(size)) 216 rescue RangeError => e 217 raise Errors::DecodingError, 218 "Not enough bytes available to decode an INET addr: #{e.message}", 219 e.backtrace 220 end
read_long()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 84 def read_long 85 top, bottom = read(8).unpack(Formats::TWO_INTS_FORMAT) 86 return (top << 32) | bottom if top <= 0x7fffffff 87 top ^= 0xffffffff 88 bottom ^= 0xffffffff 89 -((top << 32) | bottom) - 1 90 rescue RangeError => e 91 raise Errors::DecodingError, e.message, e.backtrace 92 end
read_long_string()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 155 def read_long_string 156 length = read_signed_int 157 string = read(length) 158 string.force_encoding(::Encoding::UTF_8) 159 string 160 rescue RangeError => e 161 raise Errors::DecodingError, 162 "Not enough bytes available to decode a long string: #{e.message}", 163 e.backtrace 164 end
read_option() { |id, self| ... }
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 195 def read_option 196 id = read_unsigned_short 197 value = nil 198 value = yield id, self if block_given? 199 [id, value] 200 end
read_reason_map()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 250 def read_reason_map 251 # reason_map is new in v5. Starts with an int indicating the number of key-value pairs, followed by 252 # the key-value pairs. Keys are inet's, values are short int's. 253 map = {} 254 map_size = read_int 255 map_size.times do 256 key = read_inet_addr 257 map[key] = read_short 258 end 259 map 260 end
read_short_bytes()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 187 def read_short_bytes 188 read(read_unsigned_short) 189 rescue RangeError => e 190 raise Errors::DecodingError, 191 "Not enough bytes available to decode a short bytes: #{e.message}", 192 e.backtrace 193 end
read_signed_int()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 115 def read_signed_int 116 n = read_int 117 return n if n <= 0x7fffffff 118 n - 0xffffffff - 1 119 rescue RangeError => e 120 raise Errors::DecodingError, 121 "Not enough bytes available to decode an int: #{e.message}", e.backtrace 122 end
read_signed_vint()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 309 def read_signed_vint 310 Util.decode_zigzag(read_vint) 311 end
read_smallint()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 272 def read_smallint 273 n = read_short 274 return n if n <= 0x7fff 275 n - 0xffff - 1 276 rescue RangeError => e 277 raise Errors::DecodingError, 278 "Not enough bytes available to decode a smallint: #{e.message}", e.backtrace 279 end
read_string()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 145 def read_string 146 length = read_unsigned_short 147 string = read(length) 148 string.force_encoding(::Encoding::UTF_8) 149 string 150 rescue RangeError => e 151 raise Errors::DecodingError, 152 "Not enough bytes available to decode a string: #{e.message}", e.backtrace 153 end
read_string_list()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 173 def read_string_list 174 size = read_unsigned_short 175 Array.new(size) { read_string } 176 end
read_string_map()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 230 def read_string_map 231 map = {} 232 map_size = read_unsigned_short 233 map_size.times do 234 key = read_string 235 map[key] = read_string 236 end 237 map 238 end
read_string_multimap()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 262 def read_string_multimap 263 map = {} 264 map_size = read_unsigned_short 265 map_size.times do 266 key = read_string 267 map[key] = read_string_list 268 end 269 map 270 end
read_tinyint()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 281 def read_tinyint 282 n = read_byte 283 return n if n <= 0x7f 284 n - 0xff - 1 285 rescue RangeError => e 286 raise Errors::DecodingError, 287 "Not enough bytes available to decode a tinyint: #{e.message}", e.backtrace 288 end
read_unsigned_byte()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 37 def read_unsigned_byte 38 read_byte 39 rescue RangeError => e 40 raise Errors::DecodingError, e.message, e.backtrace 41 end
read_unsigned_int_le()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 124 def read_unsigned_int_le 125 read(4).unpack(Formats::INT_FORMAT_LE).first 126 rescue RangeError => e 127 raise Errors::DecodingError, 128 "Not enough bytes available to decode an int: #{e.message}", e.backtrace 129 end
read_unsigned_short()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 131 def read_unsigned_short 132 read_short 133 rescue RangeError => e 134 raise Errors::DecodingError, 135 "Not enough bytes available to decode a short: #{e.message}", e.backtrace 136 end
read_unsigned_short_le()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 138 def read_unsigned_short_le 139 read(2).unpack(Formats::SHORT_FORMAT_LE).first 140 rescue RangeError => e 141 raise Errors::DecodingError, 142 "Not enough bytes available to decode a short: #{e.message}", e.backtrace 143 end
read_uuid(impl = Uuid)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 166 def read_uuid(impl = Uuid) 167 impl.new(read_varint(16, false)) 168 rescue Errors::DecodingError => e 169 raise Errors::DecodingError, 170 "Not enough bytes available to decode a UUID: #{e.message}", e.backtrace 171 end
read_varint(len = bytesize, signed = true)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 43 def read_varint(len = bytesize, signed = true) 44 bytes = read(len) 45 n = 0 46 bytes.each_byte do |b| 47 n = (n << 8) | b 48 end 49 n -= 2**(bytes.length * 8) if signed && bytes.getbyte(0) & 0x80 == 0x80 50 n 51 rescue RangeError => e 52 raise Errors::DecodingError, e.message, e.backtrace 53 end
read_vint()
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 290 def read_vint 291 n = read_byte 292 293 # Bits are indexed in Integer in little-endian order 294 bytes_to_read = 7.downto(0).take_while {|i| n[i] == 1}.size 295 return n unless bytes_to_read > 0 296 297 rv = n & (0xff >> bytes_to_read) 298 1.upto(bytes_to_read) do |idx| 299 new_byte = read_byte 300 rv <<= 8 301 rv |= (new_byte & 0xff) 302 end 303 304 rv 305 rescue RangeError => e 306 raise Errors::DecodingError, e.message, e.backtrace 307 end