cassandra-driver 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +4 -4
  3. data/lib/cassandra.rb +5 -3
  4. data/lib/cassandra/cluster/client.rb +88 -95
  5. data/lib/cassandra/cluster/control_connection.rb +14 -13
  6. data/lib/cassandra/column.rb +1 -9
  7. data/lib/cassandra/execution/options.rb +24 -1
  8. data/lib/cassandra/executors.rb +2 -0
  9. data/lib/cassandra/load_balancing.rb +0 -2
  10. data/lib/cassandra/protocol.rb +7 -5
  11. data/lib/cassandra/protocol/coder.rb +509 -0
  12. data/lib/cassandra/protocol/cql_byte_buffer.rb +4 -0
  13. data/lib/cassandra/protocol/cql_protocol_handler.rb +38 -57
  14. data/lib/cassandra/protocol/requests/auth_response_request.rb +1 -1
  15. data/lib/cassandra/protocol/requests/batch_request.rb +35 -19
  16. data/lib/cassandra/protocol/requests/credentials_request.rb +1 -1
  17. data/lib/cassandra/protocol/requests/execute_request.rb +3 -16
  18. data/lib/cassandra/protocol/requests/options_request.rb +1 -1
  19. data/lib/cassandra/protocol/requests/prepare_request.rb +1 -1
  20. data/lib/cassandra/protocol/requests/query_request.rb +5 -61
  21. data/lib/cassandra/protocol/requests/register_request.rb +1 -1
  22. data/lib/cassandra/protocol/requests/startup_request.rb +1 -1
  23. data/lib/cassandra/protocol/response.rb +0 -9
  24. data/lib/cassandra/protocol/responses/already_exists_error_response.rb +40 -0
  25. data/lib/cassandra/protocol/responses/auth_challenge_response.rb +0 -4
  26. data/lib/cassandra/protocol/responses/auth_success_response.rb +1 -5
  27. data/lib/cassandra/protocol/responses/authenticate_response.rb +0 -4
  28. data/lib/cassandra/protocol/responses/error_response.rb +0 -12
  29. data/lib/cassandra/protocol/responses/event_response.rb +0 -8
  30. data/lib/cassandra/protocol/responses/prepared_result_response.rb +0 -10
  31. data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +1 -1
  32. data/lib/cassandra/protocol/responses/read_timeout_error_response.rb +42 -0
  33. data/lib/cassandra/protocol/responses/ready_response.rb +0 -4
  34. data/lib/cassandra/protocol/responses/result_response.rb +0 -7
  35. data/lib/cassandra/protocol/responses/rows_result_response.rb +0 -101
  36. data/lib/cassandra/protocol/responses/schema_change_event_response.rb +0 -3
  37. data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +0 -4
  38. data/lib/cassandra/protocol/responses/status_change_event_response.rb +0 -4
  39. data/lib/cassandra/protocol/responses/supported_response.rb +0 -4
  40. data/lib/cassandra/protocol/responses/unavailable_error_response.rb +41 -0
  41. data/lib/cassandra/protocol/responses/unprepared_error_response.rb +39 -0
  42. data/lib/cassandra/protocol/responses/void_result_response.rb +0 -4
  43. data/lib/cassandra/protocol/responses/write_timeout_error_response.rb +44 -0
  44. data/lib/cassandra/protocol/v1.rb +238 -0
  45. data/lib/cassandra/session.rb +95 -16
  46. data/lib/cassandra/statements/batch.rb +33 -6
  47. data/lib/cassandra/statements/bound.rb +3 -3
  48. data/lib/cassandra/statements/prepared.rb +38 -10
  49. data/lib/cassandra/statements/simple.rb +72 -3
  50. data/lib/cassandra/table.rb +2 -3
  51. data/lib/cassandra/util.rb +18 -0
  52. data/lib/cassandra/version.rb +1 -1
  53. metadata +8 -5
  54. data/lib/cassandra/protocol/frame_decoder.rb +0 -128
  55. data/lib/cassandra/protocol/frame_encoder.rb +0 -48
  56. data/lib/cassandra/protocol/responses/detailed_error_response.rb +0 -75
  57. data/lib/cassandra/protocol/type_converter.rb +0 -389
