Skip to content

Commit fa0937e

Browse files
committed
check for sharding bounds in metadata creation
1 parent 1926f97 commit fa0937e

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

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

+36
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
import dev.zarr.zarrjava.v3.chunkgrid.RegularChunkGrid;
1111
import dev.zarr.zarrjava.v3.chunkkeyencoding.ChunkKeyEncoding;
1212
import dev.zarr.zarrjava.v3.codec.Codec;
13+
import dev.zarr.zarrjava.v3.codec.core.ShardingIndexedCodec;
14+
1315
import java.nio.ByteBuffer;
1416
import java.util.Arrays;
1517
import java.util.Map;
18+
import java.util.Optional;
1619
import javax.annotation.Nonnull;
1720
import javax.annotation.Nullable;
1821

@@ -91,6 +94,35 @@ public ArrayMetadata(
9194
"Expected node type '" + this.nodeType + "', got '" + nodeType + "'.");
9295
}
9396

97+
if (chunkGrid instanceof RegularChunkGrid) {
98+
int[] chunkShape = ((RegularChunkGrid) chunkGrid).configuration.chunkShape;
99+
if (shape.length != chunkShape.length) {
100+
throw new ZarrException("Shape (ndim=" + shape.length + ") and chunk shape (ndim=" +
101+
chunkShape.length + ") need to have the same number of dimensions.");
102+
}
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+
}
109+
110+
Optional<Codec> shardingCodec = getShardingIndexedCodec(codecs);
111+
int[] outerChunkShape = chunkShape;
112+
while (shardingCodec.isPresent()) {
113+
ShardingIndexedCodec.Configuration shardingConfig = ((ShardingIndexedCodec) shardingCodec.get()).configuration;
114+
int[] innerChunkShape = shardingConfig.chunkShape;
115+
if (outerChunkShape.length != innerChunkShape.length)
116+
throw new ZarrException("Sharding dimensions mismatch of outer chunk shape " + Arrays.toString(outerChunkShape) + " and inner chunk shape" + Arrays.toString(innerChunkShape));
117+
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));
120+
}
121+
outerChunkShape = innerChunkShape;
122+
shardingCodec = getShardingIndexedCodec(shardingConfig.codecs);
123+
}
124+
}
125+
94126
this.shape = shape;
95127
this.dataType = dataType;
96128
this.chunkGrid = chunkGrid;
@@ -227,6 +259,10 @@ public int ndim() {
227259
return shape.length;
228260
}
229261

262+
public static Optional<Codec> getShardingIndexedCodec(Codec[] codecs) {
263+
return Arrays.stream(codecs).filter(codec -> codec instanceof ShardingIndexedCodec).findFirst();
264+
}
265+
230266
public int[] chunkShape() {
231267
return ((RegularChunkGrid) this.chunkGrid).configuration.chunkShape;
232268
}

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

+1-30
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,6 @@ public ArrayMetadataBuilder withAttributes(Map<String, Object> attributes) {
126126
return this;
127127
}
128128

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-
145129
public ArrayMetadata build() throws ZarrException {
146130
if (shape == null) {
147131
throw new ZarrException("Shape needs to be provided. Please call `.withShape`.");
@@ -152,20 +136,7 @@ public ArrayMetadata build() throws ZarrException {
152136
if (chunkGrid == null) {
153137
throw new ZarrException("Chunk grid needs to be provided. Please call `.withChunkShape`.");
154138
}
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);
168-
}
139+
169140

170141
return new ArrayMetadata(shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
171142
dimensionNames,

0 commit comments

Comments
 (0)