Class: FastInserter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_inserter/fast_inserter_base.rb

Constant Summary collapse

DEFAULT_GROUP_SIZE =
1_000

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fast_inserter/fast_inserter_base.rb', line 49

def initialize(params)
  @table_name = params[:table]
  @static_columns = params[:static_columns]
  @additional_columns = params[:additional_columns]
  @variable_columns = Array(params[:variable_columns] || params[:variable_column])
  @options = params[:options] || {}

  # We want to break up the insertions into multiple transactiosn in case there
  # is a very large amount of values. This avoids PG:OutOfMemory errors and smooths
  # out the load. The second 'false' param means don't fill in the last group with nil elements.
  all_values = params[:values].map { |value| Array(value) }
  all_values.uniq! if @options[:unique]
  group_size = Integer(params[:group_size] || ENV['FAST_INSERTER_GROUP_SIZE'] || DEFAULT_GROUP_SIZE)
  @value_groups = all_values.each_slice(group_size).to_a
end

Instance Method Details

#fast_insertObject

Iterates through the value groups (which is all values in groups of smaller sizes) and generates and executes a transaction to insert those groups one at a time



67
68
69
70
71
72
73
# File 'lib/fast_inserter/fast_inserter_base.rb', line 67

def fast_insert
  return if nothing_to_insert?

  @value_groups.each do |group|
    fast_insert_group(group)
  end
end