Class: Cequel::Metal::Batch Private

Inherits:
Object
  • Object
show all
Defined in:
lib/cequel/metal/batch.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.

Encapsulates a batch operation

See Also:

  • Keyspace::batch

Since:

  • 1.0.0

Instance Method Summary collapse

Constructor Details

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

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

Parameters:

  • keyspace (Keyspace)

    the keyspace that this batch will be executed on

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

See Also:

Since:

  • 1.0.0



27
28
29
30
31
32
33
34
35
# File 'lib/cequel/metal/batch.rb', line 27

def initialize(keyspace, options = {})
  options.assert_valid_keys(:auto_apply, :unlogged, :consistency)
  @keyspace = keyspace
  @auto_apply = options[:auto_apply]
  @unlogged = options.fetch(:unlogged, false)
  @consistency = options.fetch(:consistency,
                               keyspace.default_consistency)
  reset
end

Instance Method Details

#applyObject

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.

Send the batch to Cassandra

Since:

  • 1.0.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cequel/metal/batch.rb', line 53

def apply
  return if @statements.empty?

  statement = @statements.first
  if @statements.size > 1
    statement =
      if logged?
        keyspace.client.logged_batch
      else
        keyspace.client.unlogged_batch
      end
    @statements.each { |s| statement.add(s.prepare(keyspace), arguments: s.bind_vars) }
  end

  keyspace.execute_with_options(statement, consistency: @consistency)
  execute_on_complete_hooks
end

#execute(statement) ⇒ 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.

Add a statement to the batch.

Parameters:

  • statement (String)

    CQL string

  • bind_vars (Object)

    values for bind variables

Since:

  • 1.0.0



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

def execute(statement)
  @statements << statement
  if @auto_apply && @statements.size >= @auto_apply
    apply
    reset
  end
end

#execute_with_options(statement, 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.

Since:

  • 1.0.0



93
94
95
96
97
98
99
100
101
102
# File 'lib/cequel/metal/batch.rb', line 93

def execute_with_options(statement, options)
  query_consistency = options.fetch(:consistency)
  if query_consistency && query_consistency != @consistency
    raise ArgumentError,
          "Attempting to perform query with consistency " \
          "#{query_consistency.to_s.upcase} in batch with consistency " \
          "#{@consistency.upcase}"
  end
  execute(statement)
end

#logged?Boolean

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.

Is this a logged batch?

Returns:

  • (Boolean)

Since:

  • 1.0.0



88
89
90
# File 'lib/cequel/metal/batch.rb', line 88

def logged?
  !unlogged?
end

#on_complete(&block) ⇒ 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.

Since:

  • 1.0.0



71
72
73
# File 'lib/cequel/metal/batch.rb', line 71

def on_complete(&block)
  on_complete_hooks << block
end

#unlogged?Boolean

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.

Is this an unlogged batch?

Returns:

  • (Boolean)

Since:

  • 1.0.0



79
80
81
# File 'lib/cequel/metal/batch.rb', line 79

def unlogged?
  @unlogged
end