Skip to content

Commit 4ae365b

Browse files
committed
fix: N5FactoryWithCache override should be latest possible openRead/Writer method. normalizeUri only for cache keys, not for creating the reader/writer
Signed-off-by: Caleb Hulbert <cmhulbert@gmail.com>
1 parent 34ec536 commit 4ae365b

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

src/main/java/org/janelia/saalfeldlab/n5/universe/N5FactoryWithCache.java

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
import com.google.gson.JsonElement;
44
import net.imglib2.util.Pair;
5-
import org.janelia.saalfeldlab.n5.CachedGsonKeyValueN5Reader;
6-
import org.janelia.saalfeldlab.n5.N5KeyValueReader;
7-
import org.janelia.saalfeldlab.n5.N5Reader;
8-
import org.janelia.saalfeldlab.n5.N5Writer;
5+
import org.janelia.saalfeldlab.n5.*;
96
import org.janelia.saalfeldlab.n5.hdf5.N5HDF5Reader;
107
import org.janelia.saalfeldlab.n5.zarr.ZarrKeyValueReader;
118
import org.janelia.saalfeldlab.n5.zarr.v3.ZarrV3KeyValueReader;
9+
import org.jspecify.annotations.Nullable;
1210

1311
import java.net.URI;
1412
import java.nio.file.Paths;
@@ -20,36 +18,29 @@ public class N5FactoryWithCache extends N5Factory {
2018
private final HashMap<URI, N5Reader> readerCache = new HashMap<>();
2119
private final HashMap<URI, N5Writer> writerCache = new HashMap<>();
2220

23-
@Override public N5Reader openReader(StorageFormat format, URI uri) {
21+
@Override
22+
public N5Reader openReader(StorageFormat storage, KeyValueAccess access, URI location) {
2423

25-
final URI normalUri = normalizeUri(uri);
26-
final N5Reader reader = getReaderFromCache(format, normalUri);
24+
final N5Reader reader = getReaderFromCache(storage, location);
2725
if (reader != null)
2826
return reader;
29-
return openAndCacheReader(format, normalUri);
30-
}
31-
32-
@Override public N5Writer openWriter(StorageFormat format, URI uri) {
33-
34-
final URI normalUri = normalizeUri(uri);
35-
final N5Writer writer = getWriterFromCache(format, normalUri);
36-
if (writer != null)
37-
return writer;
38-
return openAndCacheWriter(format, normalUri);
27+
return openAndCacheReader(storage, access, location);
3928
}
4029

41-
private N5Reader openAndCacheReader(StorageFormat storageFormat, URI uri) {
30+
private N5Reader openAndCacheReader(StorageFormat storageFormat, KeyValueAccess access, URI uri) {
4231

43-
final N5Reader reader = super.openReader(storageFormat, uri);
32+
final N5Reader reader = super.openReader(storageFormat, access, uri);
4433
if (reader == null)
4534
return null;
4635

47-
readerCache.put(uri, reader);
36+
URI normalUri = normalizeUri(uri);
37+
readerCache.put(normalUri, reader);
4838
return reader;
4939
}
5040

51-
protected synchronized N5Reader getReaderFromCache(StorageFormat format, URI uri) {
41+
protected synchronized N5Reader getReaderFromCache(StorageFormat format, URI location) {
5242

43+
final URI uri = normalizeUri(location);
5344
final N5Reader reader = readerCache.get(uri);
5445
if (reader == null)
5546
return null;
@@ -62,6 +53,15 @@ protected synchronized N5Reader getReaderFromCache(StorageFormat format, URI uri
6253
return reader;
6354
}
6455

56+
@Override
57+
public N5Writer openWriter(StorageFormat storage, KeyValueAccess access, URI location) {
58+
59+
final N5Writer writer = getWriterFromCache(storage, location);
60+
if (writer != null)
61+
return writer;
62+
return openAndCacheWriter(storage, access, location);
63+
}
64+
6565
private boolean canRead(N5Reader reader) {
6666

6767
try {
@@ -76,25 +76,26 @@ private boolean canRead(N5Reader reader) {
7676
}
7777
}
7878

79-
private N5Writer openAndCacheWriter(StorageFormat storageFormat, URI uri) {
79+
private N5Writer openAndCacheWriter(StorageFormat storageFormat, KeyValueAccess access, URI uri) {
8080

81-
final N5Writer writer = super.openWriter(storageFormat, uri);
81+
final N5Writer writer = super.openWriter(storageFormat, access, uri);
8282
if (writer == null)
8383
return null;
8484

85-
writerCache.put(uri, writer);
85+
URI normalUri = normalizeUri(uri);
86+
writerCache.put(normalUri, writer);
8687
return writer;
8788
}
8889

8990
protected synchronized N5Writer getWriterFromCache(StorageFormat format, URI uri) {
9091

91-
92-
final N5Writer writer = writerCache.get(uri);
92+
URI normalUri = normalizeUri(uri);
93+
final N5Writer writer = writerCache.get(normalUri);
9394
if (writer == null)
9495
return null;
9596

9697
if (!n5MatchesFormat(writer, format) || !canWrite(writer)) {
97-
writerCache.remove(uri);
98+
writerCache.remove(normalUri);
9899
return null;
99100
}
100101

@@ -136,8 +137,10 @@ private boolean n5MatchesFormat(N5Reader reader, StorageFormat format) {
136137
return reader instanceof N5KeyValueReader && !(reader instanceof ZarrV3KeyValueReader);
137138
case ZARR2:
138139
return reader instanceof ZarrKeyValueReader;
139-
case ZARR:
140+
case ZARR3:
140141
return reader instanceof ZarrV3KeyValueReader;
142+
case ZARR:
143+
return reader instanceof ZarrKeyValueReader || reader instanceof ZarrV3KeyValueReader;
141144
case HDF5:
142145
return reader instanceof N5HDF5Reader;
143146
default:

src/test/java/org/janelia/saalfeldlab/n5/universe/N5FactoryTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,16 +389,16 @@ public void testCachedFactoryKeys() throws IOException {
389389
final N5Writer writer2 = cachedFactory.openWriter(tmpPath);
390390
assertSame(writer2, writer1);
391391

392-
final N5Writer writerFromStoragePrefix = cachedFactory.openWriter("zarr:" + tmpPath);
392+
final N5Writer writerFromStoragePrefix = cachedFactory.openWriter("zarr3:" + tmpPath);
393393
assertSame(writerFromStoragePrefix, writer1);
394394

395-
final N5Writer writerFromStoragePrefix2 = cachedFactory.openWriter("zarr://" + tmpPath);
395+
final N5Writer writerFromStoragePrefix2 = cachedFactory.openWriter("zarr3://" + tmpPath);
396396
assertSame(writerFromStoragePrefix2, writer1);
397397

398398
final N5Writer writerDifferentStorageFormat = cachedFactory.openWriter(StorageFormat.N5, tmpPath);
399399
assertNotSame(writerDifferentStorageFormat, writer1);
400400

401-
final N5Writer writerFromStorageType = cachedFactory.openWriter(StorageFormat.ZARR2, tmpPath);
401+
final N5Writer writerFromStorageType = cachedFactory.openWriter(StorageFormat.ZARR3, tmpPath);
402402
assertNotSame(writerFromStorageType, writerDifferentStorageFormat);
403403
assertNotSame(writerFromStorageType, writer1);
404404

@@ -410,16 +410,16 @@ public void testCachedFactoryKeys() throws IOException {
410410
final N5Reader reader2 = cachedFactory.openReader(tmpPath);
411411
assertSame(reader2, reader1);
412412

413-
final N5Reader readerFromStoragePrefix = cachedFactory.openReader("zarr:" + tmpPath);
413+
final N5Reader readerFromStoragePrefix = cachedFactory.openReader("zarr3:" + tmpPath);
414414
assertSame(readerFromStoragePrefix, reader1);
415415

416-
final N5Reader readerFromStoragePrefix2 = cachedFactory.openReader("zarr://" + tmpPath);
416+
final N5Reader readerFromStoragePrefix2 = cachedFactory.openReader("zarr3://" + tmpPath);
417417
assertSame(readerFromStoragePrefix2, reader1);
418418

419419
final N5Reader readerDifferentStorageFormat = cachedFactory.openReader(StorageFormat.N5, tmpPath);
420420
assertNotSame(readerDifferentStorageFormat, reader1);
421421

422-
final N5Reader readerFromStorageType = cachedFactory.openReader(StorageFormat.ZARR2, tmpPath);
422+
final N5Reader readerFromStorageType = cachedFactory.openReader(StorageFormat.ZARR3, tmpPath);
423423
assertNotSame(readerFromStorageType, readerDifferentStorageFormat);
424424
assertNotSame(readerFromStorageType, reader1);
425425

0 commit comments

Comments
 (0)