Sha256: 93e8662b615d6e14b2e2895b1aef0c09b7037559b3c8281fb211c4699bb4e683

Contents?: true

Size: 1.52 KB

Versions: 37

Compression:

Stored size: 1.52 KB

Contents

module ActiveGraph
  module Core
    module QueryFindInBatches
      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

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

      private

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

      def primary_key_offset(last_record, node_var, prop_var)
        node = last_record[node_var]
        return node.send(prop_var) if node&.respond_to?(prop_var)
        return node.properties[prop_var.to_sym] if node&.respond_to?(:properties)
        last_record["#{node_var}.#{prop_var}"] # In case we're explicitly returning it
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
activegraph-12.0.0.beta.4 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.5.0.beta.3 lib/active_graph/core/query_find_in_batches.rb
activegraph-12.0.0.beta.3 lib/active_graph/core/query_find_in_batches.rb
activegraph-12.0.0.beta.2 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.5.0.beta.2 lib/active_graph/core/query_find_in_batches.rb
activegraph-12.0.0.beta.1 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.5.0.beta.1 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.5.0.alpha.1 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.4.0 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.3.1 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.3.0 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.2.0 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.1.0 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.1.0.beta.1 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.1.0.alpha.4 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.1.0.alpha.3 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.1.0.alpha.2 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.1.0.alpha.1 lib/active_graph/core/query_find_in_batches.rb
activegraph-10.2.0.beta.1 lib/active_graph/core/query_find_in_batches.rb
activegraph-11.0.2-java lib/active_graph/core/query_find_in_batches.rb