lib/mass_insert/process.rb in mass_insert-0.1.3 vs lib/mass_insert/process.rb in mass_insert-0.2.0

- old
+ new

@@ -1,33 +1,33 @@ -require 'benchmark' - module MassInsert class Process + attr_reader :values, :options - def initialize values, options + def initialize(values, options) @values = values @options = options end def start - # MassInsert process is completed by two actions. The first one - # gets queries that will be persisted. - @building_time = Benchmark.measure do - @queries = builder.build(@values, @options) + ActiveRecord::Base.transaction do + values.each_slice(per_batch).each do |batch| + query = builder.build(batch, options) + executer.execute(query) + end end - - # The second one executes queries into the database using an - # ActiveRecord connection. - @execution_time = Benchmark.measure do - executer.execute(@queries) - end end + private + def builder - @builder ||= Builder::Base.new + @builder ||= Builder.new end def executer @executer ||= Executer.new + end + + def per_batch + options[:per_batch] || Utilities.per_batch end end end