Sha256: 9a5075014d1d3531f8c26a79125694684975d24e8b86ebe9d44c14817cc8f7b9

Contents?: true

Size: 1.95 KB

Versions: 6

Compression:

Stored size: 1.95 KB

Contents

require 'active_support/core_ext/array'

module ActiveRecord::Import
  class ValueSetTooLargeError < StandardError
    attr_reader :size
    def initialize(msg = "Value set exceeds max size", size = 0)
      @size = size
      super(msg)
    end
  end

  class ValueSetsBytesParser
    attr_reader :reserved_bytes, :max_bytes, :values

    def self.parse(values, options)
      new(values, options).parse
    end

    def initialize(values, options)
      @values = values
      @reserved_bytes = options[:reserved_bytes] || 0
      @max_bytes = options.fetch(:max_bytes) { default_max_bytes }
    end

    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

    private

    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
  end

  class ValueSetsRecordsParser
    attr_reader :max_records, :values

    def self.parse(values, options)
      new(values, options).parse
    end

    def initialize(values, options)
      @values = values
      @max_records = options[:max_records]
    end

    def parse
      @values.in_groups_of(max_records, false)
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
activerecord-import-1.4.0 lib/activerecord-import/value_sets_parser.rb
activerecord-import-1.3.0 lib/activerecord-import/value_sets_parser.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/activerecord-import-1.2.0/lib/activerecord-import/value_sets_parser.rb
activerecord-import-1.2.0 lib/activerecord-import/value_sets_parser.rb
activerecord-import-1.1.0 lib/activerecord-import/value_sets_parser.rb
activerecord-import-1.0.8 lib/activerecord-import/value_sets_parser.rb