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



41
42
43
44
45
46
47
48
49
# File 'lib/cql/protocol/encoding.rb', line 41

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:



61
62
63
64
65
# File 'lib/cql/protocol/encoding.rb', line 61

def write_consistency(buffer, consistency)
  index = CONSISTENCIES.index(consistency)
  raise EncodingError, %(Unknown consistency "#{consistency}") unless index
  write_short(buffer, index)
end

#write_decimal(buffer, n) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/cql/protocol/encoding.rb', line 93

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



101
102
103
# File 'lib/cql/protocol/encoding.rb', line 101

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

#write_float(buffer, n) ⇒ Object



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

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



76
77
78
79
80
81
# File 'lib/cql/protocol/encoding.rb', line 76

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



51
52
53
54
55
56
57
58
59
# File 'lib/cql/protocol/encoding.rb', line 51

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



33
34
35
36
37
38
39
# File 'lib/cql/protocol/encoding.rb', line 33

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



67
68
69
70
71
72
73
74
# File 'lib/cql/protocol/encoding.rb', line 67

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
# File 'lib/cql/protocol/encoding.rb', line 29

def write_uuid(buffer, uuid)
  write_varint(buffer, uuid.value)
end

#write_varint(buffer, n) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/cql/protocol/encoding.rb', line 83

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