cassandra-driver 0.1.0.alpha1 → 1.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. data/.yardopts +4 -0
  2. data/README.md +117 -0
  3. data/lib/cassandra.rb +320 -0
  4. data/lib/cassandra/auth.rb +97 -0
  5. data/lib/cassandra/auth/providers.rb +16 -0
  6. data/lib/cassandra/auth/providers/password.rb +73 -0
  7. data/lib/cassandra/client.rb +144 -0
  8. data/lib/cassandra/client/batch.rb +212 -0
  9. data/lib/cassandra/client/client.rb +591 -0
  10. data/lib/cassandra/client/column_metadata.rb +54 -0
  11. data/lib/cassandra/client/connection_manager.rb +72 -0
  12. data/lib/cassandra/client/connector.rb +272 -0
  13. data/lib/cassandra/client/execute_options_decoder.rb +59 -0
  14. data/lib/cassandra/client/null_logger.rb +37 -0
  15. data/lib/cassandra/client/peer_discovery.rb +50 -0
  16. data/lib/cassandra/client/prepared_statement.rb +314 -0
  17. data/lib/cassandra/client/query_result.rb +230 -0
  18. data/lib/cassandra/client/request_runner.rb +71 -0
  19. data/lib/cassandra/client/result_metadata.rb +48 -0
  20. data/lib/cassandra/client/void_result.rb +78 -0
  21. data/lib/cassandra/cluster.rb +191 -0
  22. data/lib/cassandra/cluster/client.rb +767 -0
  23. data/lib/cassandra/cluster/connector.rb +231 -0
  24. data/lib/cassandra/cluster/control_connection.rb +420 -0
  25. data/lib/cassandra/cluster/options.rb +40 -0
  26. data/lib/cassandra/cluster/registry.rb +181 -0
  27. data/lib/cassandra/cluster/schema.rb +321 -0
  28. data/lib/cassandra/cluster/schema/type_parser.rb +138 -0
  29. data/lib/cassandra/column.rb +92 -0
  30. data/lib/cassandra/compression.rb +66 -0
  31. data/lib/cassandra/compression/compressors/lz4.rb +72 -0
  32. data/lib/cassandra/compression/compressors/snappy.rb +66 -0
  33. data/lib/cassandra/driver.rb +86 -0
  34. data/lib/cassandra/errors.rb +79 -0
  35. data/lib/cassandra/execution/info.rb +51 -0
  36. data/lib/cassandra/execution/options.rb +77 -0
  37. data/lib/cassandra/execution/trace.rb +152 -0
  38. data/lib/cassandra/future.rb +675 -0
  39. data/lib/cassandra/host.rb +75 -0
  40. data/lib/cassandra/keyspace.rb +120 -0
  41. data/lib/cassandra/listener.rb +87 -0
  42. data/lib/cassandra/load_balancing.rb +112 -0
  43. data/lib/cassandra/load_balancing/policies.rb +18 -0
  44. data/lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb +149 -0
  45. data/lib/cassandra/load_balancing/policies/round_robin.rb +95 -0
  46. data/lib/cassandra/load_balancing/policies/white_list.rb +90 -0
  47. data/lib/cassandra/protocol.rb +93 -0
  48. data/lib/cassandra/protocol/cql_byte_buffer.rb +307 -0
  49. data/lib/cassandra/protocol/cql_protocol_handler.rb +323 -0
  50. data/lib/cassandra/protocol/frame_decoder.rb +128 -0
  51. data/lib/cassandra/protocol/frame_encoder.rb +48 -0
  52. data/lib/cassandra/protocol/request.rb +38 -0
  53. data/lib/cassandra/protocol/requests/auth_response_request.rb +47 -0
  54. data/lib/cassandra/protocol/requests/batch_request.rb +76 -0
  55. data/lib/cassandra/protocol/requests/credentials_request.rb +47 -0
  56. data/lib/cassandra/protocol/requests/execute_request.rb +103 -0
  57. data/lib/cassandra/protocol/requests/options_request.rb +39 -0
  58. data/lib/cassandra/protocol/requests/prepare_request.rb +50 -0
  59. data/lib/cassandra/protocol/requests/query_request.rb +153 -0
  60. data/lib/cassandra/protocol/requests/register_request.rb +38 -0
  61. data/lib/cassandra/protocol/requests/startup_request.rb +49 -0
  62. data/lib/cassandra/protocol/requests/void_query_request.rb +24 -0
  63. data/lib/cassandra/protocol/response.rb +38 -0
  64. data/lib/cassandra/protocol/responses/auth_challenge_response.rb +41 -0
  65. data/lib/cassandra/protocol/responses/auth_success_response.rb +41 -0
  66. data/lib/cassandra/protocol/responses/authenticate_response.rb +41 -0
  67. data/lib/cassandra/protocol/responses/detailed_error_response.rb +60 -0
  68. data/lib/cassandra/protocol/responses/error_response.rb +50 -0
  69. data/lib/cassandra/protocol/responses/event_response.rb +39 -0
  70. data/lib/cassandra/protocol/responses/prepared_result_response.rb +64 -0
  71. data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +43 -0
  72. data/lib/cassandra/protocol/responses/ready_response.rb +44 -0
  73. data/lib/cassandra/protocol/responses/result_response.rb +48 -0
  74. data/lib/cassandra/protocol/responses/rows_result_response.rb +139 -0
  75. data/lib/cassandra/protocol/responses/schema_change_event_response.rb +60 -0
  76. data/lib/cassandra/protocol/responses/schema_change_result_response.rb +57 -0
  77. data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +42 -0
  78. data/lib/cassandra/protocol/responses/status_change_event_response.rb +44 -0
  79. data/lib/cassandra/protocol/responses/supported_response.rb +41 -0
  80. data/lib/cassandra/protocol/responses/topology_change_event_response.rb +34 -0
  81. data/lib/cassandra/protocol/responses/void_result_response.rb +39 -0
  82. data/lib/cassandra/protocol/type_converter.rb +384 -0
  83. data/lib/cassandra/reconnection.rb +49 -0
  84. data/lib/cassandra/reconnection/policies.rb +20 -0
  85. data/lib/cassandra/reconnection/policies/constant.rb +48 -0
  86. data/lib/cassandra/reconnection/policies/exponential.rb +79 -0
  87. data/lib/cassandra/result.rb +215 -0
  88. data/lib/cassandra/retry.rb +142 -0
  89. data/lib/cassandra/retry/policies.rb +21 -0
  90. data/lib/cassandra/retry/policies/default.rb +47 -0
  91. data/lib/cassandra/retry/policies/downgrading_consistency.rb +71 -0
  92. data/lib/cassandra/retry/policies/fallthrough.rb +39 -0
  93. data/lib/cassandra/session.rb +195 -0
  94. data/lib/cassandra/statement.rb +22 -0
  95. data/lib/cassandra/statements.rb +23 -0
  96. data/lib/cassandra/statements/batch.rb +95 -0
  97. data/lib/cassandra/statements/bound.rb +46 -0
  98. data/lib/cassandra/statements/prepared.rb +59 -0
  99. data/lib/cassandra/statements/simple.rb +58 -0
  100. data/lib/cassandra/statements/void.rb +33 -0
  101. data/lib/cassandra/table.rb +254 -0
  102. data/lib/cassandra/time_uuid.rb +141 -0
  103. data/lib/cassandra/util.rb +169 -0
  104. data/lib/cassandra/uuid.rb +104 -0
  105. data/lib/cassandra/version.rb +17 -1
  106. metadata +134 -8
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module Protocol
21
+ class SchemaChangeResultResponse < ResultResponse
22
+ attr_reader :change, :keyspace, :table
23
+
24
+ def initialize(change, keyspace, table, trace_id)
25
+ super(trace_id)
26
+ @change, @keyspace, @table = change, keyspace, table
27
+ end
28
+
29
+ def self.decode(protocol_version, buffer, length, trace_id=nil)
30
+ new(buffer.read_string, buffer.read_string, buffer.read_string, trace_id)
31
+ end
32
+
33
+ def eql?(other)
34
+ self.change == other.change && self.keyspace == other.keyspace && self.table == other.table
35
+ end
36
+ alias_method :==, :eql?
37
+
38
+ def hash
39
+ @h ||= begin
40
+ h = 0
41
+ h = ((h & 0xffffffff) * 31) ^ @change.hash
42
+ h = ((h & 0xffffffff) * 31) ^ @keyspace.hash
43
+ h = ((h & 0xffffffff) * 31) ^ @table.hash
44
+ h
45
+ end
46
+ end
47
+
48
+ def to_s
49
+ %(RESULT SCHEMA_CHANGE #@change "#@keyspace" "#@table")
50
+ end
51
+
52
+ private
53
+
54
+ RESULT_TYPES[0x05] = self
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module Protocol
21
+ class SetKeyspaceResultResponse < ResultResponse
22
+ attr_reader :keyspace
23
+
24
+ def initialize(keyspace, trace_id)
25
+ super(trace_id)
26
+ @keyspace = keyspace
27
+ end
28
+
29
+ def self.decode(protocol_version, buffer, length, trace_id=nil)
30
+ new(buffer.read_string, trace_id)
31
+ end
32
+
33
+ def to_s
34
+ %(RESULT SET_KEYSPACE "#@keyspace")
35
+ end
36
+
37
+ private
38
+
39
+ RESULT_TYPES[0x03] = self
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module Protocol
21
+ class StatusChangeEventResponse < EventResponse
22
+ TYPE = 'STATUS_CHANGE'.freeze
23
+
24
+ attr_reader :type, :change, :address, :port
25
+
26
+ def initialize(*args)
27
+ @change, @address, @port = args
28
+ @type = TYPE
29
+ end
30
+
31
+ def self.decode(protocol_version, buffer, length, trace_id=nil)
32
+ new(buffer.read_string, *buffer.read_inet)
33
+ end
34
+
35
+ def to_s
36
+ %(EVENT #@type #@change #@address:#@port)
37
+ end
38
+
39
+ private
40
+
41
+ EVENT_TYPES[TYPE] = self
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module Protocol
21
+ class SupportedResponse < Response
22
+ attr_reader :options
23
+
24
+ def initialize(options)
25
+ @options = options
26
+ end
27
+
28
+ def self.decode(protocol_version, buffer, length, trace_id=nil)
29
+ new(buffer.read_string_multimap)
30
+ end
31
+
32
+ def to_s
33
+ %(SUPPORTED #{options})
34
+ end
35
+
36
+ private
37
+
38
+ RESPONSE_TYPES[0x06] = self
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module Protocol
21
+ class TopologyChangeEventResponse < StatusChangeEventResponse
22
+ TYPE = 'TOPOLOGY_CHANGE'.freeze
23
+
24
+ def initialize(*args)
25
+ super
26
+ @type = TYPE
27
+ end
28
+
29
+ private
30
+
31
+ EVENT_TYPES[TYPE] = self
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module Protocol
21
+ class VoidResultResponse < ResultResponse
22
+ def self.decode(protocol_version, buffer, length, trace_id=nil)
23
+ new(trace_id)
24
+ end
25
+
26
+ def to_s
27
+ %(RESULT VOID)
28
+ end
29
+
30
+ def void?
31
+ true
32
+ end
33
+
34
+ private
35
+
36
+ RESULT_TYPES[0x01] = self
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,384 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module Protocol
21
+ class TypeConverter
22
+ def initialize
23
+ @from_bytes_converters = from_bytes_converters
24
+ @to_bytes_converters = to_bytes_converters
25
+ end
26
+
27
+ def from_bytes(buffer, type, size_bytes=4)
28
+ return nil if buffer.empty?
29
+ case type
30
+ when Array
31
+ return nil unless read_size(buffer, size_bytes)
32
+ case type.first
33
+ when :list
34
+ bytes_to_list(buffer, @from_bytes_converters[type[1]])
35
+ when :map
36
+ bytes_to_map(buffer, @from_bytes_converters[type[1]], @from_bytes_converters[type[2]])
37
+ when :set
38
+ bytes_to_set(buffer, @from_bytes_converters[type[1]])
39
+ end
40
+ else
41
+ @from_bytes_converters[type].call(buffer, size_bytes)
42
+ end
43
+ end
44
+
45
+ def to_bytes(buffer, type, value, size_bytes=4)
46
+ case type
47
+ when Array
48
+ unless value.nil? || value.is_a?(Enumerable)
49
+ raise InvalidValueError, 'Value for collection must be enumerable'
50
+ end
51
+ case type.first
52
+ when :list, :set
53
+ _, sub_type = type
54
+ if value
55
+ raw = CqlByteBuffer.new
56
+ raw.append_short(value.size)
57
+ value.each do |element|
58
+ to_bytes(raw, sub_type, element, 2)
59
+ end
60
+ buffer.append_bytes(raw)
61
+ else
62
+ nil_to_bytes(buffer, size_bytes)
63
+ end
64
+ when :map
65
+ _, key_type, value_type = type
66
+ if value
67
+ raw = CqlByteBuffer.new
68
+ raw.append_short(value.size)
69
+ value.each do |key, value|
70
+ to_bytes(raw, key_type, key, 2)
71
+ to_bytes(raw, value_type, value, 2)
72
+ end
73
+ buffer.append_bytes(raw)
74
+ else
75
+ nil_to_bytes(buffer, size_bytes)
76
+ end
77
+ else
78
+ raise UnsupportedColumnTypeError, %(Unsupported column collection type: #{type.first})
79
+ end
80
+ else
81
+ converter = @to_bytes_converters[type]
82
+ unless converter
83
+ raise UnsupportedColumnTypeError, %(Unsupported column type: #{type})
84
+ end
85
+ converter.call(buffer, value, size_bytes)
86
+ end
87
+ rescue TypeError => e
88
+ raise TypeError, %("#{value}" cannot be encoded as #{type.to_s.upcase}: #{e.message}), e.backtrace
89
+ end
90
+
91
+ private
92
+
93
+ def from_bytes_converters
94
+ {
95
+ :ascii => method(:bytes_to_ascii),
96
+ :bigint => method(:bytes_to_bigint),
97
+ :blob => method(:bytes_to_blob),
98
+ :boolean => method(:bytes_to_boolean),
99
+ :counter => method(:bytes_to_bigint),
100
+ :decimal => method(:bytes_to_decimal),
101
+ :double => method(:bytes_to_double),
102
+ :float => method(:bytes_to_float),
103
+ :int => method(:bytes_to_int),
104
+ :timestamp => method(:bytes_to_timestamp),
105
+ :varchar => method(:bytes_to_varchar),
106
+ :text => method(:bytes_to_varchar),
107
+ :varint => method(:bytes_to_varint),
108
+ :timeuuid => method(:bytes_to_timeuuid),
109
+ :uuid => method(:bytes_to_uuid),
110
+ :inet => method(:bytes_to_inet),
111
+ }
112
+ end
113
+
114
+ def to_bytes_converters
115
+ {
116
+ :ascii => method(:ascii_to_bytes),
117
+ :bigint => method(:bigint_to_bytes),
118
+ :blob => method(:blob_to_bytes),
119
+ :boolean => method(:boolean_to_bytes),
120
+ :counter => method(:bigint_to_bytes),
121
+ :decimal => method(:decimal_to_bytes),
122
+ :double => method(:double_to_bytes),
123
+ :float => method(:float_to_bytes),
124
+ :inet => method(:inet_to_bytes),
125
+ :int => method(:int_to_bytes),
126
+ :text => method(:varchar_to_bytes),
127
+ :varchar => method(:varchar_to_bytes),
128
+ :timestamp => method(:timestamp_to_bytes),
129
+ :timeuuid => method(:uuid_to_bytes),
130
+ :uuid => method(:uuid_to_bytes),
131
+ :varint => method(:varint_to_bytes),
132
+ }
133
+ end
134
+
135
+ def read_size(buffer, size_bytes)
136
+ if size_bytes == 2
137
+ size = buffer.read_short
138
+ return nil if size & 0x8000 == 0x8000
139
+ else
140
+ size = buffer.read_signed_int
141
+ return nil if size & 0x80000000 == 0x80000000
142
+ end
143
+ size
144
+ end
145
+
146
+ def bytes_to_ascii(buffer, size_bytes)
147
+ bytes = size_bytes == 4 ? buffer.read_bytes : buffer.read_short_bytes
148
+ bytes ? bytes.force_encoding(::Encoding::ASCII) : nil
149
+ end
150
+
151
+ def bytes_to_bigint(buffer, size_bytes)
152
+ return nil unless read_size(buffer, size_bytes)
153
+ buffer.read_long
154
+ end
155
+
156
+ def bytes_to_blob(buffer, size_bytes)
157
+ bytes = size_bytes == 4 ? buffer.read_bytes : buffer.read_short_bytes
158
+ bytes ? bytes : nil
159
+ end
160
+
161
+ def bytes_to_boolean(buffer, size_bytes)
162
+ return nil unless read_size(buffer, size_bytes)
163
+ buffer.read(1) == Constants::TRUE_BYTE
164
+ end
165
+
166
+ def bytes_to_decimal(buffer, size_bytes)
167
+ size = read_size(buffer, size_bytes)
168
+ return nil unless size
169
+ buffer.read_decimal(size)
170
+ end
171
+
172
+ def bytes_to_double(buffer, size_bytes)
173
+ return nil unless read_size(buffer, size_bytes)
174
+ buffer.read_double
175
+ end
176
+
177
+ def bytes_to_float(buffer, size_bytes)
178
+ return nil unless read_size(buffer, size_bytes)
179
+ buffer.read_float
180
+ end
181
+
182
+ def bytes_to_int(buffer, size_bytes)
183
+ return nil unless read_size(buffer, size_bytes)
184
+ buffer.read_signed_int
185
+ end
186
+
187
+ def bytes_to_timestamp(buffer, size_bytes)
188
+ return nil unless read_size(buffer, size_bytes)
189
+ timestamp = buffer.read_long
190
+ Time.at(timestamp/1000.0)
191
+ end
192
+
193
+ def bytes_to_varchar(buffer, size_bytes)
194
+ bytes = size_bytes == 4 ? buffer.read_bytes : buffer.read_short_bytes
195
+ bytes ? bytes.force_encoding(::Encoding::UTF_8) : nil
196
+ end
197
+
198
+ def bytes_to_varint(buffer, size_bytes)
199
+ size = read_size(buffer, size_bytes)
200
+ return nil unless size
201
+ buffer.read_varint(size)
202
+ end
203
+
204
+ def bytes_to_uuid(buffer, size_bytes)
205
+ return nil unless read_size(buffer, size_bytes)
206
+ buffer.read_uuid
207
+ end
208
+
209
+ def bytes_to_timeuuid(buffer, size_bytes)
210
+ return nil unless read_size(buffer, size_bytes)
211
+ buffer.read_uuid(TimeUuid)
212
+ end
213
+
214
+ def bytes_to_inet(buffer, size_bytes)
215
+ size = read_size(buffer, size_bytes)
216
+ return nil unless size
217
+ IPAddr.new_ntoh(buffer.read(size))
218
+ end
219
+
220
+ def bytes_to_list(buffer, value_converter)
221
+ list = []
222
+ size = buffer.read_short
223
+ size.times do
224
+ list << value_converter.call(buffer, 2)
225
+ end
226
+ list
227
+ end
228
+
229
+ def bytes_to_map(buffer, key_converter, value_converter)
230
+ map = {}
231
+ size = buffer.read_short
232
+ size.times do
233
+ key = key_converter.call(buffer, 2)
234
+ value = value_converter.call(buffer, 2)
235
+ map[key] = value
236
+ end
237
+ map
238
+ end
239
+
240
+ def bytes_to_set(buffer, value_converter)
241
+ set = Set.new
242
+ size = buffer.read_short
243
+ size.times do
244
+ set << value_converter.call(buffer, 2)
245
+ end
246
+ set
247
+ end
248
+
249
+ def ascii_to_bytes(buffer, value, size_bytes)
250
+ v = value && value.encode(::Encoding::ASCII)
251
+ if size_bytes == 4
252
+ buffer.append_bytes(v)
253
+ else
254
+ buffer.append_short_bytes(v)
255
+ end
256
+ end
257
+
258
+ def bigint_to_bytes(buffer, value, size_bytes)
259
+ if value
260
+ size_to_bytes(buffer, 8, size_bytes)
261
+ buffer.append_long(value)
262
+ else
263
+ nil_to_bytes(buffer, size_bytes)
264
+ end
265
+ end
266
+
267
+ def blob_to_bytes(buffer, value, size_bytes)
268
+ v = value && value.encode(::Encoding::BINARY)
269
+ if size_bytes == 4
270
+ buffer.append_bytes(v)
271
+ else
272
+ buffer.append_short_bytes(v)
273
+ end
274
+ end
275
+
276
+ def boolean_to_bytes(buffer, value, size_bytes)
277
+ if !value.nil?
278
+ size_to_bytes(buffer, 1, size_bytes)
279
+ buffer.append(value ? Constants::TRUE_BYTE : Constants::FALSE_BYTE)
280
+ else
281
+ nil_to_bytes(buffer, size_bytes)
282
+ end
283
+ end
284
+
285
+ def decimal_to_bytes(buffer, value, size_bytes)
286
+ raw = value && CqlByteBuffer.new.append_decimal(value)
287
+ if size_bytes == 4
288
+ buffer.append_bytes(raw)
289
+ else
290
+ buffer.append_short_bytes(raw)
291
+ end
292
+ end
293
+
294
+ def double_to_bytes(buffer, value, size_bytes)
295
+ if value
296
+ size_to_bytes(buffer, 8, size_bytes)
297
+ buffer.append_double(value)
298
+ else
299
+ nil_to_bytes(buffer, size_bytes)
300
+ end
301
+ end
302
+
303
+ def float_to_bytes(buffer, value, size_bytes)
304
+ if value
305
+ size_to_bytes(buffer, 4, size_bytes)
306
+ buffer.append_float(value)
307
+ else
308
+ nil_to_bytes(buffer, size_bytes)
309
+ end
310
+ end
311
+
312
+ def inet_to_bytes(buffer, value, size_bytes)
313
+ if value
314
+ size_to_bytes(buffer, value.ipv6? ? 16 : 4, size_bytes)
315
+ buffer.append(value.hton)
316
+ else
317
+ nil_to_bytes(buffer, size_bytes)
318
+ end
319
+ end
320
+
321
+ def int_to_bytes(buffer, value, size_bytes)
322
+ if value
323
+ size_to_bytes(buffer, 4, size_bytes)
324
+ buffer.append_int(value)
325
+ else
326
+ nil_to_bytes(buffer, size_bytes)
327
+ end
328
+ end
329
+
330
+ def varchar_to_bytes(buffer, value, size_bytes)
331
+ v = value && value.encode(::Encoding::UTF_8)
332
+ if size_bytes == 4
333
+ buffer.append_bytes(v)
334
+ else
335
+ buffer.append_short_bytes(v)
336
+ end
337
+ end
338
+
339
+ def timestamp_to_bytes(buffer, value, size_bytes)
340
+ if value
341
+ ms = (value.to_f * 1000).to_i
342
+ size_to_bytes(buffer, 8, size_bytes)
343
+ buffer.append_long(ms)
344
+ else
345
+ nil_to_bytes(buffer, size_bytes)
346
+ end
347
+ end
348
+
349
+ def uuid_to_bytes(buffer, value, size_bytes)
350
+ if value
351
+ size_to_bytes(buffer, 16, size_bytes)
352
+ buffer.append_uuid(value)
353
+ else
354
+ nil_to_bytes(buffer, size_bytes)
355
+ end
356
+ end
357
+
358
+ def varint_to_bytes(buffer, value, size_bytes)
359
+ raw = value && CqlByteBuffer.new.append_varint(value)
360
+ if size_bytes == 4
361
+ buffer.append_bytes(raw)
362
+ else
363
+ buffer.append_short_bytes(raw)
364
+ end
365
+ end
366
+
367
+ def size_to_bytes(buffer, size, size_bytes)
368
+ if size_bytes == 4
369
+ buffer.append_int(size)
370
+ else
371
+ buffer.append_short(size)
372
+ end
373
+ end
374
+
375
+ def nil_to_bytes(buffer, size_bytes)
376
+ if size_bytes == 4
377
+ buffer.append_int(-1)
378
+ else
379
+ buffer.append_short(-1)
380
+ end
381
+ end
382
+ end
383
+ end
384
+ end