@InterfaceAudience.Private public class LruHashMap<K extends HeapSize,V extends HeapSize> extends Object implements HeapSize, Map<K,V>
It maintains an ordered list of all entries in the map ordered by access time. When space needs to be freed becase the maximum has been reached, or the application has asked to free memory, entries will be evicted according to an LRU (least-recently-used) algorithm. That is, those entries which have not been accessed the longest will be evicted first.
Both the Key and Value Objects used for this class must extend
HeapSize
in order to track heap usage.
This class contains internal synchronization and is thread-safe.
Modifier and Type | Class and Description |
---|---|
protected static class |
LruHashMap.Entry<K extends HeapSize,V extends HeapSize>
Entry to store key/value mappings.
|
Constructor and Description |
---|
LruHashMap()
Constructs a new, empty map with the default initial capacity,
load factor and maximum memory usage.
|
LruHashMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity and
with the default load factor and maximum memory usage.
|
LruHashMap(int initialCapacity,
float loadFactor)
Constructs a new, empty map with the specified initial capacity and
load factor, and default maximum memory usage.
|
LruHashMap(int initialCapacity,
float loadFactor,
long maxMemUsage)
Constructs a new, empty map with the specified initial capacity,
load factor, and maximum memory usage.
|
LruHashMap(long maxMemUsage)
Constructs a new, empty map with the specified maximum memory usage
and with default initial capacity and load factor.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears all entries from the map.
|
boolean |
containsKey(Object key)
Checks whether there is a value in the map for the specified key.
|
boolean |
containsValue(Object value)
Checks whether this is a mapping which contains the specified value.
|
List<LruHashMap.Entry<K,V>> |
entryLruList()
Debugging function that returns a List sorted by access time.
|
Set<Map.Entry<K,V>> |
entrySet()
Intentionally unimplemented.
|
Set<LruHashMap.Entry<K,V>> |
entryTableSet()
Debugging function that returns a Set of all entries in the hash table.
|
boolean |
equals(Object o)
Intentionally unimplemented.
|
long |
freeMemory(long requestedAmount)
Free the requested amount of memory from the LRU map.
|
V |
get(Object key)
Retrieves the value associated with the specified key.
|
LruHashMap.Entry |
getHeadPtr()
Get the head of the linked list (least recently used).
|
long |
getHitCount()
Get the number of hits to the map.
|
double |
getHitRatio()
Get the hit ratio.
|
long |
getMemFree()
Get the currently available memory for this LRU in bytes.
|
long |
getMemMax()
Get the maximum memory allowed for this LRU in bytes.
|
long |
getMemUsed()
Get the currently used memory for this LRU in bytes.
|
long |
getMissCount()
Get the number of misses to the map.
|
LruHashMap.Entry |
getTailPtr()
Get the tail of the linked list (most recently used).
|
int |
hashCode()
Intentionally unimplemented.
|
long |
heapSize()
The total memory usage of this map
|
boolean |
isEmpty()
Checks whether the map is currently empty.
|
Set<K> |
keySet()
Intentionally unimplemented.
|
V |
put(K key,
V value)
Insert a key-value mapping into the map.
|
void |
putAll(Map<? extends K,? extends V> m)
Intentionally unimplemented.
|
V |
remove(Object key)
Deletes the mapping for the specified key if it exists.
|
int |
size()
Gets the size (number of entries) of the map.
|
Collection<V> |
values()
Intentionally unimplemented.
|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public LruHashMap(int initialCapacity, float loadFactor, long maxMemUsage)
initialCapacity
- the initial capacityloadFactor
- the load factormaxMemUsage
- the maximum total memory usageIllegalArgumentException
- if the initial capacity is less than oneIllegalArgumentException
- if the initial capacity is greater than
the maximum capacityIllegalArgumentException
- if the load factor is <= 0IllegalArgumentException
- if the max memory usage is too small
to support the base overheadpublic LruHashMap(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacityloadFactor
- the load factorIllegalArgumentException
- if the initial capacity is less than oneIllegalArgumentException
- if the initial capacity is greater than
the maximum capacityIllegalArgumentException
- if the load factor is <= 0public LruHashMap(int initialCapacity)
initialCapacity
- the initial capacityIllegalArgumentException
- if the initial capacity is less than oneIllegalArgumentException
- if the initial capacity is greater than
the maximum capacitypublic LruHashMap(long maxMemUsage)
maxMemUsage
- the maximum total memory usageIllegalArgumentException
- if the max memory usage is too small
to support the base overheadpublic LruHashMap()
public long getMemFree()
public long getMemMax()
public long getMemUsed()
public long getHitCount()
public long getMissCount()
public double getHitRatio()
public long freeMemory(long requestedAmount) throws Exception
requestedAmount
- memory to free from LRU in bytesException
public long heapSize()
public V get(Object key)
public V put(K key, V value)
put
in interface Map<K extends HeapSize,V extends HeapSize>
key
- the keyvalue
- the valueUnsupportedOperationException
- if either objects do not
implement HeapSizeNullPointerException
- if the key or value is nullpublic int size()
public boolean isEmpty()
public void clear()
public boolean containsKey(Object key)
containsKey
in interface Map<K extends HeapSize,V extends HeapSize>
key
- the key to checkNullPointerException
- if the key is nullpublic boolean containsValue(Object value)
containsValue
in interface Map<K extends HeapSize,V extends HeapSize>
value
- the value to checkNullPointerException
- if the value is nullpublic List<LruHashMap.Entry<K,V>> entryLruList()
public Set<LruHashMap.Entry<K,V>> entryTableSet()
public LruHashMap.Entry getHeadPtr()
public LruHashMap.Entry getTailPtr()
public boolean equals(Object o)
public int hashCode()