@@ -1,48 +0,0 @@
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
- # @private
22
- class FrameEncoder
23
- def initialize(protocol_version=1, compressor=nil)
24
- @protocol_version = protocol_version
25
- @compressor = compressor
26
- end
27
-
28
- def encode_frame(request, stream_id=0, buffer=nil)
29
- raise EncodingError, 'The stream ID must be between 0 and 127' unless 0 <= stream_id && stream_id < 128
30
- buffer ||= CqlByteBuffer.new
31
- flags = request.trace? ? 2 : 0
32
- body = request.write(@protocol_version, CqlByteBuffer.new)
33
- if @compressor && request.compressable? && @compressor.compress?(body)
34
- flags |= 1
35
- body = @compressor.compress(body)
36
- end
37
- header = [@protocol_version, flags, stream_id, request.opcode, body.bytesize]
38
- buffer << header.pack(Formats::HEADER_FORMAT)
39
- buffer << body
40
- buffer
41
- end
42
-
43
- def change_stream_id(new_stream_id, buffer, offset=0)
44
- buffer.update(offset + 2, new_stream_id.chr)
45
- end
46
- end
47
- end
48
- end
@@ -1,75 +0,0 @@
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 DetailedErrorResponse < ErrorResponse
22
- attr_reader :details
23
-
24
- def initialize(code, message, details)
25
- super(code, message)
26
- @details = details
27
- end
28
-
29
- def self.decode(code, message, protocol_version, buffer, length, trace_id=nil)
30
- details = {}
31
- case code
32
- when 0x1000 # unavailable
33
- details[:cl] = buffer.read_consistency
34
- details[:required] = buffer.read_int
35
- details[:alive] = buffer.read_int
36
- when 0x1100 # write_timeout
37
- details[:cl] = buffer.read_consistency
38
- details[:received] = buffer.read_int
39
- details[:blockfor] = buffer.read_int
40
- write_type = buffer.read_string
41
- write_type.downcase!
42
-
43
- details[:write_type] = write_type.to_sym
44
- when 0x1200 # read_timeout
45
- details[:cl] = buffer.read_consistency
46
- details[:received] = buffer.read_int
47
- details[:blockfor] = buffer.read_int
48
- details[:data_present] = buffer.read_byte != 0
49
- when 0x2400 # already_exists
50
- details[:ks] = buffer.read_string
51
- details[:table] = buffer.read_string
52
- when 0x2500
53
- details[:id] = buffer.read_short_bytes
54
- end
55
- new(code, message, details)
56
- end
57
-
58
- def to_error(statement = nil)
59
- case code
60
- when 0x1000 then Errors::UnavailableError.new(@message, statement, @details[:cl], @details[:required], @details[:alive])
61
- when 0x1100 then Errors::WriteTimeoutError.new(@message, statement, @details[:write_type], @details[:cl], @details[:blockfor], @details[:received])
62
- when 0x1200 then Errors::ReadTimeoutError.new(@message, statement, @details[:data_present], @details[:cl], @details[:blockfor], @details[:received])
63
- when 0x2400 then Errors::AlreadyExistsError.new(@message, statement, @details[:ks], @details[:table])
64
- when 0x2500 then Errors::UnpreparedError.new(@message, statement, @details[:id])
65
- else
66
- super
67
- end
68
- end
69
-
70
- def to_s
71
- "#{super} #{@details}"
72
- end
73
- end
74
- end
75
- end
@@ -1,389 +0,0 @@
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 EncodingError, '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 EncodingError, %(Unsupported column collection type: #{type.first})
79
- end
80
- else
81
- converter = @to_bytes_converters[type]
82
- unless converter
83
- raise EncodingError, %(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
- return nil if size.zero?
144
- size
145
- end
146
-
147
- def bytes_to_ascii(buffer, size_bytes)
148
- bytes = size_bytes == 4 ? buffer.read_bytes : buffer.read_short_bytes
149
- bytes ? bytes.force_encoding(::Encoding::ASCII) : nil
150
- end
151
-
152
- def bytes_to_bigint(buffer, size_bytes)
153
- return nil unless read_size(buffer, size_bytes)
154
- buffer.read_long
155
- end
156
-
157
- def bytes_to_blob(buffer, size_bytes)
158
- bytes = size_bytes == 4 ? buffer.read_bytes : buffer.read_short_bytes
159
- bytes ? bytes : nil
160
- end
161
-
162
- def bytes_to_boolean(buffer, size_bytes)
163
- return nil unless read_size(buffer, size_bytes)
164
- buffer.read(1) == Constants::TRUE_BYTE
165
- end
166
-
167
- def bytes_to_decimal(buffer, size_bytes)
168
- size = read_size(buffer, size_bytes)
169
- return nil unless size
170
- buffer.read_decimal(size)
171
- end
172
-
173
- def bytes_to_double(buffer, size_bytes)
174
- return nil unless read_size(buffer, size_bytes)
175
- buffer.read_double
176
- end
177
-
178
- def bytes_to_float(buffer, size_bytes)
179
- return nil unless read_size(buffer, size_bytes)
180
- buffer.read_float
181
- end
182
-
183
- def bytes_to_int(buffer, size_bytes)
184
- return nil unless read_size(buffer, size_bytes)
185
- buffer.read_signed_int
186
- end
187
-
188
- def bytes_to_timestamp(buffer, size_bytes)
189
- return nil unless read_size(buffer, size_bytes)
190
- timestamp = buffer.read_long
191
-
192
- seconds = timestamp / 1_000
193
- microsenconds = (timestamp % 1_000) * 1_000
194
-
195
- Time.at(seconds, microsenconds)
196
- end
197
-
198
- def bytes_to_varchar(buffer, size_bytes)
199
- bytes = size_bytes == 4 ? buffer.read_bytes : buffer.read_short_bytes
200
- bytes ? bytes.force_encoding(::Encoding::UTF_8) : nil
201
- end
202
-
203
- def bytes_to_varint(buffer, size_bytes)
204
- size = read_size(buffer, size_bytes)
205
- return nil unless size
206
- buffer.read_varint(size)
207
- end
208
-
209
- def bytes_to_uuid(buffer, size_bytes)
210
- return nil unless read_size(buffer, size_bytes)
211
- buffer.read_uuid
212
- end
213
-
214
- def bytes_to_timeuuid(buffer, size_bytes)
215
- return nil unless read_size(buffer, size_bytes)
216
- buffer.read_uuid(TimeUuid)
217
- end
218
-
219
- def bytes_to_inet(buffer, size_bytes)
220
- size = read_size(buffer, size_bytes)
221
- return nil unless size
222
- IPAddr.new_ntoh(buffer.read(size))
223
- end
224
-
225
- def bytes_to_list(buffer, value_converter)
226
- list = []
227
- size = buffer.read_short
228
- size.times do
229
- list << value_converter.call(buffer, 2)
230
- end
231
- list
232
- end
233
-
234
- def bytes_to_map(buffer, key_converter, value_converter)
235
- map = {}
236
- size = buffer.read_short
237
- size.times do
238
- key = key_converter.call(buffer, 2)
239
- value = value_converter.call(buffer, 2)
240
- map[key] = value
241
- end
242
- map
243
- end
244
-
245
- def bytes_to_set(buffer, value_converter)
246
- set = Set.new
247
- size = buffer.read_short
248
- size.times do
249
- set << value_converter.call(buffer, 2)
250
- end
251
- set
252
- end
253
-
254
- def ascii_to_bytes(buffer, value, size_bytes)
255
- v = value && value.encode(::Encoding::ASCII)
256
- if size_bytes == 4
257
- buffer.append_bytes(v)
258
- else
259
- buffer.append_short_bytes(v)
260
- end
261
- end
262
-
263
- def bigint_to_bytes(buffer, value, size_bytes)
264
- if value
265
- size_to_bytes(buffer, 8, size_bytes)
266
- buffer.append_long(value)
267
- else
268
- nil_to_bytes(buffer, size_bytes)
269
- end
270
- end
271
-
272
- def blob_to_bytes(buffer, value, size_bytes)
273
- v = value && value.encode(::Encoding::BINARY)
274
- if size_bytes == 4
275
- buffer.append_bytes(v)
276
- else
277
- buffer.append_short_bytes(v)
278
- end
279
- end
280
-
281
- def boolean_to_bytes(buffer, value, size_bytes)
282
- if !value.nil?
283
- size_to_bytes(buffer, 1, size_bytes)
284
- buffer.append(value ? Constants::TRUE_BYTE : Constants::FALSE_BYTE)
285
- else
286
- nil_to_bytes(buffer, size_bytes)
287
- end
288
- end
289
-
290
- def decimal_to_bytes(buffer, value, size_bytes)
291
- raw = value && CqlByteBuffer.new.append_decimal(value)
292
- if size_bytes == 4
293
- buffer.append_bytes(raw)
294
- else
295
- buffer.append_short_bytes(raw)
296
- end
297
- end
298
-
299
- def double_to_bytes(buffer, value, size_bytes)
300
- if value
301
- size_to_bytes(buffer, 8, size_bytes)
302
- buffer.append_double(value)
303
- else
304
- nil_to_bytes(buffer, size_bytes)
305
- end
306
- end
307
-
308
- def float_to_bytes(buffer, value, size_bytes)
309
- if value
310
- size_to_bytes(buffer, 4, size_bytes)
311
- buffer.append_float(value)
312
- else
313
- nil_to_bytes(buffer, size_bytes)
314
- end
315
- end
316
-
317
- def inet_to_bytes(buffer, value, size_bytes)
318
- if value
319
- size_to_bytes(buffer, value.ipv6? ? 16 : 4, size_bytes)
320
- buffer.append(value.hton)
321
- else
322
- nil_to_bytes(buffer, size_bytes)
323
- end
324
- end
325
-
326
- def int_to_bytes(buffer, value, size_bytes)
327
- if value
328
- size_to_bytes(buffer, 4, size_bytes)
329
- buffer.append_int(value)
330
- else
331
- nil_to_bytes(buffer, size_bytes)
332
- end
333
- end
334
-
335
- def varchar_to_bytes(buffer, value, size_bytes)
336
- v = value && value.encode(::Encoding::UTF_8)
337
- if size_bytes == 4
338
- buffer.append_bytes(v)
339
- else
340
- buffer.append_short_bytes(v)
341
- end
342
- end
343
-
344
- def timestamp_to_bytes(buffer, value, size_bytes)
345
- if value
346
- ms = (value.to_r.to_f * 1000).to_i
347
- size_to_bytes(buffer, 8, size_bytes)
348
- buffer.append_long(ms)
349
- else
350
- nil_to_bytes(buffer, size_bytes)
351
- end
352
- end
353
-
354
- def uuid_to_bytes(buffer, value, size_bytes)
355
- if value
356
- size_to_bytes(buffer, 16, size_bytes)
357
- buffer.append_uuid(value)
358
- else
359
- nil_to_bytes(buffer, size_bytes)
360
- end
361
- end
362
-
363
- def varint_to_bytes(buffer, value, size_bytes)
364
- raw = value && CqlByteBuffer.new.append_varint(value)
365
- if size_bytes == 4
366
- buffer.append_bytes(raw)
367
- else
368
- buffer.append_short_bytes(raw)
369
- end
370
- end
371
-
372
- def size_to_bytes(buffer, size, size_bytes)
373
- if size_bytes == 4
374
- buffer.append_int(size)
375
- else
376
- buffer.append_short(size)
377
- end
378
- end
379
-
380
- def nil_to_bytes(buffer, size_bytes)
381
- if size_bytes == 4
382
- buffer.append_int(-1)
383
- else
384
- buffer.append_short(-1)
385
- end
386
- end
387
- end
388
- end
389
- end