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:
java.io.Serializable
,java.util.concurrent.locks.Lock
- Direct Known Subclasses:
MapMakerInternalMap.StrongKeyStrongValueSegment
,MapMakerInternalMap.StrongKeyWeakValueSegment
,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 java.util.concurrent.locks.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
Fields Modifier and Type Field Description (package private) int
count
The number of live elements in this segment's region.(package private) MapMakerInternalMap<K,V,E,S>
map
(package private) int
maxSegmentSize
The maximum size of this map.(package private) int
modCount
Number of updates that alter the size of the table.(package private) java.util.concurrent.atomic.AtomicInteger
readCount
A counter of the number of reads since the last write, used to drain queues on a small fraction of read operations.(package private) java.util.concurrent.atomic.AtomicReferenceArray<E>
table
The per-segment table.(package private) int
threshold
The table is expanded when its size exceeds this threshold.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (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(java.lang.ref.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(java.lang.Object key, int hash)
(package private) boolean
containsValue(java.lang.Object value)
This method is a convenience for testing.(package private) E
copyEntry(E original, E newNext)
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(java.lang.ref.ReferenceQueue<K> keyReferenceQueue)
(package private) void
drainValueReferenceQueue(java.lang.ref.ReferenceQueue<V> valueReferenceQueue)
(package private) void
expand()
Expands the table if possible.(package private) V
get(java.lang.Object key, int hash)
(package private) E
getEntry(java.lang.Object key, int hash)
(package private) E
getFirst(int hash)
Returns first entry of bin for given hash.(package private) java.lang.ref.ReferenceQueue<K>
getKeyReferenceQueueForTesting()
Unsafely extracts the key reference queue used by this segment.(package private) E
getLiveEntry(java.lang.Object key, int hash)
(package private) V
getLiveValue(E entry)
Gets the value from an entry.(package private) V
getLiveValueForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry)
Unsafely returns the value of the given entry if it's still live, ornull
otherwise.(package private) java.lang.ref.ReferenceQueue<V>
getValueReferenceQueueForTesting()
Unsafely extracts the value reference queue used by this segment.(package private) MapMakerInternalMap.WeakValueReference<K,V,E>
getWeakValueReferenceForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry)
Unsafely extracts the weak value reference inside of the givenentry
.(package private) void
initTable(java.util.concurrent.atomic.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
maybeClearReferenceQueues()
Clears the reference queues used by this segment, if any.(package private) void
maybeDrainReferenceQueues()
Drains the reference queues used by this segment, if any.(package private) java.util.concurrent.atomic.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
postReadCleanup()
Performs routine cleanup following a read.(package private) void
preWriteCleanup()
Performs routine cleanup prior to executing a write.(package private) V
put(K key, int hash, V value, boolean onlyIfAbsent)
(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
remove(java.lang.Object key, int hash)
(package private) boolean
remove(java.lang.Object key, int hash, java.lang.Object value)
(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
removeTableEntryForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry)
Unsafely removes the given entry from this segment's hash table.(package private) V
replace(K key, int hash, V newValue)
(package private) boolean
replace(K key, int hash, V oldValue, V newValue)
(package private) void
runCleanup()
(package private) void
runLockedCleanup()
(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
setValue(E entry, V value)
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
tryDrainReferenceQueues()
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 Detail
-
map
final MapMakerInternalMap<K,V,E extends MapMakerInternalMap.InternalEntry<K,V,E>,S extends MapMakerInternalMap.Segment<K,V,E,S>> map
-
count
volatile int count
The number of live elements in this segment's region. This does not include unset elements which are awaiting cleanup.
-
modCount
int modCount
Number 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 threshold
The table is expanded when its size exceeds this threshold. (The value of this field is always(int) (capacity * 0.75)
.)
-
table
volatile java.util.concurrent.atomic.AtomicReferenceArray<E extends MapMakerInternalMap.InternalEntry<K,V,E>> table
The per-segment table.
-
maxSegmentSize
final int maxSegmentSize
The maximum size of this map. MapMaker.UNSET_INT if there is no maximum.
-
readCount
final java.util.concurrent.atomic.AtomicInteger readCount
A counter of the number of reads since the last write, used to drain queues on a small fraction of read operations.
-
-
Constructor Detail
-
Segment
Segment(MapMakerInternalMap<K,V,E,S> map, int initialCapacity, int maxSegmentSize)
-
-
Method Detail
-
self
abstract S 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.
-
newEntryArray
java.util.concurrent.atomic.AtomicReferenceArray<E> newEntryArray(int size)
-
initTable
void initTable(java.util.concurrent.atomic.AtomicReferenceArray<E> newTable)
-
castForTesting
abstract E castForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry)
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 knowledgable about all the implementation details of our type system trickery.
-
getKeyReferenceQueueForTesting
java.lang.ref.ReferenceQueue<K> getKeyReferenceQueueForTesting()
Unsafely extracts the key reference queue used by this segment.
-
getValueReferenceQueueForTesting
java.lang.ref.ReferenceQueue<V> getValueReferenceQueueForTesting()
Unsafely extracts the value reference queue used by this segment.
-
getWeakValueReferenceForTesting
MapMakerInternalMap.WeakValueReference<K,V,E> getWeakValueReferenceForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry)
Unsafely extracts the weak value reference inside of the givenentry
.
-
newWeakValueReferenceForTesting
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
-
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
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.
-
copyForTesting
E copyForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry, @Nullable MapMakerInternalMap.InternalEntry<K,V,?> newNext)
Unsafely returns a copy of the given entry.
-
setValueForTesting
void setValueForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry, V value)
Unsafely sets the value of the given entry.
-
newEntryForTesting
E newEntryForTesting(K key, int hash, @Nullable MapMakerInternalMap.InternalEntry<K,V,?> next)
Unsafely returns a fresh entry.
-
removeTableEntryForTesting
boolean removeTableEntryForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry)
Unsafely removes the given entry from this segment's hash table.
-
removeFromChainForTesting
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
@Nullable V getLiveValueForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry)
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
void drainKeyReferenceQueue(java.lang.ref.ReferenceQueue<K> keyReferenceQueue)
-
drainValueReferenceQueue
void drainValueReferenceQueue(java.lang.ref.ReferenceQueue<V> valueReferenceQueue)
-
clearReferenceQueue
<T> void clearReferenceQueue(java.lang.ref.ReferenceQueue<T> referenceQueue)
-
getFirst
E getFirst(int hash)
Returns first entry of bin for given hash.
-
getEntry
E getEntry(java.lang.Object key, int hash)
-
getLiveEntry
E getLiveEntry(java.lang.Object key, int hash)
-
get
V get(java.lang.Object key, int hash)
-
containsKey
boolean containsKey(java.lang.Object key, int hash)
-
containsValue
boolean containsValue(java.lang.Object value)
This method is a convenience for testing. Code should callMapMakerInternalMap.containsValue(java.lang.Object)
directly.
-
expand
void expand()
Expands the table if possible.
-
remove
V remove(java.lang.Object key, int hash)
-
remove
boolean remove(java.lang.Object key, int hash, java.lang.Object value)
-
clear
void clear()
-
removeFromChain
E removeFromChain(E first, E entry)
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
boolean reclaimKey(E entry, int hash)
Removes an entry whose key has been garbage collected.
-
reclaimValue
boolean reclaimValue(K key, int hash, MapMakerInternalMap.WeakValueReference<K,V,E> valueReference)
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
boolean removeEntryForTesting(E entry)
-
isCollected
static <K,V,E extends MapMakerInternalMap.InternalEntry<K,V,E>> boolean isCollected(E entry)
Returnstrue
if the value has been partially collected, meaning that the value is null.
-
getLiveValue
@Nullable V getLiveValue(E entry)
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()
-
-