cassandra-driver 3.0.0.beta.1-java → 3.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +106 -39
  3. data/lib/cassandra.rb +396 -148
  4. data/lib/cassandra/address_resolution.rb +1 -1
  5. data/lib/cassandra/address_resolution/policies/ec2_multi_region.rb +1 -1
  6. data/lib/cassandra/address_resolution/policies/none.rb +1 -1
  7. data/lib/cassandra/aggregate.rb +21 -7
  8. data/lib/cassandra/argument.rb +2 -2
  9. data/lib/cassandra/attr_boolean.rb +33 -0
  10. data/lib/cassandra/auth.rb +6 -5
  11. data/lib/cassandra/auth/providers.rb +1 -1
  12. data/lib/cassandra/auth/providers/password.rb +5 -13
  13. data/lib/cassandra/cassandra_logger.rb +80 -0
  14. data/lib/cassandra/cluster.rb +49 -9
  15. data/lib/cassandra/cluster/client.rb +835 -209
  16. data/lib/cassandra/cluster/connection_pool.rb +2 -2
  17. data/lib/cassandra/cluster/connector.rb +86 -27
  18. data/lib/cassandra/cluster/control_connection.rb +222 -95
  19. data/lib/cassandra/cluster/failed_connection.rb +1 -1
  20. data/lib/cassandra/cluster/metadata.rb +14 -8
  21. data/lib/cassandra/cluster/options.rb +68 -22
  22. data/lib/cassandra/cluster/registry.rb +81 -17
  23. data/lib/cassandra/cluster/schema.rb +70 -8
  24. data/lib/cassandra/cluster/schema/cql_type_parser.rb +15 -10
  25. data/lib/cassandra/cluster/schema/fetchers.rb +601 -241
  26. data/lib/cassandra/cluster/schema/fqcn_type_parser.rb +39 -38
  27. data/lib/cassandra/cluster/schema/partitioners.rb +1 -1
  28. data/lib/cassandra/cluster/schema/partitioners/murmur3.rb +6 -8
  29. data/lib/cassandra/cluster/schema/partitioners/ordered.rb +1 -1
  30. data/lib/cassandra/cluster/schema/partitioners/random.rb +1 -1
  31. data/lib/cassandra/cluster/schema/replication_strategies.rb +1 -1
  32. data/lib/cassandra/cluster/schema/replication_strategies/network_topology.rb +19 -18
  33. data/lib/cassandra/cluster/schema/replication_strategies/none.rb +1 -1
  34. data/lib/cassandra/cluster/schema/replication_strategies/simple.rb +1 -1
  35. data/lib/cassandra/column.rb +4 -23
  36. data/lib/cassandra/column_container.rb +322 -0
  37. data/lib/cassandra/compression.rb +1 -1
  38. data/lib/cassandra/compression/compressors/lz4.rb +7 -8
  39. data/lib/cassandra/compression/compressors/snappy.rb +4 -3
  40. data/lib/cassandra/driver.rb +107 -46
  41. data/lib/cassandra/errors.rb +303 -52
  42. data/lib/cassandra/execution/info.rb +16 -5
  43. data/lib/cassandra/execution/options.rb +102 -55
  44. data/lib/cassandra/execution/trace.rb +16 -9
  45. data/lib/cassandra/executors.rb +1 -1
  46. data/lib/cassandra/function.rb +19 -13
  47. data/lib/cassandra/function_collection.rb +85 -0
  48. data/lib/cassandra/future.rb +101 -49
  49. data/lib/cassandra/host.rb +25 -5
  50. data/lib/cassandra/index.rb +118 -0
  51. data/lib/cassandra/keyspace.rb +169 -33
  52. data/lib/cassandra/listener.rb +1 -1
  53. data/lib/cassandra/load_balancing.rb +2 -2
  54. data/lib/cassandra/load_balancing/policies.rb +1 -1
  55. data/lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb +39 -25
  56. data/lib/cassandra/load_balancing/policies/round_robin.rb +8 -1
  57. data/lib/cassandra/load_balancing/policies/token_aware.rb +22 -13
  58. data/lib/cassandra/load_balancing/policies/white_list.rb +18 -5
  59. data/lib/cassandra/materialized_view.rb +90 -0
  60. data/lib/cassandra/null_logger.rb +27 -6
  61. data/lib/cassandra/protocol.rb +1 -1
  62. data/lib/cassandra/protocol/coder.rb +81 -42
  63. data/lib/cassandra/protocol/cql_byte_buffer.rb +58 -44
  64. data/lib/cassandra/protocol/cql_protocol_handler.rb +57 -54
  65. data/lib/cassandra/protocol/request.rb +6 -7
  66. data/lib/cassandra/protocol/requests/auth_response_request.rb +3 -3
  67. data/lib/cassandra/protocol/requests/batch_request.rb +17 -8
  68. data/lib/cassandra/protocol/requests/credentials_request.rb +3 -3
  69. data/lib/cassandra/protocol/requests/execute_request.rb +39 -20
  70. data/lib/cassandra/protocol/requests/options_request.rb +1 -1
  71. data/lib/cassandra/protocol/requests/prepare_request.rb +5 -5
  72. data/lib/cassandra/protocol/requests/query_request.rb +28 -23
  73. data/lib/cassandra/protocol/requests/register_request.rb +2 -2
  74. data/lib/cassandra/protocol/requests/startup_request.rb +8 -8
  75. data/lib/cassandra/protocol/requests/void_query_request.rb +1 -1
  76. data/lib/cassandra/protocol/response.rb +3 -4
  77. data/lib/cassandra/protocol/responses/already_exists_error_response.rb +12 -2
  78. data/lib/cassandra/protocol/responses/auth_challenge_response.rb +4 -5
  79. data/lib/cassandra/protocol/responses/auth_success_response.rb +4 -5
  80. data/lib/cassandra/protocol/responses/authenticate_response.rb +4 -5
  81. data/lib/cassandra/protocol/responses/error_response.rb +104 -17
  82. data/lib/cassandra/protocol/responses/event_response.rb +3 -4
  83. data/lib/cassandra/protocol/responses/function_failure_error_response.rb +13 -2
  84. data/lib/cassandra/protocol/responses/prepared_result_response.rb +14 -9
  85. data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +14 -9
  86. data/lib/cassandra/protocol/responses/read_failure_error_response.rb +26 -4
  87. data/lib/cassandra/protocol/responses/read_timeout_error_response.rb +22 -3
  88. data/lib/cassandra/protocol/responses/ready_response.rb +6 -7
  89. data/lib/cassandra/protocol/responses/result_response.rb +11 -10
  90. data/lib/cassandra/protocol/responses/rows_result_response.rb +8 -7
  91. data/lib/cassandra/protocol/responses/schema_change_event_response.rb +8 -8
  92. data/lib/cassandra/protocol/responses/schema_change_result_response.rb +19 -13
  93. data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +5 -6
  94. data/lib/cassandra/protocol/responses/status_change_event_response.rb +5 -6
  95. data/lib/cassandra/protocol/responses/supported_response.rb +4 -5
  96. data/lib/cassandra/protocol/responses/topology_change_event_response.rb +4 -5
  97. data/lib/cassandra/protocol/responses/unavailable_error_response.rb +20 -3
  98. data/lib/cassandra/protocol/responses/unprepared_error_response.rb +11 -2
  99. data/lib/cassandra/protocol/responses/void_result_response.rb +4 -5
  100. data/lib/cassandra/protocol/responses/write_failure_error_response.rb +26 -4
  101. data/lib/cassandra/protocol/responses/write_timeout_error_response.rb +22 -3
  102. data/lib/cassandra/protocol/v1.rb +98 -37
  103. data/lib/cassandra/protocol/v3.rb +121 -50
  104. data/lib/cassandra/protocol/v4.rb +172 -68
  105. data/lib/cassandra/reconnection.rb +1 -1
  106. data/lib/cassandra/reconnection/policies.rb +1 -1
  107. data/lib/cassandra/reconnection/policies/constant.rb +2 -4
  108. data/lib/cassandra/reconnection/policies/exponential.rb +6 -6
  109. data/lib/cassandra/result.rb +55 -20
  110. data/lib/cassandra/retry.rb +8 -8
  111. data/lib/cassandra/retry/policies.rb +1 -1
  112. data/lib/cassandra/retry/policies/default.rb +1 -1
  113. data/lib/cassandra/retry/policies/downgrading_consistency.rb +4 -2
  114. data/lib/cassandra/retry/policies/fallthrough.rb +1 -1
  115. data/lib/cassandra/session.rb +24 -16
  116. data/lib/cassandra/statement.rb +1 -1
  117. data/lib/cassandra/statements.rb +1 -1
  118. data/lib/cassandra/statements/batch.rb +16 -10
  119. data/lib/cassandra/statements/bound.rb +10 -3
  120. data/lib/cassandra/statements/prepared.rb +62 -18
  121. data/lib/cassandra/statements/simple.rb +23 -10
  122. data/lib/cassandra/statements/void.rb +1 -1
  123. data/lib/cassandra/table.rb +53 -185
  124. data/lib/cassandra/time.rb +11 -6
  125. data/lib/cassandra/time_uuid.rb +12 -14
  126. data/lib/cassandra/timestamp_generator.rb +37 -0
  127. data/lib/cassandra/timestamp_generator/simple.rb +38 -0
  128. data/lib/cassandra/timestamp_generator/ticking_on_duplicate.rb +58 -0
  129. data/lib/cassandra/tuple.rb +4 -4
  130. data/lib/cassandra/types.rb +109 -71
  131. data/lib/cassandra/udt.rb +66 -50
  132. data/lib/cassandra/util.rb +155 -15
  133. data/lib/cassandra/uuid.rb +20 -21
  134. data/lib/cassandra/uuid/generator.rb +7 -5
  135. data/lib/cassandra/version.rb +2 -2
  136. data/lib/cassandra_murmur3.jar +0 -0
  137. data/lib/datastax/cassandra.rb +1 -1
  138. metadata +27 -16
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2015 DataStax, Inc.
4
+ # Copyright 2013-2016 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -42,7 +42,7 @@ module Cassandra
42
42
  @klass == other.klass &&
