Class: Cequel::Metal::Keyspace
- Inherits:
-
Object
- Object
- Cequel::Metal::Keyspace
- Extended by:
- Util::Forwardable
- Includes:
- Logging, MonitorMixin
- Defined in:
- lib/cequel/metal/keyspace.rb
Overview
Handle to a Cassandra keyspace (database). Keyspace objects are factories for DataSet instances and provide a handle to a Schema::Keyspace instance.
Instance Attribute Summary collapse
-
#client_compression ⇒ Symbol
readonly
The client compression option.
-
#configuration ⇒ Hash
readonly
Configuration options for this keyspace.
-
#credentials ⇒ Hash
readonly
Credentials for connect to cassandra.
-
#default_consistency ⇒ Symbol
The default consistency for queries in this keyspace.
-
#hosts ⇒ Array<String>
readonly
List of hosts to connect to.
-
#max_retries ⇒ Object
readonly
Integer maximum number of retries to reconnect to Cassandra.
-
#name ⇒ String
readonly
Name of the keyspace.
-
#port ⇒ Object
readonly
Integer port to connect to Cassandra nodes on.
-
#retry_delay ⇒ Object
readonly
Float delay between retries to reconnect to Cassandra.
-
#ssl_config ⇒ Hash
readonly
SSL Configuration options.
Class Method Summary collapse
-
.sanitize(statement, bind_vars) ⇒ String
Combine a statement with bind vars into a fully-fledged CQL query.
Instance Method Summary collapse
-
#[](table_name) ⇒ DataSet
Data set encapsulating table.
-
#batch { ... } ⇒ Object
Execute write operations in a batch.
-
#bug8733_version? ⇒ Boolean
return true if Cassandra server version is known to include bug CASSANDRA-8733.
-
#cassandra_version ⇒ String
Cassandra version number.
-
#clear_active_connections! ⇒ void
Clears all active connections.
-
#client ⇒ Cql::Client::Client
private
The low-level client provided by the adapter.
- #cluster ⇒ Object
-
#configure(configuration = {}) ⇒ void
Configure this keyspace from a hash of options.
-
#execute(statement, *bind_vars) ⇒ Enumerable
Execute a CQL query in this keyspace.
-
#execute_with_options(statement, options = {}) ⇒ Enumerable
Execute a CQL query in this keyspace with the given options.
-
#exists? ⇒ Boolean
True if the keyspace exists.
-
#initialize(configuration = {}) ⇒ Keyspace
constructor
private
A new instance of Keyspace.
-
#prepare_statement(statement) ⇒ Cassandra::Statement::Prepared
Wraps the prepare statement in the default retry strategy.
-
#sanitize ⇒ String
Combine a statement with bind vars into a fully-fledged CQL query.
-
#schema ⇒ Schema::Keyspace
Schema object providing full read/write access to database schema.
-
#write(statement, *bind_vars) ⇒ void
Write data to this keyspace using a CQL query.
-
#write_with_options(statement, bind_vars, consistency) ⇒ void
Write data to this keyspace using a CQL query at the given consistency.
Methods included from Util::Forwardable
Methods included from Logging
Constructor Details
#initialize(configuration = {}) ⇒ Keyspace
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Keyspace.
96 97 98 99 |
# File 'lib/cequel/metal/keyspace.rb', line 96 def initialize(configuration={}) configure(configuration) @lock = Monitor.new end |
Instance Attribute Details
#client_compression ⇒ Symbol (readonly)
Returns The client compression option.
36 37 38 |
# File 'lib/cequel/metal/keyspace.rb', line 36 def client_compression @client_compression end |
#configuration ⇒ Hash (readonly)
Returns configuration options for this keyspace.
17 18 19 |
# File 'lib/cequel/metal/keyspace.rb', line 17 def configuration @configuration end |
#credentials ⇒ Hash (readonly)
Returns credentials for connect to cassandra.
32 33 34 |
# File 'lib/cequel/metal/keyspace.rb', line 32 def credentials @credentials end |
#default_consistency ⇒ Symbol
Returns the default consistency for queries in this keyspace.
260 261 262 |
# File 'lib/cequel/metal/keyspace.rb', line 260 def default_consistency @default_consistency || :quorum end |
#hosts ⇒ Array<String> (readonly)
Returns list of hosts to connect to.
21 22 23 |
# File 'lib/cequel/metal/keyspace.rb', line 21 def hosts @hosts end |
#max_retries ⇒ Object (readonly)
Returns Integer maximum number of retries to reconnect to Cassandra.
25 26 27 |
# File 'lib/cequel/metal/keyspace.rb', line 25 def max_retries @max_retries end |
#name ⇒ String (readonly)
Returns name of the keyspace.
19 20 21 |
# File 'lib/cequel/metal/keyspace.rb', line 19 def name @name end |
#port ⇒ Object (readonly)
Returns Integer port to connect to Cassandra nodes on.
23 24 25 |
# File 'lib/cequel/metal/keyspace.rb', line 23 def port @port end |
#retry_delay ⇒ Object (readonly)
Returns Float delay between retries to reconnect to Cassandra.
27 28 29 |
# File 'lib/cequel/metal/keyspace.rb', line 27 def retry_delay @retry_delay end |
#ssl_config ⇒ Hash (readonly)
Returns SSL Configuration options.
34 35 36 |
# File 'lib/cequel/metal/keyspace.rb', line 34 def ssl_config @ssl_config end |
Class Method Details
.sanitize(statement, bind_vars) ⇒ String
Combine a statement with bind vars into a fully-fledged CQL query. This will no longer be needed once the CQL driver supports bound values natively.
79 80 81 82 |
# File 'lib/cequel/metal/keyspace.rb', line 79 def self.sanitize(statement, bind_vars) each_bind_var = bind_vars.each statement.gsub('?') { Type.quote(each_bind_var.next) } end |
Instance Method Details
#[](table_name) ⇒ DataSet
Returns data set encapsulating table.
161 162 163 |
# File 'lib/cequel/metal/keyspace.rb', line 161 def [](table_name) DataSet.new(table_name.to_sym, self) end |
#batch { ... } ⇒ Object
If this method is created while already in a batch of the same type (logged or unlogged), this method is a no-op.
Execute write operations in a batch. Any inserts, updates, and deletes inside this method’s block will be executed inside a CQL BATCH operation.
65 |
# File 'lib/cequel/metal/keyspace.rb', line 65 def_delegator :batch_manager, :batch |
#bug8733_version? ⇒ Boolean
return true if Cassandra server version is known to include bug CASSANDRA-8733
284 285 286 287 288 289 290 291 292 293 |
# File 'lib/cequel/metal/keyspace.rb', line 284 def bug8733_version? version_file = File.('../../../../.cassandra-versions', __FILE__) @all_versions ||= File.read(version_file).split("\n").map(&:strip) # bug exists in versions 0.3.0-2.0.12 and 2.1.0-2.1.2 @bug8733_versions ||= @all_versions[0..@all_versions.index('2.0.12')] + @all_versions[@all_versions.index('2.1.0')..@all_versions.index('2.1.2')] @bug8733_versions.include?(cassandra_version) end |
#cassandra_version ⇒ String
Returns Cassandra version number.
270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/cequel/metal/keyspace.rb', line 270 def cassandra_version return @cassandra_version if @cassandra_version statement = <<-CQL SELECT release_version FROM system.local CQL log('CQL', statement) do @cassandra_version = client_without_keyspace.execute(statement).first['release_version'] end end |
#clear_active_connections! ⇒ void
This method returns an undefined value.
Clears all active connections
243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/cequel/metal/keyspace.rb', line 243 def clear_active_connections! if defined? @client remove_instance_variable(:@client) end if defined? @client_without_keyspace remove_instance_variable(:@client_without_keyspace) end if defined? @cluster @cluster.close remove_instance_variable(:@cluster) end end |
#client ⇒ Cql::Client::Client
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the low-level client provided by the adapter.
170 171 172 173 174 |
# File 'lib/cequel/metal/keyspace.rb', line 170 def client synchronize do @client ||= cluster.connect(name) end end |
#cluster ⇒ Object
295 296 297 298 299 |
# File 'lib/cequel/metal/keyspace.rb', line 295 def cluster synchronize do @cluster ||= Cassandra.cluster() end end |
#configure(configuration = {}) ⇒ void
This method returns an undefined value.
Configure this keyspace from a hash of options
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/cequel/metal/keyspace.rb', line 129 def configure(configuration = {}) if configuration.key?(:thrift) warn "Cequel no longer uses the Thrift transport to communicate " \ "with Cassandra. The :thrift option is deprecated and ignored." end @configuration = configuration @hosts, @port = extract_hosts_and_port(configuration) @credentials = extract_credentials(configuration) @max_retries = extract_max_retries(configuration) @retry_delay = extract_retry_delay(configuration) @ssl_config = extract_ssl_config(configuration) @name = configuration[:keyspace] @default_consistency = configuration[:default_consistency].try(:to_sym) @client_compression = configuration[:client_compression].try(:to_sym) # reset the connections clear_active_connections! end |
#execute(statement, *bind_vars) ⇒ Enumerable
Execute a CQL query in this keyspace
If a connection error occurs, will retry a maximum number of
time (default 3) before re-raising the original connection
error.
189 190 191 |
# File 'lib/cequel/metal/keyspace.rb', line 189 def execute(statement, *bind_vars) (Statement.new(statement, bind_vars), { consistency: default_consistency }) end |
#execute_with_options(statement, options = {}) ⇒ Enumerable
Execute a CQL query in this keyspace with the given options
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/cequel/metal/keyspace.rb', line 202 def (statement, ={}) [:consistency] ||= default_consistency cql, = *case statement when Statement [prepare_statement(statement), {arguments: statement.bind_vars}.merge()] when Cassandra::Statements::Batch [statement, ] end log('CQL', statement) do client_retry do client.execute(cql, ) end end end |
#exists? ⇒ Boolean
Returns true if the keyspace exists.
265 266 267 |
# File 'lib/cequel/metal/keyspace.rb', line 265 def exists? cluster.has_keyspace?(name) end |
#prepare_statement(statement) ⇒ Cassandra::Statement::Prepared
Wraps the prepare statement in the default retry strategy
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/cequel/metal/keyspace.rb', line 226 def prepare_statement(statement) cql = case statement when Statement statement.cql else statement end client_retry do client.prepare(cql) end end |
#sanitize ⇒ String
Combine a statement with bind vars into a fully-fledged CQL query. This will no longer be needed once the CQL driver supports bound values natively.
88 |
# File 'lib/cequel/metal/keyspace.rb', line 88 def_delegator 'self.class', :sanitize |
#schema ⇒ Schema::Keyspace
Returns schema object providing full read/write access to database schema.
153 154 155 |
# File 'lib/cequel/metal/keyspace.rb', line 153 def schema Schema::Keyspace.new(self) end |
#write(statement, *bind_vars) ⇒ void
This method returns an undefined value.
Write data to this keyspace using a CQL query. Will be included the current batch operation if one is present.
47 |
# File 'lib/cequel/metal/keyspace.rb', line 47 def_delegator :write_target, :execute, :write |
#write_with_options(statement, bind_vars, consistency) ⇒ void
This method returns an undefined value.
Write data to this keyspace using a CQL query at the given consistency. Will be included the current batch operation if one is present.
58 59 |
# File 'lib/cequel/metal/keyspace.rb', line 58 def_delegator :write_target, :execute_with_options, :write_with_options |