Skip to content

Check sharding bounds #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/dev/zarr/zarrjava/v3/ArrayMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import dev.zarr.zarrjava.v3.chunkgrid.RegularChunkGrid;
import dev.zarr.zarrjava.v3.chunkkeyencoding.ChunkKeyEncoding;
import dev.zarr.zarrjava.v3.codec.Codec;
import dev.zarr.zarrjava.v3.codec.core.ShardingIndexedCodec;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -91,6 +94,35 @@ public ArrayMetadata(
"Expected node type '" + this.nodeType + "', got '" + nodeType + "'.");
}

if (chunkGrid instanceof RegularChunkGrid) {
int[] chunkShape = ((RegularChunkGrid) chunkGrid).configuration.chunkShape;
if (shape.length != chunkShape.length) {
throw new ZarrException("Shape (ndim=" + shape.length + ") and chunk shape (ndim=" +
chunkShape.length + ") need to have the same number of dimensions.");
}
for (int i = 0; i < shape.length; i++) {
if (shape[i] < chunkShape[i]) {
throw new ZarrException("Shape " + Arrays.toString(shape) + " can not contain chunk shape "
+ Arrays.toString(chunkShape));
}
}

Optional<Codec> shardingCodec = getShardingIndexedCodec(codecs);
int[] outerChunkShape = chunkShape;
while (shardingCodec.isPresent()) {
ShardingIndexedCodec.Configuration shardingConfig = ((ShardingIndexedCodec) shardingCodec.get()).configuration;
int[] innerChunkShape = shardingConfig.chunkShape;
if (outerChunkShape.length != innerChunkShape.length)
throw new ZarrException("Sharding dimensions mismatch of outer chunk shape " + Arrays.toString(outerChunkShape) + " and inner chunk shape" + Arrays.toString(innerChunkShape));
for (int i = 0; i < outerChunkShape.length; i++) {
if (outerChunkShape[i] < innerChunkShape[i])
throw new ZarrException("Sharding outer chunk shape " + Arrays.toString(outerChunkShape) + " can not contain inner chunk shape " + Arrays.toString(innerChunkShape));
}
outerChunkShape = innerChunkShape;
shardingCodec = getShardingIndexedCodec(shardingConfig.codecs);
}
}

this.shape = shape;
this.dataType = dataType;
this.chunkGrid = chunkGrid;
Expand Down Expand Up @@ -227,6 +259,10 @@ public int ndim() {
return shape.length;
}

public static Optional<Codec> getShardingIndexedCodec(Codec[] codecs) {
return Arrays.stream(codecs).filter(codec -> codec instanceof ShardingIndexedCodec).findFirst();
}

public int[] chunkShape() {
return ((RegularChunkGrid) this.chunkGrid).configuration.chunkShape;
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/dev/zarr/zarrjava/v3/ArrayMetadataBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import dev.zarr.zarrjava.v3.codec.CodecBuilder;
import dev.zarr.zarrjava.v3.codec.core.BytesCodec;
import dev.zarr.zarrjava.v3.codec.core.BytesCodec.Endian;
import dev.zarr.zarrjava.v3.codec.core.ShardingIndexedCodec;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -133,12 +136,8 @@ public ArrayMetadata build() throws ZarrException {
if (chunkGrid == null) {
throw new ZarrException("Chunk grid needs to be provided. Please call `.withChunkShape`.");
}
if (chunkGrid instanceof RegularChunkGrid
&& shape.length != ((RegularChunkGrid) chunkGrid).configuration.chunkShape.length) {
throw new ZarrException("Shape (ndim=" + shape.length + ") and chunk shape (ndim=" +
((RegularChunkGrid) chunkGrid).configuration.chunkShape.length +
") need to have the same number of dimensions.");
}


return new ArrayMetadata(shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
dimensionNames,
attributes
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/dev/zarr/zarrjava/ZarrTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,37 @@ public void testWriteReadWithZarrita(String codec, String codecParam) throws Exc
assert exitCode == 0;
}

@ParameterizedTest
@ValueSource(strings = {"large", "small", "nested", "wrong dims", "correct"})
public void testCheckShardingBounds(String scenario) throws Exception {
long[] shape = new long[] {4, 4};
int[] shardSize = new int[] {2, 2};
int[] chunkSize = new int[] {2, 2};

if (scenario.equals("large"))
shardSize = new int[] {8, 8};
if (scenario.equals("small"))
shardSize = new int[] {1, 1};
if (scenario.equals("wrong dims"))
shardSize = new int[] {1};
StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("illegal_shardsize");
ArrayMetadataBuilder builder = Array.metadataBuilder()
.withShape(shape)
.withDataType(DataType.UINT32).withChunkShape(shardSize);

if (scenario.equals("nested")) {
int[] nestedChunkSize = new int[]{4, 4};
builder = builder.withCodecs(c -> c.withSharding(chunkSize, c1 -> c1.withSharding(nestedChunkSize, c2 -> c2.withBytes("LITTLE"))));
} else {
builder = builder.withCodecs(c -> c.withSharding(chunkSize, c1 -> c1.withBytes("LITTLE")));
}
if (scenario.equals("correct")){
builder.build();
}else{
assertThrows(ZarrException.class, builder::build);
}
}

@ParameterizedTest
@CsvSource({"0,true", "0,false", "5, true", "5, false"})
public void testZstdCodecReadWrite(int clevel, boolean checksum) throws ZarrException, IOException {
Expand Down
Loading