43
43
  @options == other.options
44
44
  end
45
- alias :== :eql?
45
+ alias == eql?
46
46
  end
47
47
 
48
48
  # @return [String] this keyspace name
@@ -51,7 +51,14 @@ module Cassandra
51
51
  attr_reader :replication
52
52
 
53
53
  # @private
54
- def initialize(name, durable_writes, replication, tables, types, functions, aggregates)
54
+ def initialize(name,
55
+ durable_writes,
56
+ replication,
57
+ tables,
58
+ types,
59
+ functions,
60
+ aggregates,
61
+ views)
55
62
  @name = name
56
63
  @durable_writes = durable_writes
57
64
  @replication = replication
@@ -59,6 +66,15 @@ module Cassandra
59
66
  @types = types
60
67
  @functions = functions
61
68
  @aggregates = aggregates
69
+ @views = views
70
+
71
+ # Set the keyspace attribute on the tables and views.
72
+ @tables.each_value do |t|
73
+ t.set_keyspace(self)
74
+ end
75
+ @views.each_value do |v|
76
+ v.set_keyspace(self)
77
+ end
62
78
  end
63
79
 
64
80
  # @return [Boolean] whether durables writes are enabled for this keyspace
@@ -69,7 +85,7 @@ module Cassandra
69
85
  # @return [Boolean] whether this keyspace has a table with the given name
