Class: Nfcollector::Partitioner

Inherits:
Object
  • Object
show all
Defined in:
lib/nfcollector/partitioner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(partition_klass, account_id, column_index) ⇒ Partitioner

TODO: use 2.1 named parameters when we can guarantee Ruby 2.1



7
8
9
10
11
12
# File 'lib/nfcollector/partitioner.rb', line 7

def initialize(partition_klass, , column_index)
  @partition_klass = partition_klass
  @account_id      = 
  @column_index    = column_index
  @data            = {}
end

Instance Attribute Details

#column_indexesObject (readonly)

Returns the value of attribute column_indexes.



3
4
5
# File 'lib/nfcollector/partitioner.rb', line 3

def column_indexes
  @column_indexes
end

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/nfcollector/partitioner.rb', line 4

def data
  @data
end

Instance Method Details

#add_row(row) ⇒ Object



18
19
20
21
22
23
# File 'lib/nfcollector/partitioner.rb', line 18

def add_row(row)
  p_value = row_value(row)
  p_id = @partition_klass.partition_id(p_value)
  @data[p_id] ||= @partition_klass.new(p_value, @account_id, @headers)
  @data[p_id].add_row(row)
end

#commit!(account_id) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/nfcollector/partitioner.rb', line 29

def commit!()
  @data.map do |(key,partition)|
    CopyFileWriter.open(partition) do |copy|
      copy.commit!
    end
  end
end

#row_value(row) ⇒ Object



25
26
27
# File 'lib/nfcollector/partitioner.rb', line 25

def row_value(row)
  row[@column_index]
end

#set_headers!(headers) ⇒ Object



14
15
16
# File 'lib/nfcollector/partitioner.rb', line 14

def set_headers!(headers)
  @headers = headers
end