Class: ODBA::CacheEntry

Inherits:
Object show all
Defined in:
lib/odba/cache_entry.rb

Overview

:nodoc: all

Constant Summary collapse

@@id_table =
{}
@@finalizer =
proc { |object_id|
  if (odba_id = @@id_table.delete(object_id))
    ODBA.cache.invalidate odba_id
  end
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ CacheEntry

Returns a new instance of CacheEntry.



16
17
18
19
20
21
22
# File 'lib/odba/cache_entry.rb', line 16

def initialize(obj)
  @odba_id = obj.odba_id
  update obj
  @accessed_by = {}
  @odba_observers = obj.odba_observers
  @cache_entry_mutex = Mutex.new
end

Instance Attribute Details

#accessed_byObject (readonly)

Returns the value of attribute accessed_by.



15
16
17
# File 'lib/odba/cache_entry.rb', line 15

def accessed_by
  @accessed_by
end

#last_accessObject

Returns the value of attribute last_access.



14
15
16
# File 'lib/odba/cache_entry.rb', line 14

def last_access
  @last_access
end

#odba_idObject (readonly)

Returns the value of attribute odba_id.



15
16
17
# File 'lib/odba/cache_entry.rb', line 15

def odba_id
  @odba_id
end

#odba_object_idObject (readonly)

Returns the value of attribute odba_object_id.



15
16
17
# File 'lib/odba/cache_entry.rb', line 15

def odba_object_id
  @odba_object_id
end

Instance Method Details

#_odba_objectObject



80
81
82
# File 'lib/odba/cache_entry.rb', line 80

def _odba_object
  @odba_object || object_id2ref(@odba_object_id, @odba_id)
end

#object_id2ref(object_id, odba_id) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/odba/cache_entry.rb', line 37

def object_id2ref(object_id, odba_id)
  if (obj = ObjectSpace._id2ref(object_id)) \
    && obj.is_a?(Persistable) && !obj.odba_unsaved? \
    && obj.odba_id == odba_id
    obj
  end
rescue RangeError, NoMethodError
  nil
end

#odba_add_reference(object) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/odba/cache_entry.rb', line 51

def odba_add_reference(object)
  return unless object.respond_to?(:odba_id)
  @cache_entry_mutex.synchronize do
    @accessed_by.store(object.object_id, object.odba_id)
  end
  object
end

#odba_cut_connections!Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/odba/cache_entry.rb', line 59

def odba_cut_connections!
  @cache_entry_mutex.synchronize do
    @accessed_by.each { |object_id, odba_id|
      if (item = odba_id2ref(odba_id) || object_id2ref(object_id, odba_id)) \
        && item.respond_to?(:odba_cut_connection)
        item.odba_cut_connection(_odba_object)
      end
    }
  end
end

#odba_id2ref(odba_id) ⇒ Object



47
48
49
# File 'lib/odba/cache_entry.rb', line 47

def odba_id2ref(odba_id)
  odba_id && ODBA.cache.include?(odba_id) && ODBA.cache.fetch(odba_id)
end

#odba_notify_observers(*args) ⇒ Object



70
71
72
# File 'lib/odba/cache_entry.rb', line 70

def odba_notify_observers(*args)
  @odba_observers.each { |obs| obs.odba_update(*args) }
end

#odba_objectObject



74
75
76
77
78
# File 'lib/odba/cache_entry.rb', line 74

def odba_object
  @last_access = Time.now
  @odba_object = _odba_object
  @odba_object || ODBA.cache.fetch(@odba_id)
end

#odba_old?(retire_horizon = Time.now - ODBA.cache.retire_age) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/odba/cache_entry.rb', line 84

def odba_old?(retire_horizon = Time.now - ODBA.cache.retire_age)
  !_odba_object.odba_unsaved? \
    && (retire_horizon > @last_access)
end

#odba_replace!(obj) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/odba/cache_entry.rb', line 131

def odba_replace!(obj)
  oldhash = _odba_object.hash
  _odba_object.odba_replace!(obj)
  if _odba_object.hash != oldhash
    @cache_entry_mutex.synchronize do
      @accessed_by.each { |object_id, odba_id|
        if (item = odba_id2ref(odba_id) || object_id2ref(object_id, odba_id))
          item.rehash if item.respond_to? :rehash
        end
      }
    end
  end
end

#odba_retire(opts = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/odba/cache_entry.rb', line 89

def odba_retire opts = {}
  # replace with stubs in accessed_by
  instance = _odba_object
  if opts[:force]
    @cache_entry_mutex.synchronize do
      @accessed_by.each do |object_id, odba_id|
        if item = odba_id2ref(odba_id)
          item.odba_stubize instance, opts
        elsif (item = object_id2ref(object_id, odba_id))
          if item.is_a?(Persistable) && !item.is_a?(Stub)
            item.odba_stubize instance, opts
          end
        end
      end
    end
    @accessed_by.clear
    @odba_object = nil
  else
    @accessed_by.delete_if { |object_id, odba_id|
      if (item = odba_id2ref(odba_id))
        item.odba_stubize instance
      elsif (item = object_id2ref(object_id, odba_id))
        case item
        when Stub
          true
        when Array, Hash
          false
        when Persistable
          item.odba_stubize instance
        else
          true
        end
      else
        true
      end
    }
    if @accessed_by.empty?
      @odba_object = nil
    end
  end
end

#update(obj) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/odba/cache_entry.rb', line 24

def update obj
  @last_access = Time.now
  @odba_object = obj
  @odba_class = obj.class
  @odba_id = obj.odba_id
  unless @odba_object_id == obj.object_id
    @@id_table.delete @odba_object_id
    @odba_object_id = obj.object_id
    @@id_table.store @odba_object_id, @odba_id
    ObjectSpace.define_finalizer obj, @@finalizer
  end
end