diff --git a/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java b/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java index ba6dfb1d..2594c99b 100644 --- a/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java +++ b/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java @@ -1,14 +1,17 @@ package bdv.util.volatiles; +import java.util.function.Predicate; + import net.imglib2.AbstractWrappedInterval; import net.imglib2.Interval; import net.imglib2.RandomAccess; import net.imglib2.RandomAccessibleInterval; import net.imglib2.Volatile; +import net.imglib2.cache.Invalidate; public class VolatileRandomAccessibleIntervalView< T, V extends Volatile< T > > - extends AbstractWrappedInterval< RandomAccessibleInterval< V > > - implements VolatileView< T, V >, RandomAccessibleInterval< V > + extends AbstractWrappedInterval< RandomAccessibleInterval< V > > + implements VolatileView< T, V >, RandomAccessibleInterval< V >, Invalidate< Long > { private final VolatileViewData< T, V > viewData; @@ -36,4 +39,22 @@ public RandomAccess< V > randomAccess( final Interval interval ) { return sourceInterval.randomAccess( interval ); } + + @Override + public void invalidate( Long key ) + { + this.viewData.invalidate( key ); + } + + @Override + public void invalidateIf( long parallelismThreshold, Predicate< Long > condition ) + { + this.viewData.invalidateIf( parallelismThreshold, condition ); + } + + @Override + public void invalidateAll( long parallelismThreshold ) + { + this.viewData.invalidateAll( parallelismThreshold ); + } } diff --git a/src/main/java/bdv/util/volatiles/VolatileViewData.java b/src/main/java/bdv/util/volatiles/VolatileViewData.java index 61b1fbe2..58e22ee1 100644 --- a/src/main/java/bdv/util/volatiles/VolatileViewData.java +++ b/src/main/java/bdv/util/volatiles/VolatileViewData.java @@ -1,9 +1,12 @@ package bdv.util.volatiles; +import java.util.function.Predicate; + import bdv.cache.CacheControl; import net.imglib2.RandomAccessible; import net.imglib2.RandomAccessibleInterval; import net.imglib2.Volatile; +import net.imglib2.cache.Invalidate; import net.imglib2.cache.img.CachedCellImg; /** @@ -23,7 +26,7 @@ * * @author Tobias Pietzsch */ -public class VolatileViewData< T, V extends Volatile< T > > +public class VolatileViewData< T, V extends Volatile< T > > implements Invalidate< Long > { private final RandomAccessible< V > img; @@ -33,16 +36,20 @@ public class VolatileViewData< T, V extends Volatile< T > > private final V volatileType; + private final Invalidate< Long > invalidate; + public VolatileViewData( final RandomAccessible< V > img, final CacheControl cacheControl, final T type, - final V volatileType ) + final V volatileType, + final Invalidate< Long > invalidate ) { this.img = img; this.cacheControl = cacheControl; this.type = type; this.volatileType = volatileType; + this.invalidate = invalidate; } /** @@ -86,4 +93,27 @@ public V getVolatileType() { return volatileType; } + + public Invalidate< Long > getInvalidate() + { + return this.invalidate; + } + + @Override + public void invalidate( Long key ) + { + this.invalidate.invalidate( key ); + } + + @Override + public void invalidateIf( long parallelismThreshold, Predicate< Long > condition ) + { + this.invalidate.invalidateIf( parallelismThreshold, condition ); + } + + @Override + public void invalidateAll( long parallelismThreshold ) + { + this.invalidate.invalidateAll( parallelismThreshold ); + } } diff --git a/src/main/java/bdv/util/volatiles/VolatileViews.java b/src/main/java/bdv/util/volatiles/VolatileViews.java index aba6f925..ebcca28d 100644 --- a/src/main/java/bdv/util/volatiles/VolatileViews.java +++ b/src/main/java/bdv/util/volatiles/VolatileViews.java @@ -110,7 +110,8 @@ else if ( rai instanceof IntervalView ) new IntervalView<>( sourceData.getImg(), view ), sourceData.getCacheControl(), sourceData.getType(), - sourceData.getVolatileType() ); + sourceData.getVolatileType(), + sourceData.getInvalidate() ); } else if ( rai instanceof MixedTransformView ) { @@ -120,7 +121,8 @@ else if ( rai instanceof MixedTransformView ) new MixedTransformView<>( sourceData.getImg(), view.getTransformToSource() ), sourceData.getCacheControl(), sourceData.getType(), - sourceData.getVolatileType() ); + sourceData.getVolatileType(), + sourceData.getInvalidate() ); } else if ( rai instanceof WrappedImg ) { @@ -151,23 +153,22 @@ private static < T extends NativeType< T >, V extends Volatile< T > & NativeType if ( hints == null ) hints = new CacheHints( LoadingStrategy.VOLATILE, 0, false ); @SuppressWarnings( "rawtypes" ) - final VolatileCachedCellImg< V, ? > img = createVolatileCachedCellImg( grid, vtype, dirty, ( Cache ) cache, queue, hints ); + final VolatileCache< Long, Cell< ? extends VolatileArrayDataAccess< ? > > > volatileCache = createVolatileCache( grid, vtype, dirty, ( Cache ) cache, queue ); - return new VolatileViewData<>( img, queue, type, vtype ); + final VolatileCachedCellImg< V, ? extends VolatileArrayDataAccess< ? > > volatileImg = new VolatileCachedCellImg<>( grid, vtype, hints, volatileCache.unchecked()::get ); + + return new VolatileViewData<>( volatileImg, queue, type, vtype, volatileCache ); } - private static < T extends NativeType< T >, A extends VolatileArrayDataAccess< A > > VolatileCachedCellImg< T, A > createVolatileCachedCellImg( + private static < T extends NativeType< T >, A extends VolatileArrayDataAccess< A > > VolatileCache< Long, Cell< A > > createVolatileCache( final CellGrid grid, final T type, final boolean dirty, final Cache< Long, Cell< A > > cache, - final SharedQueue queue, - final CacheHints hints ) + final SharedQueue queue ) { final CreateInvalid< Long, Cell< A > > createInvalid = CreateInvalidVolatileCell.get( grid, type, dirty ); - final VolatileCache< Long, Cell< A > > volatileCache = new WeakRefVolatileCache<>( cache, queue, createInvalid ); - final VolatileCachedCellImg< T, A > volatileImg = new VolatileCachedCellImg<>( grid, type, hints, volatileCache.unchecked()::get ); - return volatileImg; + return new WeakRefVolatileCache<>( cache, queue, createInvalid ); } }