Class WeakIdentityMap<TKey, TValue>
Implements a combination of java.util.WeakHashMap
and
java.util.IdentityHashMap
.
Useful for caches that need to key off of a ==
comparison
instead of a .Equals(object)
.
This class is not a general-purpose System.Collections.Generic.IDictionary<TKey, TValue> implementation! It intentionally violates System.Collections.Generic.IDictionary<TKey, TValue>'s general contract, which mandates the use of the System.Object.Equals(System.Object) method when comparing objects. This class is designed for use only in the rare cases wherein reference-equality semantics are required.
This implementation was forked from Apache CXF
but modified to not implement the System.Collections.Generic.IDictionary<TKey, TValue> interface and
without any set views on it, as those are error-prone and inefficient,
if not implemented carefully. The map only contains System.Collections.Generic.IEnumerable<T>.GetEnumerator() implementations
on the values and not-GCed keys. Lucene's implementation also supports null
keys, but those are never weak!
The map supports two modes of operation:
reapOnRead = true
: This behaves identical to ajava.util.WeakHashMap
where it also cleans up the reference queue on every read operation (Get(Object), ContainsKey(Object), Count, GetValueEnumerator()), freeing map entries of already GCed keys.reapOnRead = false
: This mode does not call Reap() on every read operation. In this case, the reference queue is only cleaned up on write operations (like Put(TKey, TValue)). This is ideal for maps with few entries where the keys are unlikely be garbage collected, but there are lots of Get(Object) operations. The code can still call Reap() to manually clean up the queue without doing a write operation.
Inheritance
Inherited Members
Namespace: Lucene.Net.Util
Assembly: Lucene.Net.dll
Syntax
public sealed class WeakIdentityMap<TKey, TValue>
where TKey : class
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
Properties
| Improve this Doc View SourceCount
Returns the number of key-value mappings in this map. This result is a snapshot, and may not reflect unprocessed entries that will be removed before next attempted access because they are no longer referenced.
NOTE: This was size() in Lucene.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
IsEmpty
Returns true
if this map contains no key-value mappings.
Declaration
public bool IsEmpty { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Keys
Gets an System.Collections.Generic.IEnumerable<T> object containing the keys of the WeakIdentityMap<TKey, TValue>.
Declaration
public IEnumerable<TKey> Keys { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<TKey> |
Values
Gets an System.Collections.Generic.IEnumerable<T> object containing the values of the WeakIdentityMap<TKey, TValue>.
Declaration
public IEnumerable<TValue> Values { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<TValue> |
Methods
| Improve this Doc View SourceClear()
Removes all of the mappings from this map.
Declaration
public void Clear()
ContainsKey(Object)
Returns true
if this map contains a mapping for the specified key.
Declaration
public bool ContainsKey(object key)
Parameters
Type | Name | Description |
---|---|---|
System.Object | key |
Returns
Type | Description |
---|---|
System.Boolean |
Get(Object)
Returns the value to which the specified key is mapped.
Declaration
public TValue Get(object key)
Parameters
Type | Name | Description |
---|---|---|
System.Object | key |
Returns
Type | Description |
---|---|
TValue |
GetValueEnumerator()
Returns an iterator over all values of this map.
This iterator may return values whose key is already
garbage collected while iterator is consumed,
especially if Lucene.Net.Util.WeakIdentityMap`2.reapOnRead is false
.
NOTE: This was valueIterator() in Lucene.
Declaration
public IEnumerator<TValue> GetValueEnumerator()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerator<TValue> |
NewConcurrentHashMap()
Creates a new WeakIdentityMap<TKey, TValue> based on a System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>. The map cleans up the reference queue on every read operation.
Declaration
public static WeakIdentityMap<TKey, TValue> NewConcurrentHashMap()
Returns
Type | Description |
---|---|
WeakIdentityMap<TKey, TValue> |
NewConcurrentHashMap(Boolean)
Creates a new WeakIdentityMap<TKey, TValue> based on a System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.
Declaration
public static WeakIdentityMap<TKey, TValue> NewConcurrentHashMap(bool reapOnRead)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | reapOnRead | controls if the map cleans up the reference queue on every read operation. |
Returns
Type | Description |
---|---|
WeakIdentityMap<TKey, TValue> |
NewHashMap()
Creates a new WeakIdentityMap<TKey, TValue> based on a non-synchronized System.Collections.Generic.Dictionary`2. The map cleans up the reference queue on every read operation.
Declaration
public static WeakIdentityMap<TKey, TValue> NewHashMap()
Returns
Type | Description |
---|---|
WeakIdentityMap<TKey, TValue> |
NewHashMap(Boolean)
Creates a new WeakIdentityMap<TKey, TValue> based on a non-synchronized System.Collections.Generic.Dictionary`2.
Declaration
public static WeakIdentityMap<TKey, TValue> NewHashMap(bool reapOnRead)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | reapOnRead | controls if the map cleans up the reference queue on every read operation. |
Returns
Type | Description |
---|---|
WeakIdentityMap<TKey, TValue> |
Put(TKey, TValue)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.
Declaration
public TValue Put(TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | |
TValue | value |
Returns
Type | Description |
---|---|
TValue |
Reap()
This method manually cleans up the reference queue to remove all garbage
collected key/value pairs from the map. Calling this method is not needed
if reapOnRead = true
. Otherwise it might be a good idea
to call this method when there is spare time (e.g. from a background thread).
Information about the reapOnRead
setting
Declaration
public void Reap()
Remove(Object)
Removes the mapping for a key from this weak hash map if it is present.
Returns the value to which this map previously associated the key,
or null
if the map contained no mapping for the key.
A return value of null
does not necessarily indicate that
the map contained.
Declaration
public bool Remove(object key)
Parameters
Type | Name | Description |
---|---|---|
System.Object | key |
Returns
Type | Description |
---|---|
System.Boolean |