Class: Upkeep::Subscriptions::LayeredReverseIndex

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

Constant Summary collapse

LOOKUP_NOTIFICATION =
LookupInstrumentation::LOOKUP_NOTIFICATION

Instance Method Summary collapse

Methods included from LookupInstrumentation

#entries_for

Constructor Details

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

Returns a new instance of LayeredReverseIndex.



13
14
15
16
17
18
19
# File 'lib/upkeep/subscriptions/layered_reverse_index.rb', line 13

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_with_payload(changes, payload) ⇒ Object



29
30
31
32
33
34
35
36
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
# File 'lib/upkeep/subscriptions/layered_reverse_index.rb', line 29

def entries_for_with_payload(changes, payload)
  persistent_summary = persistent_index.summary
  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
  payload[:persistent_direct_index_entries] = persistent_summary.fetch(:direct).fetch(:entries)
  payload[:persistent_shape_index_entries] = persistent_summary.fetch(:shape).fetch(:entries)
  payload[:persistent_direct_lookup_keys] = persistent_summary.fetch(:direct).fetch(:lookup_keys)
  payload[:persistent_shape_lookup_keys] = persistent_summary.fetch(:shape).fetch(:lookup_keys)
  payload[:persistent_shape_keys] = persistent_summary.fetch(:shape).fetch(:shape_keys)
  payload[:persistent_shape_subscriptions] = persistent_summary.fetch(:shape).fetch(:subscriptions)

  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



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

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



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

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