Top Description Inners Fields Constructors Methods
org.h2.mvstore.cache

public Class CacheLongKeyLIRS<V>

extends Object
Class Inheritance
Type Parameters
<V>
the value type
Imports
java.lang.ref.WeakReference, java.util.ArrayList, .HashMap, .HashSet, .List, .Map, .Set, org.h2.mvstore.DataUtils

A scan resistant cache that uses keys of type long. It is meant to cache objects that are relatively costly to acquire, for example file content.

This implementation is multi-threading safe and supports concurrent access. Null keys or null values are not allowed. The map fill factor is at most 75%.

Each entry is assigned a distinct memory size, and the cache will try to use at most the specified amount of memory. The memory unit is not relevant, however it is suggested to use bytes as the unit.

This class implements an approximation of the LIRS replacement algorithm invented by Xiaodong Zhang and Song Jiang as described in https://web.cse.ohio-state.edu/~zhang.574/lirs-sigmetrics-02.html with a few smaller changes: An additional queue for non-resident entries is used, to prevent unbound memory usage. The maximum size of this queue is at most the size of the rest of the stack. About 6.25% of the mapped entries are cold.

Internally, the cache is split into a number of segments, and each segment is an individual LIRS cache.

Accessed entries are only moved to the top of the stack if at least a number of other entries have been moved to the front (8 per segment by default). Write access and moving entries to the top of the stack is synchronized per segment.

Author
Thomas Mueller

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
CacheLongKeyLIRS.Config

The cache configuration.

pack-priv static class
CacheLongKeyLIRS.Entry<
the value type
V
>

A cache entry.

private static class
CacheLongKeyLIRS.Segment<
the value type
V
>

A cache segment

Field Summary

Modifier and TypeField and Description
private long
maxMemory

The maximum memory this cache should use.

private final int
private final int
private final int
private final int
private final CacheLongKeyLIRS.Segment<V>[]
private final int
private final int

Constructor Summary

AccessConstructor and Description
public
CacheLongKeyLIRS(CacheLongKeyLIRS.Config
the configuration
config
)

Create a new cache with the given memory size.

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Remove all entries.

public boolean

Returns:

true if there is a resident entry
containsKey
(long
the key (may not be null)
key
)

Check whether there is a resident entry for the given key.

public boolean

Returns:

true if it is stored
containsValue
(V
the value
value
)

Check whether the given value is stored.

public synchronized Set<Map.Entry<Long, V>>

Returns:

the entry set
entrySet
()

Get the entry set for all resident entries.

private CacheLongKeyLIRS.Entry<V>
find(long key)

public V

Returns:

the value, or null if there is no resident entry
get
(long
the key (may not be null)
key
)

Get the value for the given key if the entry is cached.

pack-priv static int

Returns:

the hash code
getHash
(long
the key
key
)

Get the hash code for the given key.

public long

Returns:

the cache hits
getHits
()

Get the number of cache hits.

public Map<Long, V>

Returns:

the map
getMap
()

Convert this cache to a map.

public long

Returns:

data items size limit
getMaxItemSize
()

Determines max size of the data item size to fit into cache

public long

Returns:

the maximum memory
getMaxMemory
()

Get the maximum memory to use.

public long

Returns:

the memory, or 0 if there is no resident entry
getMemory
(long
the key (may not be null)
key
)

Get the memory used for the given key.

public static int

Returns:

the memory overhead per value
getMemoryOverhead
()

Get the memory overhead per value.

public long

Returns:

the cache misses
getMisses
()

Get the number of cache misses.

private CacheLongKeyLIRS.Segment<V>
getSegment(int hash)

private int
getSegmentIndex(int hash)

public long

Returns:

the used memory
getUsedMemory
()

Get the currently used memory.

public boolean

Returns:

true if it is empty
isEmpty
()

Check whether the cache is empty.

public List<Long>

Returns:

the key list
keys
(boolean
if true, only keys for the cold entries are returned
cold
,
boolean
true for non-resident entries
nonResident
)

Get the list of keys.

public Set<Long>

Returns:

the set of keys
keySet
()

Get the set of keys for resident entries.

public V

Returns:

the value, or null if there is no resident entry
peek
(long
the key (may not be null)
key
)

Get the value for the given key if the entry is cached.

public V

Returns:

the old value, or null if there was no resident entry
put
(long
the key (may not be null)
key
,
V
the value (may not be null)
value
)

Add an entry to the cache using the average memory size.

public V

Returns:

the old value, or null if there was no resident entry
put
(long
the key (may not be null)
key
,
V
the value (may not be null)
value
,
long
the memory used for the given entry
memory
)

Add an entry to the cache.

public void
putAll(Map<Long, ? extends V>
the map
m
)

Add all elements of the map to this cache.

public V

Returns:

the old value, or null if there was no resident entry
remove
(long
the key (may not be null)
key
)

Remove an entry.

private CacheLongKeyLIRS.Segment<V>
resizeIfNeeded(CacheLongKeyLIRS.Segment<V> s, int segmentIndex)

public void
setMaxMemory(long
the maximum size (1 or larger) in bytes
maxMemory
)

Set the maximum memory this cache should use.

public int

Returns:

the number of entries
size
()

Get the number of resident entries.

public int

Returns:

the number of hot entries
sizeHot
()

Get the number of hot entries in the cache.

public int

Returns:

the size of the array
sizeMapArray
()

Get the length of the internal map array.

public int

Returns:

the number of non-resident entries
sizeNonResident
()

Get the number of non-resident entries in the cache.

protected long

Returns:

the size
sizeOf
(V
the value
value
)

Get the size of the given value.

public void
trimNonResidentQueue()

Loop through segments, trimming the non resident queue.

public List<V>

Returns:

the entry set
values
()

Get the values for all resident entries.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

maxMemoryback to summary
private long maxMemory

The maximum memory this cache should use.

nonResidentQueueSizeback to summary
private final int nonResidentQueueSize
nonResidentQueueSizeHighback to summary
private final int nonResidentQueueSizeHigh
segmentCountback to summary
private final int segmentCount
segmentMaskback to summary
private final int segmentMask
segmentsback to summary
private final CacheLongKeyLIRS.Segment<V>[] segments
segmentShiftback to summary
private final int segmentShift
stackMoveDistanceback to summary
private final int stackMoveDistance

Constructor Detail

CacheLongKeyLIRSback to summary
public CacheLongKeyLIRS(CacheLongKeyLIRS.Config config)

Create a new cache with the given memory size.

Parameters
config:CacheLongKeyLIRS.Config

the configuration

Annotations
@SuppressWarnings:unchecked

Method Detail

clearback to summary
public void clear()

Remove all entries.

containsKeyback to summary
public boolean containsKey(long key)

Check whether there is a resident entry for the given key. This method does not adjust the internal state of the cache.

Parameters
key:long

the key (may not be null)

Returns:boolean

true if there is a resident entry

containsValueback to summary
public boolean containsValue(V value)

Check whether the given value is stored.

Parameters
value:V

the value

Returns:boolean

true if it is stored

entrySetback to summary
public synchronized Set<Map.Entry<Long, V>> entrySet()

Get the entry set for all resident entries.

Returns:Set<Map.Entry<Long, V>>

the entry set

findback to summary
private CacheLongKeyLIRS.Entry<V> find(long key)
getback to summary
public V get(long key)

Get the value for the given key if the entry is cached. This method adjusts the internal state of the cache sometimes, to ensure commonly used entries stay in the cache.

Parameters
key:long

the key (may not be null)

Returns:V

the value, or null if there is no resident entry

getHashback to summary
pack-priv static int getHash(long key)

Get the hash code for the given key. The hash code is further enhanced to spread the values more evenly.

Parameters
key:long

the key

Returns:int

the hash code

getHitsback to summary
public long getHits()

Get the number of cache hits.

Returns:long

the cache hits

getMapback to summary
public Map<Long, V> getMap()

Convert this cache to a map.

Returns:Map<Long, V>

the map

getMaxItemSizeback to summary
public long getMaxItemSize()

Determines max size of the data item size to fit into cache

Returns:long

data items size limit

getMaxMemoryback to summary
public long getMaxMemory()

Get the maximum memory to use.

Returns:long

the maximum memory

getMemoryback to summary
public long getMemory(long key)

Get the memory used for the given key.

Parameters
key:long

the key (may not be null)

Returns:long

the memory, or 0 if there is no resident entry

getMemoryOverheadback to summary
public static int getMemoryOverhead()

Get the memory overhead per value.

Returns:int

the memory overhead per value

