lib/superstore/scope/batches.rb in superstore-1.2.0 vs lib/superstore/scope/batches.rb in superstore-2.0.0
- old
+ new
@@ -1,32 +1,27 @@
module Superstore
- class Scope
- module Batches
- def find_each(options = {})
- find_in_batches(options) do |records|
- records.each { |record| yield record }
- end
+ module Batches
+ def find_each(options = {})
+ batch_size = options[:batch_size] || 1000
+
+ klass.adapter.scroll(self, batch_size) do |key, attributes|
+ yield klass.instantiate(key, attributes)
end
+ end
- def find_in_batches(options = {})
- batch_size = options.delete(:batch_size) || 1000
- start_key = nil
+ def find_in_batches(options = {})
+ batch_size = options[:batch_size] || 1000
+ batch = []
- scope = limit(batch_size + 1).order(:id)
- records = scope.to_a
+ find_each(options) do |record|
+ batch << record
- while records.any?
- if records.size > batch_size
- next_record = records.pop
- else
- next_record = nil
- end
-
- yield records
- break if next_record.nil?
-
- records = scope.where("#{adapter.primary_key_column} >= '#{next_record.id}'").to_a
+ if batch.size == batch_size
+ yield batch
+ batch = []
end
end
+
+ yield(batch) if batch.any?
end
end
end