Class PrivateMaxEntriesMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- com.fasterxml.jackson.databind.util.internal.PrivateMaxEntriesMap<K,V>
-
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
java.io.Serializable,java.util.concurrent.ConcurrentMap<K,V>,java.util.Map<K,V>
public final class PrivateMaxEntriesMap<K,V> extends java.util.AbstractMap<K,V> implements java.util.concurrent.ConcurrentMap<K,V>, java.io.SerializableA hash table supporting full concurrency of retrievals, adjustable expected concurrency for updates, and a maximum capacity to bound the map by. This implementation differs fromConcurrentHashMapin that it maintains a page replacement algorithm that is used to evict an entry when the map has exceeded its capacity. Unlike theJava Collections Framework, this map does not have a publicly visible constructor and instances are created through aPrivateMaxEntriesMap.Builder.An entry is evicted from the map when the entry size exceeds its
maximum capacitythreshold.An
EvictionListenermay be supplied for notification when an entry is evicted from the map. This listener is invoked on a caller's thread and will not block other threads from operating on the map. An implementation should be aware that the caller's thread will not expect long execution times or failures as a side effect of the listener being notified. Execution safety and a fast turn around time can be achieved by performing the operation asynchronously, such as by submitting a task to anExecutorService.The
concurrency leveldetermines the number of threads that can concurrently modify the table. Using a significantly higher or lower value than needed can waste space or lead to thread contention, but an estimate within an order of magnitude of the ideal value does not usually have a noticeable impact. Because placement in hash tables is essentially random, the actual concurrency will vary.This class and its views and iterators implement all of the optional methods of the
MapandIteratorinterfaces.Like
Hashtablebut unlikeHashMap, this class does not allownullto be used as a key or value. UnlikeLinkedHashMap, this class does not provide predictable iteration order. A snapshot of the keys and entries may be obtained in ascending and descending order of retention.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classPrivateMaxEntriesMap.AddTaskAdds the node to the page replacement policy.static classPrivateMaxEntriesMap.Builder<K,V>A builder that createsPrivateMaxEntriesMapinstances.(package private) static classPrivateMaxEntriesMap.DrainStatusThe draining status of the buffers.(package private) classPrivateMaxEntriesMap.EntryIteratorAn adapter to safely externalize the entry iterator.(package private) classPrivateMaxEntriesMap.EntrySetAn adapter to safely externalize the entries.(package private) classPrivateMaxEntriesMap.KeyIteratorAn adapter to safely externalize the key iterator.(package private) classPrivateMaxEntriesMap.KeySetAn adapter to safely externalize the keys.(package private) static classPrivateMaxEntriesMap.Node<K,V>A node contains the key, the weighted value, and the linkage pointers on the page-replacement algorithm's data structures.(package private) classPrivateMaxEntriesMap.RemovalTaskRemoves a node from the page replacement policy.(package private) static classPrivateMaxEntriesMap.SerializationProxy<K,V>A proxy that is serialized instead of the map.(package private) classPrivateMaxEntriesMap.UpdateTaskUpdates the weighted size and evicts an entry on overflow.(package private) classPrivateMaxEntriesMap.ValueIteratorAn adapter to safely externalize the value iterator.(package private) classPrivateMaxEntriesMap.ValuesAn adapter to safely externalize the values.(package private) static classPrivateMaxEntriesMap.WeightedValue<V>A value, its weight, and the entry's status.(package private) classPrivateMaxEntriesMap.WriteThroughEntryAn entry that allows updates to write through to the map.
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.concurrent.atomic.AtomicLongcapacity(package private) intconcurrencyLevel(package private) java.util.concurrent.ConcurrentMap<K,PrivateMaxEntriesMap.Node<K,V>>data(package private) java.util.concurrent.atomic.AtomicReference<PrivateMaxEntriesMap.DrainStatus>drainStatus(package private) java.util.Set<java.util.Map.Entry<K,V>>entrySet(package private) LinkedDeque<PrivateMaxEntriesMap.Node<K,V>>evictionDeque(package private) java.util.concurrent.locks.LockevictionLock(package private) java.util.Set<K>keySet(package private) static longMAXIMUM_CAPACITYThe maximum capacity of the map.(package private) static intNCPUThe number of CPUs(package private) static intNUMBER_OF_READ_BUFFERSThe number of read buffers to use.(package private) static intREAD_BUFFER_DRAIN_THRESHOLDThe maximum number of read operations to perform per amortized drain.(package private) static intREAD_BUFFER_INDEX_MASKMask value for indexing into the read buffer.(package private) static intREAD_BUFFER_SIZEThe maximum number of pending reads per buffer.(package private) static intREAD_BUFFER_THRESHOLDThe number of pending read operations before attempting to drain.(package private) static intREAD_BUFFERS_MASKMask value for indexing into the read buffers.(package private) java.util.concurrent.atomic.AtomicLongArrayreadBufferDrainAtWriteCount(package private) long[]readBufferReadCount(package private) java.util.concurrent.atomic.AtomicReferenceArray<PrivateMaxEntriesMap.Node<K,V>>readBuffers(package private) java.util.concurrent.atomic.AtomicLongArrayreadBufferWriteCount(package private) static longserialVersionUID(package private) java.util.Collection<V>values(package private) java.util.concurrent.atomic.AtomicLongweightedSize(package private) static intWRITE_BUFFER_DRAIN_THRESHOLDThe maximum number of write operations to perform per amortized drain.(package private) java.util.Queue<java.lang.Runnable>writeBuffer
-
Constructor Summary
Constructors Constructor Description PrivateMaxEntriesMap(PrivateMaxEntriesMap.Builder<K,V> builder)Creates an instance based on the builder's configuration.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidafterRead(PrivateMaxEntriesMap.Node<K,V> node)Performs the post-processing work required after a read.(package private) voidafterWrite(java.lang.Runnable task)Performs the post-processing work required after a write.(package private) voidapplyRead(PrivateMaxEntriesMap.Node<K,V> node)Updates the node's location in the page replacement policy.longcapacity()Retrieves the maximum capacity of the map.(package private) static intceilingNextPowerOfTwo(int x)(package private) static voidcheckArgument(boolean expression)Ensures that the argument expression is true.(package private) static voidcheckNotNull(java.lang.Object o)Ensures that the object is not null.(package private) static voidcheckState(boolean expression)Ensures that the state expression is true.voidclear()booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)(package private) voiddrainBuffers()Drains the read and write buffers up to an amortized threshold.(package private) voiddrainOnReadIfNeeded(int bufferIndex, long writeCount)Attempts to drain the buffers if it is determined to be needed when post-processing a read.(package private) voiddrainReadBuffer(int bufferIndex)Drains the read buffer up to an amortized threshold.(package private) voiddrainReadBuffers()Drains the read buffers, each up to an amortized threshold.(package private) voiddrainWriteBuffer()Drains the read buffer up to an amortized threshold.java.util.Set<java.util.Map.Entry<K,V>>entrySet()(package private) voidevict()Evicts entries from the map while it exceeds the capacity and appends evicted entries to the notification queue for processing.Vget(java.lang.Object key)(package private) booleanhasOverflowed()Determines whether the map has exceeded its capacity.booleanisEmpty()java.util.Set<K>keySet()(package private) voidmakeDead(PrivateMaxEntriesMap.Node<K,V> node)Atomically transitions the node to thedeadstate and decrements theweightedSize.(package private) voidmakeRetired(PrivateMaxEntriesMap.Node<K,V> node)Atomically transitions the node from thealivestate to theretiredstate, if a valid transition.Vput(K key, V value)(package private) Vput(K key, V value, boolean onlyIfAbsent)Adds a node to the list and the data store.VputIfAbsent(K key, V value)(package private) static intreadBufferIndex()Returns the index to the read buffer to record into.private static intreadBufferIndex(int bufferIndex, int entryIndex)private voidreadObject(java.io.ObjectInputStream stream)(package private) longrecordRead(int bufferIndex, PrivateMaxEntriesMap.Node<K,V> node)Records a read in the buffer and return its write count.Vremove(java.lang.Object key)booleanremove(java.lang.Object key, java.lang.Object value)Vreplace(K key, V value)booleanreplace(K key, V oldValue, V newValue)voidsetCapacity(long capacity)Sets the maximum capacity of the map and eagerly evicts entries until it shrinks to the appropriate size.intsize()(package private) voidtryToDrainBuffers()Attempts to acquire the eviction lock and apply the pending operations, up to the amortized threshold, to the page replacement policy.(package private) booleantryToRetire(PrivateMaxEntriesMap.Node<K,V> node, PrivateMaxEntriesMap.WeightedValue<V> expect)Attempts to transition the node from thealivestate to theretiredstate.java.util.Collection<V>values()(package private) java.lang.ObjectwriteReplace()-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
-
-
-
Field Detail
-
NCPU
static final int NCPU
The number of CPUs
-
MAXIMUM_CAPACITY
static final long MAXIMUM_CAPACITY
The maximum capacity of the map.- See Also:
- Constant Field Values
-
NUMBER_OF_READ_BUFFERS
static final int NUMBER_OF_READ_BUFFERS
The number of read buffers to use. The max of 4 was introduced due to databind#3665.
-
READ_BUFFERS_MASK
static final int READ_BUFFERS_MASK
Mask value for indexing into the read buffers.
-
READ_BUFFER_THRESHOLD
static final int READ_BUFFER_THRESHOLD
The number of pending read operations before attempting to drain. The threshold of 4 was introduced due to databind#3665.- See Also:
- Constant Field Values
-
READ_BUFFER_DRAIN_THRESHOLD
static final int READ_BUFFER_DRAIN_THRESHOLD
The maximum number of read operations to perform per amortized drain.- See Also:
- Constant Field Values
-
READ_BUFFER_SIZE
static final int READ_BUFFER_SIZE
The maximum number of pending reads per buffer.- See Also:
- Constant Field Values
-
READ_BUFFER_INDEX_MASK
static final int READ_BUFFER_INDEX_MASK
Mask value for indexing into the read buffer.- See Also:
- Constant Field Values
-
WRITE_BUFFER_DRAIN_THRESHOLD
static final int WRITE_BUFFER_DRAIN_THRESHOLD
The maximum number of write operations to perform per amortized drain.- See Also:
- Constant Field Values
-
data
final java.util.concurrent.ConcurrentMap<K,PrivateMaxEntriesMap.Node<K,V>> data
-
concurrencyLevel
final int concurrencyLevel
-
readBufferReadCount
final long[] readBufferReadCount
-
evictionDeque
final LinkedDeque<PrivateMaxEntriesMap.Node<K,V>> evictionDeque
-
weightedSize
final java.util.concurrent.atomic.AtomicLong weightedSize
-
capacity
final java.util.concurrent.atomic.AtomicLong capacity
-
evictionLock
final java.util.concurrent.locks.Lock evictionLock
-
writeBuffer
final java.util.Queue<java.lang.Runnable> writeBuffer
-
readBufferWriteCount
final java.util.concurrent.atomic.AtomicLongArray readBufferWriteCount
-
readBufferDrainAtWriteCount
final java.util.concurrent.atomic.AtomicLongArray readBufferDrainAtWriteCount
-
readBuffers
final java.util.concurrent.atomic.AtomicReferenceArray<PrivateMaxEntriesMap.Node<K,V>> readBuffers
-
drainStatus
final java.util.concurrent.atomic.AtomicReference<PrivateMaxEntriesMap.DrainStatus> drainStatus
-
keySet
transient java.util.Set<K> keySet
-
values
transient java.util.Collection<V> values
-
serialVersionUID
static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
PrivateMaxEntriesMap
PrivateMaxEntriesMap(PrivateMaxEntriesMap.Builder<K,V> builder)
Creates an instance based on the builder's configuration.
-
-
Method Detail
-
ceilingNextPowerOfTwo
static int ceilingNextPowerOfTwo(int x)
-
readBufferIndex
private static int readBufferIndex(int bufferIndex, int entryIndex)
-
checkNotNull
static void checkNotNull(java.lang.Object o)
Ensures that the object is not null.
-
checkArgument
static void checkArgument(boolean expression)
Ensures that the argument expression is true.
-
checkState
static void checkState(boolean expression)
Ensures that the state expression is true.
-
capacity
public long capacity()
Retrieves the maximum capacity of the map.- Returns:
- the maximum capacity
-
setCapacity
public void setCapacity(long capacity)
Sets the maximum capacity of the map and eagerly evicts entries until it shrinks to the appropriate size.- Parameters:
capacity- the maximum capacity of the map- Throws:
java.lang.IllegalArgumentException- if the capacity is negative
-
hasOverflowed
boolean hasOverflowed()
Determines whether the map has exceeded its capacity.
-
evict
void evict()
Evicts entries from the map while it exceeds the capacity and appends evicted entries to the notification queue for processing.
-
afterRead
void afterRead(PrivateMaxEntriesMap.Node<K,V> node)
Performs the post-processing work required after a read.- Parameters:
node- the entry in the page replacement policy
-
readBufferIndex
static int readBufferIndex()
Returns the index to the read buffer to record into.
-
recordRead
long recordRead(int bufferIndex, PrivateMaxEntriesMap.Node<K,V> node)Records a read in the buffer and return its write count.- Parameters:
bufferIndex- the index to the chosen read buffernode- the entry in the page replacement policy- Returns:
- the number of writes on the chosen read buffer
-
drainOnReadIfNeeded
void drainOnReadIfNeeded(int bufferIndex, long writeCount)Attempts to drain the buffers if it is determined to be needed when post-processing a read.- Parameters:
bufferIndex- the index to the chosen read bufferwriteCount- the number of writes on the chosen read buffer
-
afterWrite
void afterWrite(java.lang.Runnable task)
Performs the post-processing work required after a write.- Parameters:
task- the pending operation to be applied
-
tryToDrainBuffers
void tryToDrainBuffers()
Attempts to acquire the eviction lock and apply the pending operations, up to the amortized threshold, to the page replacement policy.
-
drainBuffers
void drainBuffers()
Drains the read and write buffers up to an amortized threshold.
-
drainReadBuffers
void drainReadBuffers()
Drains the read buffers, each up to an amortized threshold.
-
drainReadBuffer
void drainReadBuffer(int bufferIndex)
Drains the read buffer up to an amortized threshold.
-
applyRead
void applyRead(PrivateMaxEntriesMap.Node<K,V> node)
Updates the node's location in the page replacement policy.
-
drainWriteBuffer
void drainWriteBuffer()
Drains the read buffer up to an amortized threshold.
-
tryToRetire
boolean tryToRetire(PrivateMaxEntriesMap.Node<K,V> node, PrivateMaxEntriesMap.WeightedValue<V> expect)
Attempts to transition the node from thealivestate to theretiredstate.- Parameters:
node- the entry in the page replacement policyexpect- the expected weighted value- Returns:
- if successful
-
makeRetired
void makeRetired(PrivateMaxEntriesMap.Node<K,V> node)
Atomically transitions the node from thealivestate to theretiredstate, if a valid transition.- Parameters:
node- the entry in the page replacement policy
-
makeDead
void makeDead(PrivateMaxEntriesMap.Node<K,V> node)
Atomically transitions the node to thedeadstate and decrements theweightedSize.- Parameters:
node- the entry in the page replacement policy
-
isEmpty
public boolean isEmpty()
-
size
public int size()
-
clear
public void clear()
-
containsKey
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
get
public V get(java.lang.Object key)
-
put
V put(K key, V value, boolean onlyIfAbsent)
Adds a node to the list and the data store. If an existing node is found, then its value is updated if allowed.- Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified keyonlyIfAbsent- a write is performed only if the key is not already associated with a value- Returns:
- the prior value in the data store or null if no mapping was found
-
remove
public V remove(java.lang.Object key)
-
remove
public boolean remove(java.lang.Object key, java.lang.Object value)
-
keySet
public java.util.Set<K> keySet()
-
values
public java.util.Collection<V> values()
-
writeReplace
java.lang.Object writeReplace()
-
readObject
private void readObject(java.io.ObjectInputStream stream) throws java.io.InvalidObjectException- Throws:
java.io.InvalidObjectException
-
-