Class: Cassandra::Batch

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cassandra/batch.rb

Instance Method Summary collapse

Constructor Details

#initialize(cassandra, options) ⇒ Batch

Returns a new instance of Batch.



5
6
7
8
9
10
# File 'lib/cassandra/batch.rb', line 5

def initialize(cassandra, options) 
  @queue_size = options.delete(:queue_size) || 0
  @cassandra = cassandra
  @options = options
  @batch_queue = []
end

Instance Method Details

#<<(mutation) ⇒ Object

Append mutation to the batch queue Flush the batch queue if full



16
17
18
19
20
21
22
23
24
25
# File 'lib/cassandra/batch.rb', line 16

def <<(mutation)
  @batch_queue << mutation
  if @queue_size > 0 and @batch_queue.length >= @queue_size
    begin
      @cassandra.flush_batch(@options)
    ensure
      @batch_queue = []
    end
  end
end

#each(&block) ⇒ Object

Implement each method (required by Enumerable)



30
31
32
# File 'lib/cassandra/batch.rb', line 30

def each(&block)
  @batch_queue.each(&block)
end

#lengthObject

Queue size



37
38
39
# File 'lib/cassandra/batch.rb', line 37

def length
  @batch_queue.length
end