From 83bd2cf00df5a87c3d16c8c05fa1f7d1e8c693fc Mon Sep 17 00:00:00 2001 From: Philipp Hanslovsky Date: Tue, 10 Sep 2019 17:42:34 -0400 Subject: [PATCH 1/3] Expose Invalidate for volatile-wrapped images This is just for exploring of how that could work. --- .../bdv/util/volatiles/VolatileViewData.java | 12 +++++++++++- .../java/bdv/util/volatiles/VolatileViews.java | 17 +++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/java/bdv/util/volatiles/VolatileViewData.java b/src/main/java/bdv/util/volatiles/VolatileViewData.java index 61b1fbe2..4b78329a 100644 --- a/src/main/java/bdv/util/volatiles/VolatileViewData.java +++ b/src/main/java/bdv/util/volatiles/VolatileViewData.java @@ -4,6 +4,7 @@ import net.imglib2.RandomAccessible; import net.imglib2.RandomAccessibleInterval; import net.imglib2.Volatile; +import net.imglib2.cache.Invalidate; import net.imglib2.cache.img.CachedCellImg; /** @@ -33,16 +34,20 @@ public class VolatileViewData< T, V extends Volatile< T > > private final V volatileType; + private final Invalidate< ? > invalidate; + public VolatileViewData( final RandomAccessible< V > img, final CacheControl cacheControl, final T type, - final V volatileType ) + final V volatileType, + final Invalidate< ? > invalidate ) { this.img = img; this.cacheControl = cacheControl; this.type = type; this.volatileType = volatileType; + this.invalidate = invalidate; } /** @@ -86,4 +91,9 @@ public V getVolatileType() { return volatileType; } + + public Invalidate< ? > getInvalidate() + { + return this.invalidate; + } } diff --git a/src/main/java/bdv/util/volatiles/VolatileViews.java b/src/main/java/bdv/util/volatiles/VolatileViews.java index aba6f925..7ed8d0de 100644 --- a/src/main/java/bdv/util/volatiles/VolatileViews.java +++ b/src/main/java/bdv/util/volatiles/VolatileViews.java @@ -9,6 +9,7 @@ import net.imglib2.RandomAccessibleInterval; import net.imglib2.Volatile; import net.imglib2.cache.Cache; +import net.imglib2.cache.Invalidate; import net.imglib2.cache.img.CachedCellImg; import net.imglib2.cache.ref.WeakRefVolatileCache; import net.imglib2.cache.volatiles.CacheHints; @@ -21,6 +22,8 @@ import net.imglib2.img.cell.Cell; import net.imglib2.img.cell.CellGrid; import net.imglib2.type.NativeType; +import net.imglib2.util.Pair; +import net.imglib2.util.ValuePair; import net.imglib2.view.IntervalView; import net.imglib2.view.MixedTransformView; @@ -110,7 +113,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 +124,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,12 +156,12 @@ 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 Pair< VolatileCachedCellImg< V, ? >, Invalidate > img = createVolatileCachedCellImg( grid, vtype, dirty, ( Cache ) cache, queue, hints ); - return new VolatileViewData<>( img, queue, type, vtype ); + return new VolatileViewData<>( img.getA(), queue, type, vtype, img.getB() ); } - private static < T extends NativeType< T >, A extends VolatileArrayDataAccess< A > > VolatileCachedCellImg< T, A > createVolatileCachedCellImg( + private static < T extends NativeType< T >, A extends VolatileArrayDataAccess< A > > Pair< VolatileCachedCellImg< T, A >, Invalidate< ? > > createVolatileCachedCellImg( final CellGrid grid, final T type, final boolean dirty, @@ -167,7 +172,7 @@ private static < T extends NativeType< T >, A extends VolatileArrayDataAccess< A 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 ValuePair<>( volatileImg, volatileCache ); } } From e69195156a88ee7669aa8602e9deff4b4a518a2d Mon Sep 17 00:00:00 2001 From: Philipp Hanslovsky Date: Fri, 27 Sep 2019 13:51:54 -0400 Subject: [PATCH 2/3] Refactor to avoid returning Pair --- .../VolatileRandomAccessibleIntervalView.java | 20 +++++++++++- .../bdv/util/volatiles/VolatileViewData.java | 25 ++++++++++++--- .../bdv/util/volatiles/VolatileViews.java | 31 ++++++++----------- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java b/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java index ba6dfb1d..b15b2f87 100644 --- a/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java +++ b/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java @@ -5,10 +5,13 @@ import net.imglib2.RandomAccess; import net.imglib2.RandomAccessibleInterval; import net.imglib2.Volatile; +import net.imglib2.cache.Invalidate; + +import java.util.function.Predicate; public class VolatileRandomAccessibleIntervalView< T, V extends Volatile< T > > extends AbstractWrappedInterval< RandomAccessibleInterval< V > > - implements VolatileView< T, V >, RandomAccessibleInterval< V > + implements VolatileView< T, V >, RandomAccessibleInterval< V >, Invalidate< Long > { private final VolatileViewData< T, V > viewData; @@ -36,4 +39,19 @@ 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 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 4b78329a..9e4bea3e 100644 --- a/src/main/java/bdv/util/volatiles/VolatileViewData.java +++ b/src/main/java/bdv/util/volatiles/VolatileViewData.java @@ -7,6 +7,8 @@ import net.imglib2.cache.Invalidate; import net.imglib2.cache.img.CachedCellImg; +import java.util.function.Predicate; + /** * Metadata associated with a {@link VolatileView}. It comprises the types * of the original and volatile image, a {@link CacheControl} for the @@ -24,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; @@ -34,14 +36,14 @@ public class VolatileViewData< T, V extends Volatile< T > > private final V volatileType; - private final Invalidate< ? > invalidate; + private final Invalidate< Long > invalidate; public VolatileViewData( final RandomAccessible< V > img, final CacheControl cacheControl, final T type, final V volatileType, - final Invalidate< ? > invalidate ) + final Invalidate< Long > invalidate ) { this.img = img; this.cacheControl = cacheControl; @@ -92,8 +94,23 @@ public V getVolatileType() return volatileType; } - public Invalidate< ? > getInvalidate() + public Invalidate< Long > getInvalidate() { return this.invalidate; } + + @Override + public void invalidate(Long key) { + this.invalidate.invalidate(key); + } + + @Override + public void invalidateIf(long parallelismThreshold, Predicate 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 7ed8d0de..9d81b082 100644 --- a/src/main/java/bdv/util/volatiles/VolatileViews.java +++ b/src/main/java/bdv/util/volatiles/VolatileViews.java @@ -1,15 +1,11 @@ package bdv.util.volatiles; -import static net.imglib2.img.basictypeaccess.AccessFlags.DIRTY; -import static net.imglib2.img.basictypeaccess.AccessFlags.VOLATILE; - -import java.util.Set; - +import bdv.img.cache.CreateInvalidVolatileCell; +import bdv.img.cache.VolatileCachedCellImg; import net.imglib2.RandomAccessible; import net.imglib2.RandomAccessibleInterval; import net.imglib2.Volatile; import net.imglib2.cache.Cache; -import net.imglib2.cache.Invalidate; import net.imglib2.cache.img.CachedCellImg; import net.imglib2.cache.ref.WeakRefVolatileCache; import net.imglib2.cache.volatiles.CacheHints; @@ -22,13 +18,13 @@ import net.imglib2.img.cell.Cell; import net.imglib2.img.cell.CellGrid; import net.imglib2.type.NativeType; -import net.imglib2.util.Pair; -import net.imglib2.util.ValuePair; import net.imglib2.view.IntervalView; import net.imglib2.view.MixedTransformView; -import bdv.img.cache.CreateInvalidVolatileCell; -import bdv.img.cache.VolatileCachedCellImg; +import java.util.Set; + +import static net.imglib2.img.basictypeaccess.AccessFlags.DIRTY; +import static net.imglib2.img.basictypeaccess.AccessFlags.VOLATILE; /** * Wrap view cascades ending in {@link CachedCellImg} as volatile views. @@ -156,23 +152,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 Pair< VolatileCachedCellImg< V, ? >, Invalidate > img = createVolatileCachedCellImg( grid, vtype, dirty, ( Cache ) cache, queue, hints ); + final VolatileCache>> volatileCache = createVolatileCache( grid, vtype, dirty, ( Cache ) cache, queue ); - return new VolatileViewData<>( img.getA(), queue, type, vtype, img.getB() ); + 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 > > Pair< VolatileCachedCellImg< T, A >, Invalidate< ? > > createVolatileCachedCellImg( + private static < T extends NativeType< T >, A extends VolatileArrayDataAccess< A > > VolatileCache> 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 new ValuePair<>( volatileImg, volatileCache ); + return new WeakRefVolatileCache<>( cache, queue, createInvalid ); } } From 091c2fc7f0e477814cb384715bd7717172253b69 Mon Sep 17 00:00:00 2001 From: Philipp Hanslovsky Date: Fri, 27 Sep 2019 14:01:49 -0400 Subject: [PATCH 3/3] Apply formatter --- .../VolatileRandomAccessibleIntervalView.java | 23 +++++++++++-------- .../bdv/util/volatiles/VolatileViewData.java | 19 ++++++++------- .../bdv/util/volatiles/VolatileViews.java | 17 +++++++------- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java b/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java index b15b2f87..2594c99b 100644 --- a/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java +++ b/src/main/java/bdv/util/volatiles/VolatileRandomAccessibleIntervalView.java @@ -1,5 +1,7 @@ package bdv.util.volatiles; +import java.util.function.Predicate; + import net.imglib2.AbstractWrappedInterval; import net.imglib2.Interval; import net.imglib2.RandomAccess; @@ -7,11 +9,9 @@ import net.imglib2.Volatile; import net.imglib2.cache.Invalidate; -import java.util.function.Predicate; - public class VolatileRandomAccessibleIntervalView< T, V extends Volatile< T > > - extends AbstractWrappedInterval< RandomAccessibleInterval< V > > - implements VolatileView< T, V >, RandomAccessibleInterval< V >, Invalidate< Long > + extends AbstractWrappedInterval< RandomAccessibleInterval< V > > + implements VolatileView< T, V >, RandomAccessibleInterval< V >, Invalidate< Long > { private final VolatileViewData< T, V > viewData; @@ -41,17 +41,20 @@ public RandomAccess< V > randomAccess( final Interval interval ) } @Override - public void invalidate(Long key) { - this.viewData.invalidate(key); + public void invalidate( Long key ) + { + this.viewData.invalidate( key ); } @Override - public void invalidateIf(long parallelismThreshold, Predicate condition) { - this.viewData.invalidateIf(parallelismThreshold, condition); + public void invalidateIf( long parallelismThreshold, Predicate< Long > condition ) + { + this.viewData.invalidateIf( parallelismThreshold, condition ); } @Override - public void invalidateAll(long parallelismThreshold) { - this.viewData.invalidateAll(parallelismThreshold); + 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 9e4bea3e..58e22ee1 100644 --- a/src/main/java/bdv/util/volatiles/VolatileViewData.java +++ b/src/main/java/bdv/util/volatiles/VolatileViewData.java @@ -1,5 +1,7 @@ package bdv.util.volatiles; +import java.util.function.Predicate; + import bdv.cache.CacheControl; import net.imglib2.RandomAccessible; import net.imglib2.RandomAccessibleInterval; @@ -7,8 +9,6 @@ import net.imglib2.cache.Invalidate; import net.imglib2.cache.img.CachedCellImg; -import java.util.function.Predicate; - /** * Metadata associated with a {@link VolatileView}. It comprises the types * of the original and volatile image, a {@link CacheControl} for the @@ -100,17 +100,20 @@ public Invalidate< Long > getInvalidate() } @Override - public void invalidate(Long key) { - this.invalidate.invalidate(key); + public void invalidate( Long key ) + { + this.invalidate.invalidate( key ); } @Override - public void invalidateIf(long parallelismThreshold, Predicate condition) { - this.invalidate.invalidateIf(parallelismThreshold, condition); + public void invalidateIf( long parallelismThreshold, Predicate< Long > condition ) + { + this.invalidate.invalidateIf( parallelismThreshold, condition ); } @Override - public void invalidateAll(long parallelismThreshold) { - this.invalidate.invalidateAll(parallelismThreshold); + 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 9d81b082..ebcca28d 100644 --- a/src/main/java/bdv/util/volatiles/VolatileViews.java +++ b/src/main/java/bdv/util/volatiles/VolatileViews.java @@ -1,7 +1,10 @@ package bdv.util.volatiles; -import bdv.img.cache.CreateInvalidVolatileCell; -import bdv.img.cache.VolatileCachedCellImg; +import static net.imglib2.img.basictypeaccess.AccessFlags.DIRTY; +import static net.imglib2.img.basictypeaccess.AccessFlags.VOLATILE; + +import java.util.Set; + import net.imglib2.RandomAccessible; import net.imglib2.RandomAccessibleInterval; import net.imglib2.Volatile; @@ -21,10 +24,8 @@ import net.imglib2.view.IntervalView; import net.imglib2.view.MixedTransformView; -import java.util.Set; - -import static net.imglib2.img.basictypeaccess.AccessFlags.DIRTY; -import static net.imglib2.img.basictypeaccess.AccessFlags.VOLATILE; +import bdv.img.cache.CreateInvalidVolatileCell; +import bdv.img.cache.VolatileCachedCellImg; /** * Wrap view cascades ending in {@link CachedCellImg} as volatile views. @@ -152,14 +153,14 @@ private static < T extends NativeType< T >, V extends Volatile< T > & NativeType if ( hints == null ) hints = new CacheHints( LoadingStrategy.VOLATILE, 0, false ); @SuppressWarnings( "rawtypes" ) - final VolatileCache>> volatileCache = createVolatileCache( grid, vtype, dirty, ( Cache ) cache, queue ); + final VolatileCache< Long, Cell< ? extends VolatileArrayDataAccess< ? > > > volatileCache = createVolatileCache( grid, vtype, dirty, ( Cache ) cache, queue ); 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 > > VolatileCache> createVolatileCache( + private static < T extends NativeType< T >, A extends VolatileArrayDataAccess< A > > VolatileCache< Long, Cell< A > > createVolatileCache( final CellGrid grid, final T type, final boolean dirty,