Class: Coupler::ImportBuffer
- Inherits:
-
Object
- Object
- Coupler::ImportBuffer
- Defined in:
- lib/coupler/import_buffer.rb
Overview
This class is used during resource transformation. Its purpose is for mass inserts into the local database for speed.
Constant Summary collapse
- MAX_QUERY_SIZE =
1_048_576
Instance Attribute Summary collapse
-
#dataset ⇒ Object
writeonly
Sets the attribute dataset.
Instance Method Summary collapse
- #add(row) ⇒ Object
- #flush(lock = true) ⇒ Object
-
#initialize(columns, dataset, &progress) ⇒ ImportBuffer
constructor
A new instance of ImportBuffer.
Constructor Details
#initialize(columns, dataset, &progress) ⇒ ImportBuffer
Returns a new instance of ImportBuffer.
9 10 11 12 13 14 15 |
# File 'lib/coupler/import_buffer.rb', line 9 def initialize(columns, dataset, &progress) @columns = columns @dataset = dataset @mutex = Mutex.new @progress = progress @pending = 0 end |
Instance Attribute Details
#dataset=(value) ⇒ Object (writeonly)
Sets the attribute dataset
7 8 9 |
# File 'lib/coupler/import_buffer.rb', line 7 def dataset=(value) @dataset = value end |
Instance Method Details
#add(row) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/coupler/import_buffer.rb', line 17 def add(row) fragment = " " + @dataset.literal(row.is_a?(Hash) ? row.values_at(*@columns) : row) + "," @mutex.synchronize do init_query if @query.nil? if (@query.length + fragment.length) > MAX_QUERY_SIZE flush(false) init_query end @query << fragment @pending += 1 end end |
#flush(lock = true) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/coupler/import_buffer.rb', line 30 def flush(lock = true) begin @mutex.lock if lock if @query @dataset.db.run(@query.chomp(",")) @progress.call(@pending) if @progress @pending = 0 @query = nil end ensure @mutex.unlock if lock end end |