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

Prevent multiple loading of DatasetAttributes in N5OMEZarrImageLoader #100

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import java.io.IOException;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;

import static org.embl.mobie.io.ome.zarr.util.OmeZarrMultiscales.MULTI_SCALE_KEY;

Expand Down Expand Up @@ -98,6 +99,7 @@ public class N5OMEZarrImageLoader implements ViewerImgLoader, MultiResolutionImg
private ZarrAxes zarrAxes;
List<ZarrAxis> zarrAxesList;
private BlockingFetchQueues<Callable<?>> queue;
private ConcurrentHashMap< String, DatasetAttributes > pathToAttributes = new ConcurrentHashMap<>();

/**
* The sequenceDescription and viewRegistrations are known already, typically read from xml.
Expand Down Expand Up @@ -220,8 +222,7 @@ private void initSetups() throws IOException {
String pathName = "labels/" + label;
multiscales = getMultiscale(pathName);
for (OmeZarrMultiscales multiscale : multiscales) {
DatasetAttributes attributes = getDatasetAttributes(multiscale.datasets[0].path);
attributes = getDatasetAttributes(pathName + "/" + multiscale.datasets[0].path);
DatasetAttributes attributes = getDatasetAttributes(pathName + "/" + multiscale.datasets[0].path);

setupToMultiscale.put(setupId, multiscale);
setupToAttributes.put(setupId, attributes);
Expand All @@ -244,8 +245,13 @@ private void initSetups() throws IOException {
* @return
* @throws IOException
*/
private DatasetAttributes getDatasetAttributes(String pathName) throws IOException {
return n5.getDatasetAttributes(pathName);
private synchronized DatasetAttributes getDatasetAttributes(String pathName) throws IOException {
if ( ! pathToAttributes.containsKey( pathName ) )
{
final DatasetAttributes attributes = n5.getDatasetAttributes( pathName );
pathToAttributes.put( pathName, attributes );
}
return pathToAttributes.get( pathName );
}

/**
Expand Down Expand Up @@ -510,11 +516,6 @@ private int[] fillBlockSize(DatasetAttributes attributes) {
return tmp;
}

private SimpleCacheArrayLoader<?> createCacheArrayLoader(final N5Reader n5, final String pathName, int channel, int timepointId, CellGrid grid) throws IOException {
final DatasetAttributes attributes = n5.getDatasetAttributes(pathName);
return new N5OMEZarrCacheArrayLoader<>(n5, pathName, channel, timepointId, attributes, grid, zarrAxes);
}

private class SetupImgLoader<T extends NativeType<T>, V extends Volatile<T> & NativeType<V>>
extends AbstractViewerSetupImgLoader<T, V>
implements MultiResolutionSetupImgLoader<T> {
Expand Down Expand Up @@ -565,7 +566,7 @@ private double[][] readMipmapResolutions() throws IOException {

@Override
public RandomAccessibleInterval<V> getVolatileImage(final int timepointId, final int level, final ImgLoaderHint... hints) {
return prepareCachedImage(timepointId, level, LoadingStrategy.BUDGETED, volatileType);
return prepareCachedImage(timepointId, level, LoadingStrategy.VOLATILE, volatileType);
}

@Override
Expand Down Expand Up @@ -627,7 +628,7 @@ private <T extends NativeType<T>> RandomAccessibleInterval<T> prepareCachedImage
final int priority = numMipmapLevels() - 1 - level;
final CacheHints cacheHints = new CacheHints(loadingStrategy, priority, false);

final SimpleCacheArrayLoader<?> loader = createCacheArrayLoader(n5, pathName, setupToChannel.get(setupId), timepointId, grid);
final SimpleCacheArrayLoader<?> loader = new N5OMEZarrCacheArrayLoader<>(n5, pathName, setupToChannel.get(setupId), timepointId, attributes, grid, zarrAxes);
return cache.createImg(grid, timepointId, setupId, level, cacheHints, loader, type);
} catch (IOException e) {
log.error(String.format(
Expand Down