Class PriorityCache<K,V>
- java.lang.Object
-
- org.apache.jackrabbit.oak.segment.file.PriorityCache<K,V>
-
- Type Parameters:
K
- type of the keysV
- type of the values
public class PriorityCache<K,V> extends Object
PriorityCache
implements a partial mapping from keys of typeK
to values of typeV
. 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 ofByte.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 givensize
.PriorityCache(int size, int rehash, @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,V> weigher, int numSegments)
Create a new instance of the givensize
.PriorityCache(int size, @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,V> weigher)
Create a new instance of the givensize
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description long
estimateCurrentWeight()
static <K,V>
Supplier<PriorityCache<K,V>>factory(int size)
Static factory for creating newPriorityCache
instances.static <K,V>
Supplier<PriorityCache<K,V>>factory(int size, @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,V> weigher)
Static factory for creating newPriorityCache
instances.V
get(K key, int generation)
Look up a mapping from this cache by itskey
andgeneration
.@NotNull org.apache.jackrabbit.guava.common.cache.CacheStats
getStats()
static long
nextPowerOfTwo(int size)
Roundsize
up to the next power of two or 1 for negative values.void
purgeGenerations(@NotNull Predicate<Integer> purge)
Purge all keys from this cache whose entry's generation matches the passedpurge
predicate.boolean
put(K key, V value, int generation, byte initialCost)
Add a mapping to the cache.long
size()
String
toString()
-
-
-
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 givensize
.rehash
specifies the number of rehashes to resolve a clash.- Parameters:
size
- Size of the cache. Must be a power of2
.rehash
- Number of rehashes. Must be greater or equal to0
and smaller than32 - 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 givensize
.rehash
specifies the number of rehashes to resolve a clash.- Parameters:
size
- Size of the cache. Must be a power of2
.rehash
- Number of rehashes. Must be greater or equal to0
and smaller than32 - numberOfTrailingZeros(size)
.weigher
- Needed to provide an estimation of the cache weight in memorynumSegments
- 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 givensize
. The number of rehashes is the maximum number allowed by the givensize
. (31 - numberOfTrailingZeros(size)
.- Parameters:
size
- Size of the cache. Must be a power of2
.
-
PriorityCache
public PriorityCache(int size)
-
-
Method Detail
-
factory
public static <K,V> Supplier<PriorityCache<K,V>> factory(int size, @NotNull @NotNull org.apache.jackrabbit.guava.common.cache.Weigher<K,V> weigher)
Static factory for creating newPriorityCache
instances.- Parameters:
size
- size of the cache. Must be a power of 2.- Returns:
- a new
PriorityCache
instance of the givensize
.
-
factory
public static <K,V> Supplier<PriorityCache<K,V>> factory(int size)
Static factory for creating newPriorityCache
instances.- Parameters:
size
- size of the cache. Must be a power of 2.- Returns:
- a new
PriorityCache
instance of the givensize
.
-
nextPowerOfTwo
public static long nextPowerOfTwo(int size)
Roundsize
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 mappingvalue
- the value of the mappinggeneration
- the generation of the mappinginitialCost
- 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 itskey
andgeneration
.- Parameters:
key
- key of the mapping to look upgeneration
- generation of the mapping to look up- Returns:
- the mapping for
key
andgeneration
ornull
if this cache does not contain such a mapping.
-
purgeGenerations
public void purgeGenerations(@NotNull @NotNull Predicate<Integer> purge)
Purge all keys from this cache whose entry's generation matches the passedpurge
predicate.- Parameters:
purge
-
-
getStats
@NotNull public @NotNull org.apache.jackrabbit.guava.common.cache.CacheStats getStats()
- Returns:
- access statistics for this cache
-
estimateCurrentWeight
public long estimateCurrentWeight()
-
-