Module: Neo4j::Core::QueryFindInBatches

Included in:
Query
Defined in:
lib/neo4j-core/query_find_in_batches.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) find_each(*args)



34
35
36
37
38
# File 'lib/neo4j-core/query_find_in_batches.rb', line 34

def find_each(*args)
  find_in_batches(*args) do |batch|
    batch.each { |result| yield result }
  end
end

- (Object) find_in_batches(node_var, prop_var, options = {})



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/neo4j-core/query_find_in_batches.rb', line 4

def find_in_batches(node_var, prop_var, options = {})
  invalid_keys = options.keys.map(&:to_sym) - [:batch_size]
  fail ArgumentError, "Invalid keys: #{invalid_keys.join(', ')}" if not invalid_keys.empty?

  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 = begin
                           records.last.send(node_var).send(prop_var)
                         rescue NoMethodError
                           begin
                             records.last.send(node_var)[prop_var.to_sym]
                           rescue NoMethodError
                             records.last.send("#{node_var}.#{prop_var}") # In case we're explicitly returning it
                           end
                         end

    yield records

    break if records_size < batch_size

    records = query.where("#{node_var}.#{prop_var} > {primary_key_offset}").params(primary_key_offset: primary_key_offset).to_a
  end
end