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,54 @@
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 Client
21
+ # Represents metadata about a column in a query result set or prepared
22
+ # statement. Apart from the keyspace, table and column names there's also
23
+ # the type as a symbol (e.g. `:varchar`, `:int`, `:date`).
24
+ class ColumnMetadata
25
+ attr_reader :keyspace, :table, :column_name, :type
26
+
27
+ # @private
28
+ def initialize(*args)
29
+ @keyspace, @table, @column_name, @type = args
30
+ end
31
+
32
+ # @private
33
+ def to_ary
34
+ [@keyspace, @table, @column_name, @type]
35
+ end
36
+
37
+ def eql?(other)
38
+ self.keyspace == other.keyspace && self.table == other.table && self.column_name == other.column_name && self.type == other.type
39
+ end
40
+ alias_method :==, :eql?
41
+
42
+ def hash
43
+ @h ||= begin
44
+ h = 0
45
+ h = ((h & 33554431) * 31) ^ @keyspace.hash
46
+ h = ((h & 33554431) * 31) ^ @table.hash
47
+ h = ((h & 33554431) * 31) ^ @column_name.hash
48
+ h = ((h & 33554431) * 31) ^ @type.hash
49
+ h
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,72 @@
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 Client
21
+ # @private
22
+ class ConnectionManager
23
+ include Enumerable
24
+
25
+ def initialize
26
+ @connections = []
27
+ @lock = Mutex.new
28
+ end
29
+
30
+ def add_connections(connections)
31
+ @lock.synchronize do
32
+ @connections.concat(connections)
33
+ connections.each do |connection|
34
+ connection.on_closed do
35
+ @lock.synchronize do
36
+ @connections.delete(connection)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ def connected?
44
+ @lock.synchronize do
45
+ @connections.any?
46
+ end
47
+ end
48
+
49
+ def snapshot
50
+ @lock.synchronize do
51
+ @connections.dup
52
+ end
53
+ end
54
+
55
+ def random_connection
56
+ raise Errors::NotConnectedError unless connected?
57
+ @lock.synchronize do
58
+ @connections.sample
59
+ end
60
+ end
61
+
62
+ def each_connection(&callback)
63
+ return self unless block_given?
64
+ raise Errors::NotConnectedError unless connected?
65
+ @lock.synchronize do
66
+ @connections.each(&callback)
67
+ end
68
+ end
69
+ alias_method :each, :each_connection
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,272 @@
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 Client
21
+ # @private
22
+ class ClusterConnector
23
+ def initialize(sequence, logger)
24
+ @sequence = sequence
25
+ @logger = logger
26
+ end
27
+
28
+ def connect_all(hosts, connections_per_node)
29
+ connections = hosts.flat_map do |host|
30
+ Array.new(connections_per_node) do
31
+ f = @sequence.connect(host)
32
+ f.on_value { |connection| register_logging(connection) }
33
+ f.recover do |error|
34
+ @logger.warn('Failed connecting to node at %s: %s' % [host, error.message])
35
+ FailedConnection.new(error, host)
36
+ end
37
+ end
38
+ end
39
+ Ione::Future.all(*connections).map do |connections|
40
+ connected_connections = connections.select(&:connected?)
41
+ if connected_connections.empty?
42
+ e = connections.first.error
43
+ if e.is_a?(Cassandra::Errors::QueryError) && e.code == 0x100
44
+ e = Errors::AuthenticationError.new(e.message)
45
+ end
46
+ raise e
47
+ end
48
+ connected_connections
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def register_logging(connection)
55
+ args = [connection[:host_id], connection.host, connection.port, connection[:data_center]]
56
+ @logger.info('Connected to node %s at %s:%d in data center %s' % args)
57
+ connection.on_closed do |cause|
58
+ message = 'Connection to node %s at %s:%d in data center %s closed' % args
59
+ if cause
60
+ message << (' unexpectedly: %s' % cause.message)
61
+ @logger.warn(message)
62
+ else
63
+ @logger.info(message)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ # @private
70
+ class Connector
71
+ def initialize(steps)
72
+ @steps = steps.dup
73
+ end
74
+
75
+ def connect(host)
76
+ pending_connection = PendingConnection.new(host)
77
+ seed = Ione::Future.resolved(pending_connection)
78
+ f = @steps.reduce(seed) do |chain, step|
79
+ chain.flat_map do |pending_connection|
80
+ step.run(pending_connection)
81
+ end
82
+ end
83
+ f.map do |pending_connection|
84
+ pending_connection.connection
85
+ end
86
+ end
87
+ end
88
+
89
+ # @private
90
+ class ConnectStep
91
+ def initialize(io_reactor, protocol_handler_factory, port, connection_timeout, logger)
92
+ @io_reactor = io_reactor
93
+ @protocol_handler_factory = protocol_handler_factory
94
+ @port = port
95
+ @connection_timeout = connection_timeout
96
+ @logger = logger
97
+ end
98
+
99
+ def run(pending_connection)
100
+ @io_reactor.connect(pending_connection.host, @port, @connection_timeout, &@protocol_handler_factory).map do |connection|
101
+ pending_connection.with_connection(connection)
102
+ end
103
+ end
104
+ end
105
+
106
+ # @private
107
+ class CacheOptionsStep
108
+ def run(pending_connection)
109
+ f = pending_connection.execute(Protocol::OptionsRequest.new)
110
+ f.on_value do |supported_options|
111
+ pending_connection[:cql_version] = supported_options['CQL_VERSION']
112
+ pending_connection[:compression] = supported_options['COMPRESSION']
113
+ end
114
+ f.map(pending_connection)
115
+ end
116
+ end
117
+
118
+ # @private
119
+ class InitializeStep
120
+ def initialize(compressor, logger)
121
+ @compressor = compressor
122
+ @logger = logger
123
+ end
124
+
125
+ def run(pending_connection)
126
+ compression = @compressor && @compressor.algorithm
127
+ supported_algorithms = pending_connection[:compression]
128
+ if compression && !supported_algorithms.include?(compression)
129
+ @logger.warn(%[Compression algorithm "#{compression}" not supported (server supports "#{supported_algorithms.join('", "')}")])
130
+ compression = nil
131
+ end
132
+ @logger.debug('Using "%s" compression' % compression) if compression
133
+ supported_cql_versions = pending_connection[:cql_version]
134
+ cql_version = (supported_cql_versions && !supported_cql_versions.empty?) ? supported_cql_versions.first : '3.1.0'
135
+
136
+ f = pending_connection.execute(Protocol::StartupRequest.new(cql_version, compression))
137
+ f.map do |startup_response|
138
+ if startup_response.is_a?(AuthenticationRequired)
139
+ pending_connection.with_authentication_class(startup_response.authentication_class)
140
+ else
141
+ pending_connection
142
+ end
143
+ end
144
+ end
145
+ end
146
+
147
+ # @private
148
+ class SaslAuthenticationStep
149
+ def initialize(auth_provider)
150
+ @auth_provider = auth_provider
151
+ end
152
+
153
+ def run(pending_connection)
154
+ if pending_connection.authentication_class
155
+ begin
156
+ authenticator = @auth_provider && @auth_provider.create_authenticator(pending_connection.authentication_class)
157
+ if authenticator
158
+ token = authenticator.initial_response
159
+ challenge_cycle(pending_connection, authenticator, token)
160
+ elsif @auth_provider
161
+ Ione::Future.failed(Errors::AuthenticationError.new('Auth provider does not support the required authentication class "%s" and/or protocol version %d' % [pending_connection.authentication_class, @protocol_version]))
162
+ else
163
+ Ione::Future.failed(Errors::AuthenticationError.new('Server requested authentication, but no auth provider found'))
164
+ end
165
+ rescue => e
166
+ Ione::Future.failed(Errors::AuthenticationError.new('Auth provider raised an error: %s' % e.message))
167
+ end
168
+ else
169
+ Ione::Future.resolved(pending_connection)
170
+ end
171
+ end
172
+
173
+ def challenge_cycle(pending_connection, authenticator, response_token)
174
+ request = Protocol::AuthResponseRequest.new(response_token)
175
+ f = pending_connection.execute(request) { |raw_response| raw_response }
176
+ f.flat_map do |response|
177
+ case response
178
+ when Protocol::AuthChallengeResponse
179
+ token = authenticator.challenge_response(response.token)
180
+ challenge_cycle(pending_connection, authenticator, token)
181
+ when Protocol::AuthSuccessResponse
182
+ authenticator.authentication_successful(response.token) rescue nil
183
+ Ione::Future.resolved(pending_connection)
184
+ else
185
+ Ione::Future.resolved(pending_connection)
186
+ end
187
+ end
188
+ end
189
+ end
190
+
191
+ # @private
192
+ class CredentialsAuthenticationStep
193
+ def initialize(credentials)
194
+ @credentials = credentials
195
+ end
196
+
197
+ def run(pending_connection)
198
+ if pending_connection.authentication_class
199
+ if @credentials
200
+ request = Protocol::CredentialsRequest.new(@credentials)
201
+ pending_connection.execute(request).map(pending_connection)
202
+ else
203
+ Ione::Future.failed(Errors::AuthenticationError.new('Server requested authentication, but no credentials provided'))
204
+ end
205
+ else
206
+ Ione::Future.resolved(pending_connection)
207
+ end
208
+ end
209
+ end
210
+
211
+ # @private
212
+ class CachePropertiesStep
213
+ def run(pending_connection)
214
+ request = Protocol::QueryRequest.new('SELECT data_center, host_id FROM system.local', nil, nil, :one)
215
+ f = pending_connection.execute(request)
216
+ f.on_value do |result|
217
+ unless result.empty?
218
+ pending_connection[:host_id] = result.first['host_id']
219
+ pending_connection[:data_center] = result.first['data_center']
220
+ end
221
+ end
222
+ f.map(pending_connection)
223
+ end
224
+ end
225
+
226
+ # @private
227
+ class PendingConnection
228
+ attr_reader :host, :connection, :authentication_class
229
+
230
+ def initialize(host, connection=nil, authentication_class=nil)
231
+ @host = host
232
+ @connection = connection
233
+ @authentication_class = authentication_class
234
+ @request_runner = RequestRunner.new
235
+ end
236
+
237
+ def with_connection(connection)
238
+ self.class.new(host, connection, @authentication_class)
239
+ end
240
+
241
+ def with_authentication_class(authentication_class)
242
+ self.class.new(host, @connection, authentication_class)
243
+ end
244
+
245
+ def [](key)
246
+ @connection[key]
247
+ end
248
+
249
+ def []=(key, value)
250
+ @connection[key] = value
251
+ end
252
+
253
+ def execute(request, &block)
254
+ @request_runner.execute(@connection, request, nil, nil, &block)
255
+ end
256
+ end
257
+
258
+ # @private
259
+ class FailedConnection
260
+ attr_reader :error, :host
261
+
262
+ def initialize(error, host)
263
+ @error = error
264
+ @host = host
265
+ end
266
+
267
+ def connected?
268
+ false
269
+ end
270
+ end
271
+ end
272
+ end
@@ -0,0 +1,59 @@
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 Client
21
+ # @private
22
+ class ExecuteOptionsDecoder
23
+ def initialize(default_consistency)
24
+ @default_consistency = default_consistency
25
+ @default_options = {:consistency => @default_consistency}.freeze
26
+ end
27
+
28
+ def decode_options(*args)
29
+ if args.empty?
30
+ @default_options
31
+ elsif args.size == 1
32
+ decode_one(args.first)
33
+ else
34
+ args.each_with_object({}) do |options_or_consistency, result|
35
+ result.merge!(decode_one(options_or_consistency))
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def decode_one(options_or_consistency)
43
+ return @default_options unless options_or_consistency
44
+ case options_or_consistency
45
+ when Symbol
46
+ {:consistency => options_or_consistency}
47
+ when Hash
48
+ if options_or_consistency.include?(:consistency)
49
+ options_or_consistency
50
+ else
51
+ options = options_or_consistency.dup
52
+ options[:consistency] = @default_consistency
53
+ options
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end