70
86
  # @param name [String] table name
71
87
  def has_table?(name)
72
- @tables.has_key?(name)
88
+ @tables.key?(name)
73
89
  end
74
90
 
75
91
  # @return [Cassandra::Table, nil] a table or nil
@@ -92,13 +108,41 @@ module Cassandra
92
108
  @tables.values
93
109
  end
94
110
  end
95
- alias :tables :each_table
111
+ alias tables each_table
112
+
113
+ # @return [Boolean] whether this keyspace has a materialized view with the given name
114
+ # @param name [String] materialized view name
115
+ def has_materialized_view?(name)
116
+ @views.key?(name)
117
+ end
118
+
119
+ # @return [Cassandra::MaterializedView, nil] a materialized view or nil
120
+ # @param name [String] materialized view name
121
+ def materialized_view(name)
122
+ @views[name]
123
+ end
124
+
125
+ # Yield or enumerate each materialized view defined in this keyspace
126
+ # @overload each_materialized_view
127
+ # @yieldparam view [Cassandra::MaterializedView] current materialized view
128
+ # @return [Cassandra::Keyspace] self
129
+ # @overload each_materialized_view
130
+ # @return [Array<Cassandra::MaterializedView>] a list of materialized views
131
+ def each_materialized_view(&block)
132
+ if block_given?
133
+ @views.each_value(&block)
134
+ self
135
+ else
136
+ @views.values
137
+ end
138
+ end
139
+ alias materialized_views each_materialized_view
96
140
 
