Skip to content

Commit 8342e1c

Browse files
authored
Support smaller array size than chunk size (#9)
* inner chunks evenly divide outer chunks * add testLargerChunkSizeThanArraySize and extend testCheckShardingBounds
1 parent 122ca90 commit 8342e1c

File tree

2 files changed

+46
-34
lines changed

2 files changed

+46
-34
lines changed

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

+2-8
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,6 @@ public ArrayMetadata(
100100
throw new ZarrException("Shape (ndim=" + shape.length + ") and chunk shape (ndim=" +
101101
chunkShape.length + ") need to have the same number of dimensions.");
102102
}
103-
for (int i = 0; i < shape.length; i++) {
104-
if (shape[i] < chunkShape[i]) {
105-
throw new ZarrException("Shape " + Arrays.toString(shape) + " can not contain chunk shape "
106-
+ Arrays.toString(chunkShape));
107-
}
108-
}
109103

110104
Optional<Codec> shardingCodec = getShardingIndexedCodec(codecs);
111105
int[] outerChunkShape = chunkShape;
@@ -115,8 +109,8 @@ public ArrayMetadata(
115109
if (outerChunkShape.length != innerChunkShape.length)
116110
throw new ZarrException("Sharding dimensions mismatch of outer chunk shape " + Arrays.toString(outerChunkShape) + " and inner chunk shape" + Arrays.toString(innerChunkShape));
117111
for (int i = 0; i < outerChunkShape.length; i++) {
118-
if (outerChunkShape[i] < innerChunkShape[i])
119-
throw new ZarrException("Sharding outer chunk shape " + Arrays.toString(outerChunkShape) + " can not contain inner chunk shape " + Arrays.toString(innerChunkShape));
112+
if (outerChunkShape[i] % innerChunkShape[i] != 0)
113+
throw new ZarrException("Sharding inner chunk shape " + Arrays.toString(innerChunkShape) + " does not evenly divide the outer chunk size " + Arrays.toString(outerChunkShape));
120114
}
121115
outerChunkShape = innerChunkShape;
122116
shardingCodec = getShardingIndexedCodec(shardingConfig.codecs);

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

+44-26
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,38 @@ public void testCheckInvalidCodecConfiguration(Function<CodecBuilder, CodecBuild
255255
assertThrows(ZarrException.class, () -> Array.create(storeHandle, builder.build()));
256256
}
257257

258+
@Test
259+
public void testLargerChunkSizeThanArraySize() throws ZarrException, IOException {
260+
int[] testData = new int[16 * 16 * 16];
261+
Arrays.setAll(testData, p -> p);
262+
263+
StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("larger_chunk_size_than_array");
264+
ArrayMetadata metadata = Array.metadataBuilder()
265+
.withShape(16, 16, 16)
266+
.withDataType(DataType.UINT32)
267+
.withChunkShape(32, 32, 32)
268+
.withFillValue(0)
269+
.build();
270+
Array writeArray = Array.create(storeHandle, metadata);
271+
writeArray.write(ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{16, 16, 16}, testData));
272+
273+
//read in zarr-java
274+
Array readArray = Array.open(storeHandle);
275+
ucar.ma2.Array result = readArray.read();
276+
277+
Assertions.assertArrayEquals(testData, (int[]) result.get1DJavaArray(ucar.ma2.DataType.INT));
278+
}
279+
258280
static Stream<int[]> invalidChunkSizes() {
259281
return Stream.of(
260282
new int[]{1},
261-
new int[]{1, 1, 1},
262-
new int[] {5, 1},
263-
new int[] {1, 5}
283+
new int[]{1, 1, 1}
264284
);
265285
}
266286

267287
@ParameterizedTest
268288
@MethodSource("invalidChunkSizes")
269-
public void testCheckInvalidChunkBounds(int[] chunkSize) throws Exception {
289+
public void testCheckInvalidChunkDimensions(int[] chunkSize) {
270290
long[] shape = new long[] {4, 4};
271291

272292
StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("invalid_chunksize");
@@ -278,35 +298,33 @@ public void testCheckInvalidChunkBounds(int[] chunkSize) throws Exception {
278298
assertThrows(ZarrException.class, builder::build);
279299
}
280300

301+
static Stream<int[]> invalidShardSizes() {
302+
return Stream.of(
303+
new int[]{4}, //wrong dims
304+
new int[]{4, 4, 4}, //wrong dims
305+
new int[]{1, 1}, //smaller than inner chunk shape
306+
new int[]{5, 5}, //no exact multiple of inner chunk shape
307+
new int[]{2, 1}, //smaller than inner chunk shape in 2nd dimension
308+
new int[]{2, 5} //no exact multiple of inner chunk shape in 2nd dimension
309+
);
310+
}
311+
281312
@ParameterizedTest
282-
@ValueSource(strings = {"large", "small", "nested", "wrong dims", "correct"})
283-
public void testCheckShardingBounds(String scenario) throws Exception {
284-
long[] shape = new long[] {4, 4};
285-
int[] shardSize = new int[] {2, 2};
286-
int[] chunkSize = new int[] {2, 2};
287-
288-
if (scenario.equals("large"))
289-
shardSize = new int[] {8, 8};
290-
if (scenario.equals("small"))
291-
shardSize = new int[] {1, 1};
292-
if (scenario.equals("wrong dims"))
293-
shardSize = new int[] {1};
294-
StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("illegal_shardsize");
313+
@MethodSource("invalidShardSizes")
314+
public void testCheckShardingBounds(int[] shardSize) throws Exception {
315+
long[] shape = new long[] {10, 10};
316+
int[] innerChunkSize = new int[] {2, 2};
317+
295318
ArrayMetadataBuilder builder = Array.metadataBuilder()
296319
.withShape(shape)
297320
.withDataType(DataType.UINT32).withChunkShape(shardSize);
298321

299-
if (scenario.equals("nested")) {
322+
if (false) {
300323
int[] nestedChunkSize = new int[]{4, 4};
301-
builder = builder.withCodecs(c -> c.withSharding(chunkSize, c1 -> c1.withSharding(nestedChunkSize, c2 -> c2.withBytes("LITTLE"))));
302-
} else {
303-
builder = builder.withCodecs(c -> c.withSharding(chunkSize, c1 -> c1.withBytes("LITTLE")));
304-
}
305-
if (scenario.equals("correct")){
306-
builder.build();
307-
}else{
308-
assertThrows(ZarrException.class, builder::build);
324+
builder = builder.withCodecs(c -> c.withSharding(new int[]{2, 2}, c1 -> c1.withSharding(nestedChunkSize, c2 -> c2.withBytes("LITTLE"))));
309325
}
326+
builder = builder.withCodecs(c -> c.withSharding(innerChunkSize, c1 -> c1.withBytes("LITTLE")));
327+
assertThrows(ZarrException.class, builder::build);
310328
}
311329

312330
@ParameterizedTest

0 commit comments

Comments
 (0)