Module: OccamsRecord::Batches

Included in:
Query, RawQuery
Defined in:
lib/occams-record/batches.rb

Overview

Methods for building batch finding methods. It expects “model” and “scope” methods to be present.

Instance Method Summary collapse

Instance Method Details

#find_each(batch_size: 1000) {|OccamsRecord::Results::Row| ... } ⇒ Enumerator

Load records in batches of N and yield each record to a block if given. If no block is given, returns an Enumerator.

NOTE Unlike ActiveRecord’s find_each, order IS respected. The primary key will be appended to the ORDER BY clause to help ensure consistent batches.

Parameters:

  • batch_size (Integer) (defaults to: 1000)

Yields:

Returns:

  • (Enumerator)

    will yield each record



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/occams-record/batches.rb', line 17

def find_each(batch_size: 1000)
  enum = Enumerator.new { |y|
    batches(of: batch_size).each { |batch|
      batch.each { |record| y.yield record }
    }
  }
  if block_given?
    enum.each { |record| yield record }
  else
    enum
  end
end

#find_in_batches(batch_size: 1000) {|OccamsRecord::Results::Row| ... } ⇒ Enumerator

Load records in batches of N and yield each batch to a block if given. If no block is given, returns an Enumerator.

NOTE Unlike ActiveRecord’s find_in_batches, order IS respected. The primary key will be appended to the ORDER BY clause to help ensure consistent batches.

Parameters:

  • batch_size (Integer) (defaults to: 1000)

Yields:

Returns:

  • (Enumerator)

    will yield each batch



41
42
43
44
45
46
47
48
# File 'lib/occams-record/batches.rb', line 41

def find_in_batches(batch_size: 1000)
  enum = batches(of: batch_size)
  if block_given?
    enum.each { |batch| yield batch }
  else
    enum
  end
end