Skip to content

Commit

Permalink
Use activerecord-import for bulk importing tracepoint records
Browse files Browse the repository at this point in the history
Non-rigourous testing shows a significant speedup, even on ssds.
  • Loading branch information
gravitystorm committed Jan 23, 2019
1 parent d02e4ad commit 74e1b98
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -46,6 +46,7 @@ gem "image_optim_rails"
# Load rails plugins
gem "actionpack-page_caching"
gem "active_record_union"
gem "activerecord-import"
gem "cancancan"
gem "composite_primary_keys", "~> 11.1.0"
gem "delayed_job_active_record"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -40,6 +40,8 @@ GEM
activemodel (= 5.2.2)
activesupport (= 5.2.2)
arel (>= 9.0)
activerecord-import (0.28.1)
activerecord (>= 3.2)
activestorage (5.2.2)
actionpack (= 5.2.2)
activerecord (= 5.2.2)
Expand Down Expand Up @@ -388,6 +390,7 @@ DEPENDENCIES
aasm
actionpack-page_caching
active_record_union
activerecord-import
annotate
autoprefixer-rails (~> 8.6.3)
better_errors
Expand Down
10 changes: 9 additions & 1 deletion app/models/trace.rb
Expand Up @@ -289,6 +289,8 @@ def import
# If there are any existing points for this trace then delete them
Tracepoint.where(:gpx_id => id).delete_all

# Gather the trace points together for a bulk import
tracepoints = []
gpx.points do |point|
if first
f_lat = point.latitude
Expand All @@ -303,9 +305,15 @@ def import
tp.timestamp = point.timestamp
tp.gpx_id = id
tp.trackid = point.segment
tp.save!
tracepoints << tp
end

# Run all the before_save callbacks, and then import them in bulk with activerecord-import
tracepoints.each do |tp|
tp.run_callbacks(:save) { false }
end
Tracepoint.import(tracepoints)

if gpx.actual_points.positive?
max_lat = Tracepoint.where(:gpx_id => id).maximum(:latitude)
min_lat = Tracepoint.where(:gpx_id => id).minimum(:latitude)
Expand Down
3 changes: 3 additions & 0 deletions test/models/trace_test.rb
Expand Up @@ -221,6 +221,9 @@ def test_import_creates_tracepoints

trace.reload
assert_equal 1, Tracepoint.where(:gpx_id => trace.id).count

# Check that the tile has been set prior to the bulk import
assert_equal 3221331576, Tracepoint.where(:gpx_id => trace.id).first.tile
end
end

Expand Down

0 comments on commit 74e1b98

Please sign in to comment.