getMissesback to summary
public long getMisses()

Get the number of cache misses.

Returns:long

the cache misses

getSegmentback to summary
private CacheLongKeyLIRS.Segment<V> getSegment(int hash)
getSegmentIndexback to summary
private int getSegmentIndex(int hash)
getUsedMemoryback to summary
public long getUsedMemory()

Get the currently used memory.

Returns:long

the used memory

isEmptyback to summary
public boolean isEmpty()

Check whether the cache is empty.

Returns:boolean

true if it is empty

keysback to summary
public List<Long> keys(boolean cold, boolean nonResident)

Get the list of keys. This method allows to read the internal state of the cache.

Parameters
cold:boolean

if true, only keys for the cold entries are returned

nonResident:boolean

true for non-resident entries

Returns:List<Long>

the key list

keySetback to summary
public Set<Long> keySet()

Get the set of keys for resident entries.

Returns:Set<Long>

the set of keys

peekback to summary
public V peek(long key)

Get the value for the given key if the entry is cached. This method does not modify the internal state.

Parameters
key:long

the key (may not be null)

Returns:V

the value, or null if there is no resident entry

putback to summary
public V put(long key, V value)

Add an entry to the cache using the average memory size.

Parameters
key:long

the key (may not be null)

value:V

the value (may not be null)

Returns:V

the old value, or null if there was no resident entry

putback to summary
public V put(long key, V value, long memory)

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:long

the key (may not be null)

value:V

the value (may not be null)

memory:long

the memory used for the given entry

Returns:V

the old value, or null if there was no resident entry

putAllback to summary
public void putAll(Map<Long, ? extends V> m)

Add all elements of the map to this cache.

Parameters
m:Map<Long, ? extends V>

the map

removeback to summary
public V remove(long key)

Remove an entry. Both resident and non-resident entries can be removed.

Parameters
key:long

the key (may not be null)

Returns:V

the old value, or null if there was no resident entry

resizeIfNeededback to summary
private CacheLongKeyLIRS.Segment<V> resizeIfNeeded(CacheLongKeyLIRS.Segment<V> s, int segmentIndex)
setMaxMemoryback to summary
public 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:long

the maximum size (1 or larger) in bytes

sizeback to summary
public int size()

Get the number of resident entries.

Returns:int

the number of entries

sizeHotback to summary
public int sizeHot()

Get the number of hot entries in the cache.

Returns:int

the number of hot entries

sizeMapArrayback to summary
public int sizeMapArray()

Get the length of the internal map array.

Returns:int

the size of the array

sizeNonResidentback to summary
public int sizeNonResident()

Get the number of non-resident entries in the cache.

Returns:int

the number of non-resident entries

sizeOfback to summary
protected long sizeOf(V value)

Get the size of the given value. The default implementation returns 16.

Parameters
value:V

the value

Returns:long

the size

Annotations
@SuppressWarnings:unused
trimNonResidentQueueback to summary
public void trimNonResidentQueue()

Loop through segments, trimming the non resident queue.

valuesback to summary
public List<V> values()

Get the values for all resident entries.

Returns:List<V>

the entry set

org.h2.mvstore.cache back to summary

public Class CacheLongKeyLIRS.Config

extends Object
Class Inheritance

The cache configuration.

Field Summary

Modifier and TypeField and Description
public long
maxMemory

The maximum memory to use (1 or larger).

public final int
nonResidentQueueSize

Low water mark for the number of entries in the non-resident queue, as a factor of the number of all other entries in the map.

public final int
nonResidentQueueSizeHigh

High watermark for the number of entries in the non-resident queue, as a factor of the number of all other entries in the map

public int
segmentCount

The number of cache segments (must be a power of 2).

public int
stackMoveDistance

How many other item are to be moved to the top of the stack before the current item is moved.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

maxMemoryback to summary
public long maxMemory

The maximum memory to use (1 or larger).

nonResidentQueueSizeback to summary
public final int nonResidentQueueSize

Low water mark for the number of entries in the non-resident queue, as a factor of the number of all other entries in the map.

nonResidentQueueSizeHighback to summary
public final int nonResidentQueueSizeHigh

High watermark for the number of entries in the non-resident queue, as a factor of the number of all other entries in the map

segmentCountback to summary
public int segmentCount

The number of cache segments (must be a power of 2).