97
141
  # @return [Boolean] whether this keyspace has a user-defined type with the
98
142
  # given name
99
143
  # @param name [String] user-defined type name
100
144
  def has_type?(name)
101
- @types.has_key?(name)
145
+ @types.key?(name)
102
146
  end
103
147
 
104
148
  # @return [Cassandra::Types::UserDefined, nil] a type or nil
@@ -121,13 +165,14 @@ module Cassandra
121
165
  @types.values
122
166
  end
123
167
  end
124
- alias :types :each_type
168
+ alias types each_type
125
169
 
126
- # @return [Boolean] whether this keyspace has a function with the given name and arguments
170
+ # @return [Boolean] whether this keyspace has a function with the given name and
171
+ # arguments
127
172
  # @param name [String] function name
128
173
  # @param args [Array<String>] (var-args style) function argument types
129
174
  def has_function?(name, *args)
130
- @functions.has_key?([name.downcase, args])
175
+ !@functions.get(name.downcase, args).nil?
131
176
  end
132
177
 
133
178
  # @return [Cassandra::Function, nil] a function or nil
@@ -136,7 +181,7 @@ module Cassandra
136
181
  def function(name, *args)
137
182
  # The functions_hash datastructure is a hash <[func-name, args], Function>.
138
183
  # So construct the array-key we're looking for.
139
- @functions[[name.downcase, args]]
184
+ @functions.get(name.downcase, args)
140
185
  end
141
186
 
142
187
  # Yield or enumerate each function defined in this keyspace
@@ -147,27 +192,27 @@ module Cassandra
147
192
  # @return [Array<Cassandra::Function>] a list of functions
148
193
  def each_function(&block)
149
194
  if block_given?
150
- @functions.each_value(&block)
195
+ @functions.each_function(&block)
151
196
  self
152
197
  else
153
- @functions.values
198
+ @functions.functions
154
199
  end
155
200
  end
156
- alias :functions :each_function
201
+ alias functions each_function
157
202
 
158
203
  # @return [Boolean] whether this keyspace has an aggregate with the given
159
204
  # name and arguments
160
205
  # @param name [String] aggregate name
161
206
  # @param args [Array<String>] (var-args style) aggregate function argument types
162
207
  def has_aggregate?(name, *args)
163
- @aggregates.has_key?([name.downcase, args])
208
+ !@aggregates.get(name.downcase, args).nil?
164
209
  end
165
210
 
166
211
  # @return [Cassandra::Aggregate, nil] an aggregate or nil
167
212
  # @param name [String] aggregate name
168
213
  # @param args [Array<String>] (var-args style) aggregate function argument types
169
214
  def aggregate(name, *args)
170
- @aggregates[[name.downcase, args]]
215
+ @aggregates.get(name.downcase, args)
171
216
  end
172
217
 
173
218
  # Yield or enumerate each aggregate defined in this keyspace
