Class: Chicago::ETL::KeyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/chicago/etl/key_builder.rb

Overview

Builds a surrogate key for a dimension record, without relying on the database’s AUTO_INCREMENT functionality.

We avoid AUTO_INCREMENT because we need to be able to get the key mappings without having anything to do with the database - this allows us to use bulk load.

Defined Under Namespace

Classes: Factory

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_table) ⇒ KeyBuilder

Returns a new instance of KeyBuilder.



77
78
79
80
# File 'lib/chicago/etl/key_builder.rb', line 77

def initialize(key_table)
  @key_table = key_table
  @counter = Counter.new { key_table.max(:dimension_id) }
end

Class Method Details

.for_table(table, staging_db) ⇒ Object

Returns an appropriate key builder for a schema table, using the staging database for key management where necessary.



73
74
75
# File 'lib/chicago/etl/key_builder.rb', line 73

def self.for_table(table, staging_db)
  Factory.new(table, staging_db).make
end

Instance Method Details

#key(row) ⇒ Object

Returns a surrogate key, given a record row.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chicago/etl/key_builder.rb', line 86

def key(row)
  fetch_cache unless @key_mapping
  row_id = original_key(row)
  new_key = @key_mapping[row_id]
  
  if new_key
    [new_key, nil]
  else
    new_key = @counter.next
    @key_mapping[row_id] = new_key

    [new_key, {
       :original_id => row_id, 
       :dimension_id => new_key
     }]
  end
end

#original_key(row) ⇒ Object

Returns the original key for the row.

Overridden by subclasses.



107
108
# File 'lib/chicago/etl/key_builder.rb', line 107

def original_key(row)
end