K
- key typeV
- value typeD
- value data typepublic class IoSync<K,V,D> extends Object implements CacheLoader<K,V>, CacheRemover<K,V,D>
CacheRemover
and CacheLoader
. The
onRemoval(Object, Object)
method enqueues values for writing and
returns immediately. Actual writing is done asynchronously on separate
threads, calling the connected CacheRemover
.
IoSync
takes care of directly returning values that are reloaded
while they are written. It ensures that the final state of a value that is
enqueued for removal several times is the state that is written (eventually).
A crucial assumption is that only one thread calls get get(Object)
onRemoval(Object, Object)
with the same key simultaneously. The
current SoftRefLoaderRemoverCache
implementation guarantees that. The
same is guaranteed to the connected CacheRemover
and
CacheLoader
.
TODO: If we want to avoid ever having corrupt data on disk, then get() should block while existing value is written, i.e., synchronize both Writer and get() on entry. Otherwise the value currently being written is returned and potentially modified. If we don't care about corrupt disk data, only consistency of data in memory, then this is not a problem.
defaultParallelismThreshold
Constructor and Description |
---|
IoSync(CacheLoader<K,V> loader,
CacheRemover<K,V,D> saver,
int numThreads,
int maxQueueSize)
Create a new
IoSync that asynchronously forwards to the specified
CacheRemover . |
IoSync(T io)
Create a new
IoSync that asynchronously forwards to the specified
CacheRemover . |
IoSync(T io,
int numThreads,
int maxQueueSize)
Create a new
IoSync that asynchronously forwards to the specified
CacheRemover . |
Modifier and Type | Method and Description |
---|---|
D |
extract(V value)
Extract data out of
value . |
V |
get(K key) |
void |
invalidate(K key)
Cancel any enqueued write for
key . |
void |
invalidateAll(long parallelismThreshold)
Cancel all enqueued writes.
|
void |
invalidateIf(long parallelismThreshold,
Predicate<K> condition)
Cancel all enqueued writes for keys matching
condition . |
void |
onRemoval(K key,
D valueData)
Called when an entry is evicted from the cache.
|
CompletableFuture<Void> |
persist(K key,
D valueData)
TODO
|
V |
reconstruct(K key,
D valueData)
Construct a value from its
key and valueData . |
void |
shutdown()
Shutdown all internal
Writer instances to free resources. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
invalidateAll, invalidateIf
public IoSync(T io)
IoSync
that asynchronously forwards to the specified
CacheRemover
. Uses 1 writer thread and a bounded write queue
of size 10.io
- used to asynchronously write removed values, and to load
values that are not currently enqueued for writing.public IoSync(T io, int numThreads, int maxQueueSize)
IoSync
that asynchronously forwards to the specified
CacheRemover
. The specified number of Writer
threads
is started to handle writing values through saver
.io
- used to asynchronously write removed values, and to load
values that are not currently enqueued for writing.numThreads
- how many writer threads to start (may be 0).maxQueueSize
- the maximum size of the write queue. When the queue is full,
CacheRemover.onRemoval(Object, Object)
will block
until earlier values have been written.public IoSync(CacheLoader<K,V> loader, CacheRemover<K,V,D> saver, int numThreads, int maxQueueSize)
IoSync
that asynchronously forwards to the specified
CacheRemover
. The specified number of Writer
threads
is started to handle writing values through saver
.loader
- used to load values that are not currently enqueued
for writing.saver
- used to asynchronously write removed values.numThreads
- how many writer threads to start (may be 0).maxQueueSize
- the maximum size of the write queue. When the queue is full,
CacheRemover.onRemoval(Object, Object)
will block
until earlier values have been written.public void shutdown()
Writer
instances to free resources.
Internal threads will terminate and no data will be written to disk
after shutdown.public void onRemoval(K key, D valueData)
CacheRemover
onRemoval
in interface CacheRemover<K,V,D>
key
- key of the entry to removevalueData
- value data of the entry to removepublic CompletableFuture<Void> persist(K key, D valueData)
CacheRemover
persist
in interface CacheRemover<K,V,D>
public D extract(V value)
CacheRemover
value
. The data must be sufficient to
reconstruct an identical value, and must not contain any references to
value.extract
in interface CacheRemover<K,V,D>
public V reconstruct(K key, D valueData)
CacheRemover
key
and valueData
.reconstruct
in interface CacheRemover<K,V,D>
public V get(K key) throws Exception
get
in interface CacheLoader<K,V>
Exception
public void invalidate(K key)
key
. Blocks until all in-progress
writes for key
are finished.invalidate
in interface CacheRemover<K,V,D>
invalidate
in interface Invalidate<K>
key
- key of the entry to removepublic void invalidateIf(long parallelismThreshold, Predicate<K> condition)
condition
. Blocks
until all in-progress writes for keys matching condition
are
finished.invalidateIf
in interface CacheRemover<K,V,D>
invalidateIf
in interface Invalidate<K>
parallelismThreshold
- the (estimated) number of entries in the cache needed for this
operation to be executed in parallelcondition
- condition on keys of entries to removepublic void invalidateAll(long parallelismThreshold)
invalidateAll
in interface CacheRemover<K,V,D>
invalidateAll
in interface Invalidate<K>
parallelismThreshold
- the (estimated) number of entries in the cache needed for this
operation to be executed in parallelCopyright © 2015–2022 ImgLib2. All rights reserved.