-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathArrayMetadataBuilder.java
More file actions
187 lines (158 loc) · 5.77 KB
/
ArrayMetadataBuilder.java
File metadata and controls
187 lines (158 loc) · 5.77 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
package dev.zarr.zarrjava.v2;
import com.scalableminds.bloscjava.Blosc;
import dev.zarr.zarrjava.ZarrException;
import dev.zarr.zarrjava.core.Attributes;
import dev.zarr.zarrjava.core.chunkkeyencoding.Separator;
import dev.zarr.zarrjava.utils.Utils;
import dev.zarr.zarrjava.v2.codec.Codec;
import dev.zarr.zarrjava.v2.codec.core.BloscCodec;
import dev.zarr.zarrjava.v2.codec.core.ZlibCodec;
import dev.zarr.zarrjava.v2.codec.core.ZstdCodec;
public class ArrayMetadataBuilder {
long[] shape = null;
int[] chunks = null;
DataType dataType = null;
Order order = Order.C;
Separator dimensionSeparator = Separator.DOT;
Object fillValue = null;
Codec[] filters = null;
Codec compressor = null;
Attributes attributes = new Attributes();
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.chunks = arrayMetadata.chunks;
builder.dataType = arrayMetadata.dataType;
builder.order = arrayMetadata.order;
builder.dimensionSeparator = arrayMetadata.dimensionSeparator;
builder.fillValue = arrayMetadata.parsedFillValue;
builder.filters = arrayMetadata.filters;
builder.compressor = arrayMetadata.compressor;
if (withAttributes) {
builder.attributes = arrayMetadata.attributes;
}
return builder;
}
public ArrayMetadataBuilder withShape(long... shape) {
this.shape = shape;
return this;
}
public ArrayMetadataBuilder withChunks(int... chunks) {
this.chunks = chunks;
return this;
}
public ArrayMetadataBuilder withDataType(DataType dataTypeV2) {
this.dataType = dataTypeV2;
return this;
}
public ArrayMetadataBuilder withOrder(Order order) {
this.order = order;
return this;
}
public ArrayMetadataBuilder withDimensionSeparator(Separator dimensionSeparator) {
this.dimensionSeparator = dimensionSeparator;
return this;
}
public ArrayMetadataBuilder withFillValue(Object fillValue) {
this.fillValue = fillValue;
return this;
}
public ArrayMetadataBuilder withCompressor(Codec compressor) {
this.compressor = compressor;
return this;
}
public ArrayMetadataBuilder withBloscCompressor(
Blosc.Compressor cname, Blosc.Shuffle shuffle, int clevel, int typeSize,
int blockSize
) {
try {
this.compressor = new BloscCodec(cname, shuffle, clevel, typeSize, blockSize);
} catch (ZarrException e) {
throw new RuntimeException(e);
}
return this;
}
public ArrayMetadataBuilder withBloscCompressor(String cname, String shuffle, int clevel, int blockSize) {
return withBloscCompressor(Blosc.Compressor.fromString(cname), Blosc.Shuffle.fromString(shuffle), clevel,
dataType.getByteCount(), blockSize
);
}
public ArrayMetadataBuilder withBloscCompressor(String cname, String shuffle, int clevel) {
return withBloscCompressor(cname, shuffle, clevel, 0);
}
public ArrayMetadataBuilder withBloscCompressor(String cname, int clevel) {
return withBloscCompressor(cname, "noshuffle", clevel);
}
public ArrayMetadataBuilder withBloscCompressor(String cname) {
return withBloscCompressor(cname, 5);
}
public ArrayMetadataBuilder withBloscCompressor() {
return withBloscCompressor("zstd");
}
public ArrayMetadataBuilder withZlibCompressor(int level) {
try {
this.compressor = new ZlibCodec(level);
} catch (ZarrException e) {
throw new RuntimeException(e);
}
return this;
}
public ArrayMetadataBuilder withZlibCompressor() {
return withZlibCompressor(5);
}
public ArrayMetadataBuilder withZstdCompressor(int level, boolean checksum) {
try {
this.compressor = new ZstdCodec(level, checksum);
} catch (ZarrException e) {
throw new RuntimeException(e);
}
return this;
}
public ArrayMetadataBuilder withZstdCompressor(int level) {
return withZstdCompressor(level, ZstdCodec.DEFAULT_CHECKSUM);
}
public ArrayMetadataBuilder withZstdCompressor() {
return withZstdCompressor(ZstdCodec.DEFAULT_LEVEL);
}
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 ArrayMetadata build() throws ZarrException {
if (shape == null) {
throw new IllegalStateException("Please call `withShape` first.");
}
if (dataType == null) {
throw new IllegalStateException("Please call `withDataType` first.");
}
// If chunks are not specified, calculate default chunks
if (chunks == null) {
chunks = Utils.calculateDefaultChunks(shape);
}
return new ArrayMetadata(
2,
shape,
chunks,
dataType,
fillValue,
order,
filters,
compressor,
dimensionSeparator,
attributes
);
}
}