stackMoveDistanceback to summary
public int stackMoveDistance

How many other item are to be moved to the top of the stack before the current item is moved.

Constructor Detail

Configback to summary
public Config()
org.h2.mvstore.cache back to summary

pack-priv Class CacheLongKeyLIRS.Entry<V>

extends Object
Class Inheritance
Type Parameters
<V>
the value type

A cache entry. Each entry is either hot (low inter-reference recency; LIR), cold (high inter-reference recency; HIR), or non-resident-cold. Hot entries are in the stack only. Cold entries are in the queue, and may be in the stack. Non-resident-cold entries have their value set to null and are in the stack and in the non-resident queue.

Field Summary

Modifier and TypeField and Description
pack-priv final long
key

The key.

pack-priv CacheLongKeyLIRS.Entry<V>
mapNext

The next entry in the map (the chained entry).

pack-priv final long
memory

The estimated memory used.

pack-priv CacheLongKeyLIRS.Entry<V>
queueNext

The next entry in the queue (either the resident queue or the non-resident queue).

pack-priv CacheLongKeyLIRS.Entry<V>
queuePrev

The previous entry in the queue.

pack-priv WeakReference<V>
reference

Weak reference to the value.

pack-priv CacheLongKeyLIRS.Entry<V>
stackNext

The next entry in the stack.

pack-priv CacheLongKeyLIRS.Entry<V>
stackPrev

The previous entry in the stack.

pack-priv int
topMove

When the item was last moved to the top of the stack.

pack-priv static final int
pack-priv V
value

The value.

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
Entry(long key, V value, long memory)

pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv long
pack-priv V
pack-priv boolean

Returns:

whether the entry is hot
isHot
()

Whether this entry is hot.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

keyback to summary
pack-priv final long key

The key.

mapNextback to summary
pack-priv CacheLongKeyLIRS.Entry<V> mapNext

The next entry in the map (the chained entry).

memoryback to summary
pack-priv final long memory

The estimated memory used.

queueNextback to summary
pack-priv CacheLongKeyLIRS.Entry<V> queueNext

The next entry in the queue (either the resident queue or the non-resident queue).

queuePrevback to summary
pack-priv CacheLongKeyLIRS.Entry<V> queuePrev

The previous entry in the queue.

referenceback to summary
pack-priv WeakReference<V> reference

Weak reference to the value. Set to null for resident entries.

stackNextback to summary
pack-priv CacheLongKeyLIRS.Entry<V> stackNext

The next entry in the stack.

stackPrevback to summary
pack-priv CacheLongKeyLIRS.Entry<V> stackPrev

The previous entry in the stack.

topMoveback to summary
pack-priv int topMove

When the item was last moved to the top of the stack.

TOTAL_MEMORY_OVERHEADback to summary
pack-priv static final int TOTAL_MEMORY_OVERHEAD
valueback to summary
pack-priv V value

The value. Set to null for non-resident-cold entries.

Constructor Detail

Entryback to summary
pack-priv Entry()
Entryback to summary
pack-priv Entry(long key, V value, long memory)
Entryback to summary
pack-priv Entry(CacheLongKeyLIRS.Entry<V> old)

Method Detail

getMemoryback to summary
pack-priv long getMemory()
getValueback to summary
pack-priv V getValue()
isHotback to summary
pack-priv boolean isHot()

Whether this entry is hot. Cold entries are in one of the two queues.

Returns:boolean

whether the entry is hot

org.h2.mvstore.cache back to summary

private Class CacheLongKeyLIRS.Segment<V>

extends Object
Class Inheritance
Type Parameters
<V>
the value type

A cache segment

Field Summary

Modifier and TypeField and Description
pack-priv final CacheLongKeyLIRS.Entry<V>[]
entries

The map array.

pack-priv long
hits

The number of cache hits.

pack-priv int
mapSize

The number of (hot, cold, and non-resident) entries in the map.

private final int
mask

The bit mask that is applied to the key hash code to get the index in the map array.

private long
maxMemory

The maximum memory this cache should use in bytes.

pack-priv long
misses

The number of cache misses.

private final int
nonResidentQueueSize

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
nonResidentQueueSizeHigh

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>
queue

The queue of resident cold entries.

private final CacheLongKeyLIRS.Entry<V>
queue2

The queue of non-resident cold entries.

pack-priv int
queue2Size

