package com.google.common.collect;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.CollectPreconditions.checkRemove;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
The concurrent hash map implementation built by
MapMaker
.
This implementation is heavily derived from revision 1.96 of ConcurrentHashMap.java.
- Author(s):
- Bob Lee
- Charles Fry
- Doug Lea (
ConcurrentHashMap
)
The maximum capacity, used if a higher value is implicitly specified by either of the
constructors with arguments. MUST be a power of two <= 1<<30 to ensure that entries are
indexable using ints.
The maximum number of segments to allow; used to bound constructor arguments.
Number of (unsynchronized) retries in the containsValue method.
Number of cache access operations that can be buffered per segment before the cache's recency
ordering information is updated. This is used to avoid lock contention by recording a memento
of reads and delaying a lock acquisition until the threshold is crossed or a mutation occurs.
This must be a (2^n)-1 as it is used as a mask.
Maximum number of entries to be drained in a single cleanup run. This applies independently to
the cleanup queue and both reference queues.
Mask value for indexing into segments. The upper bits of a key's hash code are used to choose
the segment.
Shift value for indexing within segments. Helps prevent entries that end up in the same segment
from also ending up in the same bucket.
The segments, each of which is a specialized hash table.
final transient Segment<K, V>[] segments;
Strategy for comparing keys.
Strategy for comparing values.
Strategy for referencing keys.
Strategy for referencing values.
The maximum size of this map. MapMaker.UNSET_INT if there is no maximum.
How long after the last access to an entry the map will retain that entry.
How long after the last write to an entry the map will retain that entry.
Entries waiting to be consumed by the removal listener.
A listener that is invoked when an entry is removed due to expiration or garbage collection of
soft/weak entries.
Factory used to create new entries.
Measures time in a testable way.
Creates a new, empty map with the specified strategy, initial capacity and concurrency level.
int segmentCapacity = initialCapacity / segmentCount;
if (segmentCapacity * segmentCount < initialCapacity) {
while (segmentSize < segmentCapacity) {
int maximumSegmentSize = maximumSize / segmentCount + 1;
for (int i = 0; i < this.segments.length; ++i) {
for (int i = 0; i < this.segments.length; ++i) {
Creates a reference for the given value according to this value strength.
Returns the default equivalence strategy used to compare and hash keys or values referenced
at this strength. This strategy will be used unless the user explicitly specifies an
alternate strategy.
STRONG_EXPIRABLE_EVICTABLE {
return new WeakEntry<K, V>(segment.keyReferenceQueue, key, hash, next);
WEAK_EXPIRABLE_EVICTABLE {
Masks used to compute indices in the following table.
Look-up table for factories. First dimension is the reference type. The second dimension is
the result of OR-ing the feature masks.
Creates a new entry.
- Parameters:
segment
to create the entry forkey
of the entryhash
of the keynext
entry in the same bucket
Copies an entry, assigning it a new
next
entry.
- Parameters:
original
the entry to copynewNext
entry in the same bucket
@GuardedBy("Segment.this")
@GuardedBy("Segment.this")
@GuardedBy("Segment.this")
Gets the value. Does not block or throw exceptions.
Waits for a value that may still be computing. Unlike get(), this method can block (in the
case of FutureValueReference).
- Throws:
- ExecutionException if the computing thread throws an exception
Returns the entry associated with this value reference, or
null
if this value
reference is independent of any entry.
Creates a copy of this reference for the given entry.
value
may be null only for a loading reference.
Clears this reference object.
- Parameters:
newValue
the new value reference which will replace this one; this is only used during
computation to immediately notify blocked threads of the new value
Returns
true
if the value type is a computing reference (regardless of whether or not
computation has completed). This is necessary to distiguish between partially-collected
entries and computing entries, which need to be cleaned up differently.
Placeholder. Indicates that the value hasn't been set yet.
Singleton placeholder that indicates a value is being computed.
An entry in a reference map.
Entries in the map can be in the following states:
Valid:
- Live: valid key/value are set
- Computing: computation is pending
Invalid:
- Expired: time expired (key/value may still be set)
- Collected: key/value was partially collected, but not yet cleaned up
Gets the value reference from this entry.
Sets the value reference for this entry.
Gets the next entry in the chain.
Gets the key for this entry.
Gets the entry expiration time in ns.
Sets the entry expiration time in ns.
Gets the next entry in the recency list.
Sets the next entry in the recency list.
Gets the previous entry in the recency list.
Sets the previous entry in the recency list.
Gets the next entry in the recency list.
Sets the next entry in the recency list.
Gets the previous entry in the recency list.
Sets the previous entry in the recency list.
Queue that discards all elements.
Used for strongly-referenced keys.
previous.clear(valueReference);
@GuardedBy("Segment.this")
@GuardedBy("Segment.this")
@GuardedBy("Segment.this")
@GuardedBy("Segment.this")
@GuardedBy("Segment.this")
@GuardedBy("Segment.this")
@GuardedBy("Segment.this")
@GuardedBy("Segment.this")
Used for softly-referenced keys.