Class: Cequel::Keyspace

Inherits:
Object
  • Object
show all
Defined in:
lib/cequel/keyspace.rb

Overview

Handle to a Cassandra keyspace.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

See Also:



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

def initialize(configuration = {})
  @name = configuration[:keyspace]
  @hosts = configuration[:host] || configuration[:hosts]
  @thrift_options = configuration[:thrift].try(:symbolize_keys)
end

Instance Attribute Details

#connectionObject



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

def connection
  @connection ||= CassandraCQL::Database.new(
    @hosts, {:keyspace => @name}, @thrift_options
  )
end

#logger=(value) ⇒ Object (writeonly)

Set a logger for logging queries. Queries logged at INFO level



11
12
13
# File 'lib/cequel/keyspace.rb', line 11

def logger=(value)
  @logger = value
end

#slowlog=(value) ⇒ Object (writeonly)

Set a logger for logging queries. Queries logged at INFO level



11
12
13
# File 'lib/cequel/keyspace.rb', line 11

def slowlog=(value)
  @slowlog = value
end

#slowlog_threshold=(value) ⇒ Object (writeonly)

Set a logger for logging queries. Queries logged at INFO level



11
12
13
# File 'lib/cequel/keyspace.rb', line 11

def slowlog_threshold=(value)
  @slowlog_threshold = value
end

Instance Method Details

#[](column_family_name) ⇒ DataSet

Get DataSet encapsulating a column family in this keyspace

Parameters:

  • column_family_name (Symbol)

    the name of the column family

Returns:



35
36
37
# File 'lib/cequel/keyspace.rb', line 35

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

#batch(options = {}) ⇒ Object

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) (defaults to: {})

Options Hash (options):

  • :auto_apply (Fixnum)

    Automatically send batch to Cassandra after this many statements



78
79
80
81
82
83
84
# File 'lib/cequel/keyspace.rb', line 78

def batch(options = {})
  old_batch, @batch = @batch, Batch.new(self, options)
  yield
  @batch.apply
ensure
  @batch = old_batch
end

#execute(statement, *bind_vars) ⇒ Object

Execute a CQL query in this keyspace.

Parameters:

  • statement (String)

    CQL string

  • *bind_vars (Object)

    values for bind variables



45
46
47
48
49
# File 'lib/cequel/keyspace.rb', line 45

def execute(statement, *bind_vars)
  log('CQL', statement, *bind_vars) do
    connection.execute(statement, *bind_vars)
  end
end

#write(statement, *bind_vars) ⇒ Object

Write data to this keyspace using a CQL query. Will be included the current batch operation if one is present.

Parameters:

  • statement (String)

    CQL string

  • *bind_vars (Object)

    values for bind variables



57
58
59
60
61
62
63
# File 'lib/cequel/keyspace.rb', line 57

def write(statement, *bind_vars)
  if @batch
    @batch.execute(statement, *bind_vars)
  else
    execute(statement, *bind_vars)
  end
end