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
-
#cassandra_options ⇒ Hash
readonly
A hash of additional options passed to Cassandra, if any.
-
#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.
-
#error_policy ⇒ Object
readonly
The error policy object in use by this keyspace.
-
#hosts ⇒ Array<String>
readonly
List of hosts to connect to.
-
#name ⇒ String
readonly
Name of the keyspace.
-
#port ⇒ Object
readonly
Integer port to connect to Cassandra nodes on.
-
#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={}) @lock = Monitor.new configure(configuration) end |
Instance Attribute Details
#cassandra_options ⇒ Hash (readonly)
Returns A hash of additional options passed to Cassandra, if any.
34 35 36 |
# File 'lib/cequel/metal/keyspace.rb', line 34 def @cassandra_options end |
#client_compression ⇒ Symbol (readonly)
Returns The client compression option.
32 33 34 |
# File 'lib/cequel/metal/keyspace.rb', line 32 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.
28 29 30 |
# File 'lib/cequel/metal/keyspace.rb', line 28 def credentials @credentials end |
#default_consistency ⇒ Symbol
Returns the default consistency for queries in this keyspace.
266 267 268 |
# File 'lib/cequel/metal/keyspace.rb', line 266 def default_consistency @default_consistency || :quorum end |
#error_policy ⇒ Object (readonly)
Returns The error policy object in use by this keyspace.
36 37 38 |
# File 'lib/cequel/metal/keyspace.rb', line 36 def error_policy @error_policy 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 |
#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 |
#ssl_config ⇒ Hash (readonly)
Returns SSL Configuration options.
30 31 32 |
# File 'lib/cequel/metal/keyspace.rb', line 30 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.
165 166 167 |
# File 'lib/cequel/metal/keyspace.rb', line 165 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
290 291 292 293 294 295 296 297 298 299 |
# File 'lib/cequel/metal/keyspace.rb', line 290 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.
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/cequel/metal/keyspace.rb', line 276 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
247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/cequel/metal/keyspace.rb', line 247 def clear_active_connections! synchronize do 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 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.
174 175 176 177 178 |
# File 'lib/cequel/metal/keyspace.rb', line 174 def client synchronize do @client ||= cluster.connect(name) end end |
#cluster ⇒ Object
301 302 303 304 305 |
# File 'lib/cequel/metal/keyspace.rb', line 301 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
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/cequel/metal/keyspace.rb', line 133 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 @error_policy = extract_cassandra_error_policy(configuration) @cassandra_options = (configuration) @hosts, @port = extract_hosts_and_port(configuration) @credentials = extract_credentials(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.
193 194 195 |
# File 'lib/cequel/metal/keyspace.rb', line 193 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
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/cequel/metal/keyspace.rb', line 206 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 error_policy.execute_stmt(self) do client.execute(cql, ) end end end |
#exists? ⇒ Boolean
Returns true if the keyspace exists.
271 272 273 |
# File 'lib/cequel/metal/keyspace.rb', line 271 def exists? cluster.has_keyspace?(name) end |
#prepare_statement(statement) ⇒ Cassandra::Statement::Prepared
Wraps the prepare statement in the default retry strategy
230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/cequel/metal/keyspace.rb', line 230 def prepare_statement(statement) cql = case statement when Statement statement.cql else statement end error_policy.execute_stmt(self) 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.
157 158 159 |
# File 'lib/cequel/metal/keyspace.rb', line 157 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 |