Class SegmentedHashMap

java.lang.Object
com.tangosol.util.Base
com.tangosol.util.SegmentedHashMap
All Implemented Interfaces:
Map
Direct Known Subclasses:
SegmentedConcurrentMap

public class SegmentedHashMap extends Base implements Map
An implementation of java.util.Map that is optimized for heavy concurrent use.

Retrieval and update operations to the map (e.g. get, put) are non-blocking and uncontended and will reflect some state of the map. Insert and remove operations to the map (e.g. put, remove) do require internal locking.

The entries in the map are internally segmented so as to permit a high level of concurrent "locked" operations without contention.

Retrievals and updates that run concurrently with bulk operations (e.g. putAll, clear may reflect insertion or removal of only some entries. Iterators on the Map may also reflect concurrent updates made since the Iterator was created. However, Iterators will not throw ConcurrentModificationException.

Since:
Coherence 3.5
Author:
rhl 2008.12.01
  • Field Details

    • PRIME_MODULO

      protected static final int[] PRIME_MODULO
      A list of possible modulos to use.
    • DEFAULT_INITIALSIZE

      public static final int DEFAULT_INITIALSIZE
      Default initial size provides a prime modulo and is large enough that resize is not immediate. (A hash map probably uses less than 128 bytes initially.)
    • BIGGEST_MODULO

      protected static final int BIGGEST_MODULO
      Biggest possible modulo.
    • DEFAULT_LOADFACTOR

      public static final float DEFAULT_LOADFACTOR
      The default load factor is 100%, which means that the hash map will not resize until there is (on average) one entry in every bucket. The cost of scanning a linked list in a particular bucket is very low, so there is little reason for having this value below 1.0, and the goal is constant order access, so assuming a perfect hash this will provide the optimal access speed relative to size.
      See Also:
    • DEFAULT_GROWTHRATE

      public static final float DEFAULT_GROWTHRATE
      Using the default growth rate, the bucket array will grow by a factor of four. The relatively high growth rate helps to ensure less resize operations, an important consideration in a high-concurrency map.
      See Also:
    • MIN_SEGMENT_CAPACITY

      protected static final int MIN_SEGMENT_CAPACITY
      The minimum segment capacity.
      See Also:
    • SEGMENT_COUNT

      protected static final int SEGMENT_COUNT
      The number of segments to partition the hash buckets into. There is a single lock for each segment. This number is specially chosen as 61 is the largest prime number smaller than 64 (the size of the datatype used to represent the lock).
      See Also:
    • LOCK_COUNT

      protected static final int LOCK_COUNT
      The number of segment-locks. Each segment has its own lock and there is a global "intention" lock.
      See Also:
    • LOCKS_NONE

      protected static final long LOCKS_NONE
      The lock representation used to indicate that no locks are set.
      See Also:
    • LOCKS_ALL

      protected static final long LOCKS_ALL
      The lock representation used to indicate that all mutexes are locked.
      See Also:
    • LOCK_ALL_PENDING_IDX

      protected static final int LOCK_ALL_PENDING_IDX
      The mutex number used to indicate that a lock-all is pending.
      See Also:
    • LOCK_ALL_PENDING

      protected static final long LOCK_ALL_PENDING
      The bit-mask used to indicate that a lock-all is pending.
      See Also:
    • SEGMENT_LOCK_MAX_SPIN

      protected static final int SEGMENT_LOCK_MAX_SPIN
      Maximum number of times to spin while trying to acquire a segment lock before waiting.
      See Also:
    • PUTALL_THRESHOLD

      protected static final int PUTALL_THRESHOLD
      Size threshold used by the putAll operation.
      See Also:
    • NO_VALUE

      protected static final Object NO_VALUE
      Object to be used as a value representing that the Entry object is "synthetic" and while logically associated with a key, does not represent a key-value mapping in the Map.
    • EMPTY

      public static final Map<?,?> EMPTY
      An empty, immutable SegmentedHashMap instance.
    • RESIZING

      protected final Object RESIZING
      When resizing completes, a notification is issued against this object.
    • m_atomicLocks

      protected final AtomicLong m_atomicLocks
      The "segment-locks". This AtomicCounter is actually used just as an "Atomic Long" value, with each of the first 61 bits being used to represent a segment-lock. In this case, the number of "buckets" is fixed to 61 (the largest prime smaller than 64).
    • m_aSegment

      protected final SegmentedHashMap.Segment[] m_aSegment
      An array of the control-structures for the Map's segments.
    • m_aeBucket

      protected volatile SegmentedHashMap.Entry[] m_aeBucket
      The array of hash buckets. This field is declared volatile in order to reduce synchronization.
    • m_cSegmentCapacity

      protected int m_cSegmentCapacity
      The capacity of each segment (the point at which we should resize).
    • m_flLoadFactor

      protected final float m_flLoadFactor
      The determining factor for the hash map capacity given a certain number of buckets, such that capacity = bucketcount * loadfactor.
    • m_flGrowthRate

      protected final float m_flGrowthRate
      The rate of growth as a fraction of the current number of buckets, 0 < n, such that the hash map grows to bucketcount * (1 + growth-rate).
    • m_setEntries

      protected SegmentedHashMap.EntrySet m_setEntries
      The set of entries backed by this map.
    • m_setKeys

      protected SegmentedHashMap.KeySet m_setKeys
      The set of keys backed by this map.
    • m_colValues

      protected SegmentedHashMap.ValuesCollection m_colValues
      The collection of values backed by this map.
    • m_oIterActive

      protected Object m_oIterActive
      A holder for active Iterator(s): either WeakReference(<Iterator>) or WeakHashMap(<Iterator> null)
    • m_actionGetEntry

      protected SegmentedHashMap.GetEntryAction m_actionGetEntry
      The singleton action for getEntryInternal support.
    • m_actionInsert

      protected SegmentedHashMap.InsertAction m_actionInsert
      The singleton action for insert support.
    • m_actionRemove

      protected SegmentedHashMap.RemoveAction m_actionRemove
      The singleton action for remove support.
    • m_actionContainsValue

      protected SegmentedHashMap.ContainsValueAction m_actionContainsValue
      The singleton action for containsValue support.
  • Constructor Details

    • SegmentedHashMap

      public SegmentedHashMap()
      Default constructor.
    • SegmentedHashMap

      public SegmentedHashMap(int cInitialBuckets, float flLoadFactor, float flGrowthRate)
      Construct a thread-safe hash map using the specified settings.
      Parameters:
      cInitialBuckets - the initial number of hash buckets, 0 < n
      flLoadFactor - the acceptable load factor before resizing occurs, 0 < n, such that a load factor of 1.0 causes resizing when the number of entries exceeds the number of buckets
      flGrowthRate - the rate of bucket growth when a resize occurs, 0 < n, such that a growth rate of 1.0 will double the number of buckets: bucketcount = bucketcount * (1 + growthrate)
  • Method Details

    • getInsertAction

      protected SegmentedHashMap.InsertAction getInsertAction()
      Return the registered action for insert.
      Returns:
      the registered action for insert
    • setInsertAction

      protected void setInsertAction(SegmentedHashMap.InsertAction action)
      Specify the action for insert.
      Parameters:
      action - the action for insert
    • getGetEntryAction

      protected SegmentedHashMap.GetEntryAction getGetEntryAction()
      Return the registered action for getEntryInternal.
      Returns:
      the registered action for getEntryInternal
    • setGetEntryAction

      protected void setGetEntryAction(SegmentedHashMap.GetEntryAction action)
      Specify the action for getEntryInternal.
      Parameters:
      action - the action for getEntryInternal
    • getRemoveAction

      protected SegmentedHashMap.RemoveAction getRemoveAction()
      Return the registered action for remove().
      Returns:
      the registered action for remove()
    • setRemoveAction

      protected void setRemoveAction(SegmentedHashMap.RemoveAction action)
      Specify the action for remove().
      Parameters:
      action - the action for remove()
    • getContainsValueAction

      protected SegmentedHashMap.ContainsValueAction getContainsValueAction()
      Return the registered action for containsValue().
      Returns:
      the registered action for containsValue()
    • setContainsValueAction

      protected void setContainsValueAction(SegmentedHashMap.ContainsValueAction action)
      Specify the action for containsValue().
      Parameters:
      action - the action for containsValue()
    • equals

      public boolean equals(Object oThat)
      Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps t1 and t2 represent the same mappings if t1.keySet().equals(t2.keySet()) and for every key k in t1.keySet(), (t1.get(k)==null ? t2.get(k)==null : t1.get(k).equals(t2.get(k))) . This ensures that the equals method works properly across different implementations of the map interface.
      Specified by:
      equals in interface Map
      Overrides:
      equals in class Object
      Parameters:
      oThat - object to be compared for equality with this Map
      Returns:
      true if the specified object is equal to this Map
    • hashCode

      public int hashCode()
      Returns the hash code value for this Map. The hash code of a Map is defined to be the sum of the hash codes of each entry in the Map's entrySet() view. This ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() for any two maps t1 and t2, as required by the general contract of Object.hashCode.
      Specified by:
      hashCode in interface Map
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this Map
    • toString

      public String toString()
      Returns a String representation of this map.
      Overrides:
      toString in class Object
      Returns:
      a String representation of this map
    • size

      public int size()
      Returns the number of key-value mappings in this map.

      Note: Unlike some Map implementations, the size() operation on this map may be relatively expensive.

      Specified by:
      size in interface Map
      Returns:
      the number of key-value mappings in this map
    • isEmpty

      public boolean isEmpty()
      Returns true if this map contains no key-value mappings.
      Specified by:
      isEmpty in interface Map
      Returns:
      true if this map contains no key-value mappings
    • containsKey

      public boolean containsKey(Object oKey)
      Returns true iff this map contains a mapping for the specified key.
      Specified by:
      containsKey in interface Map
      Parameters:
      oKey - key whose presence in this map is to be tested
      Returns:
      true iff this map contains a mapping for the specified key
    • containsValue

      public boolean containsValue(Object oValue)
      Returns true if this map maps one or more keys to the specified value.
      Specified by:
      containsValue in interface Map
      Parameters:
      oValue - value whose presence in this map is to be tested
      Returns:
      true if this map maps one or more keys to the specified value
    • get

      public Object get(Object oKey)
      Returns the value to which this map maps the specified key. Returns null if the map contains no mapping for this key. A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.
      Specified by:
      get in interface Map
      Parameters:
      oKey - key whose associated value is to be returned
      Returns:
      the value to which this map maps the specified key, or null if the map contains no mapping for this key
    • getEntry

      public Map.Entry getEntry(Object key)
      Locate an Entry in the this map based on its key.
      Parameters:
      key - the key object to search for
      Returns:
      the Entry or null if the entry does not exist
    • put

      public Object put(Object oKey, Object oValue)
      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.
      Specified by:
      put in interface Map
      Parameters:
      oKey - key with which the specified value is to be associated
      oValue - value to be associated with the specified key
      Returns:
      previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key
    • putAll

      public void putAll(Map mapOther)
      Copies all of the mappings from the specified map to this map. putAll is semantically equivalent to:
       for (Iterator iter = mapOther.entrySet().iterator(); iter.hasNext(); )
           {
           Map.Entry entry = (Map.Entry) iter.next();
           put(entry.getKey(), entry.getValue());
           }
       
      Specified by:
      putAll in interface Map
      Parameters:
      mapOther - mappings to be stored in this map
    • remove

      public Object remove(Object oKey)
      Removes the mapping for this key from this map if present.
      Specified by:
      remove in interface Map
      Parameters:
      oKey - key whose mapping is to be removed from the map
      Returns:
      previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key
    • clear

      public void clear()
      Removes all mappings from this map.
      Specified by:
      clear in interface Map
    • entrySet

      public Set entrySet()
      Returns a set view of the mappings contained in this map. Each element in the returned set is a Map.Entry. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
      Specified by:
      entrySet in interface Map
      Returns:
      a set view of the mappings contained in this map.
    • keySet

      public Set keySet()
      Returns a Set view of the keys contained in this map. The Set is backed by the map, so changes to the map are reflected in the Set, and vice-versa. (If the map is modified while an iteration over the Set is in progress, the results of the iteration are undefined.) The Set supports element removal, which removes the corresponding entry from the map, via the Iterator.remove, Set.remove, removeAll retainAll, and clear operations. It does not support the add or addAll operations.
      Specified by:
      keySet in interface Map
      Returns:
      a Set view of the keys contained in this map
    • values

      public Collection values()
      Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
      Specified by:
      values in interface Map
      Returns:
      a collection view of the values contained in this map
    • initializeActions

      protected void initializeActions()
      Initialize the EntryAction's for this map.
    • getEntryInternal

      protected SegmentedHashMap.Entry getEntryInternal(Object oKey)
      Locate an Entry in the hash map based on its key.
      Parameters:
      oKey - the key object to search for
      Returns:
      the Entry or null
    • getEntryInternal

      protected SegmentedHashMap.Entry getEntryInternal(Object oKey, boolean fSynthetic)
      Locate an Entry in the hash map based on its key.
      Parameters:
      oKey - the key object to search for
      fSynthetic - include synthetic Entry objects representing keys that are not contained in the map
      Returns:
      the Entry or null
    • putInternal

      protected Object putInternal(Object oKey, Object oValue)
      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.
      Parameters:
      oKey - key with which the specified value is to be associated
      oValue - value to be associated with the specified key
      Returns:
      previous value associated with specified key, or NO_VALUE if there was no mapping for key. A null return indicates that the map previously associated null with the specified key
    • putInternal

      protected Object putInternal(Object oKey, Object oValue, boolean fOnlyIfAbsent)
      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.
      Parameters:
      oKey - key with which the specified value is to be associated
      oValue - value to be associated with the specified key
      fOnlyIfAbsent - if true, perform a logical insert only; no action if the key already exists
      Returns:
      previous value associated with specified key, or NO_VALUE if there was no mapping for key. A null return indicates that the map previously associated null with the specified key
    • removeInternal

      protected Object removeInternal(Object oKey, SegmentedHashMap.EntryAction actionRemove, Object oContext)
      Removes the mapping for this key from this map if present.
      Parameters:
      oKey - key whose mapping is to be removed from the map
      actionRemove - the EntryAction to apply
      oContext - the context for the remove action
      Returns:
      previous value associated with specified key, or NO_VALUE if there was no mapping for key. A null return indicates that the map previously associated null with the specified key.
    • toArrayInternal

      protected Object[] toArrayInternal(SegmentedHashMap.IterableEntryAction action, Object[] a)
      Apply the specified toArray() action to the entries in the map. The toArray() action is not applied under any segment lock and is expected to accept a List instance as a context.
      Parameters:
      action - the toArray() action
      a - the array into which the elements of the Collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose
      Returns:
      an array containing the elements returned by the specified action
      Throws:
      ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of the elements returned by the specified action
    • ensureLoadFactor

      protected void ensureLoadFactor(SegmentedHashMap.Segment segment)
      Check whether or not the specified segment is overloaded and if so, grow the bucket array (which suggests with high probability that the per-segment load will decrease).
      Parameters:
      segment - the segment to ensure the load-factor for
    • grow

      protected void grow()
      Resize the bucket array, rehashing all Entries.

      Note: caller of this method is expected to hold locks on all segments of the map while making this call.

    • grow

      protected void grow(int cNew)
      Resize the bucket array to the specified size, rehashing all Entries.

      Note: caller of this method is expected to hold locks on all segments of the map while making this call.

      Parameters:
      cNew - the minimum size to attempt to grow to
    • invokeOnAllKeys

      protected Object invokeOnAllKeys(Object oContext, boolean fLock, SegmentedHashMap.IterableEntryAction actionEntry)
      Perform an action on all Entries in the map. The action is provided as an EntryAction and (if the fLock is specified) it are invoked while holding the locks for all segments.

      The semantics of invokeOnAllKeys are equivalent to:

       for (Iterator iter = entrySet().iterator(); iter.hasNext(); )
           {
           Entry entry = (Entry) iter.next();
           actionEntry.invokeFound(...);
           }
       return oContext;
       
      Except that if fLock is specified, it is performed atomically while holding all segment-locks.
      Parameters:
      oContext - opaque context for the specified action
      fLock - true if all segment-locks should be acquired before invoking the specified action
      actionEntry - the action to perform for each entry
      Returns:
      the specified opaque context
    • invokeOnKey

      protected Object invokeOnKey(Object oKey, Object oContext, boolean fLock, SegmentedHashMap.EntryAction action)
      Perform an action on the specified key. The action operation is provided as an EntryAction and (if fLock is specified), is invoked while holding the appropriate segment lock for the key.

      The semantics of invokeOnKey are equivalent to:

       Object oResult;
       if (containsKey(oKey))
           {
           oResult = action.invokeFound(...);
           }
       else
           {
           oResult = action.invokeNotFound(...);
           }
       return oResult;
       
      Except that if fLock is specified, it is performed atomically while holding the segment-lock.
      Parameters:
      oKey - the key to act on
      oContext - opaque context for the specified action
      fLock - true iff the segment should be locked before invoking the specified action
      action - the action to invoke
      Returns:
      the result of performing the action
    • getBucketIndex

      protected int getBucketIndex(int nHash, int cBuckets)
      Calculate the bucket number for a particular hash code.
      Parameters:
      nHash - the hash code
      cBuckets - the number of buckets
      Returns:
      the bucket index for the specified hash code
    • getSegmentIndex

      protected int getSegmentIndex(int nBucket)
      Calculate the segment index for the the specified bucket.
      Parameters:
      nBucket - the bucket number
      Returns:
      the segment index
    • getSegmentForKey

      protected SegmentedHashMap.Segment getSegmentForKey(Object oKey)
      Return the Segment object for the specified key.
      Parameters:
      oKey - the key
      Returns:
      the Segment for the specified key
    • getStableBucketArray

      protected SegmentedHashMap.Entry[] getStableBucketArray()
      Get the bucket array, or if a resize is occurring, wait for the resize to complete and return the new bucket array.
      Returns:
      the latest bucket array
    • iteratorActivated

      protected void iteratorActivated(Iterator iter)
      Register the activation of an Iterator.

      Note: The map will not grow while there are any active iterators.

      Parameters:
      iter - the activated iterator
    • releaseIterator

      public void releaseIterator(Iterator iter)
      Release the (formerly-active) Iterator.

      Note: This method could be used to accelerate the destruction of unexhausted iterators.

      Parameters:
      iter - the iterator to be released
    • isActiveIterator

      protected boolean isActiveIterator()
      Determine if there are any active Iterators, which may mean that they are in the middle of iterating over the Map.
      Returns:
      true iff there is at least one active Iterator
    • lockBucket

      protected boolean lockBucket(int nBucket)
      Attempt to lock the segment corresponding to the specified bucket.
      Parameters:
      nBucket - the bucket index
      Returns:
      true iff the segment was successfully locked
    • lockSegment

      protected boolean lockSegment(int nSegment, boolean fBlock)
      Attempt to lock the specified segment.
      Parameters:
      nSegment - the segment to lock
      fBlock - should we block on trying to lock the segment
      Returns:
      true iff the segment was successfully locked
    • unlockBucket

      protected void unlockBucket(int nBucket)
      Unlock the segment corresponding to the specified bucket that was previously locked using the lockBucket(int) method.
      Parameters:
      nBucket - the bucket to unlock
    • unlockSegment

      protected void unlockSegment(int nSegment)
      Unlock the specified segment previously locked using the lockSegment(int, boolean) method.
      Parameters:
      nSegment - the segment to unlock
    • contendForSegment

      protected void contendForSegment(int nSegment)
      Wait for a segment to be unlocked.
      Parameters:
      nSegment - the segment-lock to be waited for
    • lockAllBuckets

      protected void lockAllBuckets()
      Lock everything. This method will not return until the lock is placed. It must not be called on a thread that could already hold a lock.
    • lockAllBuckets

      protected boolean lockAllBuckets(int nBucketAlreadyLocked)
      Lock everything, assuming that the segment for the specified bucket has already been locked.
      Parameters:
      nBucketAlreadyLocked - the bucket that was already locked.
      Returns:
      false if the operation failed because another thread was also trying to lock everything (indicating potential deadlock)
    • unlockAllBuckets

      protected void unlockAllBuckets()
      Unlock everything.
    • unlockAllBuckets

      protected void unlockAllBuckets(int nBucketLeaveLocked)
      Unlock everything, leaving only the segment for the specified bucket locked.
      Parameters:
      nBucketLeaveLocked - the bucket that was already locked
    • lockAllSegments

      protected boolean lockAllSegments(long lLocksHeld)
      Lock all segments except for the specified segments that have already been locked by the calling thread.
      Parameters:
      lLocksHeld - the bit-mask representing all segment-locks that the calling thread already holds
      Returns:
      false if the operation failed because another thread was also trying to lock everything (indicating potential deadlock)
    • unlockAllSegments

      protected void unlockAllSegments(long lLocksKeep)
      Unlock all segments, except the segment-locks indicated by the specified bit-vector. This method must only be called by a thread if that thread has successfully called lockAllSegments(long).
      Parameters:
      lLocksKeep - the segment-locks to keep locked
    • instantiateGetEntryAction

      protected SegmentedHashMap.GetEntryAction instantiateGetEntryAction()
      Factory for GetEntryAction
      Returns:
      a GetEntryAction
    • instantiateInsertAction

      protected SegmentedHashMap.InsertAction instantiateInsertAction()
      Factory for InsertAction
      Returns:
      an InsertAction
    • instantiateRemoveAction

      protected SegmentedHashMap.RemoveAction instantiateRemoveAction()
      Factory for RemoveAction
      Returns:
      a RemoveAction
    • instantiateContainsValueAction

      protected SegmentedHashMap.ContainsValueAction instantiateContainsValueAction()
      Factory for ContainsValueAction
      Returns:
      a ContainsValueAction
    • instantiateEntry

      protected SegmentedHashMap.Entry instantiateEntry(Object oKey, Object oValue, int nHash)
      Factory for Entry.
      Parameters:
      oKey - the key
      oValue - the value
      nHash - the hashCode value of the key
      Returns:
      a new instance of the Entry class (or a subclass thereof)
    • entryFromBucket

      protected static SegmentedHashMap.Entry entryFromBucket(SegmentedHashMap.Entry[] aeBucket, int nBucket)
      Return the first non-synthetic Entry object contained by in the specified bucket.
      Parameters:
      aeBucket - the array of hash buckets
      nBucket - the bucket index
      Returns:
      the first non-synthetic Entry in the specified bucket or null
    • instantiateEntrySet

      protected SegmentedHashMap.EntrySet instantiateEntrySet()
      Factory for EntrySet
      Returns:
      a new instance of the EntrySet class (or a subclass thereof)
    • instantiateKeySet

      protected SegmentedHashMap.KeySet instantiateKeySet()
      Factory for KeySet.
      Returns:
      a new instance of the KeySet class (or subclass thereof)
    • instantiateValuesCollection

      protected SegmentedHashMap.ValuesCollection instantiateValuesCollection()
      Factory for ValuesCollection.
      Returns:
      a new instance of the ValuesCollection class (or subclass thereof)