Class: Foobara::WeakObjectSet
- Inherits:
-
Object
- Object
- Foobara::WeakObjectSet
show all
- Includes:
- Enumerable
- Defined in:
- foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb,
foobara-0.0.130/projects/weak_object_set/lib/foobara/weak_object_set.rb
Overview
TODO: a possible optimization: have a certain number of records before the Weakref approach kicks in that way we don’t just immediately clear out useful information without any actual memory burden
Defined Under Namespace
Classes: GarbageCleaner, InvalidWtf
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(key_method = nil) ⇒ WeakObjectSet
Returns a new instance of WeakObjectSet.
75
76
77
78
79
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 75
def initialize(key_method = nil)
self.key_method = key_method
self.monitor = Monitor.new
clear
end
|
Instance Attribute Details
#garbage_cleaner ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 135
def garbage_cleaner
@garbage_cleaner ||= begin
queue = Queue.new
gc = GarbageCleaner.new(self, queue)
ObjectSpace.define_finalizer gc do
queue.close
end
gc
end
end
|
#key_method ⇒ Object
Returns the value of attribute key_method.
72
73
74
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 72
def key_method
@key_method
end
|
#key_to_object_id ⇒ Object
Returns the value of attribute key_to_object_id.
72
73
74
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 72
def key_to_object_id
@key_to_object_id
end
|
#monitor ⇒ Object
Returns the value of attribute monitor.
72
73
74
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 72
def monitor
@monitor
end
|
#object_id_to_key ⇒ Object
Returns the value of attribute object_id_to_key.
72
73
74
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 72
def object_id_to_key
@object_id_to_key
end
|
#objects ⇒ Object
Returns the value of attribute objects.
72
73
74
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 72
def objects
@objects
end
|
Instance Method Details
#<<(object) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 151
def <<(object)
object_id = object.object_id
monitor.synchronize do
existing_object = self[object_id]
if existing_object
if key_method
key = object.send(key_method)
old_key = object_id_to_key[object_id]
if key != old_key
key_to_object_id.delete(old_key)
if key
key_to_object_id[key] = object_id
object_id_to_key[object_id] = key
else
object_id_to_key.delete(object_id)
end
end
end
else
garbage_cleaner.track(object)
if key_method
key = object.send(key_method)
if key
existing_record_object_id = key_to_object_id[key]
if existing_record_object_id
delete(existing_record_object_id)
end
key_to_object_id[key] = object_id
object_id_to_key[object_id] = key
end
end
objects[object_id] = WeakRef.new(object)
object
end
end
end
|
#[](object_or_object_id) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 81
def [](object_or_object_id)
monitor.synchronize do
ref = ref_for(object_or_object_id)
object = begin
ref&.__getobj__
rescue WeakRef::RefError
nil
end
if ref&.weakref_alive?
object
end
end
end
|
#clear ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 238
def clear
monitor.synchronize do
garbage_cleaner.deactivate
self.garbage_cleaner = nil
self.objects = {}
if key_method
self.key_to_object_id = {}
self.object_id_to_key = {}
end
end
end
|
#delete(object_or_object_id) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 202
def delete(object_or_object_id)
object_id = if object_or_object_id.is_a?(::Integer)
object_or_object_id
else
object_or_object_id.object_id
end
monitor.synchronize do
if key_method
key = object_id_to_key.delete(object_id)
if key
key_to_object_id.delete(key)
end
end
objects.delete(object_id)
end
end
|
#each ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 109
def each
monitor.synchronize do
objects.each_value do |ref|
object = begin
ref.__getobj__
rescue WeakRef::RefError
nil
end
if ref.weakref_alive?
yield object
end
end
end
end
|
#empty? ⇒ Boolean
129
130
131
132
133
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 129
def empty?
monitor.synchronize do
objects.empty? || objects.values.none?(&:weakref_alive?)
end
end
|
#find_by_key(key) ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 222
def find_by_key(key)
monitor.synchronize do
unless key_method
raise "Cannot find by key if there was no key_method given."
end
object_id = key_to_object_id[key]
if object_id
self[object_id]
end
end
end
|
#ref_for(object_or_object_id) ⇒ Object
99
100
101
102
103
104
105
106
107
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 99
def ref_for(object_or_object_id)
object_id = if object_or_object_id.is_a?(::Integer)
object_or_object_id
else
object_or_object_id.object_id
end
objects[object_id]
end
|
#size ⇒ Object
125
126
127
|
# File 'foobara-0.0.130/projects/weak_object_set/src/weak_object_set.rb', line 125
def size
count
end
|