Class: Cequel::Metal::Keyspace
- Inherits:
-
Object
- Object
- Cequel::Metal::Keyspace
- Extended by:
- 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
-
#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.
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.
-
#clear_active_connections! ⇒ void
Clears all active connections.
-
#client ⇒ Cql::Client::Client
private
The low-level client provided by the adapter.
-
#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_consistency(statement, bind_vars, consistency) ⇒ Enumerable
Execute a CQL query in this keyspace with the given consistency.
-
#exists? ⇒ Boolean
True if the keyspace exists.
-
#initialize(configuration = {}) ⇒ Keyspace
constructor
private
A new instance of Keyspace.
-
#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_consistency(statement, bind_vars, consistency) ⇒ void
Write data to this keyspace using a CQL query at the given consistency.
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.
92 93 94 95 |
# File 'lib/cequel/metal/keyspace.rb', line 92 def initialize(configuration={}) configure(configuration) @lock = Monitor.new end |
Instance Attribute Details
#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.
225 226 227 |
# File 'lib/cequel/metal/keyspace.rb', line 225 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 |
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.
75 76 77 78 |
# File 'lib/cequel/metal/keyspace.rb', line 75 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.
146 147 148 |
# File 'lib/cequel/metal/keyspace.rb', line 146 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.
61 |
# File 'lib/cequel/metal/keyspace.rb', line 61 def_delegator :batch_manager, :batch |
#clear_active_connections! ⇒ void
This method returns an undefined value.
Clears all active connections
212 213 214 215 216 217 218 219 |
# File 'lib/cequel/metal/keyspace.rb', line 212 def clear_active_connections! if defined? @client remove_instance_variable(:@client) end if defined? @raw_client remove_instance_variable(:@raw_client) 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.
155 156 157 158 159 160 161 |
# File 'lib/cequel/metal/keyspace.rb', line 155 def client synchronize do @client ||= raw_client.tap do |client| client.use(name) if name end end end |
#configure(configuration = {}) ⇒ void
This method returns an undefined value.
Configure this keyspace from a hash of options
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/cequel/metal/keyspace.rb', line 116 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) @name = configuration[:keyspace] @default_consistency = configuration[:default_consistency].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.
176 177 178 |
# File 'lib/cequel/metal/keyspace.rb', line 176 def execute(statement, *bind_vars) execute_with_consistency(statement, bind_vars, default_consistency) end |
#execute_with_consistency(statement, bind_vars, consistency) ⇒ Enumerable
Execute a CQL query in this keyspace with the given consistency
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/cequel/metal/keyspace.rb', line 190 def execute_with_consistency(statement, bind_vars, consistency) retries = max_retries log('CQL', statement, *bind_vars) do begin client.execute(sanitize(statement, bind_vars), consistency || default_consistency) rescue Cql::NotConnectedError, Ione::Io::ConnectionError clear_active_connections! raise if retries == 0 retries -= 1 sleep(retry_delay) retry end end end |
#exists? ⇒ Boolean
Returns true if the keyspace exists.
230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/cequel/metal/keyspace.rb', line 230 def exists? statement = <<-CQL SELECT keyspace_name FROM system.schema_keyspaces WHERE keyspace_name = ? CQL log('CQL', statement, [name]) do raw_client.execute(sanitize(statement, [name])).any? 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.
84 |
# File 'lib/cequel/metal/keyspace.rb', line 84 def_delegator 'self.class', :sanitize |
#schema ⇒ Schema::Keyspace
Returns schema object providing full read/write access to database schema.
138 139 140 |
# File 'lib/cequel/metal/keyspace.rb', line 138 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.
43 |
# File 'lib/cequel/metal/keyspace.rb', line 43 def_delegator :write_target, :execute, :write |
#write_with_consistency(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.
54 55 |
# File 'lib/cequel/metal/keyspace.rb', line 54 def_delegator :write_target, :execute_with_consistency, :write_with_consistency |