Class: Lutaml::Model::Store
- Inherits:
-
Object
- Object
- Lutaml::Model::Store
- Defined in:
- lib/lutaml/model/store.rb
Constant Summary collapse
- COMPACTION_THRESHOLD =
Compact dead WeakRef shells once a class bucket grows past this size.
1000- COMPACTION_INTERVAL =
Once the threshold is exceeded, only compact every Nth subsequent register call. Amortises the O(N) reject! over N inserts so register stays O(1) per call rather than O(N) per call (O(N^2) cumulatively for the class).
1000
Class Method Summary collapse
- .clear ⇒ Object
- .instance ⇒ Object
- .register(object) ⇒ Object
- .reset! ⇒ Object
- .resolve(model_class, reference_key, reference_value) ⇒ Object
- .store ⇒ Object
Instance Method Summary collapse
- #clear ⇒ Object
- #compaction_count ⇒ Object
- #index_entry_count(model_key) ⇒ Object
-
#initialize ⇒ Store
constructor
A new instance of Store.
- #inserts_since_compaction ⇒ Object
- #refs_for(model_key) ⇒ Object
- #register(object) ⇒ Object
- #resolve(model_class, reference_key, reference_value) ⇒ Object
- #store ⇒ Object
Constructor Details
#initialize ⇒ Store
Returns a new instance of Store.
44 45 46 47 48 49 50 51 |
# File 'lib/lutaml/model/store.rb', line 44 def initialize @store = ::Hash.new { |hash, key| hash[key] = [] } # Nested index: { model_key => { reference_key => { value => WeakRef(object) } } } # Grouped by model_key so register only iterates this class's own indices. @index = {} @inserts_since_compaction = ::Hash.new(0) @compaction_count = 0 end |
Class Method Details
.clear ⇒ Object
35 36 37 |
# File 'lib/lutaml/model/store.rb', line 35 def clear instance.clear end |
.instance ⇒ Object
19 20 21 |
# File 'lib/lutaml/model/store.rb', line 19 def instance @instance ||= new end |
.register(object) ⇒ Object
27 28 29 |
# File 'lib/lutaml/model/store.rb', line 27 def register(object) instance.register(object) end |
.reset! ⇒ Object
23 24 25 |
# File 'lib/lutaml/model/store.rb', line 23 def reset! @instance = new end |
.resolve(model_class, reference_key, reference_value) ⇒ Object
31 32 33 |
# File 'lib/lutaml/model/store.rb', line 31 def resolve(model_class, reference_key, reference_value) instance.resolve(model_class, reference_key, reference_value) end |
.store ⇒ Object
39 40 41 |
# File 'lib/lutaml/model/store.rb', line 39 def store instance.store end |
Instance Method Details
#clear ⇒ Object
81 82 83 84 85 86 |
# File 'lib/lutaml/model/store.rb', line 81 def clear @store = ::Hash.new { |hash, key| hash[key] = [] } @index = {} @inserts_since_compaction = ::Hash.new(0) @compaction_count = 0 end |
#compaction_count ⇒ Object
106 107 108 |
# File 'lib/lutaml/model/store.rb', line 106 def compaction_count @compaction_count end |
#index_entry_count(model_key) ⇒ Object
110 111 112 |
# File 'lib/lutaml/model/store.rb', line 110 def index_entry_count(model_key) @index[model_key]&.sum { |_reference_key, entries| entries.size } || 0 end |
#inserts_since_compaction ⇒ Object
102 103 104 |
# File 'lib/lutaml/model/store.rb', line 102 def inserts_since_compaction @inserts_since_compaction end |
#refs_for(model_key) ⇒ Object
98 99 100 |
# File 'lib/lutaml/model/store.rb', line 98 def refs_for(model_key) @store[model_key] end |
#register(object) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/lutaml/model/store.rb', line 53 def register(object) model_key = object.class.to_s refs = @store[model_key] refs << WeakRef.new(object) @inserts_since_compaction[model_key] += 1 compact_if_needed(refs, model_key) update_existing_indices(object, model_key) end |
#resolve(model_class, reference_key, reference_value) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lutaml/model/store.rb', line 64 def resolve(model_class, reference_key, reference_value) model_key = model_class.to_s model_indices = @index[model_key] unless model_indices&.key?(reference_key) model_indices = ensure_model_index(model_key) build_index(model_indices, model_key, reference_key) end entry = model_indices[reference_key][reference_value] return nil unless entry obj = dereference(entry) model_indices[reference_key].delete(reference_value) unless obj obj end |