11
11
import dev .zarr .zarrjava .v3 .codec .CodecBuilder ;
12
12
import dev .zarr .zarrjava .v3 .codec .core .BytesCodec ;
13
13
import dev .zarr .zarrjava .v3 .codec .core .BytesCodec .Endian ;
14
+ import dev .zarr .zarrjava .v3 .codec .core .ShardingIndexedCodec ;
15
+
16
+ import java .util .Arrays ;
14
17
import java .util .HashMap ;
15
18
import java .util .Map ;
16
19
import java .util .function .Function ;
@@ -123,6 +126,22 @@ public ArrayMetadataBuilder withAttributes(Map<String, Object> attributes) {
123
126
return this ;
124
127
}
125
128
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
+
126
145
public ArrayMetadata build () throws ZarrException {
127
146
if (shape == null ) {
128
147
throw new ZarrException ("Shape needs to be provided. Please call `.withShape`." );
@@ -133,12 +152,21 @@ public ArrayMetadata build() throws ZarrException {
133
152
if (chunkGrid == null ) {
134
153
throw new ZarrException ("Chunk grid needs to be provided. Please call `.withChunkShape`." );
135
154
}
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 );
141
168
}
169
+
142
170
return new ArrayMetadata (shape , dataType , chunkGrid , chunkKeyEncoding , fillValue , codecs ,
143
171
dimensionNames ,
144
172
attributes
0 commit comments