Class: Prefab::ContextShapeAggregator

Inherits:
Object
  • Object
show all
Includes:
PeriodicSync
Defined in:
lib/prefab/context_shape_aggregator.rb

Constant Summary collapse

LOG =
Prefab::InternalLogger.new(ContextShapeAggregator)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PeriodicSync

#on_prepare_data, #pool, #post, #start_periodic_sync, #sync

Constructor Details

#initialize(client:, max_shapes:, sync_interval:) ⇒ ContextShapeAggregator

Returns a new instance of ContextShapeAggregator.



13
14
15
16
17
18
19
20
21
# File 'lib/prefab/context_shape_aggregator.rb', line 13

def initialize(client:, max_shapes:, sync_interval:)
  @max_shapes = max_shapes
  @client = client
  @name = 'context_shape_aggregator'

  @data = Concurrent::Set.new

  start_periodic_sync(sync_interval)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



11
12
13
# File 'lib/prefab/context_shape_aggregator.rb', line 11

def data
  @data
end

Instance Method Details

#prepare_dataObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/prefab/context_shape_aggregator.rb', line 33

def prepare_data
  duped = @data.dup
  @data.clear

  duped.inject({}) do |acc, (name, key, type)|
    acc[name] ||= {}
    acc[name][key] = type
    acc
  end
end

#push(context) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/prefab/context_shape_aggregator.rb', line 23

def push(context)
  return if @data.size >= @max_shapes

  context.contexts.each_pair do |name, name_context|
    name_context.to_h.each_pair do |key, value|
      @data.add [name, key, Prefab::ContextShape.field_type_number(value)]
    end
  end
end