Class: Serega::SeregaBatch::AttributeLoaders

Inherits:
Object
  • Object
show all
Defined in:
lib/serega/batch/attribute_loaders.rb

Overview

Batch loaders

Remembers data required to batch load all attributes

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ AttributeLoaders

Initializes new batch loaders

Parameters:

  • context (Hash)

    Serialization context, constant for the whole run



18
19
20
21
22
23
# File 'lib/serega/batch/attribute_loaders.rb', line 18

def initialize(context)
  @context = context
  @attribute_loaders = []
  @point_index = {}.compare_by_identity
  @levels = {}.compare_by_identity
end

Instance Method Details

#add_objects(plan, objects) ⇒ void

This method returns an undefined value.

Gathers a chunk of objects serialized at a plan level, so every attribute at that level shares one set of objects to batch load against.

Parameters:

  • plan (SeregaPlan)

    Plan serializing these objects

  • objects (Array)

    Objects being serialized at this level



32
33
34
# File 'lib/serega/batch/attribute_loaders.rb', line 32

def add_objects(plan, objects)
  level_for(plan).add_objects(objects)
end

#load_allObject

Loads all registered batches and attaches loaded values.

Iterates over each loader including loaders added during iteration (child serializers discovered inside a batch flush add new loaders).



61
62
63
64
65
66
67
68
# File 'lib/serega/batch/attribute_loaders.rb', line 61

def load_all
  i = 0
  while i < attribute_loaders.size
    attribute_loader = attribute_loaders[i]
    attribute_loader.attach(load_batches(attribute_loader))
    i += 1
  end
end

#remember(point, object, attacher) ⇒ void

This method returns an undefined value.

Remembers data for batch serialization:

Parameters:

  • point (SeregaPlanPoint)

    Serialization plan point

  • object (Object)

    Serialized object

  • attacher (Proc)

    Serialized value attacher



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/serega/batch/attribute_loaders.rb', line 43

def remember(point, object, attacher)
  attribute_loader = point_index[point]

  unless attribute_loader
    serializer_class = point.class.serializer_class
    attribute_loader = serializer_class::SeregaBatchAttributeLoader.new(point, level_for(point.plan))
    attribute_loaders << attribute_loader
    point_index[point] = attribute_loader
  end

  attribute_loader.store(object, attacher)
end