Class: Igniter::Store::FactLog

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/igniter/store/fact_log.rb,
lib/igniter/store/fact_log.rb

Overview

Patch the Rust-native FactLog to expose all_facts. The native append is intercepted to track which stores have been written; all_facts then collects via facts_for(store:) per known store.

Instance Method Summary collapse

Constructor Details

#initializeFactLog

Returns a new instance of FactLog.



12
13
14
15
16
17
# File 'lib/igniter/store/fact_log.rb', line 12

def initialize
  super()
  @log = []
  @by_id = {}
  @by_key = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#_native_append_unwrappedObject



87
88
89
90
91
92
93
94
# File 'lib/igniter/store/fact_log.rb', line 87

def append(fact)
  synchronize do
    @log << fact
    @by_id[fact.id] = fact
    @by_key[[fact.store, fact.key]] << fact
  end
  fact
end

#all_factsObject



65
66
67
# File 'lib/igniter/store/fact_log.rb', line 65

def all_facts
  synchronize { @log.dup }
end

#append(fact) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/igniter/store/fact_log.rb', line 19

def append(fact)
  synchronize do
    @log << fact
    @by_id[fact.id] = fact
    @by_key[[fact.store, fact.key]] << fact
  end
  fact
end

#facts_for(store:, key: nil, since: nil, as_of: nil) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/igniter/store/fact_log.rb', line 42

def facts_for(store:, key: nil, since: nil, as_of: nil)
  synchronize do
    facts = key ? @by_key[[store, key]].dup : @log.select { |fact| fact.store == store }
    facts = facts.select { |fact| fact.transaction_time >= since } if since
    facts = facts.select { |fact| fact.transaction_time <= as_of } if as_of
    facts
  end
end

#latest_for(store:, key:, as_of: nil) ⇒ Object



36
37
38
39
40
# File 'lib/igniter/store/fact_log.rb', line 36

def latest_for(store:, key:, as_of: nil)
  facts = synchronize { @by_key[[store, key]].dup }
  facts = facts.select { |fact| fact.transaction_time <= as_of } if as_of
  facts.last
end

#query_scope(store:, filters:, as_of: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/igniter/store/fact_log.rb', line 51

def query_scope(store:, filters:, as_of: nil)
  synchronize do
    seen = {}
    @by_key.each do |(s, k), facts|
      next unless s == store
      candidates = as_of ? facts.select { |f| f.transaction_time <= as_of } : facts
      latest = candidates.last
      next unless latest
      seen[k] = latest if matches_filters?(latest.value, filters)
    end
    seen.values
  end
end

#replay(fact) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/igniter/store/fact_log.rb', line 28

def replay(fact)
  synchronize do
    @log << fact
    @by_id[fact.id] = fact
    @by_key[[fact.store, fact.key]] << fact
  end
end

#sizeObject



69
70
71
# File 'lib/igniter/store/fact_log.rb', line 69

def size
  synchronize { @log.size }
end