Class: ODBA::CacheEntry
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
-
#accessed_by ⇒ Object
readonly
Returns the value of attribute accessed_by.
-
#last_access ⇒ Object
Returns the value of attribute last_access.
-
#odba_id ⇒ Object
readonly
Returns the value of attribute odba_id.
-
#odba_object_id ⇒ Object
readonly
Returns the value of attribute odba_object_id.
Instance Method Summary collapse
- #_odba_object ⇒ Object
-
#initialize(obj) ⇒ CacheEntry
constructor
A new instance of CacheEntry.
- #object_id2ref(object_id, odba_id) ⇒ Object
- #odba_add_reference(object) ⇒ Object
- #odba_cut_connections! ⇒ Object
- #odba_id2ref(odba_id) ⇒ Object
- #odba_notify_observers(*args) ⇒ Object
- #odba_object ⇒ Object
- #odba_old?(retire_horizon = Time.now - ODBA.cache.retire_age) ⇒ Boolean
- #odba_replace!(obj) ⇒ Object
- #odba_retire(opts = {}) ⇒ Object
- #update(obj) ⇒ Object
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_by ⇒ Object (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_access ⇒ Object
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_id ⇒ Object (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_id ⇒ Object (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_object ⇒ Object
72 73 74 |
# File 'lib/odba/cache_entry.rb', line 72 def _odba_object @odba_object || object_id2ref(@odba_object_id, @odba_id) end |
#object_id2ref(object_id, odba_id) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/odba/cache_entry.rb', line 35 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 => e nil end |
#odba_add_reference(object) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/odba/cache_entry.rb', line 47 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
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/odba/cache_entry.rb', line 54 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
44 45 46 |
# File 'lib/odba/cache_entry.rb', line 44 def odba_id2ref(odba_id) odba_id && ODBA.cache.include?(odba_id) && ODBA.cache.fetch(odba_id) end |
#odba_notify_observers(*args) ⇒ Object
64 65 66 |
# File 'lib/odba/cache_entry.rb', line 64 def odba_notify_observers(*args) @odba_observers.each { |obs| obs.odba_update(*args) } end |
#odba_object ⇒ Object
67 68 69 70 71 |
# File 'lib/odba/cache_entry.rb', line 67 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
75 76 77 78 |
# File 'lib/odba/cache_entry.rb', line 75 def odba_old?(retire_horizon = Time.now - ODBA.cache.retire_age) !_odba_object.odba_unsaved? \ && (retire_horizon > @last_access) end |
#odba_replace!(obj) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/odba/cache_entry.rb', line 120 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
79 80 81 82 83 84 85 86 87 88 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 |
# File 'lib/odba/cache_entry.rb', line 79 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
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/odba/cache_entry.rb', line 23 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 |