Class: Cequel::Metal::Keyspace

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging
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

Instance Method Summary collapse

Methods included from Logging

#logger=, #slowlog=, #slowlog_threshold=

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.

Parameters:

  • configuration (Options) (defaults to: {})

Options Hash (configuration):

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

    host/port of single Cassandra instance to connect to

  • :hosts (Array<String>)

    list of Cassandra instances to connect to

  • :thrift (Hash)

    Thrift options to be passed directly to Thrift client

  • :keyspace (String)

    name of keyspace to connect to

  • :pool (Integer) — default: 1

    size of connection pool

  • :pool_timeout (Integer) — default: 0

    timeout when attempting to check out connection from pool

See Also:

Since:

  • 1.0.0



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

def initialize(configuration={})
  configure(configuration)
end

Instance Attribute Details

#configurationHash (readonly)

Returns configuration options for this keyspace.

Returns:

  • (Hash)

    configuration options for this keyspace

Since:

  • 1.0.0



13
14
15
# File 'lib/cequel/metal/keyspace.rb', line 13

def configuration
  @configuration
end

#nameString (readonly)

Returns name of the keyspace.

Returns:

  • (String)

    name of the keyspace

Since:

  • 1.0.0



15
16
17
# File 'lib/cequel/metal/keyspace.rb', line 15

def name
  @name
end

Instance Method Details

#[](table_name) ⇒ DataSet

Returns data set encapsulating table.

Parameters:

  • table_name (Symbol)

    the name of the table

Returns:

  • (DataSet)

    data set encapsulating table

Since:

  • 1.0.0



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

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

Parameters:

  • options (Hash)

Yields:

  • context within which all write operations will be batched

Returns:

  • return value of block

Raises:

  • (ArgumentError)

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



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

def_delegator :batch_manager, :batch

#clear_active_connections!void

This method returns an undefined value.

Clears all active connections

Since:

  • 1.0.0



105
106
107
108
109
# File 'lib/cequel/metal/keyspace.rb', line 105

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

#configure(configuration = {}) ⇒ void

This method returns an undefined value.

Configure this keyspace from a hash of options

Parameters:

  • configuration (Options) (defaults to: {})

    configuration options

Options Hash (configuration):

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

    host/port of single Cassandra instance to connect to

  • :hosts (Array<String>)

    list of Cassandra instances to connect to

  • :thrift (Hash)

    Thrift options to be passed directly to Thrift client

  • :keyspace (String)

    name of keyspace to connect to

  • :pool (Integer) — default: 1

    size of connection pool

  • :pool_timeout (Integer) — default: 0

    timeout when attempting to check out connection from pool

Since:

  • 1.0.0



60
61
62
63
64
65
66
67
68
# File 'lib/cequel/metal/keyspace.rb', line 60

def configure(configuration = {})
  @configuration = configuration
  @hosts = configuration.fetch(
    :host, configuration.fetch(:hosts, '127.0.0.1:9160'))
  @thrift_options = configuration[:thrift].try(:symbolize_keys) || {}
  @name = configuration[:keyspace]
  # reset the connections
  clear_active_connections!
end

#execute(statement, *bind_vars) ⇒ void

This method returns an undefined value.

Execute a CQL query in this keyspace

Parameters:

  • statement (String)

    CQL string

  • bind_vars (Object)

    values for bind variables

Since:

  • 1.0.0



92
93
94
95
96
97
98
# File 'lib/cequel/metal/keyspace.rb', line 92

def execute(statement, *bind_vars)
  log('CQL', statement, *bind_vars) do
    with_connection do |conn|
      conn.execute(statement, *bind_vars)
    end
  end
end

#schemaSchema::Keyspace

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

Returns:

  • (Schema::Keyspace)

    schema object providing full read/write access to database schema

Since:

  • 1.0.0



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

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.



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

def_delegator :write_target, :execute, :write