java.lang.Object
net.i2p.data.SDSCache<V>
Type Parameters:
V - type of SimpleDataStructure objects cached

public class SDSCache<V extends SimpleDataStructure>extends Object
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 Classes
    Modifier and Type
    Class
    Description
    private static final class 
    An 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 class 
    Saves the cache when the router shuts down.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final SDSCache.IntWeakCache<V>
    The cache.
    private final int
    The byte array length for the class we are caching.
    private final Constructor<V>
    The constructor for the class we are caching.
    private static final double
    Scaling factor from the available memory.
    private static final double
    Largest scaling factor applied to the expected size.
    private static final double
    Smallest scaling factor applied to the expected size.
  • Constructor Summary

    Constructors
    Constructor
    Description
    SDSCache(Class<V> rvClass, int len, int max)
    Class that we are storing, i.e.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    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.
    Stream from which the bytes will be read.
    private static int
    hashCodeOf(byte[] data)
    We assume the data has enough randomness in it, so use the first 4 bytes for speed.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MIN_FACTOR

      private static final double MIN_FACTOR
      Smallest scaling factor applied to the expected size.
      See Also:
    • MAX_FACTOR

      private static final double MAX_FACTOR
      Largest scaling factor applied to the expected size.
      See Also:
    • FACTOR

      private static final double FACTOR
      Scaling factor from the available memory.
    • _cache

      private final SDSCache.IntWeakCache<V extends SimpleDataStructure> _cache
      The cache.
    • _datalen

      private final int _datalen
      The byte array length for the class we are caching.
    • _rvCon

      private final Constructor<V extends SimpleDataStructure> _rvCon
      The constructor for the class we are caching.
  • Constructor Details

    • SDSCache

      public SDSCache(Class<V> rvClass, int len, int max)
      Class that we are storing, i.e. an extension of SimpleDataStructure.
      Parameters:
      rvClass - the class that we are storing, i.e. an extension of SimpleDataStructure
      len - the length of the byte array in the SimpleDataStructure
      max - 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

      public V 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. 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 bytes
      NullPointerException - if data is null
    • get

      public V get(byte[] b, int off)
      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 reference
      off - 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 bytes
      NullPointerException - if b is null
    • get

      public V get(InputStream in) throws IOException
      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