Skip to content

Commit e5222ce

Browse files
committed
allow fillValue null
1 parent 86e8f6c commit e5222ce

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ default void writeChunk(long[] chunkCoords, ucar.ma2.Array chunkArray) throws Za
8585
ArrayMetadata metadata = metadata();
8686
String[] chunkKeys = metadata.chunkKeyEncoding().encodeChunkKey(chunkCoords);
8787
StoreHandle chunkHandle = storeHandle().resolve(chunkKeys);
88+
Object parsedFillValue = metadata.parsedFillValue();
8889

89-
if (MultiArrayUtils.allValuesEqual(chunkArray, metadata.parsedFillValue())) {
90+
if (parsedFillValue != null && MultiArrayUtils.allValuesEqual(chunkArray, parsedFillValue)) {
9091
chunkHandle.delete();
9192
} else {
9293
ByteBuffer chunkBytes = codecPipeline().encode(chunkArray);

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class ArrayMetadata implements dev.zarr.zarrjava.core.ArrayMetadata {
4242
@JsonIgnore
4343
public final Object parsedFillValue;
4444

45+
@Nullable
4546
public Codec[] filters;
4647
@Nullable
4748
public Codec compressor;
@@ -56,11 +57,11 @@ public ArrayMetadata(
5657
@JsonProperty(value = "shape", required = true) long[] shape,
5758
@JsonProperty(value = "chunks", required = true) int[] chunks,
5859
@JsonProperty(value = "dtype", required = true) DataType dataType,
59-
@Nullable @JsonProperty(value = "fill_value", required = true) Object fillValue, //todo test when null
60+
@Nullable @JsonProperty(value = "fill_value", required = true) Object fillValue,
6061
@JsonProperty(value = "order", required = true) Order order,
61-
@Nullable @JsonProperty(value = "dimension_separator") Separator dimensionSeparator,
62-
@Nullable @JsonProperty(value = "filters") Codec[] filters,
63-
@Nullable @JsonProperty(value = "compressor") Codec compressor
62+
@Nullable @JsonProperty(value = "filters", required = true) Codec[] filters,
63+
@Nullable @JsonProperty(value = "compressor", required = true) Codec compressor,
64+
@Nullable @JsonProperty(value = "dimension_separator") Separator dimensionSeparator
6465
) throws ZarrException {
6566
super();
6667
if (zarrFormat != this.zarrFormat) {
@@ -72,7 +73,11 @@ public ArrayMetadata(
7273
this.dataType = dataType;
7374
this.endianness = dataType.getEndianness();
7475
this.fillValue = fillValue;
75-
this.parsedFillValue = parseFillValue(fillValue, this.dataType);
76+
if (fillValue == null) {
77+
this.parsedFillValue = null;
78+
} else {
79+
this.parsedFillValue = parseFillValue(fillValue, this.dataType);
80+
}
7681
this.order = order;
7782
this.dimensionSeparator = dimensionSeparator;
7883
this.filters = filters;
@@ -107,7 +112,7 @@ public DataType dataType() {
107112
@Override
108113
public Array allocateFillValueChunk() {
109114
ucar.ma2.Array outputArray = ucar.ma2.Array.factory(dataType.getMA2DataType(), chunks);
110-
MultiArrayUtils.fill(outputArray, parsedFillValue);
115+
if (parsedFillValue != null) MultiArrayUtils.fill(outputArray, parsedFillValue);
111116
return outputArray;
112117
}
113118

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ArrayMetadataBuilder {
1313
DataType dataType = null;
1414
Order order = Order.C;
1515
Separator dimensionSeparator = Separator.DOT;
16-
Object fillValue = 0;
16+
Object fillValue = null;
1717
Codec[] filters = null;
1818
Codec compressor = null;
1919

@@ -115,9 +115,9 @@ public ArrayMetadata build() throws ZarrException {
115115
dataType,
116116
fillValue,
117117
order,
118-
dimensionSeparator,
119118
filters,
120-
compressor
119+
compressor,
120+
dimensionSeparator
121121
);
122122
}
123123
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,32 @@ public void testV2createZlib(int level) throws IOException, ZarrException {
580580
Assertions.assertEquals(7 * 6, outArray.getSize());
581581
Assertions.assertEquals(0, outArray.getByte(0));
582582
}
583+
584+
@ParameterizedTest
585+
@ValueSource(strings = {"BOOL", "INT8", "UINT8", "INT16", "UINT16", "INT32", "UINT32", "INT64", "UINT64", "FLOAT32", "FLOAT64"})
586+
public void testV2noFillValue(dev.zarr.zarrjava.v2.DataType dataType) throws IOException, ZarrException {
587+
StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("v2_no_fillvalue", dataType.name());
588+
589+
dev.zarr.zarrjava.v2.Array array = dev.zarr.zarrjava.v2.Array.create(
590+
storeHandle,
591+
dev.zarr.zarrjava.v2.Array.metadataBuilder()
592+
.withShape(15, 10)
593+
.withDataType(dataType)
594+
.withChunks(4, 5)
595+
.build()
596+
);
597+
Assertions.assertNull(array.metadata().fillValue);
598+
599+
ucar.ma2.Array outArray = array.read(new long[]{0, 0}, new int[]{1, 1});
600+
if (dataType == dev.zarr.zarrjava.v2.DataType.BOOL) {
601+
Assertions.assertFalse(outArray.getBoolean(0));
602+
} else {
603+
Assertions.assertEquals(0, outArray.getByte(0));
604+
}
605+
606+
dev.zarr.zarrjava.v2.Array array2 = dev.zarr.zarrjava.v2.Array.open(
607+
storeHandle
608+
);
609+
Assertions.assertNull(array2.metadata().fillValue);
610+
}
583611
}

0 commit comments

Comments
 (0)