Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow finer control of caches in image loaders #137

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/bdv/cache/CacheOverrider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package bdv.cache;

import bdv.img.cache.VolatileGlobalCellCache;

/**
* Allows to override the {@link bdv.img.cache.VolatileGlobalCellCache} in
* ImageLoader(s) which implement this interface.
*/

public interface CacheOverrider {

void setCache(VolatileGlobalCellCache cache);

}
15 changes: 15 additions & 0 deletions src/main/java/bdv/img/cache/VolatileGlobalCellCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ public VolatileGlobalCellCache( final int maxNumLevels, final int numFetcherThre
backingCache = new SoftRefLoaderCache<>();
}

/**
* Create a new global cache with the specified fetch queue. (It is the
* callers responsibility to create fetcher threads that serve the queue.)
* and the backing cache.
*
* @param queue
* queue to which asynchronous data loading jobs are submitted
* @param backingCache cache used to stored the cells
*/
public VolatileGlobalCellCache( final BlockingFetchQueues< Callable< ? > > queue, final LoaderCache< Key, Cell< ? > > backingCache)
{
this.queue = queue;
this.backingCache = backingCache;
}

/**
* Create a new global cache with the specified fetch queue. (It is the
* callers responsibility to create fetcher threads that serve the queue.)
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/bdv/img/hdf5/Hdf5ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import bdv.AbstractViewerSetupImgLoader;
import bdv.ViewerImgLoader;
import bdv.cache.CacheOverrider;
import bdv.cache.SharedQueue;
import bdv.img.cache.VolatileGlobalCellCache;
import bdv.util.ConstantRandomAccessible;
Expand Down Expand Up @@ -76,7 +77,7 @@
import static bdv.img.hdf5.Util.getResolutionsPath;
import static bdv.img.hdf5.Util.getSubdivisionsPath;

public class Hdf5ImageLoader implements ViewerImgLoader, MultiResolutionImgLoader
public class Hdf5ImageLoader implements ViewerImgLoader, MultiResolutionImgLoader, CacheOverrider
{
protected File hdf5File;

Expand Down Expand Up @@ -311,6 +312,20 @@ public VolatileGlobalCellCache getCacheControl()
return cache;
}

@Override
public synchronized void setCache(VolatileGlobalCellCache cache) {
if ( isOpen )
{
if ( !isOpen )
return;
if ( createdSharedQueue != null )
createdSharedQueue.shutdown();
cache.clearCache();
createdSharedQueue = null;
}
this.cache = cache;
}

public Hdf5VolatileShortArrayLoader getShortArrayLoader()
{
open();
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/bdv/img/imaris/ImarisImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
package bdv.img.imaris;

import bdv.cache.CacheOverrider;
import bdv.img.hdf5.Util;
import bdv.util.MipmapTransforms;
import ch.systemsx.cisd.hdf5.HDF5DataClass;
Expand Down Expand Up @@ -65,7 +66,7 @@
import net.imglib2.type.numeric.integer.UnsignedShortType;
import net.imglib2.type.volatiles.VolatileUnsignedShortType;

public class ImarisImageLoader< T extends NativeType< T >, V extends Volatile< T > & NativeType< V > , A extends VolatileAccess > implements ViewerImgLoader
public class ImarisImageLoader< T extends NativeType< T >, V extends Volatile< T > & NativeType< V > , A extends VolatileAccess > implements ViewerImgLoader, CacheOverrider
{
private IHDF5Access hdf5Access;

Expand Down Expand Up @@ -285,6 +286,17 @@ public CacheControl getCacheControl()
return cache;
}

@Override
public synchronized void setCache(VolatileGlobalCellCache cache) {
if ( isOpen )
{
if ( !isOpen )
return;
cache.clearCache();
}
this.cache = cache;
}

@Override
public SetupImgLoader getSetupImgLoader( final int setupId )
{
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/bdv/img/n5/N5ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import bdv.AbstractViewerSetupImgLoader;
import bdv.ViewerImgLoader;
import bdv.cache.CacheControl;
import bdv.cache.CacheOverrider;
import bdv.cache.SharedQueue;
import bdv.img.cache.SimpleCacheArrayLoader;
import bdv.img.cache.VolatileGlobalCellCache;
Expand Down Expand Up @@ -100,7 +101,7 @@
import static bdv.img.n5.BdvN5Format.DOWNSAMPLING_FACTORS_KEY;
import static bdv.img.n5.BdvN5Format.getPathName;

public class N5ImageLoader implements ViewerImgLoader, MultiResolutionImgLoader
public class N5ImageLoader implements ViewerImgLoader, MultiResolutionImgLoader, CacheOverrider
{
private final File n5File;

Expand Down Expand Up @@ -186,6 +187,20 @@ private void open()
}
}

@Override
public synchronized void setCache(VolatileGlobalCellCache cache) {
if ( isOpen )
{
if ( !isOpen )
return;
if ( createdSharedQueue != null )
createdSharedQueue.shutdown();
cache.clearCache();
createdSharedQueue = null;
}
this.cache = cache;
}

/**
* Clear the cache. Images that were obtained from
* this loader before {@link #close()} will stop working. Requesting images
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/bdv/img/remote/RemoteImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.net.URL;
import java.util.HashMap;

import bdv.cache.CacheOverrider;
import com.google.gson.GsonBuilder;

import bdv.AbstractViewerSetupImgLoader;
Expand All @@ -56,7 +57,7 @@
import net.imglib2.util.IntervalIndexer;
import net.imglib2.view.Views;

public class RemoteImageLoader implements ViewerImgLoader
public class RemoteImageLoader implements ViewerImgLoader, CacheOverrider
{
protected String baseUrl;

Expand Down Expand Up @@ -139,6 +140,17 @@ public VolatileGlobalCellCache getCacheControl()
return cache;
}

@Override
public synchronized void setCache(VolatileGlobalCellCache cache) {
if ( isOpen )
{
if ( !isOpen )
return;
cache.clearCache();
}
this.cache = cache;
}

public MipmapInfo getMipmapInfo( final int setupId )
{
tryopen();
Expand Down