Module: Dnsruby::RR::GenericCache
- Defined in:
- lib/dnsruby/resource/generic_cache.rb
Overview
Only automatically generated integer pairs belong to this cache. Explicit registrations and the public Generic.create factory retain their lifetime.
Defined Under Namespace
Constant Summary collapse
- LIMIT =
:nodoc: all
256- READ =
Hash.instance_method(:fetch)
- WRITE =
Hash.instance_method(:[]=)
- DELETE =
Hash.instance_method(:delete)
- KEYS =
Hash.instance_method(:keys)
- HAS_KEY =
Hash.instance_method(:key?)
- REMOVE_CONSTANT =
Module.instance_method(:remove_const)
Class Method Summary collapse
- .automatic?(type, klass) ⇒ Boolean
- .create(type, klass) ⇒ Object
- .eligible?(type, klass) ⇒ Boolean
- .factory(type, klass) ⇒ Object
- .find(type, klass) ⇒ Object
- .forget(snapshot, pairs = nil, removed_only: false) ⇒ Object
- .key(type, klass) ⇒ Object
- .named(name) ⇒ Object
- .peek(type, klass) ⇒ Object
- .register(snapshot, pair, value) ⇒ Object
-
.registry_pair(pair) ⇒ Object
Custom Hash keys can address a different pair from their Array contents, or need not be Arrays at all.
- .rehash ⇒ Object
-
.snapshot(pair = nil) ⇒ Object
Native Hash mutations run outside the lock.
Class Method Details
.automatic?(type, klass) ⇒ Boolean
79 80 81 82 83 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 79 def automatic?(type, klass) return false unless eligible?(type, klass) && Thread.current[:dnsruby_generic_factory] == [type, klass] Thread.current[:dnsruby_generic_factory] = nil true end |
.create(type, klass) ⇒ Object
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 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 85 def create(type, klass) # Class.new invokes the application's inherited hook. Never hold the # registry lock while running it, or while setting up the new Class. generated = Class.new(Generic) generated.const_set(:TypeValue, type) generated.const_set(:ClassValue, klass) name = key(type, klass) generated.instance_variable_set(:@generic_display_name, "Dnsruby::RR::Generic::#{name}".freeze) entry = Entry.new(name, [type, klass].freeze, generated, nil, nil, true) # WeakMap's values are weak too. The class keeps its mutable bookkeeping # alive even when a client freezes the class itself. generated.instance_variable_set(:@generic_cache_entry, entry) @lock.synchronize do registered = READ.bind_call(ClassHash, entry.pair, nil) return registered if registered found = automatic_entry(name) return retain(found) if reusable?(found) # A previous explicit factory call may have left a native constant # after its registry entry was deleted. Let lookup (including YAML) # reach the replacement class instead of that stale binding. if GenericConstantInspection::NATIVE_DEFINED.bind_call(Generic, name, false) REMOVE_CONSTANT.bind_call(Generic, name) end index = @names[name] entry.index = index || NameIndex.new entry.index.current = WeakRef.new(entry) unless index @names[name] = entry.index @indices[entry.index] = entry.index end retain(entry) end end |
.eligible?(type, klass) ⇒ Boolean
40 41 42 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 40 def eligible?(type, klass) type.instance_of?(Integer) && klass.instance_of?(Integer) && type >= 0 && klass >= 0 end |
.factory(type, klass) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 71 def factory(type, klass) previous = Thread.current[:dnsruby_generic_factory] Thread.current[:dnsruby_generic_factory] = eligible?(type, klass) ? [type, klass] : nil yield ensure Thread.current[:dnsruby_generic_factory] = previous end |
.find(type, klass) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 55 def find(type, klass) return nil unless eligible?(type, klass) pair = [type, klass] @lock.synchronize do registered = READ.bind_call(ClassHash, pair, nil) return registered if registered found = automatic_entry(key(type, klass)) retain(found) if reusable?(found) end end |
.forget(snapshot, pairs = nil, removed_only: false) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 163 def forget(snapshot, pairs = nil, removed_only: false) names = pairs&.filter_map { |pair| pair_name(pair) } @lock.synchronize do entries = snapshot.keys entries.select! { |entry| names.include?(entry.name) } if names entries += names ? names.filter_map { |name| automatic_entry(name) } : @fifo.keys entries.each do |entry| next if pairs && !pairs.any? { |pair| targets?(entry, pair) } unchanged = snapshot.key?(entry) && snapshot[entry] == entry.version unchanged &&= !READ.bind_call(ClassHash, registry_key(entry), nil).equal?(entry.klass) if removed_only discard(entry) if unchanged || stale?(entry) end end end |
.key(type, klass) ⇒ Object
44 45 46 47 48 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 44 def key(type, klass) # WeakMap uses object identity. Deduplicated strings give equal pairs # the same collectible key, which the live Class owns. -("Type#{type}_Class#{klass}".encode(Encoding::UTF_8)) end |
.named(name) ⇒ Object
50 51 52 53 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 50 def named(name) return nil unless name.ascii_only? @lock.synchronize { indexed_entry(-name.encode(Encoding::UTF_8))&.klass } end |
.peek(type, klass) ⇒ Object
66 67 68 69 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 66 def peek(type, klass) return nil unless eligible?(type, klass) @lock.synchronize { automatic_entry(key(type, klass))&.klass } end |
.register(snapshot, pair, value) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 178 def register(snapshot, pair, value) name = pair_name(pair) return unless name @lock.synchronize do identity = ClassHash.compare_by_identity? previous = snapshot.keys.find do |entry| entry.klass.equal?(value) && targets?(entry, pair) && (identity ? entry.registry_key.equal?(pair) : entry.name == name) end # FIFO eviction may have run after the explicit write but before its # cleanup. Preserve a successfully assigned automatic class. if previous && previous.automatic && !@fifo.key?(previous) && !moved?(previous) && !READ.bind_call(ClassHash, registry_key(previous), nil) actual = identity ? pair : previous.pair.dup WRITE.bind_call(ClassHash, actual, value) end entries = identity ? @fifo.keys.select { |entry| entry.registry_key.equal?(pair) } : [automatic_entry(name)].compact entries.each do |entry| next unless targets?(entry, pair) discard(entry) if READ.bind_call(ClassHash, registry_key(entry), nil).equal?(value) end end end |
.registry_pair(pair) ⇒ Object
Custom Hash keys can address a different pair from their Array contents, or need not be Arrays at all. Resolve them with Ruby's own stored-key comparison, outside the cache lock (hash/eql? may call back).
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 122 def registry_pair(pair) return pair if ClassHash.compare_by_identity? native_method = Object.instance_method(:method) if Array === pair && native_method.bind_call(pair, :hash).owner == Array && native_method.bind_call(pair, :eql?).owner == Array return pair end copy = Hash.instance_method(:dup).bind_call(ClassHash) keys = KEYS.bind_call(copy) index = 0 Hash.instance_method(:transform_values!).bind_call(copy) do |_| stored = keys[index] index += 1 stored end missing = Object.new stored = READ.bind_call(copy, pair, missing) return stored unless stored.equal?(missing) # A live entry may already have left the FIFO. An explicit deletion # still invalidates that entry when the custom key addresses its pair. entries = @lock.synchronize { automatic_entries } weak = entries.to_h { |entry| [entry.pair, entry.pair] } READ.bind_call(weak, pair, pair) end |
.rehash ⇒ Object
200 201 202 203 204 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 200 def rehash @lock.synchronize do (automatic_entries + @fifo.keys).each { |entry| discard(entry) if moved?(entry) || stale?(entry) } end end |
.snapshot(pair = nil) ⇒ Object
Native Hash mutations run outside the lock. Their cleanup must not remove a newer publication by a concurrent or reentrant lookup.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/dnsruby/resource/generic_cache.rb', line 148 def snapshot(pair = nil) name = pair_name(pair) if pair return {} if pair && !name @lock.synchronize do entries = if pair && ClassHash.compare_by_identity? @fifo.keys.select { |entry| entry.registry_key.equal?(pair) } elsif pair [automatic_entry(name)].compact else automatic_entries + @fifo.keys end entries.each_with_object({}.compare_by_identity) { |entry, saved| saved[entry] = entry.version } end end |