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 333 def append_bytes(bytes) 334 if bytes 335 append_int(bytes.bytesize) 336 append(bytes) 337 else 338 append_int(-1) 339 end 340 end
append_bytes_map(map)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 366 def append_bytes_map(map) 367 append_short(map.size) 368 map.each do |key, value| 369 append_string(key) 370 append_bytes(value) 371 end 372 self 373 end
append_consistency(consistency)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 351 def append_consistency(consistency) 352 index = CONSISTENCIES.index(consistency) 353 raise Errors::EncodingError, %(Unknown consistency "#{consistency}") if index.nil? || CONSISTENCIES[index].nil? 354 append_short(index) 355 end
append_decimal(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 393 def append_decimal(n) 394 str = n.to_s(FLOAT_STRING_FORMAT) 395 size = str.index(DECIMAL_POINT) 396 number_string = str.gsub(DECIMAL_POINT, NO_CHAR) 397 398 num = number_string.to_i 399 raw = self.class.new.append_varint(num) 400 append_int(number_string.length - size) 401 append(raw) 402 end
append_double(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 404 def append_double(n) 405 append([n].pack(Formats::DOUBLE_FORMAT)) 406 end
append_float(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 408 def append_float(n) 409 append([n].pack(Formats::FLOAT_FORMAT)) 410 end
append_int(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 298 def append_int(n) 299 append([n].pack(Formats::INT_FORMAT)) 300 end
append_long(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 375 def append_long(n) 376 top = n >> 32 377 bottom = n & 0xffffffff 378 append_int(top) 379 append_int(bottom) 380 end
append_long_string(str)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 312 def append_long_string(str) 313 append_int(str.bytesize) 314 append(str) 315 end
append_short(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 302 def append_short(n) 303 append([n].pack(Formats::SHORT_FORMAT)) 304 end
append_short_bytes(bytes)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 342 def append_short_bytes(bytes) 343 if bytes 344 append_short(bytes.bytesize) 345 append(bytes) 346 else 347 append_short(-1) 348 end 349 end
append_smallint(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 294 def append_smallint(n) 295 append_short(n) 296 end
append_string(str)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 306 def append_string(str) 307 str = str.to_s 308 append_short(str.bytesize) 309 append(str) 310 end
append_string_list(strs)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 325 def append_string_list(strs) 326 append_short(strs.size) 327 strs.each do |str| 328 append_string(str) 329 end 330 self 331 end
append_string_map(map)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 357 def append_string_map(map) 358 append_short(map.size) 359 map.each do |key, value| 360 append_string(key) 361 append_string(value) 362 end 363 self 364 end
append_tinyint(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 290 def append_tinyint(n) 291 append([n].pack(Formats::CHAR_FORMAT)) 292 end
append_uuid(uuid)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 317 def append_uuid(uuid) 318 v = uuid.value 319 append_int((v >> 96) & 0xffffffff) 320 append_int((v >> 64) & 0xffffffff) 321 append_int((v >> 32) & 0xffffffff) 322 append_int((v >> 0) & 0xffffffff) 323 end
append_varint(n)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 382 def append_varint(n) 383 num = n 384 bytes = [] 385 loop do 386 bytes << (num & 0xff) 387 num >>= 8 388 break if (num == 0 || num == -1) && (bytes.last[7] == num[7]) 389 end 390 append(bytes.reverse.pack(Formats::BYTES_FORMAT)) 391 end
eql?(other)
click to toggle source
# File lib/cassandra/protocol/cql_byte_buffer.rb 412 def eql?(other) 413 other.eql?(to_str) 414 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.new(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.new(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_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