Class PriorityCache<K,​V>

  • Type Parameters:
    K - type of the keys
    V - type of the values

    public class PriorityCache<K,​V>
    extends java.lang.Object
    PriorityCache implements a partial mapping from keys of type K to values of type V. Mappings are associates with a cost, which states how expensive it is to recreate that mapping. This cache uses the cost such that mappings with a higher cost have a lower chance of being evicted than mappings with a lower cost. When an item from this cache is successfully looked up its cost is incremented by one, unless it has reached its maximum cost of Byte.MAX_VALUE already.

    Additionally, this cache tracks a generation for mappings. Mappings of later generations always take precedence over mappings of earlier generations. That is, putting a mapping of a later generation into the cache can cause any mapping of an earlier generation to be evicted regardless of its cost.

    This cache uses rehashing to resolve clashes. The number of rehashes is configurable. When a clash cannot be resolved by rehashing the given number of times the put operation fails.

    This cache is thread safe.

    • Constructor Summary

      Constructors 
      Constructor Description
      PriorityCache​(int size)  
      PriorityCache​(int size, int rehash, @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,​V> weigher)
      Create a new instance of the given size.
      PriorityCache​(int size, int rehash, @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,​V> weigher, int numSegments)
      Create a new instance of the given size.
      PriorityCache​(int size, @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,​V> weigher)
      Create a new instance of the given size.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long estimateCurrentWeight()  
      static <K,​V>
      org.apache.jackrabbit.guava.common.base.Supplier<PriorityCache<K,​V>>
      factory​(int size)
      Static factory for creating new PriorityCache instances.
      static <K,​V>
      org.apache.jackrabbit.guava.common.base.Supplier<PriorityCache<K,​V>>
      factory​(int size, @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,​V> weigher)
      Static factory for creating new PriorityCache instances.
      V get​(K key, int generation)
      Look up a mapping from this cache by its key and generation.
      @NotNull org.apache.jackrabbit.guava.common.cache.CacheStats getStats()  
      static long nextPowerOfTwo​(int size)
      Round size up to the next power of two or 1 for negative values.
      void purgeGenerations​(@NotNull org.apache.jackrabbit.guava.common.base.Predicate<java.lang.Integer> purge)
      Purge all keys from this cache whose entry's generation matches the passed purge predicate.
      boolean put​(K key, V value, int generation, byte initialCost)
      Add a mapping to the cache.
      long size()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • PriorityCache

        public PriorityCache​(int size,
                             int rehash,
                             @NotNull
                             @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,​V> weigher)
        Create a new instance of the given size. rehash specifies the number of rehashes to resolve a clash.
        Parameters:
        size - Size of the cache. Must be a power of 2.
        rehash - Number of rehashes. Must be greater or equal to 0 and smaller than 32 - numberOfTrailingZeros(size).
        weigher - Needed to provide an estimation of the cache weight in memory
      • PriorityCache

        public PriorityCache​(int size,
                             int rehash,
                             @NotNull
                             @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,​V> weigher,
                             int numSegments)
        Create a new instance of the given size. rehash specifies the number of rehashes to resolve a clash.
        Parameters:
        size - Size of the cache. Must be a power of 2.
        rehash - Number of rehashes. Must be greater or equal to 0 and smaller than 32 - numberOfTrailingZeros(size).
        weigher - Needed to provide an estimation of the cache weight in memory
        numSegments - Number of separately locked segments. The implementation assumes an equal number of entries in each segment, requiring numSegments to divide size. Powers of 2 are a safe choice, see @param size.
      • PriorityCache

        public PriorityCache​(int size,
                             @NotNull
                             @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,​V> weigher)
        Create a new instance of the given size. The number of rehashes is the maximum number allowed by the given size. (31 - numberOfTrailingZeros(size).
        Parameters:
        size - Size of the cache. Must be a power of 2.
      • PriorityCache

        public PriorityCache​(int size)
    • Method Detail

      • factory

        public static <K,​V> org.apache.jackrabbit.guava.common.base.Supplier<PriorityCache<K,​V>> factory​(int size,
                                                                                                                     @NotNull
                                                                                                                     @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,​V> weigher)
        Static factory for creating new PriorityCache instances.
        Parameters:
        size - size of the cache. Must be a power of 2.
        Returns:
        a new PriorityCache instance of the given size.
      • factory

        public static <K,​V> org.apache.jackrabbit.guava.common.base.Supplier<PriorityCache<K,​V>> factory​(int size)
        Static factory for creating new PriorityCache instances.
        Parameters:
        size - size of the cache. Must be a power of 2.
        Returns:
        a new PriorityCache instance of the given size.
      • nextPowerOfTwo

        public static long nextPowerOfTwo​(int size)
        Round size up to the next power of two or 1 for negative values.
        Parameters:
        size -
        Returns:
        the next power of two starting from size.
      • size

        public long size()
        Returns:
        the number of mappings in this cache.
      • put

        public boolean put​(@NotNull
                           K key,
                           @NotNull
                           V value,
                           int generation,
                           byte initialCost)
        Add a mapping to the cache.
        Parameters:
        key - the key of the mapping
        value - the value of the mapping
        generation - the generation of the mapping
        initialCost - the initial cost associated with this mapping
        Returns:
        true if the mapping has been added, false otherwise.
      • get

        @Nullable
        public V get​(@NotNull
                     K key,
                     int generation)
        Look up a mapping from this cache by its key and generation.
        Parameters:
        key - key of the mapping to look up
        generation - generation of the mapping to look up
        Returns:
        the mapping for key and generation or null if this cache does not contain such a mapping.
      • purgeGenerations

        public void purgeGenerations​(@NotNull
                                     @NotNull org.apache.jackrabbit.guava.common.base.Predicate<java.lang.Integer> purge)
        Purge all keys from this cache whose entry's generation matches the passed purge predicate.
        Parameters:
        purge -
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getStats

        @NotNull
        public @NotNull org.apache.jackrabbit.guava.common.cache.CacheStats getStats()
        Returns:
        access statistics for this cache
      • estimateCurrentWeight

        public long estimateCurrentWeight()