@@ -178,17 +223,19 @@ module Cassandra
178
223
  # @return [Array<Cassandra::Aggregate>] a list of aggregates
179
224
  def each_aggregate(&block)
180
225
  if block_given?
181
- @aggregates.each_value(&block)
226
+ @aggregates.each_function(&block)
182
227
  self
183
228
  else
184
- @aggregates.values
229
+ @aggregates.functions
185
230
  end
186
231
  end
187
- alias :aggregates :each_aggregate
232
+ alias aggregates each_aggregate
188
233
 
189
234
  # @return [String] a cql representation of this keyspace
190
235
  def to_cql
191
- "CREATE KEYSPACE #{Util.escape_name(@name)} WITH replication = #{@replication.to_cql} AND durable_writes = #{@durable_writes};"
236
+ "CREATE KEYSPACE #{Util.escape_name(@name)} " \
237
+ "WITH replication = #{@replication.to_cql} AND " \
238
+ "durable_writes = #{@durable_writes};"
192
239
  end
193
240
 
194
241
  # @private
@@ -202,67 +249,151 @@ module Cassandra
202
249
  @functions == other.raw_functions &&
203
250
  @aggregates == other.raw_aggregates
204
251
  end
205
- alias :== :eql?
252
+ alias == eql?
206
253
 
207
254
  # @private
208
255
  def inspect
209
- "#<#{self.class.name}:0x#{self.object_id.to_s(16)} @name=#{@name}>"
256
+ "#<#{self.class.name}:0x#{object_id.to_s(16)} @name=#{@name}>"
210
257
  end
211
258
 
212
259
  # @private
213
260
  def update_table(table)
214
261
  tables = @tables.dup
215
262
  tables[table.name] = table
216
- Keyspace.new(@name, @durable_writes, @replication, tables, @types, @functions, @aggregates)
263
+ Keyspace.new(@name,
264
+ @durable_writes,
265
+ @replication,
266
+ tables,
267
+ @types,
268
+ @functions,
269
+ @aggregates,
270
+ @views)
217
271
  end
218
272
 
219
273
  # @private
220
274
  def delete_table(table_name)
221
275
  tables = @tables.dup
222
276
  tables.delete(table_name)
223
- Keyspace.new(@name, @durable_writes, @replication, tables, @types, @functions, @aggregates)
277
+ Keyspace.new(@name,
278
+ @durable_writes,
279
+ @replication,
280
+ tables,
281
+ @types,
282
+ @functions,
283
+ @aggregates,
284
+ @views)
285
+ end
286
+
287
+ # @private
288
+ def update_materialized_view(view)
289
+ views = @views.dup
290
+ views[view.name] = view
291
+ Keyspace.new(@name,
292
+ @durable_writes,
293
+ @replication,
294
+ @tables,
295
+ @types,
296
+ @functions,
297
+ @aggregates,
298
+ views)
299
+ end
300
+
301
+ # @private
302
+ def delete_materialized_view(view_name)
303
+ views = @views.dup
304
+ views.delete(view_name)
305
+ Keyspace.new(@name,
306
+ @durable_writes,
307
+ @replication,
308
+ @tables,
309
+ @types,
310
+ @functions,
311
+ @aggregates,
312
+ views)
224
313
  end
225
314
 
226
315
  # @private
227
316
  def update_type(type)
228
317
  types = @types.dup
229
318
  types[type.name] = type
230
- Keyspace.new(@name, @durable_writes, @replication, @tables, types, @functions, @aggregates)
319
+ Keyspace.new(@name,
320
+ @durable_writes,
321
+ @replication,
322
+ @tables,
323
+ types,
324
+ @functions,
325
+ @aggregates,
326
+ @views)
231
327
  end
232
328
 
233
329
  # @private
234
330
  def delete_type(type_name)
235
331
  types = @types.dup
236
332
  types.delete(type_name)
