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,102 @@
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 Host
21
+ # @return [IPAddr] host ip that clients use to connect to this host.
22
+ attr_reader :ip
23
+ # @note Host id can be `nil` before cluster has connected.
24
+ # @return [Cassandra::Uuid, nil] host id.
25
+ attr_reader :id
26
+ # @note Host datacenter can be `nil` before cluster has connected.
27
+ # @return [String, nil] host datacenter
28
+ attr_reader :datacenter
29
+ # @note Host rack can be `nil` before cluster has connected.
30
+ # @return [String, nil] host rack
31
+ attr_reader :rack
32
+ # @note Host's cassandra version can be `nil` before cluster has connected.
33
+ # @return [String, nil] version of cassandra that a host is running
34
+ attr_reader :release_version
35
+ # @note Host tokens will be empty before cluster has connected.
36
+ # @return [Array<String>] a list of tokens owned by this host
37
+ attr_reader :tokens
38
+ # @return [Symbol] host status. Must be `:up` or `:down`
39
+ attr_reader :status
40
+ # @note This is the public IP address of the host if the cluster is deployed across multiple Amazon EC2 regions
41
+ # (or equivalently multiple networks). Cassandra nodes in other EC2 regions use this address to connect to this
42
+ # host.
43
+ # @return [IPAddr, String] broadcast address, if available.
44
+ attr_reader :broadcast_address
45
+ # @note This is the address that other Cassandra nodes use to connect to this host.
46
+ # @return [IPAddr, String] listen address, if available.
47
+ attr_reader :listen_address
48
+
49
+ # @private
50
+ def initialize(ip,
51
+ id = nil,
52
+ rack = nil,
53
+ datacenter = nil,
54
+ release_version = nil,
55
+ tokens = EMPTY_LIST,
56
+ status = :up,
57
+ broadcast_address = nil,
58
+ listen_address = nil)
59
+ @ip = ip
60
+ @id = id
61
+ @rack = rack
62
+ @datacenter = datacenter
63
+ @release_version = release_version
64
+ @tokens = tokens
65
+ @status = status
66
+ @broadcast_address = broadcast_address.is_a?(String) ?
67
+ ::IPAddr.new(broadcast_address) : broadcast_address
68
+ @listen_address = listen_address.is_a?(String) ?
69
+ ::IPAddr.new(listen_address) : listen_address
70
+ end
71
+
72
+ # @return [Boolean] whether this host's status is `:up`
73
+ def up?
74
+ @status == :up
75
+ end
76
+
77
+ # @return [Boolean] whether this host's status is `:down`
78
+ def down?
79
+ @status == :down
80
+ end
81
+
82
+ # @private
83
+ def hash
84
+ @hash ||= begin
85
+ h = 17
86
+ h = 31 * h + @ip.hash
87
+ h
88
+ end
89
+ end
90
+
91
+ # @private
92
+ def eql?(other)
93
+ other.eql?(@ip)
94
+ end
95
+ alias == eql?
96
+
97
+ # @private
98
+ def inspect
99
+ "#<#{self.class.name}:0x#{object_id.to_s(16)} @ip=#{@ip}>"
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,118 @@
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 an index on a cassandra table
21
+ class Index
22
+ # @return [Cassandra::Table] table that the index applies to.
23
+ attr_reader :table
24
+ # @return [String] name of the index.
25
+ attr_reader :name
26
+ # @return [Symbol] kind of index: `:keys`, `:composites`, or `:custom`.
27
+ attr_reader :kind
28
+ # @return [String] name of column that the index applies to.
29
+ attr_reader :target
30
+ # @return [Hash] options of the index.
31
+ attr_reader :options
32
+
33
+ # @private
34
+ def initialize(table,
35
+ name,
36
+ kind,
37
+ target,
38
+ options)
39
+ @table = table
40
+ @name = name.freeze
41
+ @kind = kind
42
+ @options = options.freeze
43
+
44
+ # Target is a bit tricky; it may be an escaped name or not
45
+ # depending on C* version. Unify to be unescaped since a user
46
+ # who wants to know the target would want the bare column name.
47
+
48
+ @target = if target[0] == '"'
49
+ target[1..-2]
50
+ else
51
+ target
52
+ end.freeze
53
+ end
54
+
55
+ # @return [Boolean] whether or not this index uses a custom class.
56
+ def custom_index?
57
+ !@options['class_name'].nil?
58
+ end
59
+
60
+ # @return [String] name of the index class if this is a custom index; nil otherwise.
61
+ def custom_class_name
62
+ @options['class_name']
63
+ end
64
+
65
+ # @return [String] a cql representation of this index
66
+ def to_cql
67
+ keyspace_name = Util.escape_name(@table.keyspace.name)
68
+ table_name = Util.escape_name(@table.name)
69
+ index_name = Util.escape_name(@name)
70
+
71
+ # Target is interesting in that it's not necessarily a column name,
72
+ # so we can't simply escape it. If it contains a paren, we take it as is,
73
+ # otherwise assume it's a column name and escape accordingly.
74
+ escaped_target = @target.include?('(') ? @target : Util.escape_name(@target)
75
+
76
+ if custom_index?
77
+ "CREATE CUSTOM INDEX #{index_name} ON #{keyspace_name}.#{table_name} (#{escaped_target}) " \
78
+ "USING '#{@options['class_name']}'#{options_cql};"
79
+ else
80
+ "CREATE INDEX #{index_name} ON #{keyspace_name}.#{table_name} (#{escaped_target});"
81
+ end
82
+ end
83
+
84
+ # @private
85
+ def eql?(other)
86
+ other.is_a?(Index) &&
87
+ @table == other.table &&
88
+ @name == other.name &&
89
+ @kind == other.kind &&
90
+ @target == other.target &&
91
+ @options == other.options
92
+ end
93
+ alias == eql?
94
+
95
+ # @private
96
+ def inspect
97
+ "#<#{self.class.name}:0x#{object_id.to_s(16)} " \
98
+ "@name=#{@name.inspect} @table=#{@table.inspect} @kind=#{@kind.inspect} @target=#{@target.inspect}>"
99
+ end
100
+
101
+ private
102
+
103
+ def options_cql
104
+ # exclude 'class_name', 'target' keys
105
+ filtered_options = @options.reject do |key, _|
106
+ key == 'class_name' || key == 'target'
107
+ end
108
+ return '' if filtered_options.empty?
109
+
110
+ result = ' WITH OPTIONS = {'
111
+ result << filtered_options.map do |key, value|
112
+ "'#{key}': '#{value}'"
113
+ end.join(', ')
114
+ result << '}'
115
+ result
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,473 @@
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 keyspace
21
+ # @see Cassandra::Cluster#each_keyspace
22
+ # @see Cassandra::Cluster#keyspace
23
+ class Keyspace
24
+ # @private
25
+ class Replication
26
+ attr_reader :klass, :options
27
+
28
+ def initialize(klass, options)
29
+ @klass = klass
30
+ @options = options
31
+ end
32
+
33
+ def to_cql
34
+ replication = {'class' => @klass}
35
+ replication.merge!(@options)
36
+
37
+ Util.encode_hash(replication)
38
+ end
39
+
40
+ def eql?(other)
41
+ other.is_a?(Replication) &&
42
+ @klass == other.klass &&
43
+ @options == other.options
44
+ end
45
+ alias == eql?
46
+ end
47
+
48
+ # @return [String] this keyspace name
49
+ attr_reader :name
50
+ # @private
51
+ attr_reader :replication
52
+
53
+ # @private
54
+ def initialize(name,
55
+ durable_writes,
56
+ replication,
57
+ tables,
58
+ types,
59
+ functions,
60
+ aggregates,
61
+ views)
62
+ @name = name
63
+ @durable_writes = durable_writes
64
+ @replication = replication
65
+ @tables = tables
66
+ @types = types
67
+ @functions = functions
68
+ @aggregates = aggregates
69
+ @views = views
70
+
71
+ # Set the keyspace attribute on the tables and views.
72
+ # Also set up the index collection on the keyspace and the view-collection on each table.
73
+ @indexes_hash = {}
74
+ @tables.each_value do |t|
75
+ t.set_keyspace(self)
76
+ t.each_index do |index|
77
+ @indexes_hash[index.name] = index
78
+ end
79
+ end
80
+ @views.each_value do |v|
81
+ v.set_keyspace(self)
82
+ table = v.base_table
83
+ table.add_view(v) if table
84
+ end
85
+ end
86
+
87
+ # @return [Boolean] whether durables writes are enabled for this keyspace
88
+ def durable_writes?
89
+ @durable_writes
90
+ end
91
+
92
+ # @return [Boolean] whether this keyspace has a table with the given name
93
+ # @param name [String] table name
94
+ def has_table?(name)
95
+ @tables.key?(name)
96
+ end
97
+
98
+ # @return [Cassandra::Table, nil] a table or nil
99
+ # @param name [String] table name
100
+ def table(name)
101
+ @tables[name]
102
+ end
103
+
104
+ # Yield or enumerate each table defined in this keyspace
105
+ # @overload each_table
106
+ # @yieldparam table [Cassandra::Table] current table
107
+ # @return [Cassandra::Keyspace] self
108
+ # @overload each_table
109
+ # @return [Array<Cassandra::Table>] a list of tables
110
+ def each_table(&block)
111
+ if block_given?
112
+ @tables.each_value(&block)
113
+ self
114
+ else
115
+ @tables.values
116
+ end
117
+ end
118
+ alias tables each_table
119
+
120
+ # @return [Boolean] whether this keyspace has an index with the given name
121
+ # @param name [String] index name
122
+ def has_index?(name)
123
+ @indexes_hash.key?(name)
124
+ end
125
+
126
+ # @return [Cassandra::Index, nil] an index or nil
127
+ # @param name [String] index name
128
+ def index(name)
129
+ @indexes_hash[name]
130
+ end
131
+
132
+ # Yield or enumerate each index defined in this keyspace
133
+ # @overload each_index
134
+ # @yieldparam index [Cassandra::Index] current index
135
+ # @return [Cassandra::Keyspace] self
136
+ # @overload each_index
137
+ # @return [Array<Cassandra::Index>] a list of indexes
138
+ def each_index(&block)
139
+ if block_given?
140
+ @indexes_hash.each_value(&block)
141
+ self
142
+ else
143
+ @indexes_hash.values
144
+ end
145
+ end
146
+ alias indexes each_index
147
+
148
+ # @return [Boolean] whether this keyspace has a materialized view with the given name
149
+ # @param name [String] materialized view name
150
+ def has_materialized_view?(name)
151
+ # We check if the view exists *and* that its base-table is set. If base-table isn't available,
152
+ # it will be soon, so the user can poll on this method until we return a fully-baked materialized view.
153
+ @views.key?(name) && !@views[name].base_table.nil?
154
+ end
155
+
156
+ # @return [Cassandra::MaterializedView, nil] a materialized view or nil
157
+ # @param name [String] materialized view name
158
+ def materialized_view(name)
159
+ @views[name] if has_materialized_view?(name)
160
+ end
161
+
162
+ # Yield or enumerate each materialized view defined in this keyspace
163
+ # @overload each_materialized_view
164
+ # @yieldparam view [Cassandra::MaterializedView] current materialized view
165
+ # @return [Cassandra::Keyspace] self
166
+ # @overload each_materialized_view
167
+ # @return [Array<Cassandra::MaterializedView>] a list of materialized views
168
+ def each_materialized_view(&block)
169
+ if block_given?
170
+ @views.each_value do |v|
171
+ yield(v) if v.base_table
172
+ end
173
+ self
174
+ else
175
+ result = []
176
+ @views.each_value do |v|
177
+ result << v if v.base_table
178
+ end
179
+ result
180
+ end
181
+ end
182
+ alias materialized_views each_materialized_view
183
+
184
+ # @return [Boolean] whether this keyspace has a user-defined type with the
185
+ # given name
186
+ # @param name [String] user-defined type name
187
+ def has_type?(name)
188
+ @types.key?(name)
189
+ end
190
+
191
+ # @return [Cassandra::Types::UserDefined, nil] a type or nil
192
+ # @param name [String] user-defined type name
193
+ def type(name)
194
+ @types[name]
195
+ end
196
+
197
+ # Yield or enumerate each user-defined type present in this keyspace
198
+ # @overload each_type
199
+ # @yieldparam type [Cassandra::Types::UserDefined] current type
200
+ # @return [Cassandra::Keyspace] self
201
+ # @overload each_type
202
+ # @return [Array<Cassandra::Types::UserDefined>] a list of user-defined types
203
+ def each_type(&block)
204
+ if block_given?
205
+ @types.each_value(&block)
206
+ self
207
+ else
208
+ @types.values
209
+ end
210
+ end
211
+ alias types each_type
212
+
213
+ # @return [Boolean] whether this keyspace has a function with the given name and
214
+ # arguments
215
+ # @param name [String] function name
216
+ # @param args [Array<String>] (var-args style) function argument types
217
+ def has_function?(name, *args)
218
+ !@functions.get(name.downcase, args).nil?
219
+ end
220
+
221
+ # @return [Cassandra::Function, nil] a function or nil
222
+ # @param name [String] function name
223
+ # @param args [Array<String>] (var-args style) function argument types
224
+ def function(name, *args)
225
+ # The functions_hash datastructure is a hash <[func-name, args], Function>.
226
+ # So construct the array-key we're looking for.
227
+ @functions.get(name.downcase, args)
228
+ end
229
+
230
+ # Yield or enumerate each function defined in this keyspace
231
+ # @overload each_function
232
+ # @yieldparam function [Cassandra::Function] current function
233
+ # @return [Cassandra::Keyspace] self
234
+ # @overload each_function
235
+ # @return [Array<Cassandra::Function>] a list of functions
236
+ def each_function(&block)
237
+ if block_given?
238
+ @functions.each_function(&block)
239
+ self
240
+ else
241
+ @functions.functions
242
+ end
243
+ end
244
+ alias functions each_function
245
+
246
+ # @return [Boolean] whether this keyspace has an aggregate with the given
247
+ # name and arguments
248
+ # @param name [String] aggregate name
249
+ # @param args [Array<String>] (var-args style) aggregate function argument types
250
+ def has_aggregate?(name, *args)
251
+ !@aggregates.get(name.downcase, args).nil?
252
+ end
253
+
254
+ # @return [Cassandra::Aggregate, nil] an aggregate or nil
255
+ # @param name [String] aggregate name
256
+ # @param args [Array<String>] (var-args style) aggregate function argument types
257
+ def aggregate(name, *args)
258
+ @aggregates.get(name.downcase, args)
259
+ end
260
+
261
+ # Yield or enumerate each aggregate defined in this keyspace
262
+ # @overload each_aggregate
263
+ # @yieldparam aggregate [Cassandra::Aggregate] current aggregate
264
+ # @return [Cassandra::Keyspace] self
265
+ # @overload each_aggregate
266
+ # @return [Array<Cassandra::Aggregate>] a list of aggregates
267
+ def each_aggregate(&block)
268
+ if block_given?
269
+ @aggregates.each_function(&block)
270
+ self
271
+ else
272
+ @aggregates.functions
273
+ end
274
+ end
275
+ alias aggregates each_aggregate
276
+
277
+ # @return [String] a cql representation of this keyspace
278
+ def to_cql
279
+ "CREATE KEYSPACE #{Util.escape_name(@name)} " \
280
+ "WITH replication = #{@replication.to_cql} AND " \
281
+ "durable_writes = #{@durable_writes};"
282
+ end
283
+
284
+ # @private
285
+ def eql?(other)
286
+ other.is_a?(Keyspace) &&
287
+ @name == other.name &&
288
+ @durable_writes == other.durable_writes &&
289
+ @replication == other.replication &&
290
+ @tables == other.raw_tables &&
291
+ @types == other.raw_types &&
292
+ @functions == other.raw_functions &&
293
+ @aggregates == other.raw_aggregates
294
+ end
295
+ alias == eql?
296
+
297
+ # @private
298
+ def inspect
299
+ "#<#{self.class.name}:0x#{object_id.to_s(16)} @name=#{@name}>"
300
+ end
301
+
302
+ # @private
303
+ def update_table(table)
304
+ tables = @tables.dup
305
+ tables[table.name] = table
306
+ Keyspace.new(@name,
307
+ @durable_writes,
308
+ @replication,
309
+ tables,
310
+ @types,
311
+ @functions,
312
+ @aggregates,
313
+ @views)
314
+ end
315
+
316
+ # @private
317
+ def delete_table(table_name)
318
+ tables = @tables.dup
319
+ tables.delete(table_name)
320
+ Keyspace.new(@name,
321
+ @durable_writes,
322
+ @replication,
323
+ tables,
324
+ @types,
325
+ @functions,
326
+ @aggregates,
327
+ @views)
328
+ end
329
+
330
+ # @private
331
+ def update_materialized_view(view)
332
+ views = @views.dup
333
+ views[view.name] = view
334
+ Keyspace.new(@name,
335
+ @durable_writes,
336
+ @replication,
337
+ @tables,
338
+ @types,
339
+ @functions,
340
+ @aggregates,
341
+ views)
342
+ end
343
+
344
+ # @private
345
+ def delete_materialized_view(view_name)
346
+ views = @views.dup
347
+ views.delete(view_name)
348
+ Keyspace.new(@name,
349
+ @durable_writes,
350
+ @replication,
351
+ @tables,
352
+ @types,
353
+ @functions,
354
+ @aggregates,
355
+ views)
356
+ end
357
+
358
+ # @private
359
+ def update_type(type)
360
+ types = @types.dup
361
+ types[type.name] = type
362
+ Keyspace.new(@name,
363
+ @durable_writes,
364
+ @replication,
365
+ @tables,
366
+ types,
367
+ @functions,
368
+ @aggregates,
369
+ @views)
370
+ end
371
+
372
+ # @private
373
+ def delete_type(type_name)
374
+ types = @types.dup
375
+ types.delete(type_name)
376
+ Keyspace.new(@name,
377
+ @durable_writes,
378
+ @replication,
379
+ @tables,
380
+ types,
381
+ @functions,
382
+ @aggregates,
383
+ @views)
384
+ end
385
+
386
+ # @private
387
+ def update_function(function)
388
+ functions = @functions.dup
389
+ functions.add_or_update(function)
390
+ Keyspace.new(@name,
391
+ @durable_writes,
392
+ @replication,
393
+ @tables,
394
+ @types,
395
+ functions,
396
+ @aggregates,
397
+ @views)
398
+ end
399
+
400
+ # @private
401
+ def delete_function(function_name, function_args)
402
+ functions = @functions.dup
403
+ functions.delete(function_name, function_args)
404
+ Keyspace.new(@name,
405
+ @durable_writes,
406
+ @replication,
407
+ @tables,
408
+ @types,
409
+ functions,
410
+ @aggregates,
411
+ @views)
412
+ end
413
+
414
+ # @private
415
+ def update_aggregate(aggregate)
416
+ aggregates = @aggregates.dup
417
+ aggregates.add_or_update(aggregate)
418
+ Keyspace.new(@name,
419
+ @durable_writes,
420
+ @replication,
421
+ @tables,
422
+ @types,
423
+ @functions,
424
+ aggregates,
425
+ @views)
426
+ end
427
+
428
+ # @private
429
+ def delete_aggregate(aggregate_name, aggregate_args)
430
+ aggregates = @aggregates.dup
431
+ aggregates.delete(aggregate_name, aggregate_args)
432
+ Keyspace.new(@name,
433
+ @durable_writes,
434
+ @replication,
435
+ @tables,
436
+ @types,
437
+ @functions,
438
+ aggregates,
439
+ @views)
440
+ end
441
+
442
+ # @private
443
+ attr_reader :durable_writes
444
+ protected :durable_writes
445
+
446
+ protected
447
+
448
+ # @private
449
+ def raw_tables
450
+ @tables
451
+ end
452
+
453
+ # @private
454
+ def raw_materialized_views
455
+ @views
456
+ end
457
+
458
+ # @private
459
+ def raw_types
460
+ @types
461
+ end
462
+
463
+ # @private
464
+ def raw_functions
465
+ @functions
466
+ end
467
+
468
+ # @private
469
+ def raw_aggregates
470
+ @aggregates
471
+ end
472
+ end
473
+ end