Module: ActiveGraph::Core::QueryFindInBatches

Included in:
Query
Defined in:
lib/active_graph/core/query_find_in_batches.rb

Instance Method Summary collapse

Instance Method Details

#find_each(*args, &block) ⇒ Object



27
28
29
# File 'lib/active_graph/core/query_find_in_batches.rb', line 27

def find_each(*args, &block)
  find_in_batches(*args) { |batch| batch.each(&block) }
end

#find_in_batches(node_var, prop_var, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/active_graph/core/query_find_in_batches.rb', line 4

def find_in_batches(node_var, prop_var, options = {})
  validate_find_in_batches_options!(options)

  batch_size = options.delete(:batch_size) || 1000

  query = reorder(node_var => prop_var).limit(batch_size)

  records = query.to_a

  while records.any?
    records_size = records.size
    primary_key_offset = primary_key_offset(records.last, node_var, prop_var)

    yield records

    break if records_size < batch_size

    primary_key_var = ActiveGraph::Core::QueryClauses::Clause.from_key_and_single_value(node_var, prop_var)
    records = query.where("#{primary_key_var} > $primary_key_offset")
                   .params(primary_key_offset: primary_key_offset).to_a
  end
end