Sha256: 0d3632e9bbf83c517fa526b5d0aca5937352140965895b9e23923ec86fa16480

Contents?: true

Size: 1.42 KB

Versions: 49

Compression:

Stored size: 1.42 KB

Contents

module Neo4j
  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

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

      def find_each(*args)
        find_in_batches(*args) do |batch|
          batch.each { |result| yield result }
        end
      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)
        last_record.send(node_var).send(prop_var)
      rescue NoMethodError
        begin
          last_record.send(node_var)[prop_var.to_sym]
        rescue NoMethodError
          last_record.send("#{node_var}.#{prop_var}") # In case we're explicitly returning it
        end
      end
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
neo4j-core-6.0.0.alpha.5 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-6.0.0.alpha.2 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.1.7 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-6.0.0.alpha.1 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.1.6 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.1.5 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.1.4 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.1.3 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.1.2 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.1.1 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.1.0 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.1.0.rc.3 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.1.0.rc.2 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.1.0.rc.1 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.0.10 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.0.9 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.0.8 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.0.7 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.0.6 lib/neo4j-core/query_find_in_batches.rb
neo4j-core-5.0.5 lib/neo4j-core/query_find_in_batches.rb