|
10 | 10 | import dev.zarr.zarrjava.v3.chunkgrid.RegularChunkGrid;
|
11 | 11 | import dev.zarr.zarrjava.v3.chunkkeyencoding.ChunkKeyEncoding;
|
12 | 12 | import dev.zarr.zarrjava.v3.codec.Codec;
|
| 13 | +import dev.zarr.zarrjava.v3.codec.core.ShardingIndexedCodec; |
| 14 | + |
13 | 15 | import java.nio.ByteBuffer;
|
14 | 16 | import java.util.Arrays;
|
15 | 17 | import java.util.Map;
|
| 18 | +import java.util.Optional; |
16 | 19 | import javax.annotation.Nonnull;
|
17 | 20 | import javax.annotation.Nullable;
|
18 | 21 |
|
@@ -91,6 +94,35 @@ public ArrayMetadata(
|
91 | 94 | "Expected node type '" + this.nodeType + "', got '" + nodeType + "'.");
|
92 | 95 | }
|
93 | 96 |
|
| 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 | + |
94 | 126 | this.shape = shape;
|
95 | 127 | this.dataType = dataType;
|
96 | 128 | this.chunkGrid = chunkGrid;
|
@@ -227,6 +259,10 @@ public int ndim() {
|
227 | 259 | return shape.length;
|
228 | 260 | }
|
229 | 261 |
|
| 262 | + public static Optional<Codec> getShardingIndexedCodec(Codec[] codecs) { |
| 263 | + return Arrays.stream(codecs).filter(codec -> codec instanceof ShardingIndexedCodec).findFirst(); |
| 264 | + } |
| 265 | + |
230 | 266 | public int[] chunkShape() {
|
231 | 267 | return ((RegularChunkGrid) this.chunkGrid).configuration.chunkShape;
|
232 | 268 | }
|
|
0 commit comments