Show / Hide Table of Contents

    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 a java.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.

    This is a Lucene.NET INTERNAL API, use at your own risk
    Inheritance
    System.Object
    WeakIdentityMap<TKey, TValue>
    Inherited Members
    System.Object.Equals(System.Object)
    System.Object.Equals(System.Object, System.Object)
    System.Object.GetHashCode()
    System.Object.GetType()
    System.Object.MemberwiseClone()
    System.Object.ReferenceEquals(System.Object, System.Object)
    System.Object.ToString()
    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 Source

    Count

    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
    | Improve this Doc View Source

    IsEmpty

    Returns true if this map contains no key-value mappings.

    Declaration
    public bool IsEmpty { get; }
    Property Value
    Type Description
    System.Boolean
    | Improve this Doc View Source

    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>
    | Improve this Doc View Source

    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 Source

    Clear()

    Removes all of the mappings from this map.

    Declaration
    public void Clear()
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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>
    | Improve this Doc View Source

    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>
    | Improve this Doc View Source

    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>
    | Improve this Doc View Source

    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>
    | Improve this Doc View Source

    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>
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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()
    | Improve this Doc View Source

    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

    Extension Methods

    Number.IsNumber(Object)
    • Improve this Doc
    • View Source
    Back to top Copyright © 2019 Licensed to the Apache Software Foundation (ASF)