Class: Factbase::Logged
- Inherits:
-
Object
- Object
- Factbase::Logged
- Defined in:
- lib/factbase/logged.rb
Overview
A decorator of a Factbase, that logs all operations.
- Author
-
Yegor Bugayenko (yegor256@gmail.com)
- Copyright
-
Copyright © 2024-2026 Yegor Bugayenko
- License
-
MIT
Defined Under Namespace
Constant Summary collapse
- MONO =
Process::CLOCK_MONOTONIC
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(fb, log = nil, time_tolerate: 1, tube: nil) ⇒ Logged
constructor
Ctor.
- #insert ⇒ Object
- #query(term, maps = nil) ⇒ Object
- #txn ⇒ Object
Constructor Details
#initialize(fb, log = nil, time_tolerate: 1, tube: nil) ⇒ Logged
Ctor.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/factbase/logged.rb', line 25 def initialize(fb, log = nil, time_tolerate: 1, tube: nil) raise(ArgumentError, 'The "fb" is nil') if fb.nil? @origin = fb if log.nil? raise(ArgumentError, 'Either "log" or "tube" must be non-NIL') if tube.nil? @tube = tube else @tube = Tube.new(log, time_tolerate:) end end |
Class Method Details
.elapsed ⇒ Object
198 199 200 201 |
# File 'lib/factbase/logged.rb', line 198 def self.elapsed yield "in #{Time.now.ago}" end |
Instance Method Details
#insert ⇒ Object
38 39 40 41 |
# File 'lib/factbase/logged.rb', line 38 def insert @tube.say(Process.clock_gettime(MONO), "Inserted new fact ##{@origin.size} in #{Time.now.ago}") Fact.new(@origin.insert, tube: @tube) end |
#query(term, maps = nil) ⇒ Object
43 44 45 46 |
# File 'lib/factbase/logged.rb', line 43 def query(term, maps = nil) term = to_term(term) if term.is_a?(String) Query.new(term, maps, @tube, @origin) end |
#txn ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/factbase/logged.rb', line 48 def txn mono = Process.clock_gettime(MONO) id = nil rollback = false r = @origin.txn do |fbt| id = fbt.object_id yield(Factbase::Logged.new(fbt, tube: @tube)) rescue Factbase::Rollback => e rollback = true raise(e) end if rollback @tube.say(mono, "Txn ##{id} rolled back in #{Time.now.ago}") else @tube.say(mono, "Txn ##{id} touched #{r} in #{Time.now.ago}") end r end |