Class: JobIteration::ActiveRecordBatchEnumerator::ColumnManager

Inherits:
Object
  • Object
show all
Defined in:
lib/job-iteration/active_record_batch_enumerator/column_manager.rb

Overview

Utility class for the batch enumerator that manages the columns that need to be plucked. It ensures primary key columns are plucked so that records in the batch can be queried for efficiently.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation:, columns:) ⇒ ColumnManager

Returns a new instance of ColumnManager.

Parameters:

  • relation (ActiveRecord::Relation)
    • relation to manage columns for

  • columns (Array<String,Symbol>, nil)
    • set of columns to select



13
14
15
16
17
18
19
20
21
# File 'lib/job-iteration/active_record_batch_enumerator/column_manager.rb', line 13

def initialize(relation:, columns:)
  @table_name = relation.table_name
  @primary_key = Array(relation.primary_key)
  @qualified_pkey_columns = @primary_key.map { |col| qualify_column(col) }
  @columns = columns&.map(&:to_s) || @qualified_pkey_columns

  validate_columns!(relation)
  initialize_pluck_columns_and_pkey_positions
end

Instance Attribute Details

#columnsArray<String> (readonly)

Returns The list of columns to be plucked. If no columns were specified, this list contains the fully qualified primary key column(s).

Returns:

  • (Array<String>)

    The list of columns to be plucked. If no columns were specified, this list contains the fully qualified primary key column(s).



26
27
28
# File 'lib/job-iteration/active_record_batch_enumerator/column_manager.rb', line 26

def columns
  @columns
end

#pluck_columnsArray<String> (readonly)

Returns The full set of columns to be plucked from the relation. This is a superset of columns and is guaranteed to contain all of the primary key columns on the relation.

Returns:

  • (Array<String>)

    The full set of columns to be plucked from the relation. This is a superset of columns and is guaranteed to contain all of the primary key columns on the relation.



37
38
39
# File 'lib/job-iteration/active_record_batch_enumerator/column_manager.rb', line 37

def pluck_columns
  @pluck_columns
end

#primary_keyArray<String> (readonly)

Returns The list of primary key columns for the relation. These columns are not qualified with the table name.

Returns:

  • (Array<String>)

    The list of primary key columns for the relation. These columns are not qualified with the table name.



31
32
33
# File 'lib/job-iteration/active_record_batch_enumerator/column_manager.rb', line 31

def primary_key
  @primary_key
end

Instance Method Details

#pkey_values(column_values) ⇒ Array<Array>

Returns List where each item contains the primary key column values for the corresponding row. Values are guaranteed to be in the same order as the columns are listed in primary_key.

Parameters:

  • column_values (Array<Array>)

    List of rows where each row contains values as determined by pluck_columns.

Returns:

  • (Array<Array>)

    List where each item contains the primary key column values for the corresponding row. Values are guaranteed to be in the same order as the columns are listed in primary_key.



47
48
49
50
51
52
53
54
# File 'lib/job-iteration/active_record_batch_enumerator/column_manager.rb', line 47

def pkey_values(column_values)
  column_values.map do |values|
    @qualified_pkey_columns.map do |pkey_column|
      pkey_column_idx = @primary_key_index_map[pkey_column]
      values[pkey_column_idx]
    end
  end
end

#remove_missing_pkey_values(cursor) ⇒ Array

Returns The same values that were passed in, minus any primary key column values that do not appear in columns.

Parameters:

  • cursor (Array)

    A list of values for a single row, as determined by pluck_columns.

Returns:

  • (Array)

    The same values that were passed in, minus any primary key column values that do not appear in columns.



62
63
64
65
# File 'lib/job-iteration/active_record_batch_enumerator/column_manager.rb', line 62

def remove_missing_pkey_values(cursor)
  cursor.pop(@missing_pkey_count)
  cursor
end