Class: Upkeep::Subscriptions::LayeredReverseIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/upkeep/subscriptions/layered_reverse_index.rb

Constant Summary collapse

LOOKUP_NOTIFICATION =
"lookup_subscription_index.upkeep"

Instance Method Summary collapse

Constructor Details

#initialize(active_index:, persistent_index:, persistent_count:, store:, pending_index: nil) ⇒ LayeredReverseIndex

Returns a new instance of LayeredReverseIndex.



10
11
12
13
14
15
16
# File 'lib/upkeep/subscriptions/layered_reverse_index.rb', line 10

def initialize(active_index:, persistent_index:, persistent_count:, store:, pending_index: nil)
  @active_index = active_index
  @persistent_index = persistent_index
  @persistent_count = persistent_count
  @store = store
  @pending_index = pending_index
end

Instance Method Details

#entries_for(changes) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/upkeep/subscriptions/layered_reverse_index.rb', line 18

def entries_for(changes)
  if ActiveSupport::Notifications.notifier.listening?(LOOKUP_NOTIFICATION)
    payload = { changes: Array(changes).size, store: store }
    ActiveSupport::Notifications.instrument(LOOKUP_NOTIFICATION, payload) do
      entries_for_with_payload(changes, payload)
    end
  else
    entries_for_without_payload(changes)
  end
end

#entries_for_with_payload(changes, payload) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/upkeep/subscriptions/layered_reverse_index.rb', line 37

def entries_for_with_payload(changes, payload)
  active_entries = active_index.entries_for(changes)
  active_count = active_index.count
  pending_entries = pending_entries_for(changes)
  pending_count = pending_count_for_payload
  payload[:active_entries] = active_entries.size
  payload[:active_subscriptions] = active_count
  payload[:pending_entries] = pending_entries.size
  payload[:pending_subscriptions] = pending_count

  if active_count.zero?
    persistent_entries = persistent_index.entries_for(changes)
    payload[:mode] = persistent_entries.empty? && pending_entries.any? ? "pending_activation" : "persistent"
    payload[:persistent_entries] = persistent_entries.size
    apply_miss_reason(payload, active_entries: active_entries, persistent_entries: persistent_entries, pending_entries: pending_entries)
    return persistent_entries
  end

  if active_index.covers?(persistent_subscription_count)
    payload[:mode] = "active"
    payload[:persistent_entries] = 0
    apply_miss_reason(payload, active_entries: active_entries, persistent_entries: [], pending_entries: pending_entries)
    return active_entries
  end

  persistent_entries = persistent_index.entries_for(changes)
  payload[:mode] = "active_and_persistent"
  payload[:persistent_entries] = persistent_entries.size
  apply_miss_reason(payload, active_entries: active_entries, persistent_entries: persistent_entries, pending_entries: pending_entries)
  merge_entries(active_entries, persistent_entries)
end

#entries_for_without_payload(changes) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/upkeep/subscriptions/layered_reverse_index.rb', line 29

def entries_for_without_payload(changes)
  active_entries = active_index.entries_for(changes)
  return persistent_index.entries_for(changes) if active_index.count.zero?
  return active_entries if active_index.covers?(persistent_subscription_count)

  merge_entries(active_entries, persistent_index.entries_for(changes))
end

#summaryObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/upkeep/subscriptions/layered_reverse_index.rb', line 69

def summary
  persistent = persistent_index.summary
  active = active_index.summary
  pending = pending_index&.summary || { lookup_keys: 0, entries: 0, subscriptions: 0 }
  mode = active_index.covers?(persistent_subscription_count) ? :active : :active_and_persistent
  totals = if mode == :active
    active
  else
    {
      lookup_keys: active.fetch(:lookup_keys) + persistent.fetch(:lookup_keys),
      entries: active.fetch(:entries) + persistent.fetch(:entries)
    }
  end

  {
    lookup_keys: totals.fetch(:lookup_keys),
    entries: totals.fetch(:entries),
    mode: mode,
    active: active,
    pending: pending,
    persistent: persistent
  }
end