cassandra-driver 1.2.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -49,6 +49,8 @@ module Cassandra
49
49
 
50
50
  def execute(*args, &block)
51
51
  synchronize do
52
+ return if @term
53
+
52
54
  @tasks << Task.new(*args, &block)
53
55
  @cond.signal if @waiting > 0
54
56
  end
@@ -66,7 +66,6 @@ module Cassandra
66
66
  # @return [Symbol] distance to host. Must be one of
67
67
  # {Cassandra::LoadBalancing::DISTANCES}
68
68
  def distance(host)
69
- :ignore
70
69
  end
71
70
 
72
71
  # Load balancing plan is used to determine the order in which hosts
@@ -80,7 +79,6 @@ module Cassandra
80
79
  # @raise [NotImplementedError] override this method to return a plan
81
80
  # @return [Cassandra::LoadBalancing::Plan] a load balancing plan
82
81
  def plan(keyspace, statement, options)
83
- raise ::NotImplementedError, "must be implemented by a child"
84
82
  end
85
83
 
86
84
  # @return [String] a console-friendly representation of this policy
@@ -28,7 +28,6 @@ module Cassandra
28
28
 
29
29
  BYTES_FORMAT = 'C*'.freeze
30
30
  TWO_INTS_FORMAT = 'NN'.freeze
31
- HEADER_FORMAT = 'c4N'.freeze
32
31
  end
33
32
 
34
33
  module Constants
@@ -41,12 +40,15 @@ module Cassandra
41
40
  end
42
41
 
43
42
  require 'cassandra/protocol/cql_byte_buffer'
44
- require 'cassandra/protocol/type_converter'
45
43
  require 'cassandra/protocol/response'
46
44
  require 'cassandra/protocol/responses/auth_challenge_response'
47
45
  require 'cassandra/protocol/responses/auth_success_response'
48
46
  require 'cassandra/protocol/responses/error_response'
49
- require 'cassandra/protocol/responses/detailed_error_response'
47
+ require 'cassandra/protocol/responses/already_exists_error_response'
48
+ require 'cassandra/protocol/responses/read_timeout_error_response'
49
+ require 'cassandra/protocol/responses/unavailable_error_response'
50
+ require 'cassandra/protocol/responses/unprepared_error_response'
51
+ require 'cassandra/protocol/responses/write_timeout_error_response'
50
52
  require 'cassandra/protocol/responses/ready_response'
51
53
  require 'cassandra/protocol/responses/authenticate_response'
52
54
  require 'cassandra/protocol/responses/supported_response'
@@ -72,6 +74,6 @@ require 'cassandra/protocol/requests/query_request'
72
74
  require 'cassandra/protocol/requests/void_query_request'
73
75
  require 'cassandra/protocol/requests/prepare_request'
74
76
  require 'cassandra/protocol/requests/execute_request'
75
- require 'cassandra/protocol/frame_encoder'
76
- require 'cassandra/protocol/frame_decoder'
77
77
  require 'cassandra/protocol/cql_protocol_handler'
