Class: Dynamini::ItemSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamini/item_splitter.rb

Constant Summary collapse

MAX_SIZE =
380_000

Class Method Summary collapse

Class Method Details

.split(attribute_updates) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dynamini/item_splitter.rb', line 8

def split(attribute_updates)
  unprocessed_au = attribute_updates.map { |k, v| {k => v} }
  updates = []
  current_update_size = 0
  current_update = {}

  while unprocessed_au.length > 0 do
    size = au_size(unprocessed_au[0])
    if size > MAX_SIZE
      part_one, part_two = split_au(unprocessed_au[0])
      unprocessed_au.shift
      unprocessed_au.unshift(part_two)
      unprocessed_au.unshift(part_one)
    else
      current_update_size += size
      if current_update_size > MAX_SIZE
        updates.push(current_update)
        current_update_size = 0
        current_update = {}
      else
        current_update_size += size
        key, value = key_and_value(unprocessed_au[0])
        current_update[key] = value
        unprocessed_au.shift
      end
    end
  end

  updates.push(current_update) unless current_update.empty?
  updates
end