Method: OccamsRecord::Batches::CursorHelpers#find_each_with_cursor

Defined in:
lib/occams-record/batches/cursor_helpers.rb

#find_each_with_cursor(batch_size: 1000, use_transaction: true) {|OccamsRecord::Results::Row| ... } ⇒ Enumerator

Loads records in batches of N and yields each record to a block (if given). If no block is given, returns an Enumerator.

NOTE Unlike find_each, batches are loaded using a cursor, which offers better performance. Postgres only. See the docs for OccamsRecord::Cursor for more details.

Parameters:

  • batch_size (Integer) (defaults to: 1000)

    fetch this many rows at once

  • use_transaction (Boolean) (defaults to: true)

    Ensure it runs inside of a database transaction

Yields:

Returns:

  • (Enumerator)

    will yield each record



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

def find_each_with_cursor(batch_size: 1000, use_transaction: true)
  enum = Enumerator.new { |y|
    cursor.open(use_transaction: use_transaction) { |c|
      c.each(batch_size: batch_size) { |record|
        y.yield record
      }
    }
  }
  if block_given?
    enum.each { |record| yield record }
  else
    enum
  end
end