Skip to content

Commit 54175d2

Browse files
committed
detect typesize for blosc with evolve_from_core_array_metadata
1 parent 72393a9 commit 54175d2

File tree

12 files changed

+94
-19
lines changed

12 files changed

+94
-19
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,19 @@ public ArrayMetadata(
6565
this.endianness = dataType.getEndianness();
6666
this.order = order;
6767
this.dimensionSeparator = dimensionSeparator;
68-
this.filters = filters;
69-
this.compressor = compressor;
7068
this.coreArrayMetadata =
71-
new ArrayMetadata.CoreArrayMetadata(shape, chunks,
69+
new CoreArrayMetadata(shape, chunks,
7270
this.dataType,
7371
this.parsedFillValue
7472
);
73+
if (filters == null) this.filters = null;
74+
else{
75+
this.filters = new Codec[filters.length];
76+
for(int i = 0; i < filters.length; i++) {
77+
this.filters[i] = filters[i].evolve_from_core_array_metadata(this.coreArrayMetadata);
78+
}
79+
}
80+
this.compressor = compressor == null ? null : compressor.evolve_from_core_array_metadata(this.coreArrayMetadata);
7581
}
7682

7783

@@ -90,7 +96,7 @@ public DataType dataType() {
9096

9197
@Override
9298
public Array allocateFillValueChunk() {
93-
ucar.ma2.Array outputArray = ucar.ma2.Array.factory(dataType.getMA2DataType(), chunks);
99+
Array outputArray = Array.factory(dataType.getMA2DataType(), chunks);
94100
if (parsedFillValue != null) MultiArrayUtils.fill(outputArray, parsedFillValue);
95101
return outputArray;
96102
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package dev.zarr.zarrjava.v2.codec;
22

33
import com.fasterxml.jackson.annotation.JsonTypeInfo;
4+
import dev.zarr.zarrjava.ZarrException;
5+
import dev.zarr.zarrjava.v2.ArrayMetadata;
46

57
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "id")
6-
public interface Codec extends dev.zarr.zarrjava.core.codec.Codec {}
8+
public interface Codec extends dev.zarr.zarrjava.core.codec.Codec {
9+
Codec evolve_from_core_array_metadata(ArrayMetadata.CoreArrayMetadata arrayMetadata) throws ZarrException;
10+
}
711

src/main/java/dev/zarr/zarrjava/v2/codec/core/BloscCodec.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
1313
import com.scalableminds.bloscjava.Blosc;
1414
import dev.zarr.zarrjava.ZarrException;
15+
import dev.zarr.zarrjava.core.ArrayMetadata;
1516
import dev.zarr.zarrjava.utils.Utils;
1617
import dev.zarr.zarrjava.v2.codec.Codec;
1718

@@ -44,9 +45,6 @@ public BloscCodec(
4445
@JsonProperty(value = "typesize", defaultValue = "0") int typesize,
4546
@JsonProperty(value = "blocksize", defaultValue = "0") int blocksize
4647
) throws ZarrException {
47-
if (typesize < 1 && shuffle != Blosc.Shuffle.NO_SHUFFLE) {
48-
typesize = 4; //todo: in v2 typesize is not a required parameter. default to correct value based on dtype
49-
}
5048
if (clevel < 0 || clevel > 9) {
5149
throw new ZarrException("'clevel' needs to be between 0 and 9.");
5250
}
@@ -57,7 +55,6 @@ public BloscCodec(
5755
this.blocksize = blocksize;
5856
}
5957

60-
6158
@Override
6259
public ByteBuffer encode(ByteBuffer chunkBytes)
6360
throws ZarrException {
@@ -72,6 +69,20 @@ public ByteBuffer encode(ByteBuffer chunkBytes)
7269
}
7370
}
7471

72+
@Override
73+
public BloscCodec evolve_from_core_array_metadata(ArrayMetadata.CoreArrayMetadata arrayMetadata) throws ZarrException {
74+
if (typesize == 0) {
75+
return new BloscCodec(
76+
this.cname,
77+
this.shuffle,
78+
this.clevel,
79+
arrayMetadata.dataType.getByteCount(),
80+
this.blocksize
81+
);
82+
}
83+
return this;
84+
}
85+
7586
public static final class CustomShuffleSerializer extends StdSerializer<Blosc.Shuffle> {
7687

7788
public CustomShuffleSerializer() {

src/main/java/dev/zarr/zarrjava/v2/codec/core/BytesCodec.java

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

33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import dev.zarr.zarrjava.ZarrException;
6+
import dev.zarr.zarrjava.core.ArrayMetadata;
57
import dev.zarr.zarrjava.v2.codec.Codec;
68

79
import javax.annotation.Nonnull;
@@ -22,5 +24,10 @@ public BytesCodec(
2224
protected ByteOrder getByteOrder() {
2325
return endian.getByteOrder();
2426
}
27+
28+
@Override
29+
public Codec evolve_from_core_array_metadata(ArrayMetadata.CoreArrayMetadata arrayMetadata) throws ZarrException {
30+
return this;
31+
}
2532
}
2633

src/main/java/dev/zarr/zarrjava/v2/codec/core/ZlibCodec.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonProperty;
55
import dev.zarr.zarrjava.ZarrException;
6+
import dev.zarr.zarrjava.core.ArrayMetadata;
67
import dev.zarr.zarrjava.utils.Utils;
78
import dev.zarr.zarrjava.v2.codec.Codec;
89
import dev.zarr.zarrjava.core.codec.BytesBytesCodec;
@@ -52,4 +53,9 @@ public ByteBuffer encode(ByteBuffer chunkBytes) throws ZarrException {
5253
throw new ZarrException("Error in encoding zlib.", ex);
5354
}
5455
}
56+
57+
@Override
58+
public Codec evolve_from_core_array_metadata(ArrayMetadata.CoreArrayMetadata arrayMetadata) throws ZarrException {
59+
return this;
60+
}
5561
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ private Array writeMetadata(ArrayMetadata newArrayMetadata) throws ZarrException
193193
* Sets a new shape for the Zarr array. It only changes the metadata, no array data is modified or
194194
* deleted. This method returns a new instance of the Zarr array class and the old instance
195195
* becomes invalid.
196-
* TODO: test
197196
*
198197
* @param newShape the new shape of the Zarr array
199198
* @throws ZarrException if the new metadata is invalid
@@ -232,7 +231,6 @@ public Array setAttributes(Map<String, Object> newAttributes) throws ZarrExcepti
232231
* attributes as input and needs to return the new set of attributes. The attributes in the
233232
* callback may be mutated. This method overwrites and removes any existing attributes. This
234233
* method returns a new instance of the Zarr array class and the old instance becomes invalid.
235-
* TODO: test
236234
*
237235
* @param attributeMapper the callback that is used to construct the new attributes
238236
* @throws ZarrException throws ZarrException if the new metadata is invalid

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,19 @@ public ArrayMetadata(
113113
shardingCodec = getShardingIndexedCodec(shardingConfig.codecs);
114114
}
115115
}
116-
this.dataType = dataType;
117116
this.chunkGrid = chunkGrid;
117+
this.dataType = dataType;
118+
this.coreArrayMetadata =
119+
new CoreArrayMetadata(this.shape, ((RegularChunkGrid) chunkGrid).configuration.chunkShape,
120+
this.dataType,
121+
this.parsedFillValue
122+
);
123+
118124
this.chunkKeyEncoding = chunkKeyEncoding;
119125
this.codecs = codecs;
120126
this.dimensionNames = dimensionNames;
121127
this.attributes = attributes;
122128
this.storageTransformers = storageTransformers;
123-
this.coreArrayMetadata =
124-
new CoreArrayMetadata(shape, ((RegularChunkGrid) chunkGrid).configuration.chunkShape,
125-
this.dataType,
126-
this.parsedFillValue
127-
);
128129
}
129130

130131

src/test/java/dev/zarr/zarrjava/ZarrV2Test.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
public class ZarrV2Test extends ZarrTest {
2323
@ParameterizedTest
2424
@CsvSource({"blosclz,noshuffle,0", "lz4,shuffle,6", "lz4hc,bitshuffle,3", "zlib,shuffle,5", "zstd,bitshuffle,9"})
25-
public void testV2createBlosc(String cname, String shuffle, int clevel) throws IOException, ZarrException {
25+
public void testCreateBlosc(String cname, String shuffle, int clevel) throws IOException, ZarrException {
2626
Array array = Array.create(
2727
new FilesystemStore(TESTOUTPUT).resolve("v2_create_blosc", cname + "_" + shuffle + "_" + clevel),
2828
Array.metadataBuilder()
@@ -40,6 +40,20 @@ public void testV2createBlosc(String cname, String shuffle, int clevel) throws I
4040
Assertions.assertEquals(0, outArray.getByte(0));
4141
}
4242

43+
44+
45+
@ParameterizedTest
46+
@CsvSource({
47+
"BOOL", "FLOAT64"
48+
})
49+
public void testReadBloscDetectTypesize(DataType dt) throws IOException, ZarrException {
50+
String arrayname = dt == DataType.BOOL ? "bool" : "double";
51+
StoreHandle storeHandle = new FilesystemStore(TESTDATA).resolve("v2_sample", arrayname);
52+
Array array = Array.open(storeHandle);
53+
ucar.ma2.Array output = array.read(new long[]{0, 0, 0}, new int[]{3, 4, 5});
54+
Assertions.assertEquals(dt, array.metadata.dataType);
55+
}
56+
4357
@Test
4458
public void testCreate() throws IOException, ZarrException {
4559
DataType dataType = DataType.UINT32;

src/test/java/dev/zarr/zarrjava/ZarrV3Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public void testGroup() throws IOException, ZarrException {
527527
Assertions.assertArrayEquals(new int[]{5, 5}, ((Array) ((Group) group.listAsArray()[0]).listAsArray()[0]).metadata.chunkShape());
528528
}
529529

530-
@Test
530+
@Test
531531
public void testCreateArray() throws ZarrException, IOException {
532532
StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("testCreateArrayV3");
533533
Path storeHandlePath = TESTOUTPUT.resolve("testCreateArrayV3Path");

testdata/v2_sample/double/.zarray

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"shape": [
3+
16,
4+
16,
5+
16
6+
],
7+
"chunks": [
8+
2,
9+
4,
10+
8
11+
],
12+
"dtype": "<f8",
13+
"fill_value": 0.0,
14+
"order": "C",
15+
"filters": null,
16+
"dimension_separator": ".",
17+
"compressor": {
18+
"id": "blosc",
19+
"cname": "blosclz",
20+
"clevel": 3,
21+
"shuffle": 1,
22+
"blocksize": 0
23+
},
24+
"zarr_format": 2
25+
}

0 commit comments

Comments
 (0)