Created
January 12, 2013 09:28
-
-
Save andreasronge/4516914 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TransactionalExec | |
attr_accessor :commit_every, :block | |
def initialize(commit_every, max_lines = nil, verbose = true, &block) | |
@commit_every = commit_every | |
@block = block | |
@max_lines = max_lines | |
@verbose = verbose | |
end | |
def execute(enumerable) | |
time_of_all = Time.now | |
tx = Neo4j::Transaction.new | |
time_in_ruby = Time.now | |
index = 0 | |
enumerable.each do |item| | |
@block.call(item) | |
index += 1 | |
if (index % @commit_every == 0) | |
time_spent_in_ruby_code = Time.now - time_in_ruby | |
time_spent_in_write_to_file = Time.now | |
tx.success | |
tx.finish | |
tx = Neo4j::Transaction.new | |
puts " Written #{index} items. time_spend_in_ruby_code: #{time_spent_in_ruby_code}, time_spent_in_write_to_file #{Time.now - time_spent_in_write_to_file}" if @verbose | |
time_in_ruby = Time.now | |
end | |
break if @max_lines && index > @max_lines | |
end | |
tx.success | |
tx.finish | |
puts "Total, written #{index} items in #{Time.now - time_of_all} sec." if @verbose | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment