Class: Cequel::Metal::Keyspace

Inherits:
Object
  • Object
show all
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.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#slowlog=

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.

Options Hash (configuration):

  • :host (String) — default: '127.0.0.1'

    hostname of single Cassandra instance to connect to

  • :port (Integer) — default: 9042

    port on which to connect to all specified hosts

  • :max_retries (Integer)

    maximum number of retries on connection failure

  • :hosts (Array<String>)

    list of Cassandra instances to connect to (hostnames only)

  • :username (String)

    user to auth with (leave blank for no auth)

  • :password (String)

    password to auth with (leave blank for no auth)

  • :keyspace (String)

    name of keyspace to connect to

See Also:

Since:

  • 1.0.0



90
91
92
93
# File 'lib/cequel/metal/keyspace.rb', line 90

def initialize(configuration={})
  configure(configuration)
  @lock = Monitor.new
end

Instance Attribute Details

#configurationHash (readonly)

Returns configuration options for this keyspace.

Since:

  • 1.0.0



17
18
19
# File 'lib/cequel/metal/keyspace.rb', line 17

def configuration
  @configuration
end

#credentialsHash (readonly)

Returns credentials for connect to cassandra.

Since:

  • 1.0.0



30
31
32
# File 'lib/cequel/metal/keyspace.rb', line 30

def credentials
  @credentials
end

#default_consistencySymbol

Returns the default consistency for queries in this keyspace.

Since:

  • 1.1.0



218
219
220
# File 'lib/cequel/metal/keyspace.rb', line 218

def default_consistency
  @default_consistency || :quorum
end

#hostsArray<String> (readonly)

Returns list of hosts to connect to.

Since:

  • 1.0.0



21
22
23
# File 'lib/cequel/metal/keyspace.rb', line 21

def hosts
  @hosts
end

#max_retriesObject (readonly)

Returns Integer maximum number of retries to reconnect to Cassandra.

Since:

  • 1.0.0



25
26
27
# File 'lib/cequel/metal/keyspace.rb', line 25

def max_retries
  @max_retries
end

#nameString (readonly)

Returns name of the keyspace.

Since:

  • 1.0.0



19
20
21
# File 'lib/cequel/metal/keyspace.rb', line 19

def name
  @name
end

#portObject (readonly)

Returns Integer port to connect to Cassandra nodes on.

Since:

  • 1.0.0



23
24
25
# File 'lib/cequel/metal/keyspace.rb', line 23

def port
  @port
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.

Since:

  • 1.0.0



73
74
75
76
# File 'lib/cequel/metal/keyspace.rb', line 73

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.

Since:

  • 1.0.0



143
144
145
# File 'lib/cequel/metal/keyspace.rb', line 143

def [](table_name)
  DataSet.new(table_name.to_sym, self)
end

#batch { ... } ⇒ Object

Note:

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.

Examples:

Perform inserts in a batch

DB.batch do
  DB[:posts].insert(:id => 1, :title => 'One')
  DB[:posts].insert(:id => 2, :title => 'Two')
end

Yields:

  • context within which all write operations will be batched

Raises:

  • (ArgumentError)

    if attempting to start a logged batch while already in an unlogged batch, or vice versa.



59
# File 'lib/cequel/metal/keyspace.rb', line 59

def_delegator :batch_manager, :batch

#clear_active_connections!void

This method returns an undefined value.

Clears all active connections

Since:

  • 1.0.0



208
209
210
211
212
# File 'lib/cequel/metal/keyspace.rb', line 208

def clear_active_connections!
  if defined? @client
    remove_instance_variable(:@client)
  end
end

#clientCql::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.

Since:

  • 1.0.0



152
153
154
155
156
157
158
# File 'lib/cequel/metal/keyspace.rb', line 152

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

Options Hash (configuration):

  • :host (String) — default: '127.0.0.1'

    hostname of single Cassandra instance to connect to

  • :port (Integer) — default: 9042

    port on which to connect to all specified hosts

  • :max_retries (Integer)

    maximum number of retries on connection failure

  • :hosts (Array<String>)

    list of Cassandra instances to connect to (hostnames only)

  • :username (String)

    user to auth with (leave blank for no auth)

  • :password (String)

    password to auth with (leave blank for no auth)

  • :keyspace (String)

    name of keyspace to connect to

Since:

  • 1.0.0



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cequel/metal/keyspace.rb', line 114

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)

  @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.

See Also:

Since:

  • 1.0.0



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/cequel/metal/keyspace.rb', line 173

def execute(statement, *bind_vars)
  retries = max_retries

  begin
    execute_with_consistency(statement, bind_vars, default_consistency)
  rescue Cql::NotConnectedError, Ione::Io::ConnectionError
    clear_active_connections!
    raise if retries < 0
    retries -= 1
    retry
  end
end

#execute_with_consistency(statement, bind_vars, consistency) ⇒ Enumerable

Execute a CQL query in this keyspace with the given consistency

Since:

  • 1.1.0



196
197
198
199
200
201
# File 'lib/cequel/metal/keyspace.rb', line 196

def execute_with_consistency(statement, bind_vars, consistency)
  log('CQL', statement, *bind_vars) do
    client.execute(sanitize(statement, bind_vars),
                   consistency || default_consistency)
  end
end

#exists?Boolean

Returns true if the keyspace exists.

Since:

  • 1.0.0



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/cequel/metal/keyspace.rb', line 223

def exists?
  statement = "    SELECT keyspace_name\n    FROM system.schema_keyspaces\n    WHERE keyspace_name = ?\n  CQL\n\n  log('CQL', statement, [name]) do\n    raw_client.execute(sanitize(statement, [name])).any?\n  end\nend\n"

#sanitizeString

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.



82
# File 'lib/cequel/metal/keyspace.rb', line 82

def_delegator 'self.class', :sanitize

#schemaSchema::Keyspace

Returns schema object providing full read/write access to database schema.

Since:

  • 1.0.0



135
136
137
# File 'lib/cequel/metal/keyspace.rb', line 135

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.



41
# File 'lib/cequel/metal/keyspace.rb', line 41

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.



52
53
# File 'lib/cequel/metal/keyspace.rb', line 52

def_delegator :write_target, :execute_with_consistency,
:write_with_consistency