Method: Weak::Map::StrongSecondaryKeys#prune

Defined in:
lib/weak/map/strong_secondary_keys.rb

#pruneself

Cleanup data structures from the map to remove data associated with deleted or garbage collected keys and/or values. This method may be called automatically for some Weak::Map operations.

Returns:

  • (self)


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/weak/map/strong_secondary_keys.rb', line 185

def prune
  orphaned_value_keys = ::Set.new(@values.keys)
  remaining_keys = ::Set.new

  @keys.keys.each do |id|
    if orphaned_value_keys.delete?(id)
      # Here, we have found a valid value belonging to the key. As both
      # key and value are valid, we keep the @key_map entry.
      remaining_keys << id
    else
      # Here, the value was missing (i.e. garbage collected). We mark the
      # still present key as deleted
      @keys[id] = DeletedEntry.new
    end
  end

  # Mark all (remaining) values as deleted for which we have not found a
  # matching key above
  orphaned_value_keys.each do |id|
    @values[id] = DeletedEntry.new
  end

  # Finally, remove all @key_map entries for which we have not seen a
  # valid key and value above
  @key_map.keep_if { |_, id| remaining_keys.include?(id) }

  self
end