237
- Keyspace.new(@name, @durable_writes, @replication, @tables, types, @functions, @aggregates)
333
+ Keyspace.new(@name,
334
+ @durable_writes,
335
+ @replication,
336
+ @tables,
337
+ types,
338
+ @functions,
339
+ @aggregates,
340
+ @views)
238
341
  end
239
342
 
240
343
  # @private
241
344
  def update_function(function)
242
345
  functions = @functions.dup
243
- functions[[function.name, function.argument_types]] = function
244
- Keyspace.new(@name, @durable_writes, @replication, @tables, @types, functions, @aggregates)
346
+ functions.add_or_update(function)
347
+ Keyspace.new(@name,
348
+ @durable_writes,
349
+ @replication,
350
+ @tables,
351
+ @types,
352
+ functions,
353
+ @aggregates,
354
+ @views)
245
355
  end
246
356
 
247
357
  # @private
248
358
  def delete_function(function_name, function_args)
249
359
  functions = @functions.dup
250
- functions.delete([function_name, function_args])
251
- Keyspace.new(@name, @durable_writes, @replication, @tables, @types, functions, @aggregates)
360
+ functions.delete(function_name, function_args)
361
+ Keyspace.new(@name,
362
+ @durable_writes,
363
+ @replication,
364
+ @tables,
365
+ @types,
366
+ functions,
367
+ @aggregates,
368
+ @views)
252
369
  end
253
370
 
254
371
  # @private
255
372
  def update_aggregate(aggregate)
256
373
  aggregates = @aggregates.dup
257
- aggregates[[aggregate.name, aggregate.argument_types]] = aggregate
258
- Keyspace.new(@name, @durable_writes, @replication, @tables, @types, @functions, aggregates)
374
+ aggregates.add_or_update(aggregate)
375
+ Keyspace.new(@name,
376
+ @durable_writes,
377
+ @replication,
378
+ @tables,
379
+ @types,
380
+ @functions,
381
+ aggregates,
382
+ @views)
259
383
  end
260
384
 
261
385
  # @private
262
386
  def delete_aggregate(aggregate_name, aggregate_args)
263
387
  aggregates = @aggregates.dup
264
- aggregates.delete([aggregate_name, aggregate_args])
265
- Keyspace.new(@name, @durable_writes, @replication, @tables, @types, @functions, aggregates)
388
+ aggregates.delete(aggregate_name, aggregate_args)
389
+ Keyspace.new(@name,
390
+ @durable_writes,
391
+ @replication,
392
+ @tables,
393
+ @types,
394
+ @functions,
395
+ aggregates,
396
+ @views)
266
397
  end
267
398
 
268
399
  # @private
@@ -276,6 +407,11 @@ module Cassandra
276
407
  @tables
277
408
  end
278
409
 
410
+ # @private
411
+ def raw_materialized_views
412
+ @views
413
+ end
414
+
279
415
  # @private
280
416
  def raw_types
281
417
  @types
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2015 DataStax, Inc.
4
+ # Copyright 2013-2016 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2015 DataStax, Inc.
4
+ # Copyright 2013-2016 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -83,7 +83,7 @@ module Cassandra
83
83
 
84
84
  # @private
85
85
  def inspect
86
- "#<#{self.class.name}:0x#{self.object_id.to_s(16)}>"
86
+ "#<#{self.class.name}:0x#{object_id.to_s(16)}>"
87
87
  end
88
88
  end
89
89
 
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2013-2015 DataStax, Inc.
2
+ # Copyright 2013-2016 DataStax, Inc.
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2015 DataStax, Inc.
4
+ # Copyright 2013-2016 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -20,6 +20,11 @@ module Cassandra
20
20
  module LoadBalancing
21
21
  module Policies
22
22
  class DCAwareRoundRobin < Policy
23
+ # @private
24
+ LOCAL_CONSISTENCIES = [:local_quorum, :local_one].freeze
25
+ # @private
26
+ EMPTY_ARRAY = [].freeze
27
+
23
28
  # @private
24
29
  class Plan
25
30
  def initialize(local, remote, index)
@@ -56,16 +61,25 @@ module Cassandra
56
61
 
57
62
  include MonitorMixin
