static class LocalCache.Segment<K,V>
extends java.util.concurrent.locks.ReentrantLock
Modifier and Type | Field and Description |
---|---|
(package private) java.util.Queue<LocalCache.ReferenceEntry<K,V>> |
accessQueue
A queue of elements currently in the map, ordered by access time.
|
(package private) int |
count
The number of live elements in this segment's region.
|
(package private) java.lang.ref.ReferenceQueue<K> |
keyReferenceQueue
The key reference queue contains entries whose keys have been garbage collected, and which
need to be cleaned up internally.
|
(package private) LocalCache<K,V> |
map |
(package private) long |
maxSegmentWeight
The maximum weight of this segment.
|
(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.Queue<LocalCache.ReferenceEntry<K,V>> |
recencyQueue
The recency queue is used to record which entries were accessed for updating the access
list's ordering.
|
(package private) AbstractCache.StatsCounter |
statsCounter
Accumulates cache statistics.
|
(package private) java.util.concurrent.atomic.AtomicReferenceArray<LocalCache.ReferenceEntry<K,V>> |
table
The per-segment table.
|
(package private) int |
threshold
The table is expanded when its size exceeds this threshold.
|
(package private) long |
totalWeight
The weight of the live elements in this segment's region.
|
(package private) java.lang.ref.ReferenceQueue<V> |
valueReferenceQueue
The value reference queue contains value references whose values have been garbage collected,
and which need to be cleaned up internally.
|
(package private) java.util.Queue<LocalCache.ReferenceEntry<K,V>> |
writeQueue
A queue of elements currently in the map, ordered by write time.
|
Constructor and Description |
---|
Segment(LocalCache<K,V> map,
int initialCapacity,
long maxSegmentWeight,
AbstractCache.StatsCounter statsCounter) |
Modifier and Type | Method and Description |
---|---|
(package private) void |
cleanUp() |
(package private) void |
clear() |
(package private) void |
clearKeyReferenceQueue() |
(package private) void |
clearReferenceQueues()
Clears all entries from the key and value reference queues.
|
(package private) void |
clearValueReferenceQueue() |
(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) LocalCache.ReferenceEntry<K,V> |
copyEntry(LocalCache.ReferenceEntry<K,V> original,
LocalCache.ReferenceEntry<K,V> newNext)
Copies
original into a new entry chained to newNext . |
(package private) void |
drainKeyReferenceQueue() |
(package private) void |
drainRecencyQueue()
Drains the recency queue, updating eviction metadata that the entries therein were read in
the specified relative order.
|
(package private) void |
drainReferenceQueues()
Drain the key and value reference queues, cleaning up internal entries containing garbage
collected keys or values.
|
(package private) void |
drainValueReferenceQueue() |
(package private) void |
enqueueNotification(K key,
int hash,
LocalCache.ValueReference<K,V> valueReference,
RemovalCause cause) |
(package private) void |
enqueueNotification(LocalCache.ReferenceEntry<K,V> entry,
RemovalCause cause) |
(package private) void |
evictEntries()
Performs eviction if the segment is full.
|
(package private) void |
expand()
Expands the table if possible.
|
(package private) void |
expireEntries(long now) |
(package private) V |
get(K key,
int hash,
CacheLoader<? super K,V> loader) |
(package private) V |
get(java.lang.Object key,
int hash) |
(package private) V |
getAndRecordStats(K key,
int hash,
LocalCache.LoadingValueReference<K,V> loadingValueReference,
ListenableFuture<V> newValue)
Waits uninterruptibly for
newValue to be loaded, and then records loading stats. |
(package private) LocalCache.ReferenceEntry<K,V> |
getEntry(java.lang.Object key,
int hash) |
(package private) LocalCache.ReferenceEntry<K,V> |
getFirst(int hash)
Returns first entry of bin for given hash.
|
(package private) LocalCache.ReferenceEntry<K,V> |
getLiveEntry(java.lang.Object key,
int hash,
long now) |
(package private) V |
getLiveValue(LocalCache.ReferenceEntry<K,V> entry,
long now)
Gets the value from an entry.
|
(package private) LocalCache.ReferenceEntry<K,V> |
getNextEvictable() |
(package private) void |
initTable(java.util.concurrent.atomic.AtomicReferenceArray<LocalCache.ReferenceEntry<K,V>> newTable) |
(package private) LocalCache.LoadingValueReference<K,V> |
insertLoadingValueReference(K key,
int hash,
boolean checkTime)
Returns a newly inserted
LoadingValueReference , or null if the live value reference
is already loading. |
(package private) ListenableFuture<V> |
loadAsync(K key,
int hash,
LocalCache.LoadingValueReference<K,V> loadingValueReference,
CacheLoader<? super K,V> loader) |
(package private) V |
loadSync(K key,
int hash,
LocalCache.LoadingValueReference<K,V> loadingValueReference,
CacheLoader<? super K,V> loader) |
(package private) V |
lockedGetOrLoad(K key,
int hash,
CacheLoader<? super K,V> loader) |
(package private) LocalCache.ReferenceEntry<K,V> |
newEntry(K key,
int hash,
LocalCache.ReferenceEntry<K,V> next) |
(package private) java.util.concurrent.atomic.AtomicReferenceArray<LocalCache.ReferenceEntry<K,V>> |
newEntryArray(int size) |
(package private) void |
postReadCleanup()
Performs routine cleanup following a read.
|
(package private) void |
postWriteCleanup()
Performs routine cleanup following a write.
|
(package private) void |
preWriteCleanup(long now)
Performs routine cleanup prior to executing a write.
|
(package private) V |
put(K key,
int hash,
V value,
boolean onlyIfAbsent) |
(package private) boolean |
reclaimKey(LocalCache.ReferenceEntry<K,V> entry,
int hash)
Removes an entry whose key has been garbage collected.
|
(package private) boolean |
reclaimValue(K key,
int hash,
LocalCache.ValueReference<K,V> valueReference)
Removes an entry whose value has been garbage collected.
|
(package private) void |
recordLockedRead(LocalCache.ReferenceEntry<K,V> entry,
long now)
Updates the eviction metadata that
entry was just read. |
(package private) void |
recordRead(LocalCache.ReferenceEntry<K,V> entry,
long now)
Records the relative order in which this read was performed by adding
entry to the
recency queue. |
(package private) void |
recordWrite(LocalCache.ReferenceEntry<K,V> entry,
int weight,
long now)
Updates eviction metadata that
entry was just written. |
(package private) V |
refresh(K key,
int hash,
CacheLoader<? super K,V> loader,
boolean checkTime)
Refreshes the value associated with
key , unless another thread is already doing so. |
(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) void |
removeCollectedEntry(LocalCache.ReferenceEntry<K,V> entry) |
(package private) boolean |
removeEntry(LocalCache.ReferenceEntry<K,V> entry,
int hash,
RemovalCause cause) |
(package private) LocalCache.ReferenceEntry<K,V> |
removeEntryFromChain(LocalCache.ReferenceEntry<K,V> first,
LocalCache.ReferenceEntry<K,V> entry) |
(package private) boolean |
removeLoadingValue(K key,
int hash,
LocalCache.LoadingValueReference<K,V> valueReference) |
(package private) LocalCache.ReferenceEntry<K,V> |
removeValueFromChain(LocalCache.ReferenceEntry<K,V> first,
LocalCache.ReferenceEntry<K,V> entry,
K key,
int hash,
LocalCache.ValueReference<K,V> valueReference,
RemovalCause cause) |
(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 |
runLockedCleanup(long now) |
(package private) void |
runUnlockedCleanup() |
(package private) V |
scheduleRefresh(LocalCache.ReferenceEntry<K,V> entry,
K key,
int hash,
V oldValue,
long now,
CacheLoader<? super K,V> loader) |
(package private) void |
setValue(LocalCache.ReferenceEntry<K,V> entry,
K key,
V value,
long now)
Sets a new value of an entry.
|
(package private) boolean |
storeLoadedValue(K key,
int hash,
LocalCache.LoadingValueReference<K,V> oldValueReference,
V newValue) |
(package private) void |
tryDrainReferenceQueues()
Cleanup collected entries when the lock is available.
|
(package private) void |
tryExpireEntries(long now)
Cleanup expired entries when the lock is available.
|
(package private) V |
waitForLoadingValue(LocalCache.ReferenceEntry<K,V> e,
K key,
LocalCache.ValueReference<K,V> valueReference) |
getHoldCount, getOwner, getQueuedThreads, getQueueLength, getWaitingThreads, getWaitQueueLength, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isHeldByCurrentThread, isLocked, lock, lockInterruptibly, newCondition, toString, tryLock, tryLock, unlock
final LocalCache<K,V> map
volatile int count
long totalWeight
int modCount
int threshold
(int) (capacity * 0.75)
.)volatile java.util.concurrent.atomic.AtomicReferenceArray<LocalCache.ReferenceEntry<K,V>> table
final long maxSegmentWeight
final java.lang.ref.ReferenceQueue<K> keyReferenceQueue
final java.lang.ref.ReferenceQueue<V> valueReferenceQueue
final java.util.Queue<LocalCache.ReferenceEntry<K,V>> recencyQueue
final java.util.concurrent.atomic.AtomicInteger readCount
final java.util.Queue<LocalCache.ReferenceEntry<K,V>> writeQueue
final java.util.Queue<LocalCache.ReferenceEntry<K,V>> accessQueue
final AbstractCache.StatsCounter statsCounter
Segment(LocalCache<K,V> map, int initialCapacity, long maxSegmentWeight, AbstractCache.StatsCounter statsCounter)
java.util.concurrent.atomic.AtomicReferenceArray<LocalCache.ReferenceEntry<K,V>> newEntryArray(int size)
void initTable(java.util.concurrent.atomic.AtomicReferenceArray<LocalCache.ReferenceEntry<K,V>> newTable)
LocalCache.ReferenceEntry<K,V> newEntry(K key, int hash, @Nullable LocalCache.ReferenceEntry<K,V> next)
LocalCache.ReferenceEntry<K,V> copyEntry(LocalCache.ReferenceEntry<K,V> original, LocalCache.ReferenceEntry<K,V> newNext)
original
into a new entry chained to newNext
. Returns the new entry,
or null
if original
was already garbage collected.void setValue(LocalCache.ReferenceEntry<K,V> entry, K key, V value, long now)
V get(K key, int hash, CacheLoader<? super K,V> loader) throws java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException
V lockedGetOrLoad(K key, int hash, CacheLoader<? super K,V> loader) throws java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException
V waitForLoadingValue(LocalCache.ReferenceEntry<K,V> e, K key, LocalCache.ValueReference<K,V> valueReference) throws java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException
V loadSync(K key, int hash, LocalCache.LoadingValueReference<K,V> loadingValueReference, CacheLoader<? super K,V> loader) throws java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException
ListenableFuture<V> loadAsync(K key, int hash, LocalCache.LoadingValueReference<K,V> loadingValueReference, CacheLoader<? super K,V> loader)
V getAndRecordStats(K key, int hash, LocalCache.LoadingValueReference<K,V> loadingValueReference, ListenableFuture<V> newValue) throws java.util.concurrent.ExecutionException
newValue
to be loaded, and then records loading stats.java.util.concurrent.ExecutionException
V scheduleRefresh(LocalCache.ReferenceEntry<K,V> entry, K key, int hash, V oldValue, long now, CacheLoader<? super K,V> loader)
@Nullable V refresh(K key, int hash, CacheLoader<? super K,V> loader, boolean checkTime)
key
, unless another thread is already doing so.
Returns the newly refreshed value associated with key
if it was refreshed inline, or
null
if another thread is performing the refresh or if an error occurs during
refresh.@Nullable LocalCache.LoadingValueReference<K,V> insertLoadingValueReference(K key, int hash, boolean checkTime)
LoadingValueReference
, or null if the live value reference
is already loading.void tryDrainReferenceQueues()
void drainReferenceQueues()
void drainKeyReferenceQueue()
void drainValueReferenceQueue()
void clearReferenceQueues()
void clearKeyReferenceQueue()
void clearValueReferenceQueue()
void recordRead(LocalCache.ReferenceEntry<K,V> entry, long now)
entry
to the
recency queue. At write-time, or when the queue is full past the threshold, the queue will
be drained and the entries therein processed.
Note: locked reads should use recordLockedRead(com.google.common.cache.LocalCache.ReferenceEntry<K, V>, long)
.
void recordLockedRead(LocalCache.ReferenceEntry<K,V> entry, long now)
entry
was just read. This currently amounts to
adding entry
to relevant eviction lists.
Note: this method should only be called under lock, as it directly manipulates the
eviction queues. Unlocked reads should use recordRead(com.google.common.cache.LocalCache.ReferenceEntry<K, V>, long)
.
void recordWrite(LocalCache.ReferenceEntry<K,V> entry, int weight, long now)
entry
was just written. This currently amounts to
adding entry
to relevant eviction lists.void drainRecencyQueue()
void tryExpireEntries(long now)
void expireEntries(long now)
void enqueueNotification(LocalCache.ReferenceEntry<K,V> entry, RemovalCause cause)
void enqueueNotification(@Nullable K key, int hash, LocalCache.ValueReference<K,V> valueReference, RemovalCause cause)
void evictEntries()
count
.LocalCache.ReferenceEntry<K,V> getNextEvictable()
LocalCache.ReferenceEntry<K,V> getFirst(int hash)
@Nullable LocalCache.ReferenceEntry<K,V> getEntry(java.lang.Object key, int hash)
@Nullable LocalCache.ReferenceEntry<K,V> getLiveEntry(java.lang.Object key, int hash, long now)
V getLiveValue(LocalCache.ReferenceEntry<K,V> entry, long now)
@Nullable V get(java.lang.Object key, int hash)
boolean containsKey(java.lang.Object key, int hash)
boolean containsValue(java.lang.Object value)
LocalCache.containsValue(java.lang.Object)
directly.void expand()
@Nullable V remove(java.lang.Object key, int hash)
boolean storeLoadedValue(K key, int hash, LocalCache.LoadingValueReference<K,V> oldValueReference, V newValue)
boolean remove(java.lang.Object key, int hash, java.lang.Object value)
void clear()
@Nullable LocalCache.ReferenceEntry<K,V> removeValueFromChain(LocalCache.ReferenceEntry<K,V> first, LocalCache.ReferenceEntry<K,V> entry, @Nullable K key, int hash, LocalCache.ValueReference<K,V> valueReference, RemovalCause cause)
@Nullable LocalCache.ReferenceEntry<K,V> removeEntryFromChain(LocalCache.ReferenceEntry<K,V> first, LocalCache.ReferenceEntry<K,V> entry)
void removeCollectedEntry(LocalCache.ReferenceEntry<K,V> entry)
boolean reclaimKey(LocalCache.ReferenceEntry<K,V> entry, int hash)
boolean reclaimValue(K key, int hash, LocalCache.ValueReference<K,V> valueReference)
boolean removeLoadingValue(K key, int hash, LocalCache.LoadingValueReference<K,V> valueReference)
boolean removeEntry(LocalCache.ReferenceEntry<K,V> entry, int hash, RemovalCause cause)
void postReadCleanup()
void preWriteCleanup(long now)
Post-condition: expireEntries has been run.
void postWriteCleanup()
void cleanUp()
void runLockedCleanup(long now)
void runUnlockedCleanup()