Class PrivateMaxEntriesMap<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
Serializable, ConcurrentMap<K,V>, Map<K, V>
ConcurrentHashMap in that it maintains a
page replacement algorithm that is used to evict an entry when the map has
exceeded its capacity. Unlike the Java Collections Framework, this
map does not have a publicly visible constructor and instances are created
through a PrivateMaxEntriesMap.Builder.
An entry is evicted from the map when the entry size exceeds
its maximum capacity threshold.
An EvictionListener may 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 an
ExecutorService.
The concurrency level determines 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 Map and Iterator
interfaces.
Like Hashtable but unlike HashMap, this class
does not allow null to be used as a key or value. Unlike
LinkedHashMap, 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.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) final classAdds the node to the page replacement policy.static final classA builder that createsPrivateMaxEntriesMapinstances.(package private) static enumThe draining status of the buffers.(package private) final classAn adapter to safely externalize the entry iterator.(package private) final classAn adapter to safely externalize the entries.(package private) final classAn adapter to safely externalize the key iterator.(package private) final classAn adapter to safely externalize the keys.(package private) static final classA node contains the key, the weighted value, and the linkage pointers on the page-replacement algorithm's data structures.(package private) final classRemoves a node from the page replacement policy.(package private) static final classA proxy that is serialized instead of the map.(package private) final classUpdates the weighted size and evicts an entry on overflow.(package private) final classAn adapter to safely externalize the value iterator.(package private) final classAn adapter to safely externalize the values.(package private) static final classA value, its weight, and the entry's status.(package private) final classAn entry that allows updates to write through to the map.Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final AtomicLong(package private) final int(package private) final ConcurrentMap<K, PrivateMaxEntriesMap.Node<K, V>> (package private) final AtomicReference<PrivateMaxEntriesMap.DrainStatus> (package private) final LinkedDeque<PrivateMaxEntriesMap.Node<K, V>> (package private) final Lock(package private) static final longThe maximum capacity of the map.(package private) static final intThe number of CPUs(package private) static final intThe number of read buffers to use.(package private) static final intThe maximum number of read operations to perform per amortized drain.(package private) static final intMask value for indexing into the read buffer.(package private) static final intThe maximum number of pending reads per buffer.(package private) static final intThe number of pending read operations before attempting to drain.(package private) static final intMask value for indexing into the read buffers.(package private) final AtomicLongArray(package private) final long[](package private) final AtomicReferenceArray<PrivateMaxEntriesMap.Node<K, V>> (package private) final AtomicLongArray(package private) static final long(package private) Collection<V> (package private) final AtomicLong(package private) static final intThe maximum number of write operations to perform per amortized drain. -
Constructor Summary
ConstructorsConstructorDescriptionPrivateMaxEntriesMap(PrivateMaxEntriesMap.Builder<K, V> builder) Creates an instance based on the builder's configuration. -
Method Summary
Modifier and TypeMethodDescription(package private) voidafterRead(PrivateMaxEntriesMap.Node<K, V> node) Performs the post-processing work required after a read.(package private) voidafterWrite(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 voidEnsures that the object is not null.(package private) static voidcheckState(boolean expression) Ensures that the state expression is true.voidclear()booleancontainsKey(Object key) booleancontainsValue(Object value) (package private) voidDrains 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) voidDrains the read buffers, each up to an amortized threshold.(package private) voidDrains the read buffer up to an amortized threshold.entrySet()(package private) voidevict()Evicts entries from the map while it exceeds the capacity and appends evicted entries to the notification queue for processing.(package private) booleanDetermines whether the map has exceeded its capacity.booleanisEmpty()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.(package private) VAdds a node to the list and the data store.putIfAbsent(K key, V value) (package private) static intReturns the index to the read buffer to record into.private static intreadBufferIndex(int bufferIndex, int entryIndex) private voidreadObject(ObjectInputStream stream) (package private) longrecordRead(int bufferIndex, PrivateMaxEntriesMap.Node<K, V> node) Records a read in the buffer and return its write count.booleanbooleanvoidsetCapacity(long capacity) Sets the maximum capacity of the map and eagerly evicts entries until it shrinks to the appropriate size.intsize()(package private) voidAttempts 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.values()(package private) ObjectMethods inherited from interface ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
-
Field Details
-
NCPU
static final int NCPUThe number of CPUs -
MAXIMUM_CAPACITY
static final long MAXIMUM_CAPACITYThe maximum capacity of the map.- See Also:
-
NUMBER_OF_READ_BUFFERS
static final int NUMBER_OF_READ_BUFFERSThe number of read buffers to use. The max of 4 was introduced due to databind#3665. -
READ_BUFFERS_MASK
static final int READ_BUFFERS_MASKMask value for indexing into the read buffers. -
READ_BUFFER_THRESHOLD
static final int READ_BUFFER_THRESHOLDThe number of pending read operations before attempting to drain. The threshold of 4 was introduced due to databind#3665.- See Also:
-
READ_BUFFER_DRAIN_THRESHOLD
static final int READ_BUFFER_DRAIN_THRESHOLDThe maximum number of read operations to perform per amortized drain.- See Also:
-
READ_BUFFER_SIZE
static final int READ_BUFFER_SIZEThe maximum number of pending reads per buffer.- See Also:
-
READ_BUFFER_INDEX_MASK
static final int READ_BUFFER_INDEX_MASKMask value for indexing into the read buffer.- See Also:
-
WRITE_BUFFER_DRAIN_THRESHOLD
static final int WRITE_BUFFER_DRAIN_THRESHOLDThe maximum number of write operations to perform per amortized drain.- See Also:
-
data
-
concurrencyLevel
final int concurrencyLevel -
readBufferReadCount
final long[] readBufferReadCount -
evictionDeque
-
weightedSize
-
capacity
-
evictionLock
-
writeBuffer
-
readBufferWriteCount
-
readBufferDrainAtWriteCount
-
readBuffers
-
drainStatus
-
keySet
-
values
-
entrySet
-
serialVersionUID
static final long serialVersionUID- See Also:
-
-
Constructor Details
-
PrivateMaxEntriesMap
PrivateMaxEntriesMap(PrivateMaxEntriesMap.Builder<K, V> builder) Creates an instance based on the builder's configuration.
-
-
Method Details
-
ceilingNextPowerOfTwo
static int ceilingNextPowerOfTwo(int x) -
readBufferIndex
private static int readBufferIndex(int bufferIndex, int entryIndex) -
checkNotNull
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:
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
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
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
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
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
Atomically transitions the node from thealivestate to theretiredstate, if a valid transition.- Parameters:
node- the entry in the page replacement policy
-
makeDead
Atomically transitions the node to thedeadstate and decrements theweightedSize.- Parameters:
node- the entry in the page replacement policy
-
isEmpty
-
size
-
clear
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V>
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V> - Overrides:
containsValuein classAbstractMap<K,V>
-
get
-
put
-
putIfAbsent
- Specified by:
putIfAbsentin interfaceConcurrentMap<K,V> - Specified by:
putIfAbsentin interfaceMap<K,V>
-
put
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
-
remove
-
replace
-
replace
-
keySet
-
values
-
entrySet
-
writeReplace
Object writeReplace() -
readObject
- Throws:
InvalidObjectException
-