Skip to content

Commit f1f35f1

Browse files
committed
Some refactoring
1 parent 02a78bc commit f1f35f1

File tree

14 files changed

+206
-148
lines changed

14 files changed

+206
-148
lines changed

src/main/java/com/glencoesoftware/omero/zarr/ZarrPixelBuffer.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.nio.BufferOverflowException;
2424
import java.nio.ByteBuffer;
2525
import java.nio.ByteOrder;
26-
import java.nio.file.Path;
2726
import java.util.ArrayList;
2827
import java.util.Arrays;
2928
import java.util.HashMap;
@@ -34,13 +33,10 @@
3433

3534
import org.slf4j.LoggerFactory;
3635

37-
import com.bc.zarr.DataType;
38-
import com.bc.zarr.ZarrArray;
3936
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
4037
import com.github.benmanes.caffeine.cache.Caffeine;
41-
import com.glencoesoftware.omero.zarr.model.ZArray;
42-
import com.glencoesoftware.omero.zarr.model.ZArrayv2;
43-
import com.glencoesoftware.omero.zarr.model.ZarrPath;
38+
import com.glencoesoftware.omero.zarr.compat.ZArray;
39+
import com.glencoesoftware.omero.zarr.compat.ZarrPath;
4440

4541
import loci.formats.FormatTools;
4642
import ome.io.nio.DimensionsOutOfBoundsException;
@@ -262,7 +258,7 @@ public int[][] getChunks() throws IOException {
262258
List<int[]> chunks = new ArrayList<int[]>();
263259
for (Map<String, String> dataset : datasets) {
264260
ZarrPath dsPath = root.resolve(dataset.get("path"));
265-
ZArray resolutionArray = new ZArrayv2(ZarrArray.open((Path)dsPath.getPath()));
261+
ZArray resolutionArray = dsPath.getArray(); //new ZArrayv2(ZarrArray.open((Path)dsPath.getPath()));
266262
int[] shape = resolutionArray.getChunks();
267263
chunks.add(shape);
268264
}
@@ -798,9 +794,8 @@ public void setResolutionLevel(int resolutionLevel) {
798794
zIndexMap.clear();
799795
}
800796
try {
801-
array = zarrArrayCache.get(
802-
root.resolve(Integer.toString(this.resolutionLevel))).get();
803-
797+
ZarrPath p = root.resolve(Integer.toString(this.resolutionLevel));
798+
array = zarrArrayCache.get(p).get();
804799
ZArray fullResolutionArray = zarrArrayCache.get(
805800
root.resolve("0")).get();
806801

src/main/java/com/glencoesoftware/omero/zarr/ZarrPixelsService.java

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,11 @@
3636
import com.google.common.base.Splitter;
3737
import com.upplication.s3fs.OmeroS3FilesystemProvider;
3838

39-
import dev.zarr.zarrjava.ZarrException;
40-
import dev.zarr.zarrjava.store.StoreHandle;
41-
import dev.zarr.zarrjava.v3.Array;
42-
import dev.zarr.zarrjava.v3.Group;
43-
44-
import com.bc.zarr.ZarrArray;
45-
import com.bc.zarr.ZarrGroup;
4639
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
4740
import com.github.benmanes.caffeine.cache.Caffeine;
48-
import com.glencoesoftware.omero.zarr.model.ZArray;
49-
import com.glencoesoftware.omero.zarr.model.ZArrayv2;
50-
import com.glencoesoftware.omero.zarr.model.ZArrayv3;
51-
import com.glencoesoftware.omero.zarr.model.ZarrInfo;
52-
import com.glencoesoftware.omero.zarr.model.ZarrPath;
53-
import com.glencoesoftware.omero.zarr.model.ZarrPathv2;
41+
import com.glencoesoftware.omero.zarr.compat.ZArray;
42+
import com.glencoesoftware.omero.zarr.compat.ZarrInfo;
43+
import com.glencoesoftware.omero.zarr.compat.ZarrPath;
5444

5545
import ome.api.IQuery;
5646
import ome.conditions.LockTimeout;
@@ -131,14 +121,7 @@ public static Map<String, Object> getZarrMetadata(ZarrPath path)
131121
// FIXME: Really should be ZarrUtils.readAttributes() to allow for
132122
// attribute retrieval from either a ZarrArray or ZarrGroup but ZarrPath
133123
// is package private at the moment.
134-
if (path.getVersion().equals(ZarrInfo.ZARR_V2)) {
135-
return ZarrGroup.open((Path)path.getPath()).getAttributes();
136-
} else if (path.getVersion().equals(ZarrInfo.ZARR_V3)) {
137-
StoreHandle sh = ((StoreHandle)path.getPath());
138-
return Group.open(sh).metadata.attributes;
139-
} else {
140-
throw new RuntimeException("Unsupported Zarr version: " + path.getVersion());
141-
}
124+
return path.getMetadata();
142125
}
143126

144127
/**
@@ -148,20 +131,7 @@ public static Map<String, Object> getZarrMetadata(ZarrPath path)
148131
* @throws IOException
149132
*/
150133
public static ZArray getZarrArray(ZarrPath path) throws IOException {
151-
if (path.getVersion().equals(ZarrInfo.ZARR_V2)) {
152-
return new ZArrayv2(ZarrArray.open((Path)path.getPath()));
153-
} else if (path.getVersion().equals(ZarrInfo.ZARR_V3)) {
154-
StoreHandle sh = ((StoreHandle)path.getPath());
155-
Array array;
156-
try {
157-
array = (Array) Group.open(sh).get();
158-
} catch (ZarrException e) {
159-
throw new IOException(e);
160-
}
161-
return new ZArrayv3(array);
162-
} else {
163-
throw new RuntimeException("Unsupported Zarr version: " + path.getVersion());
164-
}
134+
return path.getArray();
165135
}
166136

167137
/**
@@ -337,7 +307,8 @@ public ZarrPixelBuffer getLabelImagePixelBuffer(Mask mask)
337307
throw new IllegalArgumentException(
338308
"No root for Mask:" + mask.getId());
339309
}
340-
ZarrPath zarrPath = new ZarrPathv2(asPath(root));
310+
ZarrInfo zarrInfo = new ZarrInfo(root);
311+
ZarrPath zarrPath = zarrInfo.getZarrPath();
341312
return new ZarrPixelBuffer(
342313
pixels, zarrPath, maxPlaneWidth, maxPlaneHeight,
343314
zarrMetadataCache, zarrArrayCache);
@@ -365,7 +336,7 @@ protected ZarrPixelBuffer createOmeNgffPixelBuffer(Pixels pixels) {
365336
return null;
366337
}
367338
ZarrInfo zarrInfo = new ZarrInfo(uri);
368-
log.info("OME-NGFF root is: " + uri);
339+
log.info("OME-NGFF root is: " + zarrInfo);
369340
try {
370341
ZarrPixelBuffer v = new ZarrPixelBuffer(
371342
pixels, zarrInfo.getZarrPath(), maxPlaneWidth, maxPlaneHeight,

src/main/java/com/glencoesoftware/omero/zarr/model/ZArray.java renamed to src/main/java/com/glencoesoftware/omero/zarr/compat/ZArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.glencoesoftware.omero.zarr.model;
1+
package com.glencoesoftware.omero.zarr.compat;
22

33
import java.io.IOException;
44

src/main/java/com/glencoesoftware/omero/zarr/model/ZArrayv2.java renamed to src/main/java/com/glencoesoftware/omero/zarr/compat/ZArrayv2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.glencoesoftware.omero.zarr.model;
1+
package com.glencoesoftware.omero.zarr.compat;
22

33
import java.io.IOException;
44

@@ -8,7 +8,7 @@
88
import loci.formats.FormatTools;
99
import ucar.ma2.InvalidRangeException;
1010

11-
public class ZArrayv2 implements ZArray {
11+
class ZArrayv2 implements ZArray {
1212

1313
private ZarrArray array;
1414

src/main/java/com/glencoesoftware/omero/zarr/model/ZArrayv3.java renamed to src/main/java/com/glencoesoftware/omero/zarr/compat/ZArrayv3.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.glencoesoftware.omero.zarr.model;
1+
package com.glencoesoftware.omero.zarr.compat;
22

33
import java.io.IOException;
44
import java.nio.ByteBuffer;
@@ -9,7 +9,7 @@
99
import loci.formats.FormatTools;
1010
import ucar.ma2.InvalidRangeException;
1111

12-
public class ZArrayv3 implements ZArray {
12+
class ZArrayv3 implements ZArray {
1313

1414
private Array array;
1515

@@ -38,7 +38,11 @@ public int[] getChunks() {
3838
@Override
3939
public void read(byte[] buffer, int[] shape, int[] offset) throws IOException, InvalidRangeException {
4040
try {
41-
ByteBuffer b = array.read(null, shape).getDataAsByteBuffer();
41+
long[] offsetLong = new long[offset.length];
42+
for (int i = 0; i < offset.length; i++) {
43+
offsetLong[i] = offset[i];
44+
}
45+
ByteBuffer b = array.read(offsetLong, shape).getDataAsByteBuffer();
4246
System.arraycopy(b.array(), 0, buffer, 0, buffer.length);
4347
} catch (ZarrException e) {
4448
throw new IOException(e);

src/main/java/com/glencoesoftware/omero/zarr/model/ZarrInfo.java renamed to src/main/java/com/glencoesoftware/omero/zarr/compat/ZarrInfo.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.glencoesoftware.omero.zarr.model;
1+
package com.glencoesoftware.omero.zarr.compat;
22

33
import java.io.IOException;
44
import java.net.URI;
@@ -29,9 +29,10 @@
2929

3030

3131
/**
32-
* To access the zarr data use:
33-
* For zarr version 2, resp NGFF 0.4: asPath()
34-
* For zarr version 3, resp NGFF > 0.5: asStoreHandle()
32+
* Tries to determine some properties of the zarr path,
33+
* if it's remote or local, and the zarr and ngff versions.
34+
*
35+
* To access the zarr metadata/array use getZarrPath().
3536
*/
3637
public class ZarrInfo {
3738
private static final org.slf4j.Logger log =
@@ -40,6 +41,7 @@ public class ZarrInfo {
4041
public static final ComparableVersion ZARR_V2 = new ComparableVersion("2");
4142
public static final ComparableVersion ZARR_V3 = new ComparableVersion("3");
4243
public static final ComparableVersion NGFF_V0_4 = new ComparableVersion("0.4");
44+
public static final ComparableVersion NGFF_V0_5 = new ComparableVersion("0.5");
4345

4446
private ComparableVersion zarrVersion;
4547

@@ -52,10 +54,6 @@ public class ZarrInfo {
5254
public ZarrInfo(String location) {
5355
this.location = location.endsWith("/") ? location.substring(0, location.length() - 1) : location;
5456
checkProperties();
55-
log.info("Initialized ZarrPath: " + location);
56-
log.info("Remote store: " + remote);
57-
log.info("Zarr version: " + zarrVersion);
58-
log.info("NGFF version: " + ngffVersion);
5957
}
6058

6159
/**
@@ -143,6 +141,9 @@ public ZarrPath getZarrPath() throws IOException {
143141
}
144142
}
145143

144+
private Path asPath() throws IOException {
145+
return asPath(location);
146+
}
146147
/**
147148
* Converts an NGFF root string to a path, initializing a {@link FileSystem}
148149
* if required
@@ -151,14 +152,7 @@ public ZarrPath getZarrPath() throws IOException {
151152
* directory has not been specified in configuration.
152153
* @throws IOException
153154
*/
154-
public Path asPath() throws IOException {
155-
if (location.isEmpty()) {
156-
return null;
157-
}
158-
return asPath(location);
159-
}
160-
161-
public static Path asPath(String location) throws IOException {
155+
private Path asPath(String location) throws IOException {
162156
try {
163157
URI uri = new URI(location);
164158
if ("s3".equals(uri.getScheme())) {
@@ -209,16 +203,12 @@ public boolean isRemote() {
209203
return remote;
210204
}
211205

212-
public StoreHandle asStoreHandle() {
213-
return asStoreHandle(location);
214-
}
215-
216206
/**
217207
* Return a store handle.
218208
* For zarr version 3, resp NGFF > 0.5
219209
* @return
220210
*/
221-
public static StoreHandle asStoreHandle(String location) {
211+
private StoreHandle asStoreHandle() {
222212
//TODO: Properly parse the URI to get store/endpoint, bucket, etc.
223213

224214
if (location.toLowerCase().startsWith("http://") || location.toLowerCase().startsWith("https://")) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.glencoesoftware.omero.zarr.compat;
2+
3+
import java.io.IOException;
4+
import java.util.Map;
5+
6+
import org.apache.maven.artifact.versioning.ComparableVersion;
7+
8+
public interface ZarrPath {
9+
10+
public ZarrPath resolve(String path);
11+
12+
public ComparableVersion getVersion();
13+
14+
public Map<String, Object> getMetadata() throws IOException;
15+
16+
public ZArray getArray() throws IOException;
17+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.glencoesoftware.omero.zarr.compat;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.util.Map;
6+
7+
import org.apache.maven.artifact.versioning.ComparableVersion;
8+
9+
import com.bc.zarr.ZarrArray;
10+
import com.bc.zarr.ZarrGroup;
11+
12+
class ZarrPathv2 implements ZarrPath {
13+
14+
private Path path;
15+
16+
public ZarrPathv2(Path path) {
17+
this.path = path;
18+
}
19+
20+
@Override
21+
public ZarrPath resolve(String path) {
22+
return new ZarrPathv2(this.path.resolve(path));
23+
}
24+
25+
@Override
26+
public ComparableVersion getVersion() {
27+
return ZarrInfo.ZARR_V2;
28+
}
29+
30+
@Override
31+
public Map<String, Object> getMetadata() throws IOException {
32+
return ZarrGroup.open(path).getAttributes();
33+
}
34+
35+
@Override
36+
public ZArray getArray() throws IOException {
37+
return new ZArrayv2(ZarrArray.open(path));
38+
}
39+
40+
@Override
41+
public boolean equals(Object obj) {
42+
if (this == obj) {
43+
return true;
44+
}
45+
if (obj == null || getClass() != obj.getClass()) {
46+
return false;
47+
}
48+
ZarrPathv2 that = (ZarrPathv2) obj;
49+
return path.toString().equals(that.path.toString());
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return path.toString().hashCode();
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return path.toString();
60+
}
61+
}

0 commit comments

Comments
 (0)