58
63
 
59
- def initialize(datacenter = nil, max_remote_hosts_to_use = nil, use_remote_hosts_for_local_consistency = false)
60
- datacenter = datacenter && String(datacenter)
61
- max_remote_hosts_to_use = max_remote_hosts_to_use && Integer(max_remote_hosts_to_use)
64
+ def initialize(datacenter = nil,
65
+ max_remote_hosts_to_use = 0,
66
+ use_remote_hosts_for_local_consistency = false)
67
+ datacenter &&= String(datacenter)
68
+ max_remote_hosts_to_use &&= Integer(max_remote_hosts_to_use)
62
69
 
63
- unless datacenter.nil?
64
- Util.assert_not_empty(datacenter) { "datacenter cannot be empty" }
65
- end
70
+ Util.assert_not_empty(datacenter) { 'datacenter cannot be empty' } unless datacenter.nil?
66
71
 
67
72
  unless max_remote_hosts_to_use.nil?
68
- Util.assert(max_remote_hosts_to_use >= 0) { "max_remote_hosts_to_use must be nil or >= 0" }
73
+ Util.assert(max_remote_hosts_to_use >= 0) do
74
+ 'max_remote_hosts_to_use must be nil or >= 0'
75
+ end
76
+ end
77
+
78
+ # If use_remote* is true, max_remote* must be > 0
79
+ if use_remote_hosts_for_local_consistency
80
+ Util.assert(max_remote_hosts_to_use.nil? || max_remote_hosts_to_use > 0,
81
+ 'max_remote_hosts_to_use must be nil (meaning unlimited) or > 0 when ' \
82
+ 'use_remote_hosts_for_local_consistency is true')
69
83
  end
70
84
 
71
85
  @datacenter = datacenter
@@ -80,16 +94,12 @@ module Cassandra
80
94
  end
81
95
 
82
96
  def host_up(host)
83
- if !@datacenter && host.datacenter
84
- @datacenter = host.datacenter
85
- end
97
+ @datacenter = host.datacenter if !@datacenter && host.datacenter
86
98
 
87
99
  if host.datacenter.nil? || host.datacenter == @datacenter
88
100
  synchronize { @local = @local.dup.push(host) }
89
- else
90
- if @max_remote.nil? || @remote.size < @max_remote
91
- synchronize { @remote = @remote.dup.push(host) }
92
- end
101
+ elsif @max_remote.nil? || @remote.size < @max_remote
102
+ synchronize { @remote = @remote.dup.push(host) }
93
103
  end
94
104
 
95
105
  self
@@ -130,11 +140,11 @@ module Cassandra
130
140
  def plan(keyspace, statement, options)
131
141
  local = @local
132
142
 
133
- if LOCAL_CONSISTENCIES.include?(options.consistency) && !@use_remote
134
- remote = EMPTY_ARRAY
135
- else
136
- remote = @remote
137
- end
143
+ remote = if LOCAL_CONSISTENCIES.include?(options.consistency) && !@use_remote
144
+ EMPTY_ARRAY
145
+ else
146
+ @remote
147
+ end
138
148
 
139
149
  total = local.size + remote.size
140
150
 
@@ -146,12 +156,16 @@ module Cassandra
146
156
  Plan.new(local, remote, position)
147
157
  end
148
158
 
149
- private
150
-
151
159
  # @private
152
- LOCAL_CONSISTENCIES = [:local_quorum, :local_one].freeze
153
- # @private
154
- EMPTY_ARRAY = [].freeze
160
+ def inspect
161
+ "#<#{self.class.name}:0x#{object_id.to_s(16)} " \
162
+ "datacenter=#{@datacenter.inspect}, " \
163
+ "use_remote=#{@use_remote.inspect}, " \
164
+ "max_remote=#{@max_remote.inspect}, " \
165
+ "local=#{@local.inspect}, " \
166
+ "remote=#{@remote.inspect}, " \
167
+ "position=#{@position.inspect}>"
168
+ end
155
169
  end
156
170
  end
157
171
  end