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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
SegmentedHashMap.ContainsValueAction
Action support for containsValue().protected static class
SegmentedHashMap.Entry
A map entry (key-value pair).protected static interface
SegmentedHashMap.EntryAction
An EntryAction encapsulates a logical action to be executed in the context of a key (that may or may not exist in the map).protected static class
SegmentedHashMap.EntryActionAdapter
EntryActionAdapter is a convenience class that provides default implementations for the EntryAction and IterableEntryAction interface methods.protected class
SegmentedHashMap.EntrySet
A set of entries backed by this map.protected class
SegmentedHashMap.GetEntryAction
Action support for getEntryInternal.protected class
SegmentedHashMap.InsertAction
Action support for insert.protected static interface
SegmentedHashMap.IterableEntryAction
IterableEntryAction is an EntryAction that is suitable for applying to all keys in a map.protected class
SegmentedHashMap.KeySet
A set of entries backed by this map.protected class
SegmentedHashMap.RemoveAction
Action support for remove().protected static class
SegmentedHashMap.Segment
Segment metadata.protected class
SegmentedHashMap.ValuesCollection
A collection of values backed by this map.-
Nested classes/interfaces inherited from class com.tangosol.util.Base
Base.LoggingWriter
-
-
Field Summary
Fields Modifier and Type Field Description protected static int
BIGGEST_MODULO
Biggest possible modulo.static float
DEFAULT_GROWTHRATE
Using the default growth rate, the bucket array will grow by a factor of four.static int
DEFAULT_INITIALSIZE
Default initial size provides a prime modulo and is large enough that resize is not immediate.static 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.static Map<?,?>
EMPTY
An empty, immutable SegmentedHashMap instance.protected static long
LOCK_ALL_PENDING
The bit-mask used to indicate that a lock-all is pending.protected static int
LOCK_ALL_PENDING_IDX
The mutex number used to indicate that a lock-all is pending.protected static int
LOCK_COUNT
The number of segment-locks.protected static long
LOCKS_ALL
The lock representation used to indicate that all mutexes are locked.protected static long
LOCKS_NONE
The lock representation used to indicate that no locks are set.protected SegmentedHashMap.ContainsValueAction
m_actionContainsValue
The singleton action for containsValue support.protected SegmentedHashMap.GetEntryAction
m_actionGetEntry
The singleton action for getEntryInternal support.protected SegmentedHashMap.InsertAction
m_actionInsert
The singleton action for insert support.protected SegmentedHashMap.RemoveAction
m_actionRemove
The singleton action for remove support.protected SegmentedHashMap.Entry[]
m_aeBucket
The array of hash buckets.protected SegmentedHashMap.Segment[]
m_aSegment
An array of the control-structures for the Map's segments.protected AtomicLong
m_atomicLocks
The "segment-locks".protected SegmentedHashMap.ValuesCollection
m_colValues
The collection of values backed by this map.protected int
m_cSegmentCapacity
The capacity of each segment (the point at which we should resize).protected 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).protected float
m_flLoadFactor
The determining factor for the hash map capacity given a certain number of buckets, such that capacity = bucketcount * loadfactor.protected Object
m_oIterActive
A holder for active Iterator(s): either WeakReference(<Iterator>) or WeakHashMap(<Iterator> null)protected SegmentedHashMap.EntrySet
m_setEntries
The set of entries backed by this map.protected SegmentedHashMap.KeySet
m_setKeys
The set of keys backed by this map.protected static int
MIN_SEGMENT_CAPACITY
The minimum segment capacity.protected static 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.protected static int[]
PRIME_MODULO
A list of possible modulos to use.protected static int
PUTALL_THRESHOLD
Size threshold used by the putAll operation.protected Object
RESIZING
When resizing completes, a notification is issued against this object.protected static int
SEGMENT_COUNT
The number of segments to partition the hash buckets into.protected static int
SEGMENT_LOCK_MAX_SPIN
Maximum number of times to spin while trying to acquire a segment lock before waiting.
-
Constructor Summary
Constructors Constructor Description SegmentedHashMap()
Default constructor.SegmentedHashMap(int cInitialBuckets, float flLoadFactor, float flGrowthRate)
Construct a thread-safe hash map using the specified settings.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Removes all mappings from this map.boolean
containsKey(Object oKey)
Returns true iff this map contains a mapping for the specified key.boolean
containsValue(Object oValue)
Returns true if this map maps one or more keys to the specified value.protected void
contendForSegment(int nSegment)
Wait for a segment to be unlocked.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).protected static SegmentedHashMap.Entry
entryFromBucket(SegmentedHashMap.Entry[] aeBucket, int nBucket)
Return the first non-synthetic Entry object contained by in the specified bucket.Set
entrySet()
Returns a set view of the mappings contained in this map.boolean
equals(Object oThat)
Compares the specified object with this map for equality.Object
get(Object oKey)
Returns the value to which this map maps the specified key.protected int
getBucketIndex(int nHash, int cBuckets)
Calculate the bucket number for a particular hash code.protected SegmentedHashMap.ContainsValueAction
getContainsValueAction()
Return the registered action for containsValue().Map.Entry
getEntry(Object key)
Locate an Entry in the this map based on its key.protected SegmentedHashMap.Entry
getEntryInternal(Object oKey)
Locate an Entry in the hash map based on its key.protected SegmentedHashMap.Entry
getEntryInternal(Object oKey, boolean fSynthetic)
Locate an Entry in the hash map based on its key.protected SegmentedHashMap.GetEntryAction
getGetEntryAction()
Return the registered action for getEntryInternal.protected SegmentedHashMap.InsertAction
getInsertAction()
Return the registered action for insert.protected SegmentedHashMap.RemoveAction
getRemoveAction()
Return the registered action for remove().protected SegmentedHashMap.Segment
getSegmentForKey(Object oKey)
Return the Segment object for the specified key.protected int
getSegmentIndex(int nBucket)
Calculate the segment index for the the specified bucket.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.protected void
grow()
Resize the bucket array, rehashing all Entries.protected void
grow(int cNew)
Resize the bucket array to the specified size, rehashing all Entries.int
hashCode()
Returns the hash code value for this Map.protected void
initializeActions()
Initialize the EntryAction's for this map.protected SegmentedHashMap.ContainsValueAction
instantiateContainsValueAction()
Factory for ContainsValueActionprotected SegmentedHashMap.Entry
instantiateEntry(Object oKey, Object oValue, int nHash)
Factory for Entry.protected SegmentedHashMap.EntrySet
instantiateEntrySet()
Factory for EntrySetprotected SegmentedHashMap.GetEntryAction
instantiateGetEntryAction()
Factory for GetEntryActionprotected SegmentedHashMap.InsertAction
instantiateInsertAction()
Factory for InsertActionprotected SegmentedHashMap.KeySet
instantiateKeySet()
Factory for KeySet.protected SegmentedHashMap.RemoveAction
instantiateRemoveAction()
Factory for RemoveActionprotected SegmentedHashMap.ValuesCollection
instantiateValuesCollection()
Factory for ValuesCollection.protected Object
invokeOnAllKeys(Object oContext, boolean fLock, SegmentedHashMap.IterableEntryAction actionEntry)
Perform an action on all Entries in the map.protected Object
invokeOnKey(Object oKey, Object oContext, boolean fLock, SegmentedHashMap.EntryAction action)
Perform an action on the specified key.protected boolean
isActiveIterator()
Determine if there are any active Iterators, which may mean that they are in the middle of iterating over the Map.boolean
isEmpty()
Returns true if this map contains no key-value mappings.protected void
iteratorActivated(Iterator iter)
Register the activation of an Iterator.Set
keySet()
Returns a Set view of the keys contained in this map.protected void
lockAllBuckets()
Lock everything.protected boolean
lockAllBuckets(int nBucketAlreadyLocked)
Lock everything, assuming that the segment for the specified bucket has already been locked.protected boolean
lockAllSegments(long lLocksHeld)
Lock all segments except for the specified segments that have already been locked by the calling thread.protected boolean
lockBucket(int nBucket)
Attempt to lock the segment corresponding to the specified bucket.protected boolean
lockSegment(int nSegment, boolean fBlock)
Attempt to lock the specified segment.Object
put(Object oKey, Object oValue)
Associates the specified value with the specified key in this map.void
putAll(Map mapOther)
Copies all of the mappings from the specified map to this map.protected Object
putInternal(Object oKey, Object oValue)
Associates the specified value with the specified key in this map.protected Object
putInternal(Object oKey, Object oValue, boolean fOnlyIfAbsent)
Associates the specified value with the specified key in this map.void
releaseIterator(Iterator iter)
Release the (formerly-active) Iterator.Object
remove(Object oKey)
Removes the mapping for this key from this map if present.protected Object
removeInternal(Object oKey, SegmentedHashMap.EntryAction actionRemove, Object oContext)
Removes the mapping for this key from this map if present.protected void
setContainsValueAction(SegmentedHashMap.ContainsValueAction action)
Specify the action for containsValue().protected void
setGetEntryAction(SegmentedHashMap.GetEntryAction action)
Specify the action for getEntryInternal.protected void
setInsertAction(SegmentedHashMap.InsertAction action)
Specify the action for insert.protected void
setRemoveAction(SegmentedHashMap.RemoveAction action)
Specify the action for remove().int
size()
Returns the number of key-value mappings in this map.protected Object[]
toArrayInternal(SegmentedHashMap.IterableEntryAction action, Object[] a)
Apply the specified toArray() action to the entries in the map.String
toString()
Returns a String representation of this map.protected void
unlockAllBuckets()
Unlock everything.protected void
unlockAllBuckets(int nBucketLeaveLocked)
Unlock everything, leaving only the segment for the specified bucket locked.protected void
unlockAllSegments(long lLocksKeep)
Unlock all segments, except the segment-locks indicated by the specified bit-vector.protected void
unlockBucket(int nBucket)
Unlock the segment corresponding to the specified bucket that was previously locked using thelockBucket(int)
method.protected void
unlockSegment(int nSegment)
Unlock the specified segment previously locked using thelockSegment(int, boolean)
method.Collection
values()
Returns a collection view of the values contained in this map.-
Methods inherited from class com.tangosol.util.Base
azzert, azzert, azzert, azzertFailed, breakLines, breakLines, capitalize, checkNotEmpty, checkNotNull, checkRange, computeSafeWaitTime, decimalValue, dup, dup, ensureBigDecimal, ensureClassLoader, ensureRuntimeException, ensureRuntimeException, equals, equalsDeep, err, err, err, err, err, escape, formatDateTime, getCallerStackFrame, getCommonMonitor, getCommonMonitor, getCommonMonitor, getContextClassLoader, getContextClassLoader, getDeepMessage, getErr, getLastSafeTimeMillis, getLog, getMaxDecDigits, getMaxHexDigits, getOriginalException, getOut, getProcessRandom, getRandom, getRandomBinary, getRandomBinary, getRandomString, getSafeTimeMillis, getStackFrame, getStackFrames, getStackTrace, getStackTrace, getStackTrace, getThreadFactory, getTimeZone, getUpTimeMillis, hashCode, hexValue, indentString, indentString, isDecimal, isHex, isLogEcho, isOctal, log, log, log, log, log, makeInteger, makeLong, makeThread, mergeArray, mergeBooleanArray, mergeByteArray, mergeCharArray, mergeDoubleArray, mergeFloatArray, mergeIntArray, mergeLongArray, mod, mod, octalValue, out, out, out, out, out, pad, parseBandwidth, parseBandwidth, parseDelimitedString, parseHex, parseHex, parseMemorySize, parseMemorySize, parsePercentage, parseTime, parseTime, parseTimeNanos, parseTimeNanos, printStackTrace, randomize, randomize, randomize, randomize, read, read, read, read, read, read, read, replace, setErr, setLog, setLogEcho, setOut, sleep, toBandwidthString, toBandwidthString, toCharEscape, toCrc, toCrc, toCrc, toCrc, toCrc, toDecString, toDelimitedString, toDelimitedString, toDelimitedString, toDelimitedString, toHex, toHex, toHexDump, toHexEscape, toHexEscape, toHexEscape, toHexEscape, toHexString, toMemorySizeString, toMemorySizeString, toQuotedCharEscape, toQuotedStringEscape, toSqlString, toString, toString, toStringEscape, toUnicodeEscape, trace, trace, trace, trace, trace, trace, trace, trace, trace, truncateString, truncateString, wait
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Field Detail
-
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:
- Constant Field Values
-
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:
- Constant Field Values
-
MIN_SEGMENT_CAPACITY
protected static final int MIN_SEGMENT_CAPACITY
The minimum segment capacity.- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
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:
- Constant Field Values
-
LOCKS_NONE
protected static final long LOCKS_NONE
The lock representation used to indicate that no locks are set.- See Also:
- Constant Field Values
-
LOCKS_ALL
protected static final long LOCKS_ALL
The lock representation used to indicate that all mutexes are locked.- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
LOCK_ALL_PENDING
protected static final long LOCK_ALL_PENDING
The bit-mask used to indicate that a lock-all is pending.- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
PUTALL_THRESHOLD
protected static final int PUTALL_THRESHOLD
Size threshold used by the putAll operation.- See Also:
- Constant Field Values
-
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 Detail
-
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 < nflLoadFactor
- 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 bucketsflGrowthRate
- 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 Detail
-
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.
-
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.
-
toString
public String toString()
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.
-
isEmpty
public boolean isEmpty()
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 interfaceMap
- 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 interfaceMap
- 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.
-
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 interfaceMap
- Parameters:
oKey
- key with which the specified value is to be associatedoValue
- 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()); }
-
remove
public Object remove(Object oKey)
Removes the mapping for this key from this map if present.
-
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.
-
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.
-
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.
-
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 forfSynthetic
- 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 associatedoValue
- 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 associatedoValue
- value to be associated with the specified keyfOnlyIfAbsent
- 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 mapactionRemove
- the EntryAction to applyoContext
- 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() actiona
- 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 actionfLock
- true if all segment-locks should be acquired before invoking the specified actionactionEntry
- 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 onoContext
- opaque context for the specified actionfLock
- true iff the segment should be locked before invoking the specified actionaction
- 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 codecBuckets
- 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 lockfBlock
- 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 thelockBucket(int)
method.- Parameters:
nBucket
- the bucket to unlock
-
unlockSegment
protected void unlockSegment(int nSegment)
Unlock the specified segment previously locked using thelockSegment(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 calledlockAllSegments(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 keyoValue
- the valuenHash
- 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 bucketsnBucket
- 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)
-
-