Class: Serega::SeregaEngine::LevelQueue
- Inherits:
-
Object
- Object
- Serega::SeregaEngine::LevelQueue
- Defined in:
- lib/serega/engine/level_queue.rb
Overview
Level-order queue of serialization levels for one serialization run.
#serialize on the object serializer enqueues a level (objects + their result
containers) instead of resolving values inline. #run then processes the
queue: each level resolves its attributes and, for relations, enqueues child
levels — which the loop picks up because it re-reads the size each iteration.
Processing level-by-level lets a named batch loader and preloads run once per
level over all of its objects.
Instance Method Summary collapse
-
#enqueue(serializer, objects) ⇒ Array<Hash>
Adds objects to the level for their plan (creating it on first use), so objects serialized under the same plan — even from different parents — share one level and load once.
-
#initialize ⇒ LevelQueue
constructor
A new instance of LevelQueue.
-
#run ⇒ void
Processes every level, including child levels enqueued while processing.
Constructor Details
#initialize ⇒ LevelQueue
Returns a new instance of LevelQueue.
19 20 21 22 |
# File 'lib/serega/engine/level_queue.rb', line 19 def initialize @levels = [] @levels_by_plan = {}.compare_by_identity end |
Instance Method Details
#enqueue(serializer, objects) ⇒ Array<Hash>
Adds objects to the level for their plan (creating it on first use), so objects serialized under the same plan — even from different parents — share one level and load once. The level creates a result container per object and returns them for the caller to embed in its parent.
33 34 35 36 37 38 39 40 41 |
# File 'lib/serega/engine/level_queue.rb', line 33 def enqueue(serializer, objects) level = @levels_by_plan[serializer.plan] unless level level = Level.new(serializer) @levels_by_plan[serializer.plan] = level @levels << level end level.add(objects) end |
#run ⇒ void
This method returns an undefined value.
Processes every level, including child levels enqueued while processing.
45 46 47 48 49 50 51 |
# File 'lib/serega/engine/level_queue.rb', line 45 def run i = 0 while i < @levels.size @levels[i].process i += 1 end end |