Package org.h2.mvstore.cache
Class CacheLongKeyLIRS.Segment<V>
java.lang.Object
org.h2.mvstore.cache.CacheLongKeyLIRS.Segment<V>
- Type Parameters:
V
- the value type
- Enclosing class:
CacheLongKeyLIRS<V>
A cache segment
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) final CacheLongKeyLIRS.Entry<V>[]
The map array.(package private) long
The number of cache hits.(package private) int
The number of (hot, cold, and non-resident) entries in the map.private final int
The bit mask that is applied to the key hash code to get the index in the map array.private long
The maximum memory this cache should use in bytes.(package private) long
The number of cache misses.private final int
Low watermark for the number of entries in the non-resident queue, as a factor of the number of entries in the map.private final int
High watermark for the number of entries in the non-resident queue, as a factor of the number of entries in the map.private final CacheLongKeyLIRS.Entry
<V> The queue of resident cold entries.private final CacheLongKeyLIRS.Entry
<V> The queue of non-resident cold entries.(package private) int
The size of the LIRS queue for non-resident cold entries.(package private) int
The size of the LIRS queue for resident cold entries.private final CacheLongKeyLIRS.Entry
<V> The stack of recently referenced elements.private int
The number of times any item was moved to the top of the stack.private final int
How many other item are to be moved to the top of the stack before the current item is moved.private int
The number of entries in the stack.(package private) long
The currently used memory. -
Constructor Summary
ConstructorsConstructorDescriptionSegment
(long maxMemory, int stackMoveDistance, int len, int nonResidentQueueSize, int nonResidentQueueSizeHigh) Create a new cache segment.Segment
(CacheLongKeyLIRS.Segment<V> old, int len) Create a new cache segment from an existing one. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Access an item, moving the entry to the top of the stack or front of the queue if found.private void
private void
private void
private void
private void
private void
evict()
Evict cold entries (resident and non-resident) until the memory limit is reached.private void
(package private) CacheLongKeyLIRS.Entry
<V> find
(long key, int hash) Try to find an entry in the map.(package private) V
Get the value from the given entry.(package private) int
Calculate the new number of hash table buckets if the internal map should be re-sized.keys
(boolean cold, boolean nonResident) Get the list of keys.keySet()
Get the set of keys for resident entries.private void
Ensure the last entry of the stack is cold.(package private) V
Add an entry to the cache.(package private) V
remove
(long key, int hash) Remove an entry.private void
private void
Remove the entry from the stack.(package private) void
setMaxMemory
(long maxMemory) Set the maximum memory this cache should use.(package private) void
-
Field Details
-
mapSize
int mapSizeThe number of (hot, cold, and non-resident) entries in the map. -
queueSize
int queueSizeThe size of the LIRS queue for resident cold entries. -
queue2Size
int queue2SizeThe size of the LIRS queue for non-resident cold entries. -
hits
long hitsThe number of cache hits. -
misses
long missesThe number of cache misses. -
entries
The map array. The size is always a power of 2. -
usedMemory
long usedMemoryThe currently used memory. -
stackMoveDistance
private final int stackMoveDistanceHow many other item are to be moved to the top of the stack before the current item is moved. -
maxMemory
private long maxMemoryThe maximum memory this cache should use in bytes. -
mask
private final int maskThe bit mask that is applied to the key hash code to get the index in the map array. The mask is the length of the array minus one. -
nonResidentQueueSize
private final int nonResidentQueueSizeLow watermark for the number of entries in the non-resident queue, as a factor of the number of entries in the map. -
nonResidentQueueSizeHigh
private final int nonResidentQueueSizeHighHigh watermark for the number of entries in the non-resident queue, as a factor of the number of entries in the map. -
stack
The stack of recently referenced elements. This includes all hot entries, and the recently referenced cold entries. Resident cold entries that were not recently referenced, as well as non-resident cold entries, are not in the stack.There is always at least one entry: the head entry.
-
stackSize
private int stackSizeThe number of entries in the stack. -
queue
The queue of resident cold entries.There is always at least one entry: the head entry.
-
queue2
The queue of non-resident cold entries.There is always at least one entry: the head entry.
-
stackMoveCounter
private int stackMoveCounterThe number of times any item was moved to the top of the stack.
-
-
Constructor Details
-
Segment
Segment(long maxMemory, int stackMoveDistance, int len, int nonResidentQueueSize, int nonResidentQueueSizeHigh) Create a new cache segment.- Parameters:
maxMemory
- the maximum memory to usestackMoveDistance
- the number of other entries to be moved to the top of the stack before moving an entry to the toplen
- the number of hash table buckets (must be a power of 2)nonResidentQueueSize
- the non-resident queue size low watermark factornonResidentQueueSizeHigh
- the non-resident queue size high watermark factor
-
Segment
Segment(CacheLongKeyLIRS.Segment<V> old, int len) Create a new cache segment from an existing one. The caller must synchronize on the old segment, to avoid concurrent modifications.- Parameters:
old
- the old segmentlen
- the number of hash table buckets (must be a power of 2)
-
-
Method Details
-
getNewMapLen
int getNewMapLen()Calculate the new number of hash table buckets if the internal map should be re-sized.- Returns:
- 0 if no resizing is needed, or the new length
-
addToMap
-
get
Get the value from the given entry. This method adjusts the internal state of the cache sometimes, to ensure commonly used entries stay in the cache.- Parameters:
e
- the entry- Returns:
- the value, or null if there is no resident entry
-
access
Access an item, moving the entry to the top of the stack or front of the queue if found.- Parameters:
e
- entry to record access for
-
put
Add an entry to the cache. The entry may or may not exist in the cache yet. This method will usually mark unknown entries as cold and known entries as hot.- Parameters:
key
- the key (may not be null)hash
- the hashvalue
- the value (may not be null)memory
- the memory used for the given entry- Returns:
- the old value, or null if there was no resident entry
-
remove
Remove an entry. Both resident and non-resident entries can be removed.- Parameters:
key
- the key (may not be null)hash
- the hash- Returns:
- the old value, or null if there was no resident entry
-
evict
private void evict()Evict cold entries (resident and non-resident) until the memory limit is reached. The new entry is added as a cold entry, except if it is the only entry. -
evictBlock
private void evictBlock() -
trimNonResidentQueue
void trimNonResidentQueue() -
convertOldestHotToCold
private void convertOldestHotToCold() -
pruneStack
private void pruneStack()Ensure the last entry of the stack is cold. -
find
Try to find an entry in the map.- Parameters:
key
- the keyhash
- the hash- Returns:
- the entry (might be a non-resident)
-
addToStack
-
addToStackBottom
-
removeFromStack
Remove the entry from the stack. The head itself must not be removed.- Parameters:
e
- the entry
-
addToQueue
-
removeFromQueue
-
keys
Get the list of keys. This method allows to read the internal state of the cache.- Parameters:
cold
- if true, only keys for the cold entries are returnednonResident
- true for non-resident entries- Returns:
- the key list
-
keySet
Get the set of keys for resident entries.- Returns:
- the set of keys
-
setMaxMemory
void setMaxMemory(long maxMemory) Set the maximum memory this cache should use. This will not immediately cause entries to get removed however; it will only change the limit. To resize the internal array, call the clear method.- Parameters:
maxMemory
- the maximum size (1 or larger) in bytes
-