Class: Cequel::Metal::BatchManager Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Manage a current batch per thread. Used by Keyspace

Since:

  • 1.0.0

Instance Method Summary collapse

Constructor Details

#initialize(keyspace) ⇒ BatchManager

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

Parameters:

  • keyspace (Keyspace)

    keyspace to make writes to

Since:

  • 1.0.0



14
15
16
# File 'lib/cequel/metal/batch_manager.rb', line 14

def initialize(keyspace)
  @keyspace = keyspace
end

Instance Method Details

#batch(options = {}) { ... } ⇒ Object

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.

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

Options Hash (options):

  • :auto_apply (Integer)

    If specified, flush the batch after this many statements have been added.

  • :unlogged (Boolean) — default: false

    Whether to use an [unlogged batch]( www.datastax.com/dev/blog/atomic-batches-in-cassandra-1-2). Logged batches guarantee atomicity (but not isolation) at the cost of a performance penalty; unlogged batches are useful for bulk write operations but behave the same as discrete writes.

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.

Since:

  • 1.0.0



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cequel/metal/batch_manager.rb', line 39

def batch(options = {})
  new_batch = Batch.new(keyspace, options)

  if current_batch
    if current_batch.unlogged? && new_batch.logged?
      fail ArgumentError,
           "Already in an unlogged batch; can't start a logged batch."
    end
    return yield(current_batch)
  end

  begin
    self.current_batch = new_batch
    yield(new_batch).tap { new_batch.apply }
  ensure
    self.current_batch = nil
  end
end