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
@@ -32,7 +32,7 @@ module Cassandra
32
32
  false
33
33
  end
34
34
 
35
- def write(protocol_version, buffer)
35
+ def write(buffer, protocol_version, encoder)
36
36
  buffer.append_string_map(@options)
37
37
  end
38
38
 
@@ -19,15 +19,6 @@
19
19
  module Cassandra
20
20
  module Protocol
21
21
  class Response
22
- def self.decode(opcode, protocol_version, buffer, length, trace_id)
23
- response_class = RESPONSE_TYPES[opcode]
24
- if response_class
25
- response_class.decode(protocol_version, buffer, length, trace_id)
26
- else
27
- raise Errors::DecodingError, "Unsupported opcode #{opcode.inspect}"
28
- end
29
- end
30
-
31
22
  private
32
23
 
33
24
  RESPONSE_TYPES = [
@@ -0,0 +1,40 @@
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 AlreadyExistsErrorResponse < ErrorResponse
22
+ attr_reader :keyspace, :table
23
+
24
+ def initialize(code, message, keyspace, table)
25
+ super(code, message)
26
+
27
+ @keyspace = keyspace
28
+ @table = table
29
+ end
30
+
31
+ def to_error(statement = nil)
32
+ Errors::AlreadyExistsError.new(@message, statement, @keyspace, @table)
33
+ end
34
+
35
+ def to_s
36
+ "#{super} #{@keyspace} #{@table}"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -21,10 +21,6 @@ module Cassandra
21
21
  class AuthChallengeResponse < Response
22
22
  attr_reader :token
23
23
 
24
- def self.decode(protocol_version, buffer, length, trace_id=nil)
25
- new(buffer.read_bytes)
26
- end
27
-
28
24
  def initialize(token)
29
25
  @token = token
30
26
  end
@@ -21,16 +21,12 @@ module Cassandra
21
21
  class AuthSuccessResponse < Response
22
22
  attr_reader :token
23
23
 
24
- def self.decode(protocol_version, buffer, length, trace_id=nil)
25
- new(buffer.read_bytes)
26
- end
27
-
28
24
  def initialize(token)
29
25
  @token = token
30
26
  end
31
27
 
32
28
  def to_s
33
- %(AUTH_SUCCESS #{@token.bytesize})
29
+ %(AUTH_SUCCESS #{@token && @token.bytesize})
34
30
  end
35
31
 
36
32
  private
@@ -21,10 +21,6 @@ module Cassandra
21
21
  class AuthenticateResponse < Response
22
22
  attr_reader :authentication_class
23
23
 
24
- def self.decode(protocol_version, buffer, length, trace_id=nil)
25
- new(buffer.read_string)
26
- end
27
-
28
24
  def initialize(authentication_class)
29
25
  @authentication_class = authentication_class
30
26
  end
@@ -25,18 +25,6 @@ module Cassandra
25
25
  @code, @message = args
26
26
  end
27
27
 
28
- def self.decode(protocol_version, buffer, length, trace_id=nil)
29
- code = buffer.read_int
30
- message = buffer.read_string
31
- case code
32
- when 0x1000, 0x1100, 0x1200, 0x2400, 0x2500
33
- new_length = length - 4 - 4 - message.bytesize
34
- DetailedErrorResponse.decode(code, message, protocol_version, buffer, new_length)
35
- else
36
- new(code, message)
37
- end
38
- end
39
-
40
28
  def to_s
41
29
  hex_code = @code.to_s(16).rjust(4, '0').upcase
42
30
  %(ERROR 0x#{hex_code} "#@message")
@@ -19,14 +19,6 @@
19
19
  module Cassandra
20
20
  module Protocol
21
21
  class EventResponse < ResultResponse
22
- def self.decode(protocol_version, buffer, length, trace_id=nil)
23
- type = buffer.read_string
24
- impl = EVENT_TYPES[type]
25
- raise Errors::DecodingError, %(Unsupported event type: "#{type}") unless impl
26
- new_length = length - 4 - type.bytesize
27
- impl.decode(protocol_version, buffer, new_length, trace_id)
28
- end
29
-
30
22
  private
31
23
 
32
24
  RESPONSE_TYPES[0x0c] = self
@@ -26,16 +26,6 @@ module Cassandra
26
26
  @id, @metadata, @result_metadata = id, metadata, result_metadata
27
27
  end
28
28
 
29
- def self.decode(protocol_version, buffer, length, trace_id=nil)
30
- id = buffer.read_short_bytes
31
- metadata, _ = RowsResultResponse.read_metadata(protocol_version, buffer)
32
- result_metadata = nil
33
- if protocol_version > 1
34
- result_metadata, _, _ = RowsResultResponse.read_metadata(protocol_version, buffer)
35
- end
36
- new(id, metadata, result_metadata, trace_id)
37
- end
38
-
39
29
  def eql?(other)
40
30
  self.id == other.id && self.metadata == other.metadata && self.trace_id == other.trace_id
41
31
  end
@@ -27,7 +27,7 @@ module Cassandra
27
27
 
28
28
  def materialize(metadata)
29
29
  @metadata = metadata
30
- @rows = RowsResultResponse.read_rows(@protocol_version, @raw_rows, @metadata)
30
+ @rows = Coder.read_values_v1(@raw_rows, @metadata)
31
31
  end
32
32
 
33
33
  def rows
@@ -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 ReadTimeoutErrorResponse < ErrorResponse
22
+ attr_reader :consistency, :received, :blockfor, :data_present
23
+
24
+ def initialize(code, message, consistency, received, blockfor, data_present)
25
+ super(code, message)
26
+
27
+ @consistency = consistency
28
+ @received = received
29
+ @blockfor = blockfor
30
+ @data_present = data_present
31
+ end
32
+
33
+ def to_error(statement = nil)
34
+ Errors::ReadTimeoutError.new(@message, statement, @data_present, @consistency, @blockfor, @received)
35
+ end
36
+
37
+ def to_s
38
+ "#{super} #{@consistency} #{@received} #{@blockfor} #{@data_present}"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -19,10 +19,6 @@
19
19
  module Cassandra
20
20
  module Protocol
21
21
  class ReadyResponse < Response
22
- def self.decode(protocol_version, buffer, length, trace_id=nil)
23
- new
24
- end
25
-
26
22
  def eql?(rs)
27
23
  self.class === rs
28
24
  end
@@ -25,13 +25,6 @@ module Cassandra
25
25
  @trace_id = trace_id
26
26
  end
27
27
 
28
- def self.decode(protocol_version, buffer, length, trace_id=nil)
29
- kind = buffer.read_int
30
- impl = RESULT_TYPES[kind]
31
- raise Errors::DecodingError, %(Unsupported result kind: #{kind}) unless impl
32
- impl.decode(protocol_version, buffer, length - 4, trace_id)
33
- end
34
-
35
28
  def void?
36
29
  false
37
30
  end
@@ -26,18 +26,6 @@ module Cassandra
26
26
  @rows, @metadata, @paging_state = rows, metadata, paging_state
27
27
  end
28
28
 
29
- def self.decode(protocol_version, buffer, length, trace_id=nil)
30
- original_buffer_length = buffer.length
31
- column_specs, columns_count, paging_state = read_metadata(protocol_version, buffer)
32
- if column_specs.nil?
33
- consumed_bytes = original_buffer_length - buffer.length
34
- remaining_bytes = CqlByteBuffer.new(buffer.read(length - consumed_bytes))
35
- RawRowsResultResponse.new(protocol_version, remaining_bytes, paging_state, trace_id)
36
- else
37
- new(read_rows(protocol_version, buffer, column_specs), column_specs, paging_state, trace_id)
38
- end
39
- end
40
-
41
29
  def to_s
42
30
  %(RESULT ROWS #@metadata #@rows)
43
31
  end
@@ -45,95 +33,6 @@ module Cassandra
45
33
  private
46
34
 
47
35
  RESULT_TYPES[0x02] = self
48
-
49
- COLUMN_TYPES = [
50
- nil,
51
- :ascii,
52
- :bigint,
53
- :blob,
54
- :boolean,
55
- :counter,
56
- :decimal,
57
- :double,
58
- :float,
59
- :int,
60
- :text,
61
- :timestamp,
62
- :uuid,
63
- :varchar,
64
- :varint,
65
- :timeuuid,
66
- :inet,
67
- ].freeze
68
-
69
- TYPE_CONVERTER = TypeConverter.new
70
-
71
- GLOBAL_TABLES_SPEC_FLAG = 0x01
72
- HAS_MORE_PAGES_FLAG = 0x02
73
- NO_METADATA_FLAG = 0x04
74
-
75
- def self.read_column_type(buffer)
76
- id, type = buffer.read_option do |id, b|
77
- if id > 0 && id <= 0x10
78
- COLUMN_TYPES[id]
79
- elsif id == 0x20
80
- sub_type = read_column_type(buffer)
81
- [:list, sub_type]
82
- elsif id == 0x21
83
- key_type = read_column_type(buffer)
84
- value_type = read_column_type(buffer)
85
- [:map, key_type, value_type]
86
- elsif id == 0x22
87
- sub_type = read_column_type(buffer)
88
- [:set, sub_type]
89
- else
90
- raise Errors::DecodingError, %(Unsupported column type: #{id})
91
- end
92
- end
93
- type
94
- end
95
-
96
- def self.read_metadata(protocol_version, buffer)
97
- flags = buffer.read_int
98
- columns_count = buffer.read_int
99
- paging_state = nil
100
- column_specs = nil
101
- if flags & HAS_MORE_PAGES_FLAG != 0
102
- paging_state = buffer.read_bytes
103
- end
104
- if flags & NO_METADATA_FLAG == 0
105
- if flags & GLOBAL_TABLES_SPEC_FLAG != 0
106
- global_keyspace_name = buffer.read_string
107
- global_table_name = buffer.read_string
108
- end
109
- column_specs = columns_count.times.map do
110
- if global_keyspace_name
111
- keyspace_name = global_keyspace_name
112
- table_name = global_table_name
113
- else
114
- keyspace_name = buffer.read_string
115
- table_name = buffer.read_string
116
- end
117
- column_name = buffer.read_string
118
- type = read_column_type(buffer)
119
- [keyspace_name, table_name, column_name, type]
120
- end
121
- end
122
- [column_specs, columns_count, paging_state]
123
- end
124
-
125
- def self.read_rows(protocol_version, buffer, column_specs)
126
- rows_count = buffer.read_int
127
- rows = []
128
- rows_count.times do |row_index|
129
- row = {}
130
- column_specs.each do |column_spec|
131
- row[column_spec[2]] = TYPE_CONVERTER.from_bytes(buffer, column_spec[3])
132
- end
133
- rows << row
134
- end
135
- rows
136
- end
137
36
  end
138
37
  end
139
38
  end
@@ -28,9 +28,6 @@ module Cassandra
28
28
  @type = TYPE
29
29
  end
30
30
 
31
- def self.decode(protocol_version, buffer, length, trace_id=nil)
32
- new(buffer.read_string, buffer.read_string, buffer.read_string)
33
- end
34
31
 
35
32
  def eql?(rs)
36
33
  rs.type == self.type && rs.change == self.change && rs.keyspace == self.keyspace && rs.table == self.table
@@ -26,10 +26,6 @@ module Cassandra
26
26
  @keyspace = keyspace
27
27
  end
28
28
 
29
- def self.decode(protocol_version, buffer, length, trace_id=nil)
30
- new(buffer.read_string, trace_id)
31
- end
32
-
33
29
  def to_s
34
30
  %(RESULT SET_KEYSPACE "#@keyspace")
35
31
  end
@@ -28,10 +28,6 @@ module Cassandra
28
28
  @type = TYPE
29
29
  end
30
30
 
31
- def self.decode(protocol_version, buffer, length, trace_id=nil)
32
- new(buffer.read_string, *buffer.read_inet)
33
- end
34
-
35
31
  def to_s
36
32
  %(EVENT #@type #@change #@address:#@port)
37
33
  end
@@ -25,10 +25,6 @@ module Cassandra
25
25
  @options = options
26
26
  end
27
27
 
28
- def self.decode(protocol_version, buffer, length, trace_id=nil)
29
- new(buffer.read_string_multimap)
30
- end
31
-
32
28
  def to_s
33
29
  %(SUPPORTED #{options})
34
30
  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 UnavailableErrorResponse < ErrorResponse
22
+ attr_reader :consistency, :required, :alive
23
+
24
+ def initialize(code, message, consistency, required, alive)
25
+ super(code, message)
26
+
27
+ @consistency = consistency
28
+ @required = required
29
+ @alive = alive
30
+ end
31
+
32
+ def to_error(statement = nil)
33
+ Errors::UnavailableError.new(@message, statement, @consistency, @required, @alive)
34
+ end
35
+
36
+ def to_s
37
+ "#{super} #{@consistency} #{@required} #{@alive}"
38
+ end
39
+ end
40
+ end
41
+ 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 UnpreparedErrorResponse < ErrorResponse
22
+ attr_reader :id
23
+
24
+ def initialize(code, message, id)
25
+ super(code, message)
26
+
27
+ @id = id
28
+ end
29
+
30
+ def to_error(statement = nil)
31
+ Errors::UnpreparedError.new(@message, statement, @id)
32
+ end
33
+
34
+ def to_s
35
+ "#{super} #{@id}"
36
+ end
37
+ end
38
+ end
39
+ end