java.lang.Object
net.i2p.data.SDSCache<V>
- Type Parameters:
V- type of SimpleDataStructure objects cached
A least recently used cache with a max size, for SimpleDataStructures. The index to the cache is the first 4 bytes of the data, so the data must be sufficiently random.
This caches the SDS objects, and also uses SimpleByteCache to cache the unused byte arrays themselves
Following is sample usage:
private static final SDSCache< Foo> _cache = new SDSCache(Foo.class, LENGTH, 1024);
public static Foo create(byte[] data) {
return _cache.get(data);
}
public static Foo create(byte[] data, int off) {
return _cache.get(data, off);
}
public static Foo create(InputStream in) throws IOException {
return _cache.get(in);
}
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classAn int-keyed, weak-value cache, replacing a ConcurrentHashMap<Integer, WeakReference<V>> so the hot lookup neither hashes an Integer nor boxes the key - the first 4 bytes of a SimpleDataStructure almost always fall outside the Integer cache.private static classSaves the cache when the router shuts down.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final SDSCache.IntWeakCache<V> The cache.private final intThe byte array length for the class we are caching.private final Constructor<V> The constructor for the class we are caching.private static final doubleScaling factor from the available memory.private static final doubleLargest scaling factor applied to the expected size.private static final doubleSmallest scaling factor applied to the expected size.Constructor Summary
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clear all entries from the cache.get(byte[] data) WARNING - If the SDS is found in the cache, the passed-in byte array will be returned to the SimpleByteCache for reuse.get(byte[] b, int off) Non-null byte array containing the data, data will be copied to not hold the reference.get(InputStream in) Stream from which the bytes will be read.private static inthashCodeOf(byte[] data) We assume the data has enough randomness in it, so use the first 4 bytes for speed.
Field Details
MIN_FACTOR
private static final double MIN_FACTORSmallest scaling factor applied to the expected size.- See Also:
MAX_FACTOR
private static final double MAX_FACTORLargest scaling factor applied to the expected size.- See Also:
FACTOR
private static final double FACTORScaling factor from the available memory._cache
The cache._datalen
private final int _datalenThe byte array length for the class we are caching._rvCon
The constructor for the class we are caching.
Constructor Details
SDSCache
Class that we are storing, i.e. an extension of SimpleDataStructure.- Parameters:
rvClass- the class that we are storing, i.e. an extension of SimpleDataStructurelen- the length of the byte array in the SimpleDataStructuremax- maximum size of the cache assuming 128MB of mem. The actual max size will be scaled based on available memory.
Method Details
clear
public void clear()Clear all entries from the cache.get
WARNING - If the SDS is found in the cache, the passed-in byte array will be returned to the SimpleByteCache for reuse. Do NOT save a reference to the passed-in data, or use or modify it, after this call.- Parameters:
data- non-null, the byte array for the SimpleDataStructure- Returns:
- the cached value if available, otherwise makes a new object and returns it
- Throws:
IllegalArgumentException- if data is not the correct number of bytesNullPointerException- if data is null
get
Non-null byte array containing the data, data will be copied to not hold the reference.- Parameters:
b- non-null byte array containing the data, data will be copied to not hold the referenceoff- offset in the array to start reading from- Returns:
- the cached value if available, otherwise makes a new object and returns it
- Throws:
ArrayIndexOutOfBoundsException- if not enough bytesNullPointerException- if b is null
get
Stream from which the bytes will be read.- Parameters:
in- a stream from which the bytes will be read- Returns:
- the cached value if available, otherwise makes a new object and returns it
- Throws:
IOException- if not enough bytes
hashCodeOf
private static int hashCodeOf(byte[] data) We assume the data has enough randomness in it, so use the first 4 bytes for speed.- Parameters:
data- the raw encoded bytes, at least 4 bytes long- Returns:
- the first 4 bytes of data combined into an int, used as the cache key