|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.hd.d.pg2k.svrCore.MemoryTools.SimpleLRUMapAutoSizeForHitRate<K,V>
public static final class MemoryTools.SimpleLRUMapAutoSizeForHitRate<K,V>
Simplified map with fixed capacity bounds that discards excess items in LRU (Least Recently Used) order to meet a hit/miss ratio goal. This adjusts its size to maintain a specific hit/miss ratio in get(), a miss being a get() that returns null, trimming away older items at insert to maintain the ratio.
This will also discard entries LRU if memory is under stress while the map is above its minimum size.
This provides optional fixed lower and upper sizes. Attempts to insert more items than the ceiling capacity will result in old items being removed in LRU order, thus leaving the newest items in the cache.
Each put() or get() makes its key/value pair the most-recently used and thus the last to be removed from the map of current key/value pairs when a series of put()s forces the map to overflow.
Thread-safe.
A lock can be held on instances of this object to make compound operations atomic.
Does not support the full Map interface.
| Field Summary | |
|---|---|
private int |
cacheTrimTryCount
Count to trigger possible trim of cache size; non-negative. |
private int |
countGetMisses
Count of get() calls that returned null, ie misses; non-negative and no larger than countGets. |
private int |
countGets
Count of get() calls; non-negative and usually no larger than maxCapacity. |
static float |
DEFAULT_LOAD_FACTOR
Default load factor. |
static float |
DEFAULT_MAX_MISS_RATE
Default maximum miss rate ]0.0,1.0[. |
static int |
DEFAULT_MIN_SIZE
Default minimum size/capacity, also initial capacity; strictly positive. |
private java.util.LinkedHashMap<K,V> |
lhm
Underlying LinkedHashMap on which this is based; never null. |
private int |
maxCapacity
The maximum size/capacity; strictly positive and greater than minSize. |
private int |
minSize
The minimum size/capacity; strictly positive. |
private int |
missFracPower
The rounded negative log2 of the maximum miss rate usable as an int shift; in the range [1,30]. |
private java.lang.String |
name
Name of this container instance for tracking purposes, or null if none. |
private int |
trimCheckRate
Number of non-miss get()s after which we consider trimming the map if meeting the max-miss-rate targets; strictly positive. |
| Constructor Summary | |
|---|---|
private |
MemoryTools.SimpleLRUMapAutoSizeForHitRate()
Create an instance with all defaults and therefore an effectively-unbounded upper capacity of Integer.MAX_VALUE. |
private |
MemoryTools.SimpleLRUMapAutoSizeForHitRate(float maxMissRate,
int minSize,
int maxCapacity,
float loadFactor,
java.lang.String name)
Create an instance with the given parameters. |
| Method Summary | ||
|---|---|---|
void |
clear()
Clear the map and reset the hit/miss counters. |
|
void |
compact()
Compact by discarding some or all LRU items above the minimum size, ignoring the miss-rate target. |
|
private int |
computeCapacityCap()
Computes target map size cap based on system memory availability; in range [minSize,maxCapacity]. |
|
static
|
create()
Create an instance with all defaults and therefore an effectively-unbounded upper capacity of Integer.MAX_VALUE. |
|
static
|
create(float maxMissRate,
int minSize,
int maxCapacity,
float loadFactor,
java.lang.String name)
Create an instance with the given parameters. |
|
static
|
create(float maxMissRate,
int minSize,
java.lang.String name)
Create an instance with the given parameters. |
|
static
|
create(int minSize,
int maxCapacity,
java.lang.String name)
Create an instance with all defaults except for the specified minimum/maximum capacities. |
|
V |
get(K key)
Get an entry from the map, making it the Most Recently Used; null if no such element. |
|
java.util.Map<K,V> |
getCopy()
Take a copy of the map contents, which does not alter LRU; never null. |
|
private boolean |
meetingMissRateTarget()
Returns true if we are meeting/bettering the miss-rate target. |
|
V |
put(K key,
V value)
Put an entry in the map, making it the Most Recently Used, returning the previous value if any. |
|
V |
remove(K key)
Remove an entry from the map and return the removed value, if any, else null. |
|
int |
size()
Return the number of entries in the map; non-negative. |
|
java.lang.String |
toString()
Provide a human-readable summary of status. |
|
private void |
trimOldestEntriesIfPossible(boolean ignoreMissRateTarget,
boolean all)
Trim oldest entries providing that the miss-rate target and minimum size are met. |
|
private void |
trimOldestEntryIfPossible(boolean ignoreMissRateTarget)
Trim (at most one) oldest entry providing that the miss-rate target and minimum size are met. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final float DEFAULT_MAX_MISS_RATE
public static final float DEFAULT_LOAD_FACTOR
public static final int DEFAULT_MIN_SIZE
private final java.util.LinkedHashMap<K,V> lhm
private final int minSize
private final int maxCapacity
private final int missFracPower
private final int trimCheckRate
private final java.lang.String name
private int countGets
private int countGetMisses
private int cacheTrimTryCount
| Constructor Detail |
|---|
private MemoryTools.SimpleLRUMapAutoSizeForHitRate()
private MemoryTools.SimpleLRUMapAutoSizeForHitRate(float maxMissRate,
int minSize,
int maxCapacity,
float loadFactor,
java.lang.String name)
maxMissRate - the target cache maximum miss rate;
in the range 0.0f to 1.0f exclusive (typically << 0.1)minSize - the minimum (and initial) capacity;
non-negativemaxCapacity - the maximum capacity;
greater than minCapacity (and this strictly positive)loadFactor - the load factor of the underlying hash table;
in the range 0.0f to 1.0f exclusive (typically ~0.7f)| Method Detail |
|---|
public static <K,V> MemoryTools.SimpleLRUMapAutoSizeForHitRate<K,V> create()
public static <K,V> MemoryTools.SimpleLRUMapAutoSizeForHitRate<K,V> create(int minSize,
int maxCapacity,
java.lang.String name)
public static <K,V> MemoryTools.SimpleLRUMapAutoSizeForHitRate<K,V> create(float maxMissRate,
int minSize,
java.lang.String name)
maxMissRate - the target cache maximum miss rate;
in the range 0.0f to 1.0f exclusive (typically << 0.1)minSize - the minimum (and initial) capacity;
strictly positivename - for memory-footprint tuning/debugging; may be nullprivate boolean meetingMissRateTarget()
We avoid potentially-slow floating-point arithmetic on the call, since it may be made quite often.
private int computeCapacityCap()
Always returns Integer.MAX_VALUE if maxCapacity is Integer.MAX_VALUE.
public V get(K key)
public static <K,V> MemoryTools.SimpleLRUMapAutoSizeForHitRate<K,V> create(float maxMissRate,
int minSize,
int maxCapacity,
float loadFactor,
java.lang.String name)
maxMissRate - the target cache maximum miss rate;
in the range 0.0f to 1.0f exclusive (typically << 0.1)minSize - the minimum (and initial) capacity;
strictly positivemaxCapacity - the maximum capacity;
greater than minCapacity (and this strictly positive)loadFactor - the load factor of the underlying hash table;
in the range 0.0f to 1.0f exclusive (typically ~0.7f)name - for memory-footprint tuning/debugging; may be null
private void trimOldestEntriesIfPossible(boolean ignoreMissRateTarget,
boolean all)
ignoreMissRateTarget - if true, then ignore the miss-rate target when trimming.private void trimOldestEntryIfPossible(boolean ignoreMissRateTarget)
ignoreMissRateTarget - if true, then ignore the miss-rate target when trimming.public void compact()
May be automatically called in the background when memory is low/stressed.
compact in interface MemoryTools.Compactable
public V put(K key,
V value)
public V remove(K key)
public void clear()
public int size()
public java.util.Map<K,V> getCopy()
public java.lang.String toString()
toString in class java.lang.Object
|
DHD Multimedia Gallery V1.53.0 | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||