Skip to content

Commit 93cad67

Browse files
committed
check sharding bounds
1 parent 4304f2d commit 93cad67

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

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

+33-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import dev.zarr.zarrjava.v3.codec.CodecBuilder;
1212
import dev.zarr.zarrjava.v3.codec.core.BytesCodec;
1313
import dev.zarr.zarrjava.v3.codec.core.BytesCodec.Endian;
14+
import dev.zarr.zarrjava.v3.codec.core.ShardingIndexedCodec;
15+
16+
import java.util.Arrays;
1417
import java.util.HashMap;
1518
import java.util.Map;
1619
import java.util.function.Function;
@@ -123,6 +126,22 @@ public ArrayMetadataBuilder withAttributes(Map<String, Object> attributes) {
123126
return this;
124127
}
125128

129+
public void verifyShardingBounds(int[] outerChunkShape, Codec[] codecs) throws ZarrException {
130+
for (Codec codec : codecs) {
131+
if (codec instanceof ShardingIndexedCodec) {
132+
ShardingIndexedCodec.Configuration shardingConfig = ((ShardingIndexedCodec) codec).configuration;
133+
int[] innerChunkShape = shardingConfig.chunkShape;
134+
if (outerChunkShape.length != innerChunkShape.length)
135+
throw new ZarrException("Sharding dimensions mismatch of outer chunk shape " + Arrays.toString(outerChunkShape) + " and inner chunk shape" + Arrays.toString(innerChunkShape));
136+
for (int i = 0; i < outerChunkShape.length; i++) {
137+
if (outerChunkShape[i] < innerChunkShape[i])
138+
throw new ZarrException("Sharding outer chunk shape " + Arrays.toString(outerChunkShape) + " can not contain inner chunk shape " + Arrays.toString(innerChunkShape));
139+
}
140+
verifyShardingBounds(innerChunkShape, shardingConfig.codecs);
141+
}
142+
}
143+
}
144+
126145
public ArrayMetadata build() throws ZarrException {
127146
if (shape == null) {
128147
throw new ZarrException("Shape needs to be provided. Please call `.withShape`.");
@@ -133,12 +152,21 @@ public ArrayMetadata build() throws ZarrException {
133152
if (chunkGrid == null) {
134153
throw new ZarrException("Chunk grid needs to be provided. Please call `.withChunkShape`.");
135154
}
136-
if (chunkGrid instanceof RegularChunkGrid
137-
&& shape.length != ((RegularChunkGrid) chunkGrid).configuration.chunkShape.length) {
138-
throw new ZarrException("Shape (ndim=" + shape.length + ") and chunk shape (ndim=" +
139-
((RegularChunkGrid) chunkGrid).configuration.chunkShape.length +
140-
") need to have the same number of dimensions.");
155+
if (chunkGrid instanceof RegularChunkGrid) {
156+
int[] chunkShape = ((RegularChunkGrid) chunkGrid).configuration.chunkShape;
157+
if (shape.length != chunkShape.length) {
158+
throw new ZarrException("Shape (ndim=" + shape.length + ") and chunk shape (ndim=" +
159+
chunkShape.length + ") need to have the same number of dimensions.");
160+
}
161+
for (int i = 0; i < shape.length; i++) {
162+
if (shape[i] < chunkShape[i]) {
163+
throw new ZarrException("Shape " + Arrays.toString(shape) + " can not contain chunk shape "
164+
+ Arrays.toString(chunkShape));
165+
}
166+
}
167+
verifyShardingBounds(chunkShape, codecs);
141168
}
169+
142170
return new ArrayMetadata(shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
143171
dimensionNames,
144172
attributes

0 commit comments

Comments
 (0)