class ActiveRecord::Import::ValueSetsBytesParser
def self.parse(values, options)
def self.parse(values, options) new(values, options).parse end
def default_max_bytes
def default_max_bytes values_in_bytes = values.sum(&:bytesize) comma_separated_bytes = values.size - 1 reserved_bytes + values_in_bytes + comma_separated_bytes end
def initialize(values, options)
def initialize(values, options) @values = values @reserved_bytes = options[:reserved_bytes] || 0 @max_bytes = options.fetch(:max_bytes) { default_max_bytes } end
def parse
def parse value_sets = [] arr = [] current_size = 0 values.each_with_index do |val, i| comma_bytes = arr.size insert_size = reserved_bytes + val.bytesize if insert_size > max_bytes raise ValueSetTooLargeError.new("#{insert_size} bytes exceeds the max allowed for an insert [#{@max_bytes}]", insert_size) end bytes_thus_far = reserved_bytes + current_size + val.bytesize + comma_bytes if bytes_thus_far <= max_bytes current_size += val.bytesize arr << val else value_sets << arr arr = [val] current_size = val.bytesize end # if we're on the last iteration push whatever we have in arr to value_sets value_sets << arr if i == (values.size - 1) end value_sets end