Class: BulkProcessor::RowChunker::Balanced

Inherits:
Object
  • Object
show all
Defined in:
lib/bulk_processor/row_chunker/balanced.rb

Overview

Determine the partitions for a balanced break up of the input CSV file. All partitions will have a size within 1 row of every other partition.

Instance Method Summary collapse

Constructor Details

#initialize(num_chunks) ⇒ Balanced

Returns a new instance of Balanced.



6
7
8
# File 'lib/bulk_processor/row_chunker/balanced.rb', line 6

def initialize(num_chunks)
  @num_chunks = num_chunks
end

Instance Method Details

#ranges_for(csv) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bulk_processor/row_chunker/balanced.rb', line 10

def ranges_for(csv)
  ideal_size = csv.count / num_chunks
  num_chunks.times.map do |index|
    start_index = index * ideal_size
    if index == num_chunks - 1
      # force the last chunk to go to the very last row
      end_index = csv.count - 1
    else
      end_index = start_index + ideal_size - 1
    end
    (start_index..end_index)
  end
end