78
+ require 'cassandra/protocol/v1'
79
+ require 'cassandra/protocol/coder'
@@ -0,0 +1,509 @@
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
+ module Coder; extend self
22
+ GLOBAL_TABLES_SPEC_FLAG = 0x01
23
+ HAS_MORE_PAGES_FLAG = 0x02
24
+ NO_METADATA_FLAG = 0x04
25
+
26
+ def write_values_v1(buffer, values, types)
27
+ if values && values.size > 0
28
+ buffer.append_short(values.size)
29
+ values.each_with_index do |value, index|
30
+ write_value_v1(buffer, value, types[index])
31
+ end
32
+ buffer
33
+ else
34
+ buffer.append_short(0)
35
+ end
36
+ end
37
+
38
+ def write_value_v1(buffer, value, type)
39
+ case type
40
+ when :ascii then write_ascii(buffer, value)
41
+ when :bigint, :counter then write_bigint(buffer, value)
42
+ when :blob then write_blob(buffer, value)
43
+ when :boolean then write_boolean(buffer, value)
44
+ when :decimal then write_decimal(buffer, value)
45
+ when :double then write_double(buffer, value)
46
+ when :float then write_float(buffer, value)
47
+ when :int then write_int(buffer, value)
48
+ when :inet then write_inet(buffer, value)
49
+ when :varchar, :text then write_varchar(buffer, value)
50
+ when :timestamp then write_timestamp(buffer, value)
51
+ when :timeuuid, :uuid then write_uuid(buffer, value)
52
+ when :varint then write_varint(buffer, value)
53
+ when ::Array
54
+ case type.first
55
+ when :list, :set
56
+ if value
57
+ raw = CqlByteBuffer.new
58
+ value_type = type[1]
59
+
60
+ raw.append_short(value.size)
61
+ value.each do |element|
62
+ write_short_value(raw, element, value_type)
63
+ end
64
+
65
+ buffer.append_bytes(raw)
66
+ else
67
+ buffer.append_int(-1)
68
+ end
69
+ when :map
70
+ if value
71
+ raw = CqlByteBuffer.new
72
+ key_type = type[1]
73
+ value_type = type[2]
74
+
75
+ raw.append_short(value.size)
76
+ value.each do |key, value|
77
+ write_short_value(raw, key, key_type)
78
+ write_short_value(raw, value, value_type)
79
+ end
80
+
81
+ buffer.append_bytes(raw)
82
+ else
83
+ buffer.append_int(-1)
84
+ end
85
+ else
86
+ raise Errors::EncodingError, %(Unsupported value type: #{type})
87
+ end
88
+ else
89
+ raise Errors::EncodingError, %(Unsupported value type: #{type})
90
+ end
91
+ end
92
+
93
+ def read_values_v1(buffer, column_metadata)
94
+ ::Array.new(buffer.read_int) do |i|
95
+ row = ::Hash.new
96
+
97
+ column_metadata.each do |(_, _, column, type)|
98
+ row[column] = read_value_v1(buffer, type)
99
+ end
100
+
101
+ row
102
+ end
103
+ end
104
+
105
+ def read_value_v1(buffer, type)
106
+ case type
107
+ when :ascii then read_ascii(buffer)
108
+ when :bigint, :counter then read_bigint(buffer)
109
+ when :blob then buffer.read_bytes
110
+ when :boolean then read_boolean(buffer)
111
+ when :decimal then read_decimal(buffer)
112
+ when :double then read_double(buffer)
113
+ when :float then read_float(buffer)
114
+ when :int then read_int(buffer)
115
+ when :timestamp then read_timestamp(buffer)
116
+ when :varchar, :text then read_varchar(buffer)
117
+ when :varint then read_varint(buffer)
118
+ when :uuid then read_uuid(buffer)
119
+ when :timeuuid then read_uuid(buffer, TimeUuid)
120
+ when :inet then read_inet(buffer)
121
+ when ::Array
122
+ case type.first
123
+ when :list
124
+ return nil unless read_size(buffer)
125
+
126
+ value_type = type[1]
127
+ ::Array.new(buffer.read_short) { read_short_value(buffer, value_type) }
128
+ when :map
129
+ return nil unless read_size(buffer)
130
+
131
+ key_type = type[1]
132
+ value_type = type[2]
133
+
134
+ value = ::Hash.new
135
+
136
+ buffer.read_short.times do
137
+ value[read_short_value(buffer, key_type)] = read_short_value(buffer, value_type)
138
+ end
139
+
140
+ value
141
+ when :set
142
+ return nil unless read_size(buffer)
143
+
144
+ value_type = type[1]
145
+
146
+ value = ::Set.new
147
+
148
+ buffer.read_short.times do
149
+ value << read_short_value(buffer, value_type)
150
+ end
151
+
152
+ value
153
+ when :custom
154
+ buffer.read_bytes
155
+ else
156
+ raise Errors::DecodingError, %(Unsupported complex value type: #{type})
157
+ end
158
+ else
159
+ raise Errors::DecodingError, %(Unsupported value type: #{type})
160
+ end
161
+ end
162
+
163
+ def read_metadata_v1(buffer)
164
+ flags = buffer.read_int
165
+ count = buffer.read_int
166
+
167
+ paging_state = nil
168
+ paging_state = buffer.read_bytes if flags & HAS_MORE_PAGES_FLAG != 0
169
+ column_specs = nil
170
+
171
+ if flags & NO_METADATA_FLAG == 0
172
+ if flags & GLOBAL_TABLES_SPEC_FLAG != 0
173
+ keyspace_name = buffer.read_string
174
+ table_name = buffer.read_string
175
+
176
+ column_specs = ::Array.new(count) do |i|
177
+ [keyspace_name, table_name, buffer.read_string, read_type_v1(buffer)]
178
+ end
179
+ else
180
+ column_specs = ::Array.new(count) do |i|
181
+ [buffer.read_string, buffer.read_string, buffer.read_string, read_type_v1(buffer)]
182
+ end
183
+ end
184
+ end
185
+
186
+ [column_specs, paging_state]
187
+ end
188
+
189
+ def read_type_v1(buffer)
190
+ case buffer.read_unsigned_short
191
+ when 0x0000 then [:custom, buffer.read_string]
192
+ when 0x0001 then :ascii
193
+ when 0x0002 then :bigint
194
+ when 0x0003 then :blob
195
+ when 0x0004 then :boolean
196
+ when 0x0005 then :counter
197
+ when 0x0006 then :decimal
198
+ when 0x0007 then :double
199
+ when 0x0008 then :float
200
+ when 0x0009 then :int
201
+ when 0x000A then :text
202
+ when 0x000B then :timestamp
203
+ when 0x000C then :uuid
204
+ when 0x000D then :varchar
205
+ when 0x000E then :varint
206
+ when 0x000F then :timeuuid
207
+ when 0x0010 then :inet
208
+ when 0x0020 then [:list, read_type_v1(buffer)]
209
+ when 0x0021 then [:map, read_type_v1(buffer), read_type_v1(buffer)]
210
+ when 0x0022 then [:set, read_type_v1(buffer)]
211
+ else
212
+ raise Errors::DecodingError, %(Unsupported column type: #{id})
213
+ end
214
+ end
215
+
216
+ def read_ascii(buffer)
217
+ value = buffer.read_bytes
218
+ value && value.force_encoding(::Encoding::ASCII)
219
+ end
220
+
221
+ def read_bigint(buffer)
222
+ read_size(buffer) && buffer.read_long
223
+ end
224
+
225
+ alias :read_counter :read_bigint
226
+
227
+ def read_boolean(buffer)
228
+ read_size(buffer) && buffer.read(1) == Constants::TRUE_BYTE
229
+ end
230
+
231
+ def read_decimal(buffer)
232
+ size = read_size(buffer)
233
+ size && buffer.read_decimal(size)
234
+ end
235
+
236
+ def read_double(buffer)
237
+ read_size(buffer) && buffer.read_double
238
+ end
239
+
240
+ def read_float(buffer)
241
+ read_size(buffer) && buffer.read_float
242
+ end
243
+
244
+ def read_int(buffer)
245
+ read_size(buffer) && buffer.read_signed_int
246
+ end
247
+
248
+ def read_timestamp(buffer)
249
+ return nil unless read_size(buffer)
250
+
251
+ timestamp = buffer.read_long
252
+ seconds = timestamp / 1_000
253
+ microsenconds = (timestamp % 1_000) * 1_000
254
+
255
+ ::Time.at(seconds, microsenconds)
256
+ end
257
+
258
+ def read_uuid(buffer, klass = Uuid)
259
+ read_size(buffer) && buffer.read_uuid(klass)
260
+ end
261
+
262
+ def read_varchar(buffer)
263
+ value = buffer.read_bytes
264
+ value && value.force_encoding(::Encoding::UTF_8)
265
+ end
266
+
267
+ def read_varint(buffer)
268
+ size = read_size(buffer)
269
+ size && buffer.read_varint(size)
270
+ end
271
+
272
+ def read_inet(buffer)
273
+ size = read_size(buffer)
274
+ size && ::IPAddr.new_ntoh(buffer.read(size))
275
+ end
276
+
277
+ def write_ascii(buffer, value)
278
+ buffer.append_bytes(value && value.encode(::Encoding::ASCII))
279
+ end
280
+
281
+ def write_bigint(buffer, value)
282
+ if value
283
+ buffer.append_int(8)
284
+ buffer.append_long(value)
285
+ else
286
+ buffer.append_int(-1)
287
+ end
288
+ end
289
+
290
+ alias :write_counter :write_bigint
291
+
292
+ def write_blob(buffer, value)
293
+ buffer.append_bytes(value && value.encode(::Encoding::BINARY))
294
+ end
295
+
296
+ def write_boolean(buffer, value)
297
+ if !value.nil?
298
+ buffer.append_int(1)
299
+ buffer.append(value ? Constants::TRUE_BYTE : Constants::FALSE_BYTE)
300
+ else
301
+ buffer.append_int(-1)
302
+ end
303
+ end
304
+
305
+ def write_decimal(buffer, value)
306
+ buffer.append_bytes(value && CqlByteBuffer.new.append_decimal(value))
307
+ end
308
+
309
+ def write_double(buffer, value)
310
+ if value
311
+ buffer.append_int(8)
312
+ buffer.append_double(value)
313
+ else
314
+ buffer.append_int(-1)
315
+ end
316
+ end
317
+
318
+ def write_float(buffer, value)
319
+ if value
320
+ buffer.append_int(4)
321
+ buffer.append_float(value)
322
+ else
323
+ buffer.append_int(-1)
324
+ end
325
+ end
326
+
327
+ def write_int(buffer, value)
328
+ if value
329
+ buffer.append_int(4)
330
+ buffer.append_int(value)
331
+ else
332
+ buffer.append_int(-1)
333
+ end
334
+ end
335
+
336
+ def write_inet(buffer, value)
337
+ if value
338
+ buffer.append_int(value.ipv6? ? 16 : 4)
339
+ buffer.append(value.hton)
340
+ else
341
+ buffer.append_int(-1)
342
+ end
343
+ end
344
+
345
+ def write_timestamp(buffer, value)
346
+ if value
347
+ ms = (value.to_r.to_f * 1000).to_i
348
+ buffer.append_int(8)
349
+ buffer.append_long(ms)
350
+ else
351
+ buffer.append_int(-1)
352
+ end
353
+ end
354
+
355
+ def write_varchar(buffer, value)
356
+ buffer.append_bytes(value && value.encode(::Encoding::UTF_8))
357
+ end
358
+
359
+ def write_uuid(buffer, value)
360
+ if value
361
+ buffer.append_int(16)
362
+ buffer.append_uuid(value)
363
+ else
364
+ buffer.append_int(-1)
365
+ end
366
+ end
367
+
368
+ def write_varint(buffer, value)
369
+ buffer.append_bytes(value && CqlByteBuffer.new.append_varint(value))
370
+ end
371
+
372
+ def read_short_size(buffer)
373
+ size = buffer.read_short
374
+
375
+ return nil if size & 0x8000 == 0x8000 || (size == 0)
376
+
377
+ size
378
+ end
379
+
380
+ def read_short_value(buffer, type)
381
+ case type
382
+ when :ascii
383
+ value = buffer.read_short_bytes
384
+ value && value.force_encoding(::Encoding::ASCII)
385
+ when :bigint, :counter
386
+ read_short_size(buffer) && buffer.read_long
387
+ when :blob
388
+ value = buffer.read_short_bytes
389
+ value && value.force_encoding(::Encoding::BINARY)
390
+ when :boolean
391
+ read_short_size(buffer) && buffer.read(1) == Constants::TRUE_BYTE
392
+ when :decimal
393
+ size = read_short_size(buffer)
394
+ size && buffer.read_decimal(size)
395
+ when :double
396
+ read_short_size(buffer) && buffer.read_double
397
+ when :float
398
+ read_short_size(buffer) && buffer.read_float
399
+ when :int
400
+ read_short_size(buffer) && buffer.read_signed_int
401
+ when :inet
402
+ size = read_short_size(buffer)
403
+ size && ::IPAddr.new_ntoh(buffer.read(size))
404
+ when :varchar, :text
405
+ value = buffer.read_short_bytes
406
+ value && value.force_encoding(::Encoding::UTF_8)
407
+ when :timestamp
408
+ return nil unless read_short_size(buffer)
409
+
410
+ timestamp = buffer.read_long
411
+ seconds = timestamp / 1_000
412
+ microsenconds = (timestamp % 1_000) * 1_000
413
+
414
+ ::Time.at(seconds, microsenconds)
415
+ when :timeuuid
416
+ read_short_size(buffer) && buffer.read_uuid(TimeUuid)
417
+ when :uuid
418
+ read_short_size(buffer) && buffer.read_uuid
419
+ when :varint
420
+ size = read_short_size(buffer)
421
+ size && buffer.read_varint(size)
422
+ else
423
+ raise Errors::EncodingError, %(Unsupported short value type: #{type})
424
+ end
425
+ end
426
+
427
+ def write_short_value(buffer, value, type)
428
+ case type
429
+ when :ascii
430
+ buffer.append_short_bytes(value && value.encode(::Encoding::ASCII))
431
+ when :bigint, :counter
432
+ if value
433
+ buffer.append_short(8)
434
+ buffer.append_long(value)
435
+ else
436
+ buffer.append_short(-1)
437
+ end
438
+ when :blob
439
+ buffer.append_short_bytes(value && value.encode(::Encoding::BINARY))
440
+ when :boolean
441
+ if !value.nil?
442
+ buffer.append_short(1)
443
+ buffer.append(value ? Constants::TRUE_BYTE : Constants::FALSE_BYTE)
444
+ else
445
+ buffer.append_short(-1)
446
+ end
447
+ when :decimal
448
+ buffer.append_short_bytes(value && CqlByteBuffer.new.append_decimal(value))
449
+ when :double
450
+ if value
451
+ buffer.append_short(8)
452
+ buffer.append_double(value)
453
+ else
454
+ buffer.append_short(-1)
455
+ end
456
+ when :float
457
+ if value
458
+ buffer.append_short(4)
459
+ buffer.append_float(value)
460
+ else
461
+ buffer.append_short(-1)
462
+ end
463
+ when :inet
464
+ if value
465
+ buffer.append_short(value.ipv6? ? 16 : 4)
466
+ buffer.append(value.hton)
467
+ else
468
+ buffer.append_short(-1)
469
+ end
470
+ when :int
471
+ if value
472
+ buffer.append_short(4)
473
+ buffer.append_int(value)
474
+ else
475
+ buffer.append_short(-1)
476
+ end
477
+ when :varchar, :text
478
+ buffer.append_short_bytes(value && value.encode(::Encoding::UTF_8))
479
+ when :timestamp
480
+ if value
481
+ buffer.append_short(8)
482
+ buffer.append_long((value.to_f * 1000).to_i)
483
+ else
484
+ buffer.append_short(-1)
485
+ end
486
+ when :timeuuid, :uuid
487
+ if value
488
+ buffer.append_short(16)
489
+ buffer.append_uuid(value)
490
+ else
491
+ buffer.append_short(-1)
492
+ end
493
+ when :varint
494
+ buffer.append_short_bytes(value && CqlByteBuffer.new.append_varint(value))
495
+ else
496
+ raise Errors::EncodingError, %(Unsupported short value type: #{type})
497
+ end
498
+ end
499
+
500
+ def read_size(buffer)
501
+ size = buffer.read_signed_int
502
+
503
+ return nil if (size & 0x80000000 == 0x80000000) || (size == 0)
504
+
505
+ size
506
+ end
507
+ end
508
+ end
509
+ end