Repository navigation
Expand file tree
/
Copy pathArrayMetadataBuilder.java
More file actions
208 lines (177 loc) · 7.62 KB
/
Copy pathArrayMetadataBuilder.java
File metadata and controls
208 lines (177 loc) · 7.62 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
package dev.zarr.zarrjava.v3;
import dev.zarr.zarrjava.ZarrException;
import dev.zarr.zarrjava.core.Attributes;
import dev.zarr.zarrjava.core.chunkkeyencoding.Separator;
import dev.zarr.zarrjava.core.codec.core.BytesCodec.Endian;
import dev.zarr.zarrjava.utils.Utils;
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.chunkkeyencoding.DefaultChunkKeyEncoding;
import dev.zarr.zarrjava.v3.chunkkeyencoding.TemplateChunkKeyEncoding;
import dev.zarr.zarrjava.v3.chunkkeyencoding.V2ChunkKeyEncoding;
import dev.zarr.zarrjava.v3.codec.Codec;
import dev.zarr.zarrjava.v3.codec.CodecBuilder;
import dev.zarr.zarrjava.v3.codec.core.BytesCodec;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class ArrayMetadataBuilder {
long[] shape = null;
DataType dataType = null;
ChunkGrid chunkGrid = null;
ChunkKeyEncoding chunkKeyEncoding =
new DefaultChunkKeyEncoding(new DefaultChunkKeyEncoding.Configuration(Separator.SLASH));
Object fillValue = 0;
Codec[] codecs = new Codec[]{new BytesCodec(Endian.LITTLE)};
Attributes attributes = new Attributes();
Map<String, Object>[] storageTransformers = new HashMap[]{};
String[] dimensionNames = null;
Map<String, Object> extraFields = null;
protected ArrayMetadataBuilder() {
}
protected static ArrayMetadataBuilder fromArrayMetadata(ArrayMetadata arrayMetadata) {
return fromArrayMetadata(arrayMetadata, true);
}
protected static ArrayMetadataBuilder fromArrayMetadata(ArrayMetadata arrayMetadata, boolean withAttributes) {
ArrayMetadataBuilder builder = new ArrayMetadataBuilder();
builder.shape = arrayMetadata.shape;
builder.dataType = arrayMetadata.dataType;
builder.chunkGrid = arrayMetadata.chunkGrid;
builder.chunkKeyEncoding = arrayMetadata.chunkKeyEncoding;
builder.fillValue = arrayMetadata.parsedFillValue;
builder.codecs = arrayMetadata.codecs;
builder.dimensionNames = arrayMetadata.dimensionNames;
builder.storageTransformers = arrayMetadata.storageTransformers;
builder.extraFields = arrayMetadata.extraFields();
if (withAttributes) {
builder.attributes = arrayMetadata.attributes;
}
return builder;
}
public ArrayMetadataBuilder withShape(long... shape) {
this.shape = shape;
return this;
}
public ArrayMetadataBuilder withDataType(DataType dataType) {
this.dataType = dataType;
return this;
}
public ArrayMetadataBuilder withDataType(String dataType) {
this.dataType = DataType.valueOf(dataType);
return this;
}
public ArrayMetadataBuilder withChunkShape(int... chunkShape) {
this.chunkGrid = new RegularChunkGrid(new RegularChunkGrid.Configuration(chunkShape));
return this;
}
public ArrayMetadataBuilder withDefaultChunkKeyEncoding(Separator separator) {
this.chunkKeyEncoding = new DefaultChunkKeyEncoding(
new DefaultChunkKeyEncoding.Configuration(separator));
return this;
}
public ArrayMetadataBuilder withDefaultChunkKeyEncoding() {
this.chunkKeyEncoding = new DefaultChunkKeyEncoding(
new DefaultChunkKeyEncoding.Configuration(Separator.SLASH));
return this;
}
public ArrayMetadataBuilder withDefaultChunkKeyEncoding(String separator) {
this.chunkKeyEncoding =
new DefaultChunkKeyEncoding(
new DefaultChunkKeyEncoding.Configuration(Separator.valueOf(separator)));
return this;
}
public ArrayMetadataBuilder withV2ChunkKeyEncoding(Separator separator) {
this.chunkKeyEncoding = new V2ChunkKeyEncoding(new V2ChunkKeyEncoding.Configuration(separator));
return this;
}
public ArrayMetadataBuilder withV2ChunkKeyEncoding() {
this.chunkKeyEncoding = new V2ChunkKeyEncoding(
new V2ChunkKeyEncoding.Configuration(Separator.DOT));
return this;
}
public ArrayMetadataBuilder withV2ChunkKeyEncoding(String separator) {
this.chunkKeyEncoding =
new V2ChunkKeyEncoding(new V2ChunkKeyEncoding.Configuration(Separator.valueOf(separator)));
return this;
}
/**
* Uses the template chunk key encoding, e.g. {@code withTemplateChunkKeyEncoding("images/{-1}/{*}.jpeg", "_")}.
*
* @param separator joins the values of {@code {*}}; may be {@code null} if the format has no {@code {*}}
*/
public ArrayMetadataBuilder withTemplateChunkKeyEncoding(String format, String separator) throws ZarrException {
this.chunkKeyEncoding = new TemplateChunkKeyEncoding(format, separator);
return this;
}
public ArrayMetadataBuilder withChunkKeyEncoding(ChunkKeyEncoding chunkKeyEncoding) {
this.chunkKeyEncoding = chunkKeyEncoding;
return this;
}
public ArrayMetadataBuilder withFillValue(Object fillValue) {
this.fillValue = fillValue;
return this;
}
public ArrayMetadataBuilder withCodecs(Codec... codecs) {
this.codecs = codecs;
return this;
}
public ArrayMetadataBuilder withCodecs(Function<CodecBuilder, CodecBuilder> codecBuilder) {
if (dataType == null) {
throw new IllegalStateException("Please call `withDataType` first.");
}
CodecBuilder nestedCodecBuilder = new CodecBuilder(dataType);
this.codecs = codecBuilder.apply(nestedCodecBuilder)
.build();
return this;
}
public ArrayMetadataBuilder withDimensionNames(String... dimensionNames) {
this.dimensionNames = dimensionNames;
return this;
}
public ArrayMetadataBuilder putAttribute(String key, Object value) {
this.attributes.put(key, value);
return this;
}
public ArrayMetadataBuilder withAttributes(Attributes attributes) {
if (this.attributes == null) {
this.attributes = attributes;
} else {
this.attributes.putAll(attributes);
}
return this;
}
public ArrayMetadataBuilder withStorageTransformers(Map<String, Object>[] storageTransformers) {
this.storageTransformers = storageTransformers;
return this;
}
/**
* Sets members of the metadata document that zarr-java itself does not interpret. Every value
* needs to be a map carrying {@code "must_understand": false}, otherwise {@link #build()} fails.
*
* @param extraFields the extra fields to write into the metadata document
*/
public ArrayMetadataBuilder withExtraFields(Map<String, Object> extraFields) {
this.extraFields = extraFields;
return this;
}
public ArrayMetadata build() throws ZarrException {
if (shape == null) {
throw new ZarrException("Shape needs to be provided. Please call `.withShape`.");
}
if (dataType == null) {
throw new ZarrException("Data type needs to be provided. Please call `.withDataType`.");
}
// If chunk grid is not specified, calculate default chunks
if (chunkGrid == null) {
int[] defaultChunks = Utils.calculateDefaultChunks(shape);
chunkGrid = new RegularChunkGrid(new RegularChunkGrid.Configuration(defaultChunks));
}
return new ArrayMetadata(shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
dimensionNames,
attributes,
storageTransformers,
extraFields
);
}
}