yugabyte-ycql-driver 3.2.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +13 -0
  3. data/README.md +242 -0
  4. data/ext/cassandra_murmur3/cassandra_murmur3.c +178 -0
  5. data/ext/cassandra_murmur3/extconf.rb +2 -0
  6. data/lib/cassandra/address_resolution.rb +36 -0
  7. data/lib/cassandra/address_resolution/policies.rb +2 -0
  8. data/lib/cassandra/address_resolution/policies/ec2_multi_region.rb +56 -0
  9. data/lib/cassandra/address_resolution/policies/none.rb +35 -0
  10. data/lib/cassandra/aggregate.rb +123 -0
  11. data/lib/cassandra/argument.rb +51 -0
  12. data/lib/cassandra/attr_boolean.rb +33 -0
  13. data/lib/cassandra/auth.rb +100 -0
  14. data/lib/cassandra/auth/providers.rb +17 -0
  15. data/lib/cassandra/auth/providers/password.rb +65 -0
  16. data/lib/cassandra/cassandra_logger.rb +80 -0
  17. data/lib/cassandra/cluster.rb +331 -0
  18. data/lib/cassandra/cluster/client.rb +1612 -0
  19. data/lib/cassandra/cluster/connection_pool.rb +78 -0
  20. data/lib/cassandra/cluster/connector.rb +372 -0
  21. data/lib/cassandra/cluster/control_connection.rb +962 -0
  22. data/lib/cassandra/cluster/failed_connection.rb +35 -0
  23. data/lib/cassandra/cluster/metadata.rb +142 -0
  24. data/lib/cassandra/cluster/options.rb +145 -0
  25. data/lib/cassandra/cluster/registry.rb +284 -0
  26. data/lib/cassandra/cluster/schema.rb +405 -0
  27. data/lib/cassandra/cluster/schema/cql_type_parser.rb +112 -0
  28. data/lib/cassandra/cluster/schema/fetchers.rb +1627 -0
  29. data/lib/cassandra/cluster/schema/fqcn_type_parser.rb +175 -0
  30. data/lib/cassandra/cluster/schema/partitioners.rb +21 -0
  31. data/lib/cassandra/cluster/schema/partitioners/murmur3.rb +45 -0
  32. data/lib/cassandra/cluster/schema/partitioners/ordered.rb +37 -0
  33. data/lib/cassandra/cluster/schema/partitioners/random.rb +37 -0
  34. data/lib/cassandra/cluster/schema/replication_strategies.rb +21 -0
  35. data/lib/cassandra/cluster/schema/replication_strategies/network_topology.rb +102 -0
  36. data/lib/cassandra/cluster/schema/replication_strategies/none.rb +39 -0
  37. data/lib/cassandra/cluster/schema/replication_strategies/simple.rb +44 -0
  38. data/lib/cassandra/column.rb +66 -0
  39. data/lib/cassandra/column_container.rb +326 -0
  40. data/lib/cassandra/compression.rb +69 -0
  41. data/lib/cassandra/compression/compressors/lz4.rb +73 -0
  42. data/lib/cassandra/compression/compressors/snappy.rb +69 -0
  43. data/lib/cassandra/custom_data.rb +53 -0
  44. data/lib/cassandra/driver.rb +260 -0
  45. data/lib/cassandra/errors.rb +784 -0
  46. data/lib/cassandra/execution/info.rb +69 -0
  47. data/lib/cassandra/execution/options.rb +267 -0
  48. data/lib/cassandra/execution/profile.rb +153 -0
  49. data/lib/cassandra/execution/profile_manager.rb +71 -0
  50. data/lib/cassandra/execution/trace.rb +192 -0
  51. data/lib/cassandra/executors.rb +113 -0
  52. data/lib/cassandra/function.rb +156 -0
  53. data/lib/cassandra/function_collection.rb +85 -0
  54. data/lib/cassandra/future.rb +794 -0
  55. data/lib/cassandra/host.rb +102 -0
  56. data/lib/cassandra/index.rb +118 -0
  57. data/lib/cassandra/keyspace.rb +473 -0
  58. data/lib/cassandra/listener.rb +87 -0
  59. data/lib/cassandra/load_balancing.rb +121 -0
  60. data/lib/cassandra/load_balancing/policies.rb +20 -0
  61. data/lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb +172 -0
  62. data/lib/cassandra/load_balancing/policies/round_robin.rb +141 -0
  63. data/lib/cassandra/load_balancing/policies/token_aware.rb +149 -0
  64. data/lib/cassandra/load_balancing/policies/white_list.rb +100 -0
  65. data/lib/cassandra/materialized_view.rb +92 -0
  66. data/lib/cassandra/null_logger.rb +56 -0
  67. data/lib/cassandra/protocol.rb +102 -0
  68. data/lib/cassandra/protocol/coder.rb +1085 -0
  69. data/lib/cassandra/protocol/cql_byte_buffer.rb +418 -0
  70. data/lib/cassandra/protocol/cql_protocol_handler.rb +448 -0
  71. data/lib/cassandra/protocol/request.rb +41 -0
  72. data/lib/cassandra/protocol/requests/auth_response_request.rb +51 -0
  73. data/lib/cassandra/protocol/requests/batch_request.rb +117 -0
  74. data/lib/cassandra/protocol/requests/credentials_request.rb +51 -0
  75. data/lib/cassandra/protocol/requests/execute_request.rb +122 -0
  76. data/lib/cassandra/protocol/requests/options_request.rb +39 -0
  77. data/lib/cassandra/protocol/requests/prepare_request.rb +59 -0
  78. data/lib/cassandra/protocol/requests/query_request.rb +112 -0
  79. data/lib/cassandra/protocol/requests/register_request.rb +38 -0
  80. data/lib/cassandra/protocol/requests/startup_request.rb +49 -0
  81. data/lib/cassandra/protocol/requests/void_query_request.rb +24 -0
  82. data/lib/cassandra/protocol/response.rb +28 -0
  83. data/lib/cassandra/protocol/responses/already_exists_error_response.rb +50 -0
  84. data/lib/cassandra/protocol/responses/auth_challenge_response.rb +36 -0
  85. data/lib/cassandra/protocol/responses/auth_success_response.rb +36 -0
  86. data/lib/cassandra/protocol/responses/authenticate_response.rb +36 -0
  87. data/lib/cassandra/protocol/responses/error_response.rb +142 -0
  88. data/lib/cassandra/protocol/responses/event_response.rb +30 -0
  89. data/lib/cassandra/protocol/responses/function_failure_error_response.rb +52 -0
  90. data/lib/cassandra/protocol/responses/prepared_result_response.rb +62 -0
  91. data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +59 -0
  92. data/lib/cassandra/protocol/responses/read_failure_error_response.rb +71 -0
  93. data/lib/cassandra/protocol/responses/read_timeout_error_response.rb +61 -0
  94. data/lib/cassandra/protocol/responses/ready_response.rb +43 -0
  95. data/lib/cassandra/protocol/responses/result_response.rb +42 -0
  96. data/lib/cassandra/protocol/responses/rows_result_response.rb +39 -0
  97. data/lib/cassandra/protocol/responses/schema_change_event_response.rb +73 -0
  98. data/lib/cassandra/protocol/responses/schema_change_result_response.rb +70 -0
  99. data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +37 -0
  100. data/lib/cassandra/protocol/responses/status_change_event_response.rb +39 -0
  101. data/lib/cassandra/protocol/responses/supported_response.rb +36 -0
  102. data/lib/cassandra/protocol/responses/topology_change_event_response.rb +33 -0
  103. data/lib/cassandra/protocol/responses/unavailable_error_response.rb +58 -0
  104. data/lib/cassandra/protocol/responses/unprepared_error_response.rb +48 -0
  105. data/lib/cassandra/protocol/responses/void_result_response.rb +34 -0
  106. data/lib/cassandra/protocol/responses/write_failure_error_response.rb +73 -0
  107. data/lib/cassandra/protocol/responses/write_timeout_error_response.rb +63 -0
  108. data/lib/cassandra/protocol/v1.rb +326 -0
  109. data/lib/cassandra/protocol/v3.rb +358 -0
  110. data/lib/cassandra/protocol/v4.rb +478 -0
  111. data/lib/cassandra/reconnection.rb +49 -0
  112. data/lib/cassandra/reconnection/policies.rb +20 -0
  113. data/lib/cassandra/reconnection/policies/constant.rb +46 -0
  114. data/lib/cassandra/reconnection/policies/exponential.rb +79 -0
  115. data/lib/cassandra/result.rb +276 -0
  116. data/lib/cassandra/retry.rb +154 -0
  117. data/lib/cassandra/retry/policies.rb +21 -0
  118. data/lib/cassandra/retry/policies/default.rb +53 -0
  119. data/lib/cassandra/retry/policies/downgrading_consistency.rb +73 -0
  120. data/lib/cassandra/retry/policies/fallthrough.rb +39 -0
  121. data/lib/cassandra/session.rb +270 -0
  122. data/lib/cassandra/statement.rb +32 -0
  123. data/lib/cassandra/statements.rb +23 -0
  124. data/lib/cassandra/statements/batch.rb +146 -0
  125. data/lib/cassandra/statements/bound.rb +65 -0
  126. data/lib/cassandra/statements/prepared.rb +235 -0
  127. data/lib/cassandra/statements/simple.rb +118 -0
  128. data/lib/cassandra/statements/void.rb +38 -0
  129. data/lib/cassandra/table.rb +240 -0
  130. data/lib/cassandra/time.rb +103 -0
  131. data/lib/cassandra/time_uuid.rb +78 -0
  132. data/lib/cassandra/timestamp_generator.rb +37 -0
  133. data/lib/cassandra/timestamp_generator/simple.rb +38 -0
  134. data/lib/cassandra/timestamp_generator/ticking_on_duplicate.rb +58 -0
  135. data/lib/cassandra/trigger.rb +67 -0
  136. data/lib/cassandra/tuple.rb +131 -0
  137. data/lib/cassandra/types.rb +1704 -0
  138. data/lib/cassandra/udt.rb +443 -0
  139. data/lib/cassandra/util.rb +464 -0
  140. data/lib/cassandra/uuid.rb +110 -0
  141. data/lib/cassandra/uuid/generator.rb +212 -0
  142. data/lib/cassandra/version.rb +21 -0
  143. data/lib/datastax/cassandra.rb +47 -0
  144. data/lib/ycql.rb +842 -0
  145. metadata +243 -0
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 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
+ class Cluster
21
+ class Schema
22
+ # @private
23
+ module ReplicationStrategies
24
+ # @private
25
+ class None
26
+ def replication_map(token_hosts, token_ring, replication_options)
27
+ replication_map = ::Hash.new
28
+
29
+ token_hosts.each do |token, host|
30
+ replication_map[token] = [host].freeze
31
+ end
32
+
33
+ replication_map
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 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
+ class Cluster
21
+ class Schema
22
+ # @private
23
+ module ReplicationStrategies
24
+ # @private
25
+ class Simple
26
+ def replication_map(token_hosts, token_ring, replication_options)
27
+ factor = Integer(replication_options['replication_factor'])
28
+ size = token_ring.size
29
+ factor = size if size < factor
30
+ replication_map = ::Hash.new
31
+
32
+ token_ring.each_with_index do |token, i|
33
+ replication_map[token] = factor.times.map do |j|
34
+ token_hosts[token_ring[(i + j) % size]]
35
+ end.freeze
36
+ end
37
+
38
+ replication_map
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 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 column
21
+ # @see Cassandra::Table#each_column
22
+ # @see Cassandra::Table#column
23
+ class Column
24
+ # @return [String] column name
25
+ attr_reader :name
26
+ # @return [Cassandra::Type] column type
27
+ attr_reader :type
28
+ # @return [Symbol] column order (`:asc` or `:desc`)
29
+ attr_reader :order
30
+
31
+ # @private
32
+ def initialize(name, type, order, is_static = false, is_frozen = false)
33
+ @name = name
34
+ @type = type
35
+ @order = order
36
+ @static = is_static
37
+ @frozen = is_frozen
38
+ end
39
+
40
+ # @return [Boolean] whether the column is static
41
+ def static?
42
+ @static
43
+ end
44
+
45
+ # @return [Boolean] whether the column is frozen
46
+ def frozen?
47
+ @frozen
48
+ end
49
+
50
+ # @private
51
+ def inspect
52
+ "#<#{self.class.name}:0x#{object_id.to_s(16)} @name=#{@name} @type=#{@type}>"
53
+ end
54
+
55
+ # @private
56
+ def eql?(other)
57
+ other.is_a?(Column) &&
58
+ @name == other.name &&
59
+ @type == other.type &&
60
+ @order == other.order &&
61
+ @static == other.static? &&
62
+ @frozen == other.frozen?
63
+ end
64
+ alias == eql?
65
+ end
66
+ end
@@ -0,0 +1,326 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 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
+ # This class contains all the logic needed for manipulating columns of an object.
21
+ class ColumnContainer
22
+ # Encapsulates all of the configuration options of a column-container.
23
+ class Options
24
+ # @return [String] the comment attribute of this column-container.
25
+ attr_reader :comment
26
+ # @return [Float] the chance with which a read repair is triggered for this column-container.
27
+ attr_reader :read_repair_chance
28
+ # @return [Float] the cluster local read repair chance for this column-container.
29
+ attr_reader :local_read_repair_chance
30
+ # @return [Integer] the tombstone garbage collection grace time in seconds for this column-container.
31
+ attr_reader :gc_grace_seconds
32
+ # @return [Hash] the caching options for this column-container.
33
+ attr_reader :caching
34
+ # @return [Float] the false positive chance for the Bloom filter of this column-container.
35
+ attr_reader :bloom_filter_fp_chance
36
+ # @return [Integer] how often (in milliseconds) to flush the memtable of this column-container.
37
+ attr_reader :memtable_flush_period_in_ms
38
+ # @return [Integer] the default TTL for this column-container.
39
+ attr_reader :default_time_to_live
40
+ # Return the speculative retry setting of this column-container, which determines how much
41
+ # response delay the coordinator node will tolerate from the chosen replica before
42
+ # retrying the request on other replicas. This setting can be expressed as a fixed
43
+ # delay in ms (e.g. 10ms) or as a percentile indicating "when the response time has
44
+ # exceeded the Nth percentile of read response times for this object" (e.g. 99percentile).
45
+ # @return [String] the speculative retry setting of this column-container.
46
+ attr_reader :speculative_retry
47
+ # Return the index interval of this column-container; Cassandra will hold `1/index_interval` of row keys in
48
+ # memory.
49
+ # @return [Integer] the index interval of this column-container. May be nil, indicating a default value of 128.
50
+ attr_reader :index_interval
51
+ # @return [Hash] compression settings
52
+ attr_reader :compression
53
+ # When compression is enabled, this option defines the probability
54
+ # with which checksums for compressed blocks are checked during reads.
55
+ # @return [Float] the probability of checking checksums on compressed blocks.
56
+ attr_reader :crc_check_chance
57
+ # @return [Hash] the extension options of this column-container.
58
+ attr_reader :extensions
59
+
60
+ # @return [ColumnContainer::Compaction] the compaction strategy of this column-container.
61
+ attr_reader :compaction_strategy
62
+
63
+ # @return whether or not change data capture is enabled on this table.
64
+ attr_reader :cdc
65
+
66
+ # @private
67
+ def initialize(comment,
68
+ read_repair_chance,
69
+ local_read_repair_chance,
70
+ gc_grace_seconds,
71
+ caching,
72
+ bloom_filter_fp_chance,
73
+ populate_io_cache_on_flush,
74
+ memtable_flush_period_in_ms,
75
+ default_time_to_live,
76
+ speculative_retry,
77
+ index_interval,
78
+ replicate_on_write,
79
+ min_index_interval,
80
+ max_index_interval,
81
+ compaction_strategy,
82
+ compression,
83
+ compact_storage,
84
+ crc_check_chance,
85
+ extensions,
86
+ cdc)
87
+ @comment = comment
88
+ @read_repair_chance = read_repair_chance
89
+ @local_read_repair_chance = local_read_repair_chance
90
+ @gc_grace_seconds = gc_grace_seconds
91
+ @caching = caching
92
+ @bloom_filter_fp_chance = bloom_filter_fp_chance
93
+ @populate_io_cache_on_flush = populate_io_cache_on_flush
94
+ @memtable_flush_period_in_ms = memtable_flush_period_in_ms
95
+ @default_time_to_live = default_time_to_live
96
+ @speculative_retry = speculative_retry
97
+ @index_interval = index_interval
98
+ @replicate_on_write = replicate_on_write
99
+ @min_index_interval = min_index_interval
100
+ @max_index_interval = max_index_interval
101
+ @compaction_strategy = compaction_strategy
102
+ @compression = compression
103
+ @compact_storage = compact_storage
104
+ @crc_check_chance = crc_check_chance
105
+ @extensions = extensions
106
+ @cdc = cdc
107
+ end
108
+
109
+ # Return whether to replicate counter updates to other replicas. It is *strongly* recommended
110
+ # that this setting be `true`. Otherwise, counter updates are only written to one replica
111
+ # and fault tolerance is sacrificed.
112
+ # @return [Boolean] whether to replicate counter updates to other replicas.
113
+ def replicate_on_write?
114
+ @replicate_on_write
115
+ end
116
+
117
+ # @return [Boolean] whether to populate the I/O cache on flush of this
118
+ # column-container. May be nil, indicating a default value of `false`.
119
+ def populate_io_cache_on_flush?
120
+ @populate_io_cache_on_flush
121
+ end
122
+
123
+ # @return [Boolean] whether this column-container uses compact storage.
124
+ def compact_storage?
125
+ @compact_storage
126
+ end
127
+
128
+ # @private
129
+ def to_cql
130
+ options = []
131
+
132
+ options << 'COMPACT STORAGE' if @compact_storage
133
+ unless @bloom_filter_fp_chance.nil?
134
+ options << "bloom_filter_fp_chance = #{Util.encode_object(@bloom_filter_fp_chance)}"
135
+ end
136
+ options << "caching = #{Util.encode_object(@caching)}" unless @caching.nil?
137
+ options << 'cdc = true' if @cdc
138
+ options << "comment = #{Util.encode_object(@comment)}" unless @comment.nil?
139
+ options << "compaction = #{@compaction_strategy.to_cql}" unless @compaction_strategy.nil?
140
+ options << "compression = #{Util.encode_object(@compression)}" unless @compression.nil?
141
+ options << "crc_check_chance = #{Util.encode_object(@crc_check_chance)}" unless @crc_check_chance.nil?
142
+ unless @local_read_repair_chance.nil?
143
+ options << "dclocal_read_repair_chance = #{Util.encode_object(@local_read_repair_chance)}"
144
+ end
145
+ unless @default_time_to_live.nil?
146
+ options << "default_time_to_live = #{Util.encode_object(@default_time_to_live)}"
147
+ end
148
+ options << "gc_grace_seconds = #{Util.encode_object(@gc_grace_seconds)}" unless @gc_grace_seconds.nil?
149
+ options << "index_interval = #{Util.encode_object(@index_interval)}" unless @index_interval.nil?
150
+ options << "max_index_interval = #{Util.encode_object(@max_index_interval)}" unless @max_index_interval.nil?
151
+ unless @memtable_flush_period_in_ms.nil?
152
+ options << "memtable_flush_period_in_ms = #{Util.encode_object(@memtable_flush_period_in_ms)}"
153
+ end
154
+ options << "min_index_interval = #{Util.encode_object(@min_index_interval)}" unless @min_index_interval.nil?
155
+ unless @populate_io_cache_on_flush.nil?
156
+ options << "populate_io_cache_on_flush = '#{@populate_io_cache_on_flush}'"
157
+ end
158
+ options << "read_repair_chance = #{Util.encode_object(@read_repair_chance)}" unless @read_repair_chance.nil?
159
+ options << "replicate_on_write = '#{@replicate_on_write}'" unless @replicate_on_write.nil?
160
+ options << "speculative_retry = #{Util.encode_object(@speculative_retry)}" unless @speculative_retry.nil?
161
+ options.join("\nAND ")
162
+ end
163
+
164
+ # @private
165
+ def eql?(other)
166
+ other.is_a?(Options) &&
167
+ @comment == other.comment &&
168
+ @read_repair_chance == other.read_repair_chance &&
169
+ @local_read_repair_chance == other.local_read_repair_chance &&
170
+ @gc_grace_seconds == other.gc_grace_seconds &&
171
+ @caching == other.caching &&
172
+ @bloom_filter_fp_chance == other.bloom_filter_fp_chance &&
173
+ @populate_io_cache_on_flush == other.populate_io_cache_on_flush? &&
174
+ @memtable_flush_period_in_ms == other.memtable_flush_period_in_ms &&
175
+ @default_time_to_live == other.default_time_to_live &&
176
+ @speculative_retry == other.speculative_retry &&
177
+ @index_interval == other.index_interval &&
178
+ @replicate_on_write == other.replicate_on_write? &&
179
+ @compaction_strategy == other.compaction_strategy &&
180
+ @compression == other.compression &&
181
+ @compact_storage == other.compact_storage? &&
182
+ @crc_check_chance == other.crc_check_chance &&
183
+ @extensions == other.extensions &&
184
+ @cdc == other.cdc
185
+ end
186
+ alias == eql?
187
+ end
188
+
189
+ # Encapsulates the compaction strategy of a column-container.
190
+ class Compaction
191
+ # @return [String] the name of the Cassandra class that performs compaction.
192
+ attr_reader :class_name
193
+ # @return [Hash] compaction strategy options
194
+ attr_reader :options
195
+
196
+ # @private
197
+ def initialize(class_name, options)
198
+ @class_name = class_name
199
+ @options = options
200
+ end
201
+
202
+ # @private
203
+ def to_cql
204
+ compaction = {'class' => @class_name}
205
+ compaction.merge!(@options)
206
+
207
+ Util.encode_hash(compaction)
208
+ end
209
+
210
+ # @private
211
+ def eql?(other)
212
+ other.is_a?(Compaction) &&
213
+ @class_name == other.class_name &&
214
+ @options == other.options
215
+ end
216
+ alias == eql?
217
+ end
218
+
219
+ # @return [String] name of this column-container
220
+ attr_reader :name
221
+ # @return [Cassandra::Uuid] the id of this object in Cassandra.
222
+ attr_reader :id
223
+ # @return [Cassandra::Keyspace] the keyspace that this column-container belongs to.
224
+ attr_reader :keyspace
225
+ # @return [ColumnContainer::Options] collection of configuration options of this column-container.
226
+ attr_reader :options
227
+ # @return [Array<Cassandra::Column>] ordered list of column-names that make up the partition-key.
228
+ attr_reader :partition_key
229
+ # @return [Array<Cassandra::Column>] ordered list of column-names that make up the clustering-columns.
230
+ attr_reader :clustering_columns
231
+ # @return [Array<Cassandra::Column>] primary key of this column-container. It's the combination of
232
+ # `partition_key` and `clustering_columns`.
233
+ # @note This composition produces a flat list, so it will not be possible for the caller to distinguish
234
+ # partition-key columns from clustering-columns.
235
+ attr_reader :primary_key
236
+
237
+ # @private
238
+ def initialize(keyspace,
239
+ name,
240
+ partition_key,
241
+ clustering_columns,
242
+ other_columns,
243
+ options,
244
+ id)
245
+ @keyspace = keyspace
246
+ @name = name.freeze
247
+ @partition_key = partition_key.freeze
248
+ @clustering_columns = clustering_columns.freeze
249
+ @options = options
250
+ @id = id
251
+
252
+ # Make one array of all the columns, ordered with partition key, clustering
253
+ # columns, then other columns. Make a hash as well, to support random access
254
+ # to column metadata for a given column name. Save off the primary key (which
255
+ # is partition-key + clustering-columns) while we're at it.
256
+
257
+ @primary_key = @partition_key.dup.concat(@clustering_columns).freeze
258
+ @columns = @primary_key.dup.concat(other_columns).freeze
259
+ @columns_hash = @columns.each_with_object({}) do |col, h|
260
+ h[col.name] = col
261
+ end
262
+ end
263
+
264
+ # @param name [String] column name
265
+ # @return [Boolean] whether this column-container has a given column
266
+ def has_column?(name)
267
+ @columns_hash.key?(name)
268
+ end
269
+
270
+ # @param name [String] column name
271
+ # @return [Cassandra::Column, nil] a column or nil
272
+ def column(name)
273
+ @columns_hash[name]
274
+ end
275
+
276
+ # Yield or enumerate each column defined in this column-container
277
+ # @overload each_column
278
+ # @yieldparam column [Cassandra::Column] current column
279
+ # @return [Cassandra::ColumnContainer] self
280
+ # @overload each_column
281
+ # @return [Array<Cassandra::Column>] a list of columns
282
+ def each_column(&block)
283
+ if block_given?
284
+ @columns.each(&block)
285
+ self
286
+ else
287
+ @columns
288
+ end
289
+ end
290
+ alias columns each_column
291
+
292
+ # @private
293
+ # keyspace attribute may be nil because when this object was constructed, we didn't have
294
+ # its keyspace constructed yet. So allow updating @keyspace, thus
295
+ # allowing fetchers to create keyspace, table/view, and hook them together without
296
+ # worrying about chickens and eggs.
297
+ # rubocop:disable Naming/AccessorMethodName
298
+ def set_keyspace(keyspace)
299
+ @keyspace = keyspace
300
+ end
301
+
302
+ # @private
303
+ def inspect
304
+ "#<#{self.class.name}:0x#{object_id.to_s(16)} " \
305
+ "@keyspace=#{@keyspace.name} @name=#{@name}>"
306
+ end
307
+
308
+ # @private
309
+ def eql?(other)
310
+ other.is_a?(ColumnContainer) &&
311
+ @keyspace == other.keyspace &&
312
+ @name == other.name &&
313
+ @partition_key == other.partition_key &&
314
+ @clustering_columns == other.clustering_columns &&
315
+ @columns == other.raw_columns &&
316
+ @options == other.options
317
+ end
318
+ alias == eql?
319
+
320
+ # @private
321
+ def raw_columns
322
+ @columns
323
+ end
324
+ protected :raw_columns
325
+ end
326
+ end