Class: Factbase::Logged

Inherits:
Object
  • Object
show all
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

Classes: Fact, Query, Tube

Constant Summary collapse

MONO =
Process::CLOCK_MONOTONIC

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fb, log = nil, time_tolerate: 1, tube: nil) ⇒ Logged

Ctor.

Parameters:

  • fb (Factbase)

    The factbase to decorate

  • log (Object) (defaults to: nil)

    The logging facility

  • time_tolerate (Integer) (defaults to: 1)

    How many seconds are OK per request

  • tube (Print) (defaults to: nil)

    The tube to use, if log is NIL

Raises:

  • (ArgumentError)


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

.elapsedObject



198
199
200
201
# File 'lib/factbase/logged.rb', line 198

def self.elapsed
  yield
  "in #{Time.now.ago}"
end

Instance Method Details

#insertObject



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

#txnObject



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