Class: Coupler::Models::Scenario::Runner
- Inherits:
-
Object
- Object
- Coupler::Models::Scenario::Runner
- Defined in:
- lib/coupler/models/scenario/runner.rb
Constant Summary collapse
- LIMIT =
10000
Instance Method Summary collapse
-
#initialize(parent, &progress) ⇒ Runner
constructor
A new instance of Runner.
- #run! ⇒ Object
Constructor Details
#initialize(parent, &progress) ⇒ Runner
Returns a new instance of Runner.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/coupler/models/scenario/runner.rb', line 7 def initialize(parent, &progress) @parent = parent @progress = progress @matcher = parent.matcher @type = parent.linkage_type if @type == 'cross-linkage' @resources = [parent.resource_1, parent.resource_1] else @resources = parent.resources end @run_number = @parent.run_count + 1 @mutex = Mutex.new @group_number = 0 setup_pairs create_tables end |
Instance Method Details
#run! ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/coupler/models/scenario/runner.rb', line 24 def run! @parent.local_database do |scenario_db| @groups_dataset = scenario_db[@groups_table_name] @groups_buffer = ImportBuffer.new(@groups_column_names, @groups_dataset) @join_dataset = scenario_db[@join_table_name] @join_buffer = ImportBuffer.new([:record_id, :which, :group_id], @join_dataset) # Group records for each dataset. This step is the same for both # self-linkage and dual-linkage. However, this is the only step # for self-linkage. Dual-linkage requires another pass after # this. # # Cross-matching on a single dataset is treated as a dual-linkage. # @pairs = @phase_one_pairs tw = ThreadsWait.new databases_to_close = [] @resources.each_with_index do |resource, i| dataset = resource.final_dataset databases_to_close << dataset.db primary_key = resource.primary_key_sym which = @type == 'self-linkage' ? nil : i resource_thread = phase_one_thread(dataset, primary_key, which) tw.join_nowait(resource_thread) end tw.all_waits databases_to_close.each do |db| # tidy up db.disconnect ::Sequel::DATABASES.delete(db) end if @type != 'self-linkage' # Phase 2! secondary_groups_ds = scenario_db[@secondary_groups_table_name].order(:group_1_id, :group_2_id) @join_buffer = ImportBuffer.new([:group_1_id, :group_2_id], secondary_groups_ds) @pairs = @phase_two_pairs phase_two(@groups_dataset, :id) @join_buffer.flush # Update groups and groups_records tw = ThreadsWait.new count = secondary_groups_ds.count offset = 0 last_group_id = @group_number while offset < count dataset = secondary_groups_ds.limit(10, offset) offset += 10 dataset.each do |row| thread = Thread.new(row[:group_1_id], row[:group_2_id]) do |group_1_id, group_2_id| new_group_id = get_next_group_id @join_dataset.filter(:group_id => [group_1_id, group_2_id]).update(:group_id => new_group_id) @groups_dataset.filter(:id => group_1_id).update(:id => new_group_id) @groups_dataset.filter(:id => group_2_id).delete end thread.abort_on_exception = true tw.join_nowait(thread) end tw.all_waits end # Clean up groups and records that don't match @groups_dataset.filter(:id <= last_group_id).delete @join_dataset.filter(:group_id <= last_group_id).delete # Don't need the secondary table anymore scenario_db.drop_table(@secondary_groups_table_name) end # Calculate some summary stats @join_dataset.group_and_count(:group_id, :which).each do |row| col = row[:which] ? :"resource_#{row[:which]+1}_count" : :"resource_1_count" @groups_dataset.filter(:id => row[:group_id]).update(col => row[:count]) end end end |