Class: Upkeep::Invalidation::Planner

Inherits:
Object
  • Object
show all
Defined in:
lib/upkeep/invalidation/planner.rb

Defined Under Namespace

Classes: Plan, PlannedTarget

Instance Method Summary collapse

Constructor Details

#initialize(store:) ⇒ Planner

Returns a new instance of Planner.



48
49
50
# File 'lib/upkeep/invalidation/planner.rb', line 48

def initialize(store:)
  @store = store
end

Instance Method Details

#plan(changes) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/upkeep/invalidation/planner.rb', line 52

def plan(changes)
  changes = Array(changes)
  payload = { change_count: changes.size }
  ActiveSupport::Notifications.instrument("plan.upkeep", payload) do
    @delivery_strategy_cache = {}
    candidate_entries = store.reverse_index.entries_for(changes)
    matched_entries = candidate_entries.select { |entry| changes.any? { |change| entry.dependency.matches_change?(change) } }
    entries_by_subscription_id = matched_entries.group_by(&:subscription_id)
    subscriptions_by_id = entries_by_subscription_id.keys.to_h do |subscription_id|
      [subscription_id, store.fetch(subscription_id)]
    end
    represented_subscriber_ids = matched_entries.flat_map(&:represented_subscriber_ids).uniq
    represented_subscriber_ids = subscriptions_by_id.values.map(&:subscriber_id).uniq if represented_subscriber_ids.empty?
    shared_delivery = represented_subscriber_ids.size > 1

    targets = entries_by_subscription_id.flat_map do |subscription_id, entries|
      targets_for_subscription(
        subscriptions_by_id.fetch(subscription_id),
        entries,
        changes,
        shared_delivery: shared_delivery
      )
    end

    plan = Plan.new(deduplicate_targets(targets), candidate_entries, matched_entries)
    payload.merge!(payload_for(plan))
    plan
  end
ensure
  @delivery_strategy_cache = nil
end