Skip to content

Commit c091e60

Browse files
committed
add core.Node
1 parent c9b6d5d commit c091e60

9 files changed

Lines changed: 116 additions & 146 deletions

File tree

.github/workflows/ci.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ jobs:
3636
- name: Install uv
3737
uses: astral-sh/setup-uv@v6
3838

39-
- name: Set up zarr-python
40-
run: |
41-
uv venv && uv init
42-
uv add zarr
43-
4439
- name: Download testdata
4540
run: |
4641
mkdir testoutput

src/main/java/dev/zarr/zarrjava/core/Array.java

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@
1414
import java.util.Arrays;
1515
import java.util.stream.Stream;
1616

17-
public interface Array {
17+
public abstract class Array extends Node {
1818

19-
ArrayMetadata metadata();
20-
21-
StoreHandle storeHandle();
19+
protected CodecPipeline codecPipeline;
20+
protected abstract ArrayMetadata metadata();
2221

23-
CodecPipeline codecPipeline();
22+
protected Array(StoreHandle storeHandle) throws ZarrException {
23+
super(storeHandle);
24+
}
2425

2526
/**
2627
* Writes a ucar.ma2.Array into the Zarr array at a specified offset. The shape of the Zarr array
2728
* needs be large enough for the write.
2829
*
29-
* @param offset the offset where to write the data
30-
* @param array the data to write
30+
* @param offset the offset where to write the data
31+
* @param array the data to write
3132
* @param parallel utilizes parallelism if true
3233
*/
33-
default void write(long[] offset, ucar.ma2.Array array, boolean parallel) {
34+
public void write(long[] offset, ucar.ma2.Array array, boolean parallel) {
3435
ArrayMetadata metadata = metadata();
3536
if (offset.length != metadata.ndim()) {
3637
throw new IllegalArgumentException("'offset' needs to have rank '" + metadata.ndim() + "'.");
@@ -82,19 +83,19 @@ default void write(long[] offset, ucar.ma2.Array array, boolean parallel) {
8283
*
8384
* @param chunkCoords The coordinates of the chunk as computed by the offset of the chunk divided
8485
* by the chunk shape.
85-
* @param chunkArray The data to write into the chunk
86+
* @param chunkArray The data to write into the chunk
8687
* @throws ZarrException throws ZarrException if the write fails
8788
*/
88-
default void writeChunk(long[] chunkCoords, ucar.ma2.Array chunkArray) throws ZarrException {
89+
public void writeChunk(long[] chunkCoords, ucar.ma2.Array chunkArray) throws ZarrException {
8990
ArrayMetadata metadata = metadata();
9091
String[] chunkKeys = metadata.chunkKeyEncoding().encodeChunkKey(chunkCoords);
91-
StoreHandle chunkHandle = storeHandle().resolve(chunkKeys);
92+
StoreHandle chunkHandle = storeHandle.resolve(chunkKeys);
9293
Object parsedFillValue = metadata.parsedFillValue();
9394

9495
if (parsedFillValue != null && MultiArrayUtils.allValuesEqual(chunkArray, parsedFillValue)) {
9596
chunkHandle.delete();
9697
} else {
97-
ByteBuffer chunkBytes = codecPipeline().encode(chunkArray);
98+
ByteBuffer chunkBytes = codecPipeline.encode(chunkArray);
9899
chunkHandle.set(chunkBytes);
99100
}
100101
}
@@ -108,22 +109,21 @@ default void writeChunk(long[] chunkCoords, ucar.ma2.Array chunkArray) throws Za
108109
* @throws ZarrException throws ZarrException if the requested chunk is outside the array's domain or if the read fails
109110
*/
110111
@Nonnull
111-
default ucar.ma2.Array readChunk(long[] chunkCoords)
112-
throws ZarrException {
112+
public ucar.ma2.Array readChunk(long[] chunkCoords) throws ZarrException {
113113
ArrayMetadata metadata = metadata();
114114
if (!chunkIsInArray(chunkCoords)) {
115115
throw new ZarrException("Attempting to read data outside of the array's domain.");
116116
}
117117

118118
final String[] chunkKeys = metadata.chunkKeyEncoding().encodeChunkKey(chunkCoords);
119-
final StoreHandle chunkHandle = storeHandle().resolve(chunkKeys);
119+
final StoreHandle chunkHandle = storeHandle.resolve(chunkKeys);
120120

121121
ByteBuffer chunkBytes = chunkHandle.read();
122122
if (chunkBytes == null) {
123123
return metadata.allocateFillValueChunk();
124124
}
125125

126-
return codecPipeline().decode(chunkBytes);
126+
return codecPipeline.decode(chunkBytes);
127127
}
128128

129129

@@ -134,7 +134,7 @@ default ucar.ma2.Array readChunk(long[] chunkCoords)
134134
*
135135
* @param array the data to write
136136
*/
137-
default void write(ucar.ma2.Array array) {
137+
public void write(ucar.ma2.Array array) {
138138
write(new long[metadata().ndim()], array);
139139
}
140140

@@ -144,20 +144,20 @@ default void write(ucar.ma2.Array array) {
144144
* Utilizes no parallelism.
145145
*
146146
* @param offset the offset where to write the data
147-
* @param array the data to write
147+
* @param array the data to write
148148
*/
149-
default void write(long[] offset, ucar.ma2.Array array) {
149+
public void write(long[] offset, ucar.ma2.Array array) {
150150
write(offset, array, false);
151151
}
152152

153153
/**
154154
* Writes a ucar.ma2.Array into the Zarr array at the beginning of the Zarr array. The shape of
155155
* the Zarr array needs be large enough for the write.
156156
*
157-
* @param array the data to write
157+
* @param array the data to write
158158
* @param parallel utilizes parallelism if true
159159
*/
160-
default void write(ucar.ma2.Array array, boolean parallel) {
160+
public void write(ucar.ma2.Array array, boolean parallel) {
161161
write(new long[metadata().ndim()], array, parallel);
162162
}
163163

@@ -168,7 +168,7 @@ default void write(ucar.ma2.Array array, boolean parallel) {
168168
* @throws ZarrException throws ZarrException if the read fails
169169
*/
170170
@Nonnull
171-
default ucar.ma2.Array read() throws ZarrException {
171+
public ucar.ma2.Array read() throws ZarrException {
172172
return read(new long[metadata().ndim()], Utils.toIntArray(metadata().shape()));
173173
}
174174

@@ -177,11 +177,11 @@ default ucar.ma2.Array read() throws ZarrException {
177177
* Utilizes no parallelism.
178178
*
179179
* @param offset the offset where to start reading
180-
* @param shape the shape of the data to read
180+
* @param shape the shape of the data to read
181181
* @throws ZarrException throws ZarrException if the requested data is outside the array's domain or if the read fails
182182
*/
183183
@Nonnull
184-
default ucar.ma2.Array read(final long[] offset, final int[] shape) throws ZarrException {
184+
public ucar.ma2.Array read(final long[] offset, final int[] shape) throws ZarrException {
185185
return read(offset, shape, false);
186186
}
187187

@@ -192,11 +192,11 @@ default ucar.ma2.Array read(final long[] offset, final int[] shape) throws ZarrE
192192
* @throws ZarrException throws ZarrException if the requested data is outside the array's domain or if the read fails
193193
*/
194194
@Nonnull
195-
default ucar.ma2.Array read(final boolean parallel) throws ZarrException {
195+
public ucar.ma2.Array read(final boolean parallel) throws ZarrException {
196196
return read(new long[metadata().ndim()], Utils.toIntArray(metadata().shape()), parallel);
197197
}
198198

199-
default boolean chunkIsInArray(long[] chunkCoords) {
199+
boolean chunkIsInArray(long[] chunkCoords) {
200200
final int[] chunkShape = metadata().chunkShape();
201201
for (int dimIdx = 0; dimIdx < metadata().ndim(); dimIdx++) {
202202
if (chunkCoords[dimIdx] < 0
@@ -210,15 +210,14 @@ default boolean chunkIsInArray(long[] chunkCoords) {
210210
/**
211211
* Reads a part of the Zarr array based on a requested offset and shape into an ucar.ma2.Array.
212212
*
213-
* @param offset the offset where to start reading
214-
* @param shape the shape of the data to read
213+
* @param offset the offset where to start reading
214+
* @param shape the shape of the data to read
215215
* @param parallel utilizes parallelism if true
216216
* @throws ZarrException throws ZarrException if the requested data is outside the array's domain or if the read fails
217217
*/
218218
@Nonnull
219-
default ucar.ma2.Array read(final long[] offset, final int[] shape, final boolean parallel) throws ZarrException {
219+
public ucar.ma2.Array read(final long[] offset, final int[] shape, final boolean parallel) throws ZarrException {
220220
ArrayMetadata metadata = metadata();
221-
CodecPipeline codecPipeline = codecPipeline();
222221
if (offset.length != metadata.ndim()) {
223222
throw new IllegalArgumentException("'offset' needs to have rank '" + metadata.ndim() + "'.");
224223
}
@@ -258,7 +257,7 @@ default ucar.ma2.Array read(final long[] offset, final int[] shape, final boolea
258257
}
259258

260259
final String[] chunkKeys = metadata.chunkKeyEncoding().encodeChunkKey(chunkCoords);
261-
final StoreHandle chunkHandle = storeHandle().resolve(chunkKeys);
260+
final StoreHandle chunkHandle = storeHandle.resolve(chunkKeys);
262261
if (!chunkHandle.exists()) {
263262
return;
264263
}
@@ -281,11 +280,11 @@ default ucar.ma2.Array read(final long[] offset, final int[] shape, final boolea
281280
return outputArray;
282281
}
283282

284-
default ArrayAccessor access() {
283+
public ArrayAccessor access() {
285284
return new ArrayAccessor(this);
286285
}
287286

288-
final class ArrayAccessor {
287+
public static final class ArrayAccessor {
289288
@Nullable
290289
long[] offset;
291290
@Nullable
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package dev.zarr.zarrjava.core;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
5+
import dev.zarr.zarrjava.store.StoreHandle;
6+
import dev.zarr.zarrjava.v3.codec.CodecRegistry;
7+
import javax.annotation.Nonnull;
8+
9+
public abstract class Node {
10+
11+
@Nonnull
12+
public final StoreHandle storeHandle;
13+
14+
protected Node(@Nonnull StoreHandle storeHandle) {
15+
this.storeHandle = storeHandle;
16+
}
17+
18+
public static ObjectMapper makeObjectMapper() {
19+
ObjectMapper objectMapper = new ObjectMapper();
20+
objectMapper.registerModule(new Jdk8Module());
21+
objectMapper.registerSubtypes(CodecRegistry.getNamedTypes());
22+
return objectMapper;
23+
}
24+
25+
}

src/main/java/dev/zarr/zarrjava/v2/Array.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717
import java.util.function.Function;
1818
import java.util.stream.Collectors;
1919

20-
public class Array implements dev.zarr.zarrjava.core.Array {
20+
public class Array extends dev.zarr.zarrjava.core.Array {
2121

2222
static final String ZARRAY = ".zarray";
2323
public ArrayMetadata metadata;
24-
public StoreHandle storeHandle;
25-
CodecPipeline codecPipeline;
24+
25+
protected ArrayMetadata metadata() {
26+
return metadata;
27+
}
2628

2729
protected Array(StoreHandle storeHandle, ArrayMetadata arrayMetadata) throws IOException, ZarrException {
28-
this.storeHandle = storeHandle;
30+
super(storeHandle);
2931
this.metadata = arrayMetadata;
3032
this.codecPipeline = new CodecPipeline(Utils.concatArrays(
3133
new Codec[]{},
@@ -127,20 +129,4 @@ public String toString() {
127129
metadata.dataType
128130
);
129131
}
130-
131-
@Override
132-
public ArrayMetadata metadata() {
133-
return metadata;
134-
}
135-
136-
@Override
137-
public StoreHandle storeHandle() {
138-
return storeHandle;
139-
}
140-
141-
@Override
142-
public CodecPipeline codecPipeline() {
143-
return codecPipeline;
144-
}
145-
146132
}

src/main/java/dev/zarr/zarrjava/v3/Array.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
import java.util.stream.Collectors;
1515
import javax.annotation.Nonnull;
1616

17-
public class Array extends Node implements dev.zarr.zarrjava.core.Array {
17+
public class Array extends dev.zarr.zarrjava.core.Array implements Node {
1818

1919
public ArrayMetadata metadata;
20-
CodecPipeline codecPipeline;
2120

22-
protected Array(StoreHandle storeHandle, ArrayMetadata arrayMetadata)
23-
throws ZarrException {
21+
protected ArrayMetadata metadata(){
22+
return metadata;
23+
}
24+
25+
protected Array(StoreHandle storeHandle, ArrayMetadata arrayMetadata) throws ZarrException {
2426
super(storeHandle);
2527
this.metadata = arrayMetadata;
2628
this.codecPipeline = new CodecPipeline(arrayMetadata.codecs, arrayMetadata.coreArrayMetadata);
@@ -113,21 +115,6 @@ public static ArrayMetadataBuilder metadataBuilder(ArrayMetadata existingMetadat
113115
return ArrayMetadataBuilder.fromArrayMetadata(existingMetadata);
114116
}
115117

116-
@Override
117-
public CodecPipeline codecPipeline() {
118-
return codecPipeline;
119-
}
120-
121-
122-
123-
124-
@Override
125-
public ArrayMetadata metadata() {
126-
return metadata;
127-
}
128-
129-
130-
131118
private Array writeMetadata(ArrayMetadata newArrayMetadata) throws ZarrException, IOException {
132119
ObjectMapper objectMapper = makeObjectMapper();
133120
ByteBuffer metadataBytes = ByteBuffer.wrap(objectMapper.writeValueAsBytes(newArrayMetadata));

src/main/java/dev/zarr/zarrjava/v3/Group.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import dev.zarr.zarrjava.ZarrException;
5+
import dev.zarr.zarrjava.core.Node;
56
import dev.zarr.zarrjava.store.StoreHandle;
67
import dev.zarr.zarrjava.utils.Utils;
78
import java.io.IOException;
@@ -13,6 +14,8 @@
1314
import javax.annotation.Nonnull;
1415
import javax.annotation.Nullable;
1516

17+
import static dev.zarr.zarrjava.v3.Node.ZARR_JSON;
18+
1619
public class Group extends Node {
1720

1821
public GroupMetadata metadata;
Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
11
package dev.zarr.zarrjava.v3;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
5-
import dev.zarr.zarrjava.store.StoreHandle;
6-
import dev.zarr.zarrjava.v3.codec.CodecRegistry;
7-
import javax.annotation.Nonnull;
8-
9-
public class Node {
10-
11-
public final static String ZARR_JSON = "zarr.json";
12-
13-
@Nonnull
14-
public final StoreHandle storeHandle;
15-
16-
protected Node(@Nonnull StoreHandle storeHandle) {
17-
this.storeHandle = storeHandle;
18-
}
19-
20-
public static ObjectMapper makeObjectMapper() {
21-
ObjectMapper objectMapper = new ObjectMapper();
22-
objectMapper.registerModule(new Jdk8Module());
23-
objectMapper.registerSubtypes(CodecRegistry.getNamedTypes());
24-
return objectMapper;
25-
}
26-
27-
public StoreHandle storeHandle() {
28-
return storeHandle;
29-
}
3+
public interface Node {
4+
String ZARR_JSON = "zarr.json";
305

316
}

0 commit comments

Comments
 (0)