Module: Cql::Protocol::Encoding

Extended by:
Encoding
Included in:
Encoding, Request, TypeConverter
Defined in:
lib/cql/protocol/encoding.rb

Instance Method Summary collapse

Instance Method Details

#write_bytes(buffer, bytes) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/cql/protocol/encoding.rb', line 45

def write_bytes(buffer, bytes)
  if bytes
    write_int(buffer, bytes.bytesize)
    buffer << bytes
  else
    write_int(buffer, -1)
  end
  buffer
end

#write_consistency(buffer, consistency) ⇒ Object

Raises:



65
66
67
68
69
# File 'lib/cql/protocol/encoding.rb', line 65

def write_consistency(buffer, consistency)
  index = CONSISTENCIES.index(consistency)
  raise EncodingError, %(Unknown consistency "#{consistency}") if index.nil? || CONSISTENCIES[index].nil?
  write_short(buffer, index)
end

#write_decimal(buffer, n) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/cql/protocol/encoding.rb', line 97

def write_decimal(buffer, n)
  sign, number_string, _, size = n.split
  num = number_string.to_i
  raw = write_varint('', num)
  write_int(buffer, number_string.length - size)
  buffer << raw
end

#write_double(buffer, n) ⇒ Object



105
106
107
# File 'lib/cql/protocol/encoding.rb', line 105

def write_double(buffer, n)
  buffer << [n].pack(Formats::DOUBLE_FORMAT)
end

#write_float(buffer, n) ⇒ Object



109
110
111
# File 'lib/cql/protocol/encoding.rb', line 109

def write_float(buffer, n)
  buffer << [n].pack(Formats::FLOAT_FORMAT)
end

#write_int(buffer, n) ⇒ Object



8
9
10
# File 'lib/cql/protocol/encoding.rb', line 8

def write_int(buffer, n)
  buffer << [n].pack(Formats::INT_FORMAT)
end

#write_long(buffer, n) ⇒ Object



80
81
82
83
84
85
# File 'lib/cql/protocol/encoding.rb', line 80

def write_long(buffer, n)
  top = n >> 32
  bottom = n & 0xffffffff
  write_int(buffer, top)
  write_int(buffer, bottom)
end

#write_long_string(buffer, str) ⇒ Object



23
24
25
26
27
# File 'lib/cql/protocol/encoding.rb', line 23

def write_long_string(buffer, str)
  buffer << [str.bytesize].pack(Formats::INT_FORMAT)
  buffer << str
  buffer
end

#write_short(buffer, n) ⇒ Object



12
13
14
# File 'lib/cql/protocol/encoding.rb', line 12

def write_short(buffer, n)
  buffer << [n].pack(Formats::SHORT_FORMAT)
end

#write_short_bytes(buffer, bytes) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/cql/protocol/encoding.rb', line 55

def write_short_bytes(buffer, bytes)
  if bytes
    write_short(buffer, bytes.bytesize)
    buffer << bytes
  else
    write_short(buffer, -1)
  end
  buffer
end

#write_string(buffer, str) ⇒ Object



16
17
18
19
20
21
# File 'lib/cql/protocol/encoding.rb', line 16

def write_string(buffer, str)
  str = str.to_s
  buffer << [str.bytesize].pack(Formats::SHORT_FORMAT)
  buffer << str
  buffer
end

#write_string_list(buffer, strs) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/cql/protocol/encoding.rb', line 37

def write_string_list(buffer, strs)
  buffer << [strs.size].pack(Formats::SHORT_FORMAT)
  strs.each do |str|
    write_string(buffer, str)
  end
  buffer
end

#write_string_map(buffer, map) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/cql/protocol/encoding.rb', line 71

def write_string_map(buffer, map)
  buffer << [map.size].pack(Formats::SHORT_FORMAT)
  map.each do |key, value|
    write_string(buffer, key)
    write_string(buffer, value)
  end
  buffer
end

#write_uuid(buffer, uuid) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/cql/protocol/encoding.rb', line 29

def write_uuid(buffer, uuid)
  v = uuid.value
  write_int(buffer, (v >> 96) & 0xffffffff)
  write_int(buffer, (v >> 64) & 0xffffffff)
  write_int(buffer, (v >> 32) & 0xffffffff)
  write_int(buffer, v & 0xffffffff)
end

#write_varint(buffer, n) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/cql/protocol/encoding.rb', line 87

def write_varint(buffer, n)
  num = n
  bytes = []
  until num == 0 || num == -1
    bytes << (num & 0xff)
    num = num >> 8
  end
  buffer << bytes.reverse.pack(Formats::BYTES_FORMAT)
end