The size of the LIRS queue for non-resident cold entries.

pack-priv int
queueSize

The size of the LIRS queue for resident cold entries.

private final CacheLongKeyLIRS.Entry<V>
stack

The stack of recently referenced elements.

private int
stackMoveCounter

The number of times any item was moved to the top of the stack.

private final int
stackMoveDistance

How many other item are to be moved to the top of the stack before the current item is moved.

private int
stackSize

The number of entries in the stack.

pack-priv long
usedMemory

The currently used memory.

Constructor Summary

AccessConstructor and Description
pack-priv
Segment(long
the maximum memory to use
maxMemory
,
int
the number of other entries to be moved to the top of the stack before moving an entry to the top
stackMoveDistance
,
int
the number of hash table buckets (must be a power of 2)
len
,
int
the non-resident queue size low watermark factor
nonResidentQueueSize
,
int
the non-resident queue size high watermark factor
nonResidentQueueSizeHigh
)

Create a new cache segment.

pack-priv
Segment(CacheLongKeyLIRS.Segment<V>
the old segment
old
,
int
the number of hash table buckets (must be a power of 2)
len
)

Create a new cache segment from an existing one.

Method Summary

Modifier and TypeMethod and Description
private void
access(CacheLongKeyLIRS.Entry<V>
entry to record access for
e
)

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
pack-priv CacheLongKeyLIRS.Entry<V>

Returns:

the entry (might be a non-resident)
find
(long
the key
key
,
int
the hash
hash
)

Try to find an entry in the map.

pack-priv synchronized V

Returns:

the value, or null if there is no resident entry
get
(CacheLongKeyLIRS.Entry<V>
the entry
e
)

Get the value from the given entry.

pack-priv int

Returns:

0 if no resizing is needed, or the new length
getNewMapLen
()

Calculate the new number of hash table buckets if the internal map should be re-sized.

pack-priv synchronized List<Long>

Returns:

the key list
keys
(boolean
if true, only keys for the cold entries are returned
cold
,
boolean
true for non-resident entries
nonResident
)

Get the list of keys.

pack-priv synchronized Set<Long>

Returns:

the set of keys
keySet
()

Get the set of keys for resident entries.

private void
pruneStack()

Ensure the last entry of the stack is cold.

pack-priv synchronized V

Returns:

the old value, or null if there was no resident entry
put
(long
the key (may not be null)
key
,
int
the hash
hash
,
V
the value (may not be null)
value
,
long
the memory used for the given entry
memory
)

Add an entry to the cache.

pack-priv synchronized V

Returns:

the old value, or null if there was no resident entry
remove
(long
the key (may not be null)
key
,
int
the hash
hash
)

Remove an entry.

private void
private void
removeFromStack(CacheLongKeyLIRS.Entry<V>
the entry
e
)

Remove the entry from the stack.

pack-priv void
setMaxMemory(long
the maximum size (1 or larger) in bytes
maxMemory
)

Set the maximum memory this cache should use.

pack-priv void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

entriesback to summary
pack-priv final CacheLongKeyLIRS.Entry<V>[] entries

The map array. The size is always a power of 2.

hitsback to summary
pack-priv long hits

The number of cache hits.

mapSizeback to summary
pack-priv int mapSize

The number of (hot, cold, and non-resident) entries in the map.

maskback to summary
private final int mask

The 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.

maxMemoryback to summary
private long maxMemory

The maximum memory this cache should use in bytes.

missesback to summary
pack-priv long misses

The number of cache misses.

nonResidentQueueSizeback to summary
private final int nonResidentQueueSize

Low watermark for the number of entries in the non-resident queue, as a factor of the number of entries in the map.

nonResidentQueueSizeHighback to summary
private final int nonResidentQueueSizeHigh

High watermark for the number of entries in the non-resident queue, as a factor of the number of entries in the map.

queueback to summary
private final CacheLongKeyLIRS.Entry<V> queue

The queue of resident cold entries.

There is always at least one entry: the head entry.

queue2back to summary
private final CacheLongKeyLIRS.Entry<V> queue2

The queue of non-resident cold entries.

There is always at least one entry: the head entry.

queue2Sizeback to summary
pack-priv int queue2Size

The size of the LIRS queue for non-resident cold entries.

queueSizeback to summary
pack-priv int queueSize

The size of the LIRS queue for resident cold entries.

stackback to summary
private final CacheLongKeyLIRS.Entry<V> 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.

stackMoveCounterback to summary
private int stackMoveCounter

The number of times any item was moved to the top of the stack.

stackMoveDistanceback to summary
private final int stackMoveDistance

How many other item are to be moved to the top of the stack before the current item is moved.

stackSizeback to summary
private int stackSize

The number of entries in the stack.

usedMemoryback to summary
pack-priv long usedMemory

The currently used memory.

Constructor Detail

Segmentback to summary
pack-priv Segment(long maxMemory, int stackMoveDistance, int len, int nonResidentQueueSize, int nonResidentQueueSizeHigh)

Create a new cache segment.

Parameters
maxMemory:long

the maximum memory to use

stackMoveDistance:int

the number of other entries to be moved to the top of the stack before moving an entry to the top

len:int

the number of hash table buckets (must be a power of 2)

nonResidentQueueSize:int

the non-resident queue size low watermark factor

nonResidentQueueSizeHigh:int

the non-resident queue size high watermark factor

Segmentback to summary
pack-priv 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:CacheLongKeyLIRS.Segment<V>

the old segment

len:int

the number of hash table buckets (must be a power of 2)

Method Detail

accessback to summary
private void access(CacheLongKeyLIRS.Entry<V> e)

Access an item, moving the entry to the top of the stack or front of the queue if found.

Parameters
e:CacheLongKeyLIRS.Entry<V>

entry to record access for

addToMapback to summary
private void addToMap(CacheLongKeyLIRS.Entry<V> e)
addToQueueback to summary
private void addToQueue(CacheLongKeyLIRS.Entry<V> q, CacheLongKeyLIRS.Entry<V> e)
addToStackback to summary
private void addToStack(CacheLongKeyLIRS.Entry<V> e)
addToStackBottomback to summary
private void addToStackBottom(CacheLongKeyLIRS.Entry<V> e)
convertOldestHotToColdback to summary
private void convertOldestHotToCold()
evictback to summary
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.

evictBlockback to summary
private void evictBlock()
findback to summary
pack-priv CacheLongKeyLIRS.Entry<V> find(long key, int hash)

Try to find an entry in the map.

Parameters
key:long

the key

hash:int

the hash

Returns:CacheLongKeyLIRS.Entry<V>

the entry (might be a non-resident)

getback to summary
pack-priv synchronized V get(CacheLongKeyLIRS.Entry<V> e)

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:CacheLongKeyLIRS.Entry<V>

the entry

Returns:V

the value, or null if there is no resident entry

getNewMapLenback to summary
pack-priv int getNewMapLen()

Calculate the new number of hash table buckets if the internal map should be re-sized.

Returns:int

0 if no resizing is needed, or the new length

keysback to summary
pack-priv synchronized List<Long> keys(boolean cold, boolean nonResident)

Get the list of keys. This method allows to read the internal state of the cache.

Parameters
cold:boolean

if true, only keys for the cold entries are returned

nonResident:boolean

true for non-resident entries

Returns:List<Long>

the key list

keySetback to summary
pack-priv synchronized Set<Long> keySet()

Get the set of keys for resident entries.

Returns:Set<Long>

the set of keys

pruneStackback to summary
private void pruneStack()

Ensure the last entry of the stack is cold.

putback to summary
pack-priv synchronized V put(long key, int hash, V value, long memory)

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:long

the key (may not be null)

hash:int

the hash

value:V

the value (may not be null)

memory:long

the memory used for the given entry

Returns:V

the old value, or null if there was no resident entry

removeback to summary
pack-priv synchronized V remove(long key, int hash)

Remove an entry. Both resident and non-resident entries can be removed.

Parameters
key:long

the key (may not be null)

hash:int

the hash

Returns:V

the old value, or null if there was no resident entry

removeFromQueueback to summary
private void removeFromQueue(CacheLongKeyLIRS.Entry<V> e)
removeFromStackback to summary
private void removeFromStack(CacheLongKeyLIRS.Entry<V> e)

Remove the entry from the stack. The head itself must not be removed.

Parameters
e:CacheLongKeyLIRS.Entry<V>

the entry

setMaxMemoryback to summary
pack-priv 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:long

the maximum size (1 or larger) in bytes

trimNonResidentQueueback to summary
pack-priv void trimNonResidentQueue()