# File lib/active_graph/core/query_find_in_batches.rb 38 def primary_key_offset(last_record, node_var, prop_var) 39 node = last_record[node_var] 40 return node.send(prop_var) if node&.respond_to?(prop_var) 41 return node.properties[prop_var.to_sym] if node&.respond_to?(:properties) 42 last_record["#{node_var}.#{prop_var}"] # In case we're explicitly returning it 43 end
module ActiveGraph::Core::QueryFindInBatches
Public Instance Methods
find_each(*args, &block)
click to toggle source
# File lib/active_graph/core/query_find_in_batches.rb 27 def find_each(*args, &block) 28 find_in_batches(*args) { |batch| batch.each(&block) } 29 end
find_in_batches(node_var, prop_var, options = {}) { |records| ... }
click to toggle source
# File lib/active_graph/core/query_find_in_batches.rb 4 def find_in_batches(node_var, prop_var, options = {}) 5 validate_find_in_batches_options!(options) 6 7 batch_size = options.delete(:batch_size) || 1000 8 9 query = reorder(node_var => prop_var).limit(batch_size) 10 11 records = query.to_a 12 13 while records.any? 14 records_size = records.size 15 primary_key_offset = primary_key_offset(records.last, node_var, prop_var) 16 17 yield records 18 19 break if records_size < batch_size 20 21 primary_key_var = ActiveGraph::Core::QueryClauses::Clause.from_key_and_single_value(node_var, prop_var) 22 records = query.where("#{primary_key_var} > $primary_key_offset") 23 .params(primary_key_offset: primary_key_offset).to_a 24 end 25 end
Private Instance Methods
primary_key_offset(last_record, node_var, prop_var)
click to toggle source
validate_find_in_batches_options!(options)
click to toggle source
# File lib/active_graph/core/query_find_in_batches.rb 33 def validate_find_in_batches_options!(options) 34 invalid_keys = options.keys.map(&:to_sym) - [:batch_size] 35 fail ArgumentError, "Invalid keys: #{invalid_keys.join(', ')}" if not invalid_keys.empty? 36 end