22
33import com .google .gson .JsonElement ;
44import 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 .*;
96import org .janelia .saalfeldlab .n5 .hdf5 .N5HDF5Reader ;
107import org .janelia .saalfeldlab .n5 .zarr .ZarrKeyValueReader ;
118import org .janelia .saalfeldlab .n5 .zarr .v3 .ZarrV3KeyValueReader ;
9+ import org .jspecify .annotations .Nullable ;
1210
1311import java .net .URI ;
1412import 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 :
0 commit comments