-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathArrayMetadata.java
More file actions
234 lines (205 loc) · 9.37 KB
/
Copy pathArrayMetadata.java
File metadata and controls
234 lines (205 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package dev.zarr.zarrjava.v3;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import dev.zarr.zarrjava.ZarrException;
import dev.zarr.zarrjava.core.Attributes;
import dev.zarr.zarrjava.v3.chunkgrid.ChunkGrid;
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 javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
public final class ArrayMetadata extends dev.zarr.zarrjava.core.ArrayMetadata {
public static final String NODE_TYPE = "array";
static final int ZARR_FORMAT = 3;
@JsonProperty("zarr_format")
public final int zarrFormat = ZARR_FORMAT;
@JsonProperty("node_type")
public final String nodeType = NODE_TYPE;
@JsonProperty("data_type")
public final DataType dataType;
@JsonProperty("chunk_grid")
public final ChunkGrid chunkGrid;
@JsonProperty("chunk_key_encoding")
public final ChunkKeyEncoding chunkKeyEncoding;
@JsonProperty("codecs")
public final Codec[] codecs;
@Nullable
@JsonProperty("attributes")
public final Attributes attributes;
@Nullable
@JsonProperty("dimension_names")
public final String[] dimensionNames;
@Nullable
@JsonProperty("storage_transformers")
public final Map<String, Object>[] storageTransformers;
/**
* Members of the metadata document that zarr-java does not know about. The Zarr v3 specification
* requires that these are ignored when they declare {@code "must_understand": false}, and that
* they are rejected otherwise. They are kept here so that rewriting the metadata does not drop
* extensions written by another implementation.
*/
private final Map<String, Object> extraFields;
@JsonIgnore
public CoreArrayMetadata coreArrayMetadata;
public ArrayMetadata(
long[] shape, DataType dataType, ChunkGrid chunkGrid, ChunkKeyEncoding chunkKeyEncoding,
Object fillValue,
@Nonnull Codec[] codecs,
@Nullable String[] dimensionNames,
@Nullable Attributes attributes,
@Nullable Map<String, Object>[] storageTransformers
) throws ZarrException {
this(ZARR_FORMAT, NODE_TYPE, shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
dimensionNames,
attributes, storageTransformers
);
}
public ArrayMetadata(
long[] shape, DataType dataType, ChunkGrid chunkGrid, ChunkKeyEncoding chunkKeyEncoding,
Object fillValue,
@Nonnull Codec[] codecs,
@Nullable String[] dimensionNames,
@Nullable Attributes attributes,
@Nullable Map<String, Object>[] storageTransformers,
@Nullable Map<String, Object> extraFields
) throws ZarrException {
this(ZARR_FORMAT, NODE_TYPE, shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
dimensionNames,
attributes, storageTransformers, extraFields
);
}
public ArrayMetadata(
int zarrFormat,
String nodeType,
long[] shape,
DataType dataType,
ChunkGrid chunkGrid,
ChunkKeyEncoding chunkKeyEncoding,
Object fillValue,
@Nonnull Codec[] codecs,
@Nullable String[] dimensionNames,
@Nullable Attributes attributes,
@Nullable Map<String, Object>[] storageTransformers
) throws ZarrException {
this(zarrFormat, nodeType, shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
dimensionNames, attributes, storageTransformers, null
);
}
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public ArrayMetadata(
@JsonProperty(value = "zarr_format", required = true) int zarrFormat,
@JsonProperty(value = "node_type", required = true) String nodeType,
@JsonProperty(value = "shape", required = true) long[] shape,
@JsonProperty(value = "data_type", required = true) DataType dataType,
@JsonProperty(value = "chunk_grid", required = true) ChunkGrid chunkGrid,
@JsonProperty(value = "chunk_key_encoding", required = true) ChunkKeyEncoding chunkKeyEncoding,
@JsonProperty(value = "fill_value", required = true) Object fillValue,
@Nonnull @JsonProperty(value = "codecs") Codec[] codecs,
@Nullable @JsonProperty(value = "dimension_names") String[] dimensionNames,
@Nullable @JsonProperty(value = "attributes") Attributes attributes,
@Nullable @JsonProperty(value = "storage_transformers") Map<String, Object>[] storageTransformers,
@Nullable @JsonAnySetter Map<String, Object> extraFields
) throws ZarrException {
super(shape, fillValue, dataType);
this.extraFields = ExtraFields.validatedArrayFields(extraFields);
if (zarrFormat != this.zarrFormat) {
throw new ZarrException(
"Expected zarr format '" + this.zarrFormat + "', got '" + zarrFormat + "'.");
}
if (!nodeType.equals(this.nodeType)) {
throw new ZarrException(
"Expected node type '" + this.nodeType + "', got '" + nodeType + "'.");
}
if (storageTransformers != null && storageTransformers.length > 0) {
throw new ZarrException(
"Storage transformers are not supported in this version of Zarr Java.");
}
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.");
}
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] != 0)
throw new ZarrException("Sharding inner chunk shape " + Arrays.toString(innerChunkShape) + " does not evenly divide the outer chunk size " + Arrays.toString(outerChunkShape));
}
outerChunkShape = innerChunkShape;
shardingCodec = getShardingIndexedCodec(shardingConfig.codecs);
}
}
this.chunkGrid = chunkGrid;
this.dataType = dataType;
this.coreArrayMetadata =
new CoreArrayMetadata(this.shape, ((RegularChunkGrid) chunkGrid).configuration.chunkShape,
this.dataType,
this.parsedFillValue
);
this.chunkKeyEncoding = chunkKeyEncoding;
this.codecs = codecs;
this.dimensionNames = dimensionNames;
this.attributes = attributes;
this.storageTransformers = storageTransformers;
}
/**
* The members of the metadata document that zarr-java does not know about, but that declared
* {@code "must_understand": false} and could therefore be ignored. They are written back out
* unchanged, so that extensions written by another implementation survive a metadata rewrite.
*
* @return the extra fields, never {@code null}
*/
@JsonAnyGetter
public Map<String, Object> extraFields() {
return extraFields;
}
public static Optional<Codec> getShardingIndexedCodec(Codec[] codecs) {
return Arrays.stream(codecs).filter(codec -> codec instanceof ShardingIndexedCodec).findFirst();
}
public ucar.ma2.Array allocateFillValueChunk() {
return coreArrayMetadata.allocateFillValueChunk();
}
@Override
public ChunkKeyEncoding chunkKeyEncoding() {
return chunkKeyEncoding;
}
@Override
public Object parsedFillValue() {
return parsedFillValue;
}
@Nonnull
@Override
public Attributes attributes() throws ZarrException {
if (attributes == null) {
throw new ZarrException("Array attributes have not been set.");
}
return attributes;
}
public int[] chunkShape() {
return ((RegularChunkGrid) this.chunkGrid).configuration.chunkShape;
}
@Override
public DataType dataType() {
return dataType;
}
public int chunkSize() {
return coreArrayMetadata.chunkSize();
}
public int chunkByteLength() {
return coreArrayMetadata.chunkByteLength();
}
}