Class SegmentedHashMap
- All Implemented Interfaces:
Map
- Direct Known Subclasses:
SegmentedConcurrentMap
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
Modifier and TypeClassDescriptionprotected static class
Action support for containsValue().protected static class
A map entry (key-value pair).protected static interface
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
EntryActionAdapter is a convenience class that provides default implementations for the EntryAction and IterableEntryAction interface methods.protected class
A set of entries backed by this map.protected class
Action support for getEntryInternal.protected class
Action support for insert.protected static interface
IterableEntryAction is an EntryAction that is suitable for applying to all keys in a map.protected class
A set of entries backed by this map.protected class
Action support for remove().protected static class
Segment metadata.protected class
A collection of values backed by this map.Nested classes/interfaces inherited from class com.tangosol.util.Base
Base.LoggingWriter
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final int
Biggest possible modulo.static final float
Using the default growth rate, the bucket array will grow by a factor of four.static final int
Default initial size provides a prime modulo and is large enough that resize is not immediate.static final float
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 final Map
<?, ?> An empty, immutable SegmentedHashMap instance.protected static final long
The bit-mask used to indicate that a lock-all is pending.protected static final int
The mutex number used to indicate that a lock-all is pending.protected static final int
The number of segment-locks.protected static final long
The lock representation used to indicate that all mutexes are locked.protected static final long
The lock representation used to indicate that no locks are set.protected SegmentedHashMap.ContainsValueAction
The singleton action for containsValue support.protected SegmentedHashMap.GetEntryAction
The singleton action for getEntryInternal support.protected SegmentedHashMap.InsertAction
The singleton action for insert support.protected SegmentedHashMap.RemoveAction
The singleton action for remove support.protected SegmentedHashMap.Entry[]
The array of hash buckets.protected final SegmentedHashMap.Segment[]
An array of the control-structures for the Map's segments.protected final AtomicLong
The "segment-locks".protected SegmentedHashMap.ValuesCollection
The collection of values backed by this map.protected int
The capacity of each segment (the point at which we should resize).protected final float
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 final float
The determining factor for the hash map capacity given a certain number of buckets, such that capacity = bucketcount * loadfactor.protected Object
A holder for active Iterator(s): either WeakReference(<Iterator>) or WeakHashMap(<Iterator> null)protected SegmentedHashMap.EntrySet
The set of entries backed by this map.protected SegmentedHashMap.KeySet
The set of keys backed by this map.protected static final int
The minimum segment capacity.protected static final Object
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 final int[]
A list of possible modulos to use.protected static final int
Size threshold used by the putAll operation.protected final Object
When resizing completes, a notification is issued against this object.protected static final int
The number of segments to partition the hash buckets into.protected static final int
Maximum number of times to spin while trying to acquire a segment lock before waiting. -
Constructor Summary
ConstructorDescriptionDefault constructor.SegmentedHashMap
(int cInitialBuckets, float flLoadFactor, float flGrowthRate) Construct a thread-safe hash map using the specified settings. -
Method Summary
Modifier and TypeMethodDescriptionvoid
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.entrySet()
Returns a set view of the mappings contained in this map.boolean
Compares the specified object with this map for equality.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
Return the registered action for containsValue().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
Return the registered action for getEntryInternal.protected SegmentedHashMap.InsertAction
Return the registered action for insert.protected SegmentedHashMap.RemoveAction
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[]
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
Initialize the EntryAction's for this map.protected SegmentedHashMap.ContainsValueAction
Factory for ContainsValueActionprotected SegmentedHashMap.Entry
instantiateEntry
(Object oKey, Object oValue, int nHash) Factory for Entry.protected SegmentedHashMap.EntrySet
Factory for EntrySetprotected SegmentedHashMap.GetEntryAction
Factory for GetEntryActionprotected SegmentedHashMap.InsertAction
Factory for InsertActionprotected SegmentedHashMap.KeySet
Factory for KeySet.protected SegmentedHashMap.RemoveAction
Factory for RemoveActionprotected SegmentedHashMap.ValuesCollection
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
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.keySet()
Returns a Set view of the keys contained in this map.protected void
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.Associates the specified value with the specified key in this map.void
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.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
Specify the action for containsValue().protected void
Specify the action for getEntryInternal.protected void
Specify the action for insert.protected void
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.toString()
Returns a String representation of this map.protected void
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.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 Details
-
PRIME_MODULO
protected static final int[] PRIME_MODULOA list of possible modulos to use. -
DEFAULT_INITIALSIZE
public static final int DEFAULT_INITIALSIZEDefault 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_MODULOBiggest possible modulo. -
DEFAULT_LOADFACTOR
public static final float DEFAULT_LOADFACTORThe 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_GROWTHRATEUsing 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_CAPACITYThe minimum segment capacity.- See Also:
-
SEGMENT_COUNT
protected static final int SEGMENT_COUNTThe 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_COUNTThe 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_NONEThe lock representation used to indicate that no locks are set.- See Also:
-
LOCKS_ALL
protected static final long LOCKS_ALLThe lock representation used to indicate that all mutexes are locked.- See Also:
-
LOCK_ALL_PENDING_IDX
protected static final int LOCK_ALL_PENDING_IDXThe mutex number used to indicate that a lock-all is pending.- See Also:
-
LOCK_ALL_PENDING
protected static final long LOCK_ALL_PENDINGThe bit-mask used to indicate that a lock-all is pending.- See Also:
-
SEGMENT_LOCK_MAX_SPIN
protected static final int SEGMENT_LOCK_MAX_SPINMaximum number of times to spin while trying to acquire a segment lock before waiting.- See Also:
-
PUTALL_THRESHOLD
protected static final int PUTALL_THRESHOLDSize threshold used by the putAll operation.- See Also:
-
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
An empty, immutable SegmentedHashMap instance. -
RESIZING
When resizing completes, a notification is issued against this object. -
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
An array of the control-structures for the Map's segments. -
m_aeBucket
The array of hash buckets. This field is declared volatile in order to reduce synchronization. -
m_cSegmentCapacity
protected int m_cSegmentCapacityThe capacity of each segment (the point at which we should resize). -
m_flLoadFactor
protected final float m_flLoadFactorThe determining factor for the hash map capacity given a certain number of buckets, such that capacity = bucketcount * loadfactor. -
m_flGrowthRate
protected final float m_flGrowthRateThe 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
The set of entries backed by this map. -
m_setKeys
The set of keys backed by this map. -
m_colValues
The collection of values backed by this map. -
m_oIterActive
A holder for active Iterator(s): either WeakReference(<Iterator>) or WeakHashMap(<Iterator> null) -
m_actionGetEntry
The singleton action for getEntryInternal support. -
m_actionInsert
The singleton action for insert support. -
m_actionRemove
The singleton action for remove support. -
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 < 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 Details
-
getInsertAction
Return the registered action for insert.- Returns:
- the registered action for insert
-
setInsertAction
Specify the action for insert.- Parameters:
action
- the action for insert
-
getGetEntryAction
Return the registered action for getEntryInternal.- Returns:
- the registered action for getEntryInternal
-
setGetEntryAction
Specify the action for getEntryInternal.- Parameters:
action
- the action for getEntryInternal
-
getRemoveAction
Return the registered action for remove().- Returns:
- the registered action for remove()
-
setRemoveAction
Specify the action for remove().- Parameters:
action
- the action for remove()
-
getContainsValueAction
Return the registered action for containsValue().- Returns:
- the registered action for containsValue()
-
setContainsValueAction
Specify the action for containsValue().- Parameters:
action
- the action for containsValue()
-
equals
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
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
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
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
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
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
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
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
Removes the mapping for this key from this map if present. -
clear
public void clear()Removes all mappings from this map. -
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
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
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
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
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
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
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
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
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
Return the Segment object for the specified key.- Parameters:
oKey
- the key- Returns:
- the Segment for the specified key
-
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
Register the activation of an Iterator.Note: The map will not grow while there are any active iterators.
- Parameters:
iter
- the activated iterator
-
releaseIterator
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
Factory for GetEntryAction- Returns:
- a GetEntryAction
-
instantiateInsertAction
Factory for InsertAction- Returns:
- an InsertAction
-
instantiateRemoveAction
Factory for RemoveAction- Returns:
- a RemoveAction
-
instantiateContainsValueAction
Factory for ContainsValueAction- Returns:
- a ContainsValueAction
-
instantiateEntry
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
Factory for EntrySet- Returns:
- a new instance of the EntrySet class (or a subclass thereof)
-
instantiateKeySet
Factory for KeySet.- Returns:
- a new instance of the KeySet class (or subclass thereof)
-
instantiateValuesCollection
Factory for ValuesCollection.- Returns:
- a new instance of the ValuesCollection class (or subclass thereof)
-