public abstract class AbstractStringCache extends Object
The basic function of the cache is to maintain two structures. The first
(provided by this class) tracks the usage of strings in the cache via a
'seen' ConcurrentMap
. As the cache sees more usage it will
periodically trim entries from the map that are not being used often (this is
to limit memory usage). Once the cache thinks it has seen enough usage it
will trigger the building of the runtime cache. This runtime cache does not
require any locking as it is read-only after being constructed.
There are two controls on the amount of caching instances of this class will do:
getMaxCacheLength()
limits the length of the encoded bytes this
class will try and retrieve from the cache. Setting this value to zero
disables the cache and limit any memory overhead.getMaxCacheEntries()
controls the number of cached entries in
the runtime-cache and how often the accumulated statistics are trimmed. Each
entry represents a single encoded string.Modifier and Type | Class and Description |
---|---|
static class |
AbstractStringCache.ReverseIntegerComparator
ReverseIntegerComparator provides a
Comparator that considers
higher value integers to be less then lower value integers. |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MAX_CACHE_ENTRIES
The default maximum number of entries to keep in the cache.
|
static int |
DEFAULT_MAX_CACHE_LENGTH
The default maximum length byte array to cache.
|
protected static int |
MAX_MULTIPLIER
The maximum value for the multiplier.
|
protected int |
myMaxCacheLength
The maximum length of a string to cache.
|
protected int |
myMaxCachEntries
The maximum number of entries to have in the cache.
|
protected int |
myMaxCachEntriesMultiplier
The multiplier for the number of times the cache is used before we
re-load the cache.
|
protected ConcurrentMap<String,com.allanbank.mongodb.bson.io.SeenString> |
mySeen
The map of seen strings that will be used periodically to build the cache
cache.
|
protected AtomicInteger |
myUseCount
The number of time the cache has been used since the last re-load of
runtime, read-only cache.
|
Constructor and Description |
---|
AbstractStringCache()
Creates a new AbstractStringCache.
|
Modifier and Type | Method and Description |
---|---|
protected SortedMap<Integer,List<com.allanbank.mongodb.bson.io.SeenString>> |
buildCacheGroups()
Builds a map of the seen strings at each count.
|
protected void |
clear()
Clears the cache.
|
int |
getMaxCacheEntries()
Returns the maximum node count.
|
int |
getMaxCacheLength()
Returns the maximum length of strings to cache.
|
protected abstract void |
rebuildCache()
Rebuilds the cache from the current
mySeen map. |
void |
setMaxCacheEntries(int maxCacheEntries)
Sets the value of maximum number of cached strings.
|
void |
setMaxCacheLength(int maxlength)
Sets the value of maximum length of strings to cache to the new value.
|
void |
used(String decoded,
byte[] source,
int offset,
int length)
Notification that a string/byte[] have been used.
|
public static final int DEFAULT_MAX_CACHE_ENTRIES
public static final int DEFAULT_MAX_CACHE_LENGTH
protected static final int MAX_MULTIPLIER
protected int myMaxCacheLength
protected int myMaxCachEntries
protected volatile int myMaxCachEntriesMultiplier
protected final ConcurrentMap<String,com.allanbank.mongodb.bson.io.SeenString> mySeen
protected final AtomicInteger myUseCount
public AbstractStringCache()
public int getMaxCacheEntries()
public int getMaxCacheLength()
public void setMaxCacheEntries(int maxCacheEntries)
maxCacheEntries
- The new value for the maximum number of cached strings.public void setMaxCacheLength(int maxlength)
maxlength
- The new value for the maximum length of strings to cache.public void used(String decoded, byte[] source, int offset, int length)
source
- The bytes in the string.offset
- The offset of the first byte.length
- The length of the bytes with a terminal zero byte.decoded
- The decoded string.protected SortedMap<Integer,List<com.allanbank.mongodb.bson.io.SeenString>> buildCacheGroups()
protected void clear()
protected abstract void rebuildCache()
Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.