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,46 @@
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 Statements
21
+ # a Bound statement is created using {Cassandra::Statements::Prepared#bind}
22
+ class Bound
23
+ include Statement
24
+
25
+ # @return [String] original cql used to prepare this statement
26
+ attr_reader :cql
27
+ # @return [Array<Object>] a list of positional parameters for the cql
28
+ attr_reader :params
29
+ # @private
30
+ attr_reader :params_metadata, :result_metadata
31
+
32
+ # @private
33
+ def initialize(cql, params_metadata, result_metadata, params)
34
+ @cql = cql
35
+ @params_metadata = params_metadata
36
+ @result_metadata = result_metadata
37
+ @params = params
38
+ end
39
+
40
+ # @return [String] a CLI-friendly bound statement representation
41
+ def inspect
42
+ "#<#{self.class.name}:0x#{self.object_id.to_s(16)} @cql=#{@cql.inspect} @params=#{@params}>"
43
+ end
44
+ end
45
+ end
46
+ 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 Statements
21
+ class Prepared
22
+ include Statement
23
+
24
+ # @return [String] original cql used to prepare this statement
25
+ attr_reader :cql
26
+ # @return [Cassandra::Execution::Info] execution info for PREPARE request
27
+ attr_reader :execution_info
28
+
29
+ # @private
30
+ attr_reader :params_metadata
31
+ # @private
32
+ attr_reader :result_metadata
33
+
34
+ # @private
35
+ def initialize(cql, params_metadata, result_metadata, execution_info)
36
+ @cql = cql
37
+ @params_metadata = params_metadata
38
+ @result_metadata = result_metadata
39
+ @execution_info = execution_info
40
+ end
41
+
42
+ # Creates a statement bound with specific arguments
43
+ # @param args [*Object] arguments to bind, must contain the same number
44
+ # of parameters as the number of positional arguments (`?`) in the
45
+ # original cql passed to {Cassandra::Session#prepare}
46
+ # @return [Cassandra::Statements::Bound] bound statement
47
+ def bind(*args)
48
+ raise ::ArgumentError, "expecting exactly #{@params_metadata.size} bind parameters, #{args.size} given" if args.size != @params_metadata.size
49
+
50
+ Bound.new(@cql, @params_metadata, @result_metadata, args)
51
+ end
52
+
53
+ # @return [String] a CLI-friendly prepared statement representation
54
+ def inspect
55
+ "#<#{self.class.name}:0x#{self.object_id.to_s(16)} @cql=#{@cql.inspect}>"
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,58 @@
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 Statements
21
+ class Simple
22
+ include Statement
23
+
24
+ # @return [String] original cql used to prepare this statement
25
+ attr_reader :cql
26
+ # @return [Array<Object>] a list of positional parameters for the cql
27
+ attr_reader :params
28
+
29
+ # @param cql [String] a cql statement
30
+ # @param params [*Object] positional arguments for the query
31
+ #
32
+ # @raise [ArgumentError] if cql statement given is not a String
33
+ def initialize(cql, *params)
34
+ unless cql.is_a?(::String)
35
+ raise ::ArgumentError, "cql must be a string, #{cql.inspect} given"
36
+ end
37
+
38
+ @cql = cql
39
+ @params = params
40
+ end
41
+
42
+ # @return [String] a CLI-friendly simple statement representation
43
+ def inspect
44
+ "#<#{self.class.name}:0x#{self.object_id.to_s(16)} @cql=#{@cql.inspect} @params=#{@params.inspect}>"
45
+ end
46
+
47
+ # @param other [Object, Cassandra::Statements::Simple] object to compare
48
+ # @return [Boolean] whether the statements are equal
49
+ def eql?(other)
50
+ other.is_a?(Simple) &&
51
+ @cql == other.cql &&
52
+ @params == other.params
53
+ end
54
+
55
+ alias :== :eql?
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,33 @@
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 Statements
21
+ # This statement is passed to {Cassandra::LoadBalancing::Policy#plan} when
22
+ # establishing connections and preparing statements
23
+ class Void
24
+ include Statement
25
+
26
+ # Returns nothing
27
+ # @return [nil] there is no cql for the void statement
28
+ def cql
29
+ nil
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,254 @@
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
+ # Represents a cassandra table
21
+ # @see Cassandra::Keyspace#each_table
22
+ # @see Cassandra::Keyspace#table
23
+ class Table
24
+ # @private
25
+ class Options
26
+ attr_reader :comment, :read_repair_chance, :local_read_repair_chance,
27
+ :gc_grace_seconds, :caching, :bloom_filter_fp_chance,
28
+ :populate_io_cache_on_flush, :memtable_flush_period_in_ms,
29
+ :default_time_to_live, :speculative_retry, :index_interval,
30
+ :replicate_on_write, :compaction_strategy, :compact_storage,
31
+ :compression_parameters
32
+
33
+ def initialize(data, compaction_strategy, compression_parameters, compact_storage, cassandra_version)
34
+ @comment = data['comment']
35
+ @read_repair_chance = data['read_repair_chance']
36
+ @local_read_repair_chance = data['local_read_repair_chance']
37
+ @gc_grace_seconds = data['gc_grace_seconds']
38
+ @caching = data['caching']
39
+ @bloom_filter_fp_chance = data['bloom_filter_fp_chance'] || 0.01
40
+ @populate_io_cache_on_flush = data['populate_io_cache_on_flush'] || false
41
+ @memtable_flush_period_in_ms = data['memtable_flush_period_in_ms'] || 0
42
+ @default_time_to_live = data['default_time_to_live'] || 0
43
+ @speculative_retry = data['speculative_retry'] || 'NONE'
44
+ @index_interval = data['index_interval'] || 128
45
+ @replicate_on_write = data['replicate_on_write'] || true
46
+ @compaction_strategy = compaction_strategy
47
+ @compression_parameters = compression_parameters
48
+ @compact_storage = compact_storage
49
+ @cassandra_version = cassandra_version
50
+ end
51
+
52
+ def replicate_on_write?
53
+ @replicate_on_write
54
+ end
55
+
56
+ def populate_io_cache_on_flush?
57
+ @populate_io_cache_on_flush
58
+ end
59
+
60
+ def compact_storage?
61
+ @compact_storage
62
+ end
63
+
64
+ def to_cql
65
+ options = []
66
+
67
+ options << 'COMPACT STORAGE' if @compact_storage
68
+ options << "bloom_filter_fp_chance = #{@bloom_filter_fp_chance}"
69
+ options << "caching = '#{@caching}'"
70
+ options << "comment = '#{@comment}'" if @comment
71
+ options << "compaction = #{@compaction_strategy.to_cql}"
72
+ options << "compression = #{Util.encode_hash(@compression_parameters)}"
73
+ options << "dclocal_read_repair_chance = #{@local_read_repair_chance}"
74
+ options << "default_time_to_live = #{@default_time_to_live}" if !@cassandra_version.start_with?('1')
75
+ options << "gc_grace_seconds = #{@gc_grace_seconds}"
76
+ options << "index_interval = #{@index_interval}" if !@cassandra_version.start_with?('1')
77
+ options << "populate_io_cache_on_flush = '#{@populate_io_cache_on_flush}'"
78
+ options << "read_repair_chance = #{@read_repair_chance}"
79
+ options << "replicate_on_write = '#{@replicate_on_write}'" if @cassandra_version.start_with?('1') || @cassandra_version.start_with?('2.0')
80
+ options << "speculative_retry = '#{@speculative_retry}'" if !@cassandra_version.start_with?('1')
81
+
82
+ options.join("\nAND ")
83
+ end
84
+
85
+ def eql?(other)
86
+ other.is_a?(Options) &&
87
+ @comment == other.comment &&
88
+ @read_repair_chance == other.read_repair_chance &&
89
+ @local_read_repair_chance == other.local_read_repair_chance &&
90
+ @gc_grace_seconds == other.gc_grace_seconds &&
91
+ @caching == other.caching &&
92
+ @bloom_filter_fp_chance == other.bloom_filter_fp_chance &&
93
+ @populate_io_cache_on_flush == other.populate_io_cache_on_flush &&
94
+ @memtable_flush_period_in_ms == other.memtable_flush_period_in_ms &&
95
+ @default_time_to_live == other.default_time_to_live &&
96
+ @speculative_retry == other.speculative_retry &&
97
+ @index_interval == other.index_interval &&
98
+ @replicate_on_write == other.replicate_on_write &&
99
+ @compaction_strategy == other.compaction_strategy &&
100
+ @compression_parameters == other.compression_parameters &&
101
+ @compact_storage == other.compact_storage &&
102
+ @cassandra_version == other.cassandra_version
103
+ end
104
+ alias :== :eql?
105
+
106
+ attr_reader :cassandra_version
107
+ protected :cassandra_version
108
+ end
109
+
110
+ # @private
111
+ class Compaction
112
+ attr_reader :klass, :options
113
+
114
+ def initialize(klass, options)
115
+ @klass = klass
116
+ @options = options
117
+ end
118
+
119
+ def to_cql
120
+ compaction = {'class' => @klass}
121
+ compaction.merge!(@options)
122
+
123
+ Util.encode_hash(compaction)
124
+ end
125
+
126
+ def eql?(other)
127
+ other.is_a?(Compaction) &&
128
+ @klass == other.klass &&
129
+ @options == other.options
130
+ end
131
+ alias :== :eql?
132
+ end
133
+
134
+ # @private
135
+ attr_reader :keyspace
136
+ # @return [String] table name
137
+ attr_reader :name
138
+ # @private
139
+ attr_reader :options
140
+
141
+ # @private
142
+ def initialize(keyspace, name, partition_key, clustering_columns, columns, options, clustering_order)
143
+ @keyspace = keyspace
144
+ @name = name
145
+ @partition_key = partition_key
146
+ @clustering_columns = clustering_columns
147
+ @columns = columns
148
+ @options = options
149
+ @clustering_order = clustering_order
150
+ end
151
+
152
+ # @param name [String] column name
153
+ # @return [Boolean] whether this table has a given column
154
+ def has_column?(name)
155
+ @columns.has_key?(name)
156
+ end
157
+
158
+ # @param name [String] column name
159
+ # @return [Cassandra::Column, nil] a column or nil
160
+ def column(name)
161
+ @columns[name]
162
+ end
163
+
164
+ # Yield or enumerate each column defined in this table
165
+ # @overload each_column
166
+ # @yieldparam column [Cassandra::Column] current column
167
+ # @return [Array<Cassandra::Column>] a list of columns
168
+ # @overload each_column
169
+ # @return [Enumerator<Cassandra::Column>] an enumerator
170
+ def each_column(&block)
171
+ @columns.values.each(&block)
172
+ end
173
+ alias :columns :each_column
174
+
175
+ # @return [String] a cql representation of this table
176
+ def to_cql
177
+ cql = "CREATE TABLE #{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)} (\n"
178
+ first = true
179
+ @columns.each do |(_, column)|
180
+ next if column.name.empty?
181
+ if first
182
+ first = false
183
+ else
184
+ cql << ",\n" unless first
185
+ end
186
+ cql << " #{column.to_cql}"
187
+ end
188
+ cql << ",\n PRIMARY KEY ("
189
+ if @partition_key.one?
190
+ cql << @partition_key.first.name
191
+ else
192
+ cql << '('
193
+ first = true
194
+ @partition_key.each do |column|
195
+ if first
196
+ first = false
197
+ else
198
+ cql << ', '
199
+ end
200
+ cql << column.name
201
+ end
202
+ cql << ')'
203
+ end
204
+ @clustering_columns.each do |column|
205
+ cql << ", #{column.name}"
206
+ end
207
+ cql << ')'
208
+
209
+ cql << "\n)\nWITH "
210
+
211
+ if @clustering_order.any? {|o| o != :asc}
212
+ cql << "CLUSTERING ORDER BY ("
213
+ first = true
214
+ @clustering_columns.zip(@clustering_order) do |column, order|
215
+ if first
216
+ first = false
217
+ else
218
+ cql << ', '
219
+ end
220
+ cql << "#{column.name} #{order.to_s.upcase}"
221
+ end
222
+ cql << ")\n AND "
223
+ end
224
+
225
+ cql << @options.to_cql.split("\n").join("\n ")
226
+
227
+ cql << ';'
228
+ end
229
+
230
+ # @return [Boolean] whether this table is equal to the other
231
+ def eql?(other)
232
+ other.is_a?(Table) &&
233
+ @keyspace == other.keyspace &&
234
+ @name == other.name &&
235
+ @partition_key == other.partition_key &&
236
+ @clustering_columns == other.clustering_columns &&
237
+ @columns == other.raw_columns &&
238
+ @options == other.options &&
239
+ @clustering_order == other.clustering_order
240
+ end
241
+ alias :== :eql?
242
+
243
+ # @private
244
+ attr_reader :partition_key, :clustering_columns, :clustering_order
245
+ protected :partition_key, :clustering_columns, :clustering_order
246
+
247
+ protected
248
+
249
+ # @private
250
+ def raw_columns
251
+ @columns
252
+ end
253
+ end
254
+ end