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



23
24
25
26
27
28
# File 'lib/serega/batch/attribute_loaders.rb', line 23

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



37
38
39
# File 'lib/serega/batch/attribute_loaders.rb', line 37

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).



66
67
68
69
70
71
72
73
# File 'lib/serega/batch/attribute_loaders.rb', line 66

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



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/serega/batch/attribute_loaders.rb', line 48

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