Package com.google.common.collect
Class MapMakerInternalMap.Segment<K,V,E extends MapMakerInternalMap.InternalEntry<K,V,E>,S extends MapMakerInternalMap.Segment<K,V,E,S>>
java.lang.Object
java.util.concurrent.locks.ReentrantLock
com.google.common.collect.MapMakerInternalMap.Segment<K,V,E,S>
- All Implemented Interfaces:
Serializable
,Lock
- Direct Known Subclasses:
MapMakerInternalMap.StrongKeyDummyValueSegment
,MapMakerInternalMap.StrongKeyStrongValueSegment
,MapMakerInternalMap.StrongKeyWeakValueSegment
,MapMakerInternalMap.WeakKeyDummyValueSegment
,MapMakerInternalMap.WeakKeyStrongValueSegment
,MapMakerInternalMap.WeakKeyWeakValueSegment
- Enclosing class:
MapMakerInternalMap<K,
V, E extends MapMakerInternalMap.InternalEntry<K, V, E>, S extends MapMakerInternalMap.Segment<K, V, E, S>>
abstract static class MapMakerInternalMap.Segment<K,V,E extends MapMakerInternalMap.InternalEntry<K,V,E>,S extends MapMakerInternalMap.Segment<K,V,E,S>>
extends ReentrantLock
Segments are specialized versions of hash tables. This subclass inherits from ReentrantLock
opportunistically, just to simplify some locking and avoid separate construction.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) int
The number of live elements in this segment's region.(package private) final MapMakerInternalMap
<K, V, E, S> (package private) int
Number of updates that alter the size of the table.(package private) final AtomicInteger
A counter of the number of reads since the last write, used to drain queues on a small fraction of read operations.(package private) AtomicReferenceArray
<E> The per-segment table.(package private) int
The table is expanded when its size exceeds this threshold. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) abstract E
castForTesting
(MapMakerInternalMap.InternalEntry<K, V, ?> entry) Unsafe cast of the given entry toE
, the type of the specificMapMakerInternalMap.InternalEntry
implementation type.(package private) void
clear()
(package private) <T> void
clearReferenceQueue
(ReferenceQueue<T> referenceQueue) (package private) boolean
clearValueForTesting
(K key, int hash, MapMakerInternalMap.WeakValueReference<K, V, ? extends MapMakerInternalMap.InternalEntry<K, V, ?>> valueReference) Clears a value that has not yet been set, and thus does not require count to be modified.(package private) boolean
containsKey
(Object key, int hash) (package private) boolean
containsValue
(Object value) This method is a convenience for testing.(package private) E
Returns a copy of the givenentry
.(package private) E
copyForTesting
(MapMakerInternalMap.InternalEntry<K, V, ?> entry, MapMakerInternalMap.InternalEntry<K, V, ?> newNext) Unsafely returns a copy of the given entry.(package private) void
drainKeyReferenceQueue
(ReferenceQueue<K> keyReferenceQueue) (package private) void
drainValueReferenceQueue
(ReferenceQueue<V> valueReferenceQueue) (package private) void
expand()
Expands the table if possible.(package private) V
(package private) E
(package private) E
getFirst
(int hash) Returns first entry of bin for given hash.(package private) ReferenceQueue
<K> Unsafely extracts the key reference queue used by this segment.(package private) E
getLiveEntry
(Object key, int hash) (package private) V
getLiveValue
(E entry) Gets the value from an entry.(package private) V
Unsafely returns the value of the given entry if it's still live, ornull
otherwise.(package private) ReferenceQueue
<V> Unsafely extracts the value reference queue used by this segment.(package private) MapMakerInternalMap.WeakValueReference
<K, V, E> Unsafely extracts the weak value reference inside of the givenentry
.(package private) void
initTable
(AtomicReferenceArray<E> newTable) (package private) static <K,
V, E extends MapMakerInternalMap.InternalEntry<K, V, E>>
booleanisCollected
(E entry) Returnstrue
if the value has been partially collected, meaning that the value is null.(package private) void
Clears the reference queues used by this segment, if any.(package private) void
Drains the reference queues used by this segment, if any.(package private) AtomicReferenceArray
<E> newEntryArray
(int size) (package private) E
newEntryForTesting
(K key, int hash, MapMakerInternalMap.InternalEntry<K, V, ?> next) Unsafely returns a fresh entry.(package private) MapMakerInternalMap.WeakValueReference
<K, V, E> newWeakValueReferenceForTesting
(MapMakerInternalMap.InternalEntry<K, V, ?> entry, V value) Unsafely creates of a freshMapMakerInternalMap.WeakValueReference
, referencing the givenvalue
, for the givenentry
(package private) void
Performs routine cleanup following a read.(package private) void
Performs routine cleanup prior to executing a write.(package private) V
(package private) boolean
reclaimKey
(E entry, int hash) Removes an entry whose key has been garbage collected.(package private) boolean
reclaimValue
(K key, int hash, MapMakerInternalMap.WeakValueReference<K, V, E> valueReference) Removes an entry whose value has been garbage collected.(package private) V
(package private) boolean
(package private) boolean
removeEntryForTesting
(E entry) (package private) E
removeFromChain
(E first, E entry) Removes an entry from within a table.(package private) E
removeFromChainForTesting
(MapMakerInternalMap.InternalEntry<K, V, ?> first, MapMakerInternalMap.InternalEntry<K, V, ?> entry) Unsafely removes the given entry from the given chain in this segment's hash table.(package private) boolean
Unsafely removes the given entry from this segment's hash table.(package private) V
(package private) boolean
(package private) void
(package private) void
(package private) abstract S
self()
(package private) void
setTableEntryForTesting
(int i, MapMakerInternalMap.InternalEntry<K, V, ?> entry) Unsafely sets the given index of this segment's internal hash table to be the given entry.(package private) void
Sets the value of the givenentry
.(package private) void
setValueForTesting
(MapMakerInternalMap.InternalEntry<K, V, ?> entry, V value) Unsafely sets the value of the given entry.(package private) void
setWeakValueReferenceForTesting
(MapMakerInternalMap.InternalEntry<K, V, ?> entry, MapMakerInternalMap.WeakValueReference<K, V, ? extends MapMakerInternalMap.InternalEntry<K, V, ?>> valueReference) Unsafely sets the weak value reference inside the givenentry
to be the givenvalueReference
(package private) void
Cleanup collected entries when the lock is available.Methods inherited from class java.util.concurrent.locks.ReentrantLock
getHoldCount, getOwner, getQueuedThreads, getQueueLength, getWaitingThreads, getWaitQueueLength, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isHeldByCurrentThread, isLocked, lock, lockInterruptibly, newCondition, toString, tryLock, tryLock, unlock
-
Field Details
-
map
final MapMakerInternalMap<K,V, mapE extends MapMakerInternalMap.InternalEntry<K, V, E>, S extends MapMakerInternalMap.Segment<K, V, E, S>> -
count
volatile int countThe number of live elements in this segment's region. This does not include unset elements which are awaiting cleanup. -
modCount
int modCountNumber of updates that alter the size of the table. This is used during bulk-read methods to make sure they see a consistent snapshot: If modCounts change during a traversal of segments computing size or checking containsValue, then we might have an inconsistent view of state so (usually) must retry. -
threshold
int thresholdThe table is expanded when its size exceeds this threshold. (The value of this field is always(int) (capacity * 0.75)
.) -
table
@CheckForNull volatile AtomicReferenceArray<E extends MapMakerInternalMap.InternalEntry<K,V, tableE>> The per-segment table. -
readCount
A counter of the number of reads since the last write, used to drain queues on a small fraction of read operations.
-
-
Constructor Details
-
Segment
Segment(MapMakerInternalMap<K, V, E, S> map, int initialCapacity)
-
-
Method Details
-
self
Returnsthis
up-casted to the specificMapMakerInternalMap.Segment
implementation typeS
.This method exists so that the
MapMakerInternalMap.Segment
code can be generic in terms ofS
, the type of the concrete implementation. -
maybeDrainReferenceQueues
void maybeDrainReferenceQueues()Drains the reference queues used by this segment, if any. -
maybeClearReferenceQueues
void maybeClearReferenceQueues()Clears the reference queues used by this segment, if any. -
setValue
Sets the value of the givenentry
. -
copyEntry
Returns a copy of the givenentry
. -
newEntryArray
-
initTable
-
castForTesting
Unsafe cast of the given entry toE
, the type of the specificMapMakerInternalMap.InternalEntry
implementation type.This method is provided as a convenience for tests. Otherwise they'd need to be knowledgeable about all the implementation details of our type system trickery.
-
getKeyReferenceQueueForTesting
ReferenceQueue<K> getKeyReferenceQueueForTesting()Unsafely extracts the key reference queue used by this segment. -
getValueReferenceQueueForTesting
ReferenceQueue<V> getValueReferenceQueueForTesting()Unsafely extracts the value reference queue used by this segment. -
getWeakValueReferenceForTesting
MapMakerInternalMap.WeakValueReference<K,V, getWeakValueReferenceForTestingE> (MapMakerInternalMap.InternalEntry<K, V, ?> entry) Unsafely extracts the weak value reference inside of the givenentry
. -
newWeakValueReferenceForTesting
MapMakerInternalMap.WeakValueReference<K,V, newWeakValueReferenceForTestingE> (MapMakerInternalMap.InternalEntry<K, V, ?> entry, V value) Unsafely creates of a freshMapMakerInternalMap.WeakValueReference
, referencing the givenvalue
, for the givenentry
-
setWeakValueReferenceForTesting
void setWeakValueReferenceForTesting(MapMakerInternalMap.InternalEntry<K, V, ?> entry, MapMakerInternalMap.WeakValueReference<K, V, ? extends MapMakerInternalMap.InternalEntry<K, V, ?>> valueReference) Unsafely sets the weak value reference inside the givenentry
to be the givenvalueReference
-
setTableEntryForTesting
Unsafely sets the given index of this segment's internal hash table to be the given entry. -
copyForTesting
E copyForTesting(MapMakerInternalMap.InternalEntry<K, V, ?> entry, @CheckForNull MapMakerInternalMap.InternalEntry<K, V, ?> newNext) Unsafely returns a copy of the given entry. -
setValueForTesting
Unsafely sets the value of the given entry. -
newEntryForTesting
Unsafely returns a fresh entry. -
removeTableEntryForTesting
Unsafely removes the given entry from this segment's hash table. -
removeFromChainForTesting
@CheckForNull E removeFromChainForTesting(MapMakerInternalMap.InternalEntry<K, V, ?> first, MapMakerInternalMap.InternalEntry<K, V, ?> entry) Unsafely removes the given entry from the given chain in this segment's hash table. -
getLiveValueForTesting
Unsafely returns the value of the given entry if it's still live, ornull
otherwise. -
tryDrainReferenceQueues
void tryDrainReferenceQueues()Cleanup collected entries when the lock is available. -
drainKeyReferenceQueue
-
drainValueReferenceQueue
-
clearReferenceQueue
-
getFirst
Returns first entry of bin for given hash. -
getEntry
-
getLiveEntry
-
get
-
containsKey
-
containsValue
This method is a convenience for testing. Code should callMapMakerInternalMap.containsValue(java.lang.Object)
directly. -
put
-
expand
void expand()Expands the table if possible. -
replace
-
replace
-
remove
-
remove
-
clear
void clear() -
removeFromChain
Removes an entry from within a table. All entries following the removed node can stay, but all preceding ones need to be cloned.This method does not decrement count for the removed entry, but does decrement count for all partially collected entries which are skipped. As such callers which are modifying count must re-read it after calling removeFromChain.
- Parameters:
first
- the first entry of the tableentry
- the entry being removed from the table- Returns:
- the new first entry for the table
-
reclaimKey
Removes an entry whose key has been garbage collected. -
reclaimValue
Removes an entry whose value has been garbage collected. -
clearValueForTesting
boolean clearValueForTesting(K key, int hash, MapMakerInternalMap.WeakValueReference<K, V, ? extends MapMakerInternalMap.InternalEntry<K, V, ?>> valueReference) Clears a value that has not yet been set, and thus does not require count to be modified. -
removeEntryForTesting
-
isCollected
Returnstrue
if the value has been partially collected, meaning that the value is null. -
getLiveValue
Gets the value from an entry. Returnsnull
if the entry is invalid or partially-collected. -
postReadCleanup
void postReadCleanup()Performs routine cleanup following a read. Normally cleanup happens during writes, or from the cleanupExecutor. If cleanup is not observed after a sufficient number of reads, try cleaning up from the read thread. -
preWriteCleanup
void preWriteCleanup()Performs routine cleanup prior to executing a write. This should be called every time a write thread acquires the segment lock, immediately after acquiring the lock. -
runCleanup
void runCleanup() -
runLockedCleanup
void runLockedCleanup()
-