Class: Cequel::Batch

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

Overview

Encapsulates a batch operation

See Also:

  • Keyspace::batch

Instance Method Summary collapse

Constructor Details

#initialize(keyspace, options = {}) ⇒ Batch

TODO:

support batch-level consistency options

Returns a new instance of Batch.

Parameters:

  • keyspace (Keyspace)

    the keyspace that this batch will be executed on

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :auto_apply (Fixnum)

    Automatically send batch to Cassandra after this many statements

See Also:



19
20
21
22
23
# File 'lib/cequel/batch.rb', line 19

def initialize(keyspace, options = {})
  @keyspace = keyspace
  @auto_apply = options[:auto_apply]
  reset
end

Instance Method Details

#applyObject

Send the batch to Cassandra



42
43
44
45
46
# File 'lib/cequel/batch.rb', line 42

def apply
  return if @statement_count.zero?
  @statement.append("APPLY BATCH\n")
  @keyspace.execute(*@statement.args)
end

#execute(cql, *bind_vars) ⇒ Object

Add a statement to the batch.

Parameters:

  • statement (String)

    CQL string

  • *bind_vars (Object)

    values for bind variables



30
31
32
33
34
35
36
37
# File 'lib/cequel/batch.rb', line 30

def execute(cql, *bind_vars)
  @statement.append("#{cql}\n", *bind_vars)
  @statement_count += 1
  if @auto_apply && @statement_count >= @auto_apply
    apply
    reset
  end
end