Class SharedBoundedContainer<K,V>
- Type Parameters:
K- The key typeV- The value type
- All Implemented Interfaces:
Iterable<InternalCacheEntry<K,V>>, Consumer<Iterable<InternalCacheEntry<K, V>>>, DataContainer<K, V>, EvictionListener<K, V>, InternalDataContainer<K, V>
onEntryChosenForEviction(boolean, Object, InternalCacheEntry)
must be invoked externally whenever an entry is evicted from the provided caffeineMap.
To guarantee consistency any write operations must be done while in the lock for the corresponding key of the provided caffeineMap. This means any writes to any of the maps per segment must only be done while in this secured region.
This class is implemented so that every single key read and write operation are constant time and iteration is O(N)
where N is the size of the entries in the individual container and not the shared. However, due to this the
clear() and clear(IntSet) methods are not O(1) as in many other implementations but rather are O(N).
Please see them respectively for more details.
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractInternalDataContainer
AbstractInternalDataContainer.EntryIteratorNested classes/interfaces inherited from interface DataContainer
DataContainer.ComputeAction<K,V> -
Field Summary
Fields inherited from class DefaultSegmentedDataContainer
maps, mapSupplier, shouldStopSegmentsFields inherited from class AbstractInternalDataContainer
configuration, entryFactory, evictionManager, expirationManager, keyPartitioner, listeners, orderer, passivator, timeService -
Constructor Summary
ConstructorsConstructorDescriptionSharedBoundedContainer(PeekableTouchableCaffeineMap<K, V> caffeineMap, Supplier<PeekableTouchableMap<K, V>> peekableTouchableMapSupplier, int numSegments) -
Method Summary
Modifier and TypeMethodDescriptionvoidaccept(Iterable<InternalCacheEntry<K, V>> internalCacheEntries) longcapacity()Returns the capacity of the underlying container.voidcleanUp()Method used to cleanup any pending data, such as evictionsvoidclear()Removes all entries that map only to this container and its map entries.voidClears only the entries that map to the given segments.protected voidcomputeEntryRemoved(int segment, K key, InternalCacheEntry<K, V> value) This method is invoked every time an entry is removed inside a compute blockprotected voidcomputeEntryWritten(int segment, K key, InternalCacheEntry<K, V> value) This method is invoked every time an entry is written inside a compute blockbooleanTests whether an entry exists in the containerlongReturns how large the eviction size is currently.getMapForSegment(int segment) voidonEntryChosenForEviction(boolean pre, K key, InternalCacheEntry<K, V> value) Method invoked every time an entry is evicted from the cache along with its key.Retrieves a cache entry.protected voidputEntryInMap(PeekableTouchableMap<K, V> map, int segment, K key, InternalCacheEntry<K, V> ice) protected InternalCacheEntry<K, V> removeEntryInMap(PeekableTouchableMap<K, V> map, int segment, Object key) voidremoveSegments(IntSet segments) Removes and un-associates the given segments.voidresize(long newSize) Resizes the capacity of the underlying container.voidstart()Methods inherited from class DefaultSegmentedDataContainer
addSegments, forEach, forEachSegment, getSegmentForKey, iterator, iterator, iteratorIncludingExpired, iteratorIncludingExpired, publisher, publisher, size, size, sizeIncludingExpired, sizeIncludingExpired, spliterator, spliterator, spliteratorIncludingExpired, spliteratorIncludingExpired, stopMethods inherited from class AbstractInternalDataContainer
addRemovalListener, applyListener, compute, compute, containsKey, entryAdded, entryRemoved, entryUpdated, evict, evict, expiredIterationPredicate, filterExpiredEntries, get, get, handleEviction, hasExpirable, peek, put, put, remove, remove, removeRemovalListener, segmentRemoved, touch
-
Constructor Details
-
SharedBoundedContainer
public SharedBoundedContainer(PeekableTouchableCaffeineMap<K, V> caffeineMap, Supplier<PeekableTouchableMap<K, V>> peekableTouchableMapSupplier, int numSegments)
-
-
Method Details
-
start
public void start()- Overrides:
startin classDefaultSegmentedDataContainer<K,V>
-
getMapForSegment
- Overrides:
getMapForSegmentin classDefaultSegmentedDataContainer<K,V>
-
computeEntryRemoved
Description copied from class:AbstractInternalDataContainerThis method is invoked every time an entry is removed inside a compute block- Overrides:
computeEntryRemovedin classAbstractInternalDataContainer<K,V> - Parameters:
segment- the segment for the keykey- key passed to compute methodvalue- the old value
-
computeEntryWritten
Description copied from class:AbstractInternalDataContainerThis method is invoked every time an entry is written inside a compute block- Overrides:
computeEntryWrittenin classAbstractInternalDataContainer<K,V> - Parameters:
segment- the segment for the keykey- key passed to compute methodvalue- the new value
-
putEntryInMap
protected void putEntryInMap(PeekableTouchableMap<K, V> map, int segment, K key, InternalCacheEntry<K, V> ice) - Overrides:
putEntryInMapin classAbstractInternalDataContainer<K,V>
-
removeEntryInMap
protected InternalCacheEntry<K,V> removeEntryInMap(PeekableTouchableMap<K, V> map, int segment, Object key) - Overrides:
removeEntryInMapin classAbstractInternalDataContainer<K,V>
-
clear
public void clear()Removes all entries that map only to this container and its map entries. To not clear the underlying caffeineMap completely we must do additional work, seeclear(IntSet)for implementation and performance details.- Specified by:
clearin interfaceDataContainer<K,V> - Overrides:
clearin classDefaultSegmentedDataContainer<K,V>
-
clear
Clears only the entries that map to the given segments. Note that to ensure the shared caffeineMap is not adversely affected we have to iterate over all of the entries in the map segment storage and then individually remove those values from the caffeineMap. This way we still have consistency as the caffeineMap lock is acquired before removing from the map segment.This iteration means that this clear operation performs with O(N) time complexity as we must only remove the entries we control from the caffeineMap and our segment maps.
Due to clearing a map or its segments should be done much less frequently than other operations in this implementation, these two clear methods have increased time complexity to allow all other operations to have their best respective time complexity implementations.
- Specified by:
clearin interfaceInternalDataContainer<K,V> - Overrides:
clearin classAbstractInternalDataContainer<K,V> - Parameters:
segments- segments of entries to remove
-
accept
-
removeSegments
Description copied from interface:InternalDataContainerRemoves and un-associates the given segments. This will notify any listeners registered viaInternalDataContainer.addRemovalListener(Consumer)of entries that were removed due to no longer being associated with this container. There is no guarantee if the consumer is invoked once or multiple times for a given group of segments and could be in any order.When this method is invoked an implementation is free to remove any entries that don't map to segments currently associated with this container. Note that entries that were removed due to their segments never being associated with this container do not notify listeners registered via
InternalDataContainer.addRemovalListener(Consumer).- Specified by:
removeSegmentsin interfaceInternalDataContainer<K,V> - Overrides:
removeSegmentsin classDefaultSegmentedDataContainer<K,V> - Parameters:
segments- segments that should no longer be associated with this container
-
peek
Description copied from interface:DataContainerRetrieves a cache entry. This method does not update or reorder any of the internal constructs. I.e., expiration does not happen, and in the case of the LRU container, the entry is not moved to the end of the chain.- Specified by:
peekin interfaceDataContainer<K,V> - Overrides:
peekin classAbstractInternalDataContainer<K,V> - Parameters:
k- key under which entry is stored- Returns:
- entry, if it exists, or null if not
-
containsKey
Description copied from interface:DataContainerTests whether an entry exists in the container- Specified by:
containsKeyin interfaceDataContainer<K,V> - Overrides:
containsKeyin classAbstractInternalDataContainer<K,V> - Parameters:
k- key to test- Returns:
- true if entry exists and has not expired; false otherwise
-
cleanUp
public void cleanUp()Description copied from interface:InternalDataContainerMethod used to cleanup any pending data, such as evictions- Specified by:
cleanUpin interfaceInternalDataContainer<K,V>
-
capacity
public long capacity()Description copied from interface:DataContainerReturns the capacity of the underlying container. This is only supported if the container is bounded. AnUnsupportedOperationExceptionis thrown otherwise.- Specified by:
capacityin interfaceDataContainer<K,V> - Returns:
- the capacity of a bounded container
-
resize
public void resize(long newSize) Description copied from interface:DataContainerResizes the capacity of the underlying container. This is only supported if the container is bounded. AnUnsupportedOperationExceptionis thrown otherwise.- Specified by:
resizein interfaceDataContainer<K,V> - Parameters:
newSize- the new size
-
evictionSize
public long evictionSize()Description copied from interface:DataContainerReturns how large the eviction size is currently. This is only supported if the container is bounded. AnUnsupportedOperationExceptionis thrown otherwise. This value will always be lower than the value returned fromDataContainer.capacity()- Specified by:
evictionSizein interfaceDataContainer<K,V> - Returns:
- how large the counted eviction is
-
onEntryChosenForEviction
Description copied from interface:EvictionListenerMethod invoked every time an entry is evicted from the cache along with its key. Note that when the pre value is true the entry is still in the cache and the lock is held for this entry. When invoked with pre being false is after the fact without the lock held. If any exception is thrown during this invocation it is not deterministic if it will stop the operation and may only be logged.- Specified by:
onEntryChosenForEvictionin interfaceEvictionListener<K,V> - Parameters:
pre- Whether this is just before the eviction completes or not.key- The key tied the removed entryvalue- The entry that is removed due to eviction
-