@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.
|
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_INITIAL_CAPACITY
The default capacity of the hash table
|
private static float |
DEFAULT_LOAD_FACTOR
The default load factor to use
|
private static long |
DEFAULT_MAX_MEM_USAGE
The default size (in bytes) of the LRU
|
private LruHashMap.Entry[] |
entries
Entries in the map
|
private LruHashMap.Entry<K,V> |
headPtr
Pointer to least recently used entry
|
private long |
hitCount
Number of successful (found) get() calls
|
private float |
loadFactor
Load factor allowed (usually 75%)
|
private static org.apache.commons.logging.Log |
LOG |
private static int |
MAXIMUM_CAPACITY
The maxmum capacity of the hash table
|
private long |
memFree
Amount of available memory
|
private long |
memTotal
Maximum memory usage of this map
|
private long |
missCount
Number of unsuccessful (not found) get() calls
|
private static int |
OVERHEAD
Memory overhead of this Object (for HeapSize)
|
private int |
size
Number of key/vals in the map
|
private LruHashMap.Entry<K,V> |
tailPtr
Pointer to most recently used entry
|
private int |
threshold
Size at which we grow hash
|
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 |
---|---|
private long |
addEntry(int hash,
K key,
V value,
int bucketIndex)
Adds a new entry with the specified key, value, hash code, and
bucket index to the map.
|
private int |
calculateCapacity(int proposedCapacity)
Calculates the capacity of the array backing the hash
by normalizing capacity to a power of 2 and enforcing
capacity limits.
|
private int |
calculateThreshold(int capacity,
float factor)
Calculates the threshold of the map given the capacity and load
factor.
|
private void |
checkAndFreeMemory(long memNeeded)
Evicts and frees based on LRU until at least as much memory as requested
is available.
|
private void |
checkKey(K key)
Enforces key constraints.
|
private void |
checkValue(V value)
Enforces value constraints.
|
void |
clear()
Clears all entries from the map.
|
private long |
clearAll()
Clears all the entries in 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.
|
private long |
evictFromLru()
Evicts based on LRU.
|
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.
|
private long |
getMinimumUsage()
Returns the minimum memory usage of the base map structure.
|
long |
getMissCount()
Get the number of misses to the map.
|
LruHashMap.Entry |
getTailPtr()
Get the tail of the linked list (most recently used).
|
private void |
growTable(int newCapacity)
Recreates the entire contents of the hashmap into a new array
with double the capacity.
|
private int |
hash(Object key)
Gets the hash code for the specified key.
|
int |
hashCode()
Intentionally unimplemented.
|
private int |
hashIndex(int hashValue,
int length)
Determines the index into the current hash table for the specified
hashValue.
|
long |
heapSize()
The total memory usage of this map
|
private void |
init()
Set the initial heap usage of this class.
|
boolean |
isEmpty()
Checks whether the map is currently empty.
|
private boolean |
isEqual(Object x,
Object y)
Compares two objects for equality.
|
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.
|
private void |
removeEntry(LruHashMap.Entry<K,V> entry)
Removes the specified entry from the map and LRU structure.
|
private LruHashMap.Entry<K,V> |
removeEntryForKey(K key)
Removes and returns the entry associated with the specified
key.
|
int |
size()
Gets the size (number of entries) of the map.
|
private void |
updateLru(LruHashMap.Entry<K,V> e)
Moves the specified entry to the most recently used slot of the
LRU.
|
Collection<V> |
values()
Intentionally unimplemented.
|
private static final org.apache.commons.logging.Log LOG
private static final long DEFAULT_MAX_MEM_USAGE
private static final int DEFAULT_INITIAL_CAPACITY
private static final int MAXIMUM_CAPACITY
private static final float DEFAULT_LOAD_FACTOR
private static final int OVERHEAD
private final float loadFactor
private int size
private int threshold
private LruHashMap.Entry[] entries
private LruHashMap.Entry<K extends HeapSize,V extends HeapSize> headPtr
private LruHashMap.Entry<K extends HeapSize,V extends HeapSize> tailPtr
private long memTotal
private long memFree
private long hitCount
private long missCount
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 nullprivate void checkKey(K key)
key
- the keyNullPointerException
- if the key is nullUnsupportedOperationException
- if the key class does not
implement the HeapSize interfaceprivate void checkValue(V value)
value
- the valueNullPointerException
- if the value is nullUnsupportedOperationException
- if the value class does not
implement the HeapSize interfaceprivate long getMinimumUsage()
private void checkAndFreeMemory(long memNeeded)
memNeeded
- the amount of memory needed in bytesprivate long evictFromLru()
private void updateLru(LruHashMap.Entry<K,V> e)
e
- entry that was accessedprivate void removeEntry(LruHashMap.Entry<K,V> entry)
entry
- entry to be removedprivate LruHashMap.Entry<K,V> removeEntryForKey(K key)
key
- key of the entry to be deletedprivate long addEntry(int hash, K key, V value, int bucketIndex)
hash
- hash value of keykey
- the keyvalue
- the valuebucketIndex
- index into hash array to store this entryprivate long clearAll()
private void growTable(int newCapacity)
newCapacity
- the new size of the hash entriesprivate int hash(Object key)
key
- the key to get a hash value forprivate boolean isEqual(Object x, Object y)
x
- the first valuey
- the second valueprivate int hashIndex(int hashValue, int length)
hashValue
- the hash valuelength
- the current number of hash bucketsprivate int calculateCapacity(int proposedCapacity)
proposedCapacity
- the proposed capacityprivate int calculateThreshold(int capacity, float factor)
capacity
- the size of the arrayfactor
- the load factor of the hashprivate void init()
public 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()
Copyright © 2007–2019 The Apache Software Foundation. All rights reserved.