Class: Factbase::Looged

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/looged.rb

Overview

A decorator of a Factbase, that logs all operations.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Defined Under Namespace

Classes: Fact, Query

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fb, loog) ⇒ Looged

Returns a new instance of Looged.



35
36
37
38
39
40
# File 'lib/factbase/looged.rb', line 35

def initialize(fb, loog)
  raise 'The "fb" is nil' if fb.nil?
  @fb = fb
  raise 'The "loog" is nil' if loog.nil?
  @loog = loog
end

Class Method Details

.elapsedObject



187
188
189
190
191
# File 'lib/factbase/looged.rb', line 187

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

Instance Method Details

#dupObject



44
45
46
# File 'lib/factbase/looged.rb', line 44

def dup
  Factbase::Looged.new(@fb.dup, @loog)
end

#insertObject



48
49
50
51
52
# File 'lib/factbase/looged.rb', line 48

def insert
  f = @fb.insert
  @loog.debug("Inserted new fact ##{@fb.size}")
  Fact.new(f, @loog)
end

#query(query) ⇒ Object



54
55
56
# File 'lib/factbase/looged.rb', line 54

def query(query)
  Query.new(@fb, query, @loog)
end

#txn(this = self) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/factbase/looged.rb', line 58

def txn(this = self, &)
  start = Time.now
  id = nil
  rollback = false
  r =
    @fb.txn(this) do |fbt|
      id = fbt.object_id
      yield fbt
    rescue Factbase::Rollback => e
      rollback = true
      raise e
    end
  if rollback
    @loog.debug("Txn ##{id} rolled back in #{start.ago}")
  else
    @loog.debug("Txn ##{id} #{r ? 'modified' : 'didn\'t touch'} the factbase in #{start.ago}")
  end
  r
end