Module: OccamsRecord::Batches::CursorHelpers

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

Instance Method Summary collapse

Instance Method Details

#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

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

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

NOTE Unlike find_in_batches, 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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/occams-record/batches/cursor_helpers.rb', line 43

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