-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCodecBuilder.java
More file actions
168 lines (142 loc) · 5.55 KB
/
CodecBuilder.java
File metadata and controls
168 lines (142 loc) · 5.55 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
package dev.zarr.zarrjava.v3.codec;
import com.scalableminds.bloscjava.Blosc;
import dev.zarr.zarrjava.ZarrException;
import dev.zarr.zarrjava.core.codec.ArrayArrayCodec;
import dev.zarr.zarrjava.core.codec.ArrayBytesCodec;
import dev.zarr.zarrjava.core.codec.BytesBytesCodec;
import dev.zarr.zarrjava.core.codec.core.BytesCodec.Endian;
import dev.zarr.zarrjava.v3.DataType;
import dev.zarr.zarrjava.v3.codec.core.*;
import dev.zarr.zarrjava.v3.codec.core.BytesCodec.Configuration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
public class CodecBuilder extends dev.zarr.zarrjava.core.codec.CodecBuilder {
protected List<Codec> codecs;
public CodecBuilder(DataType dataType) {
super(dataType);
this.codecs = new ArrayList<>();
}
public CodecBuilder withBlosc(
Blosc.Compressor cname, Blosc.Shuffle shuffle, int clevel, int typeSize,
int blockSize
) {
try {
codecs.add(new BloscCodec(
new BloscCodec.Configuration(cname, shuffle, clevel, typeSize, blockSize)));
} catch (ZarrException e) {
throw new RuntimeException(e);
}
return this;
}
public CodecBuilder withBlosc(String cname, String shuffle, int clevel, int blockSize) {
return withBlosc(Blosc.Compressor.fromString(cname), Blosc.Shuffle.fromString(shuffle), clevel,
dataType.getByteCount(), blockSize
);
}
public CodecBuilder withBlosc(String cname, String shuffle, int clevel) {
return withBlosc(cname, shuffle, clevel, 0);
}
public CodecBuilder withBlosc(String cname, int clevel) {
return withBlosc(cname, "noshuffle", clevel);
}
public CodecBuilder withBlosc(String cname) {
return withBlosc(cname, 5);
}
public CodecBuilder withBlosc() {
return withBlosc("zstd");
}
public CodecBuilder withTranspose(int[] order) {
codecs.add(new TransposeCodec(new TransposeCodec.Configuration(order)));
return this;
}
public CodecBuilder withBytes(Endian endian) {
if (dataType.getByteCount() <= 1)
codecs.add(new BytesCodec());
else
codecs.add(new BytesCodec(endian));
return this;
}
public CodecBuilder withBytes(String endian) {
return withBytes(BytesCodec.Endian.valueOf(endian));
}
public CodecBuilder withBytes() {
return withBytes(Endian.nativeOrder());
}
public CodecBuilder withGzip(int level) {
try {
codecs.add(new GzipCodec(new GzipCodec.Configuration(level)));
} catch (ZarrException e) {
throw new RuntimeException(e);
}
return this;
}
public CodecBuilder withGzip() {
return withGzip(5);
}
public CodecBuilder withZstd(int level, boolean checksum) {
try {
codecs.add(new ZstdCodec(new ZstdCodec.Configuration(level, checksum)));
} catch (ZarrException e) {
throw new RuntimeException(e);
}
return this;
}
public CodecBuilder withZstd() {
return withZstd(5, true);
}
public CodecBuilder withZstd(int level) {
return withZstd(level, true);
}
public CodecBuilder withSharding(int[] chunkShape) {
try {
codecs.add(
new ShardingIndexedCodec(new ShardingIndexedCodec.Configuration(chunkShape,
new Codec[]{new BytesCodec(new Configuration(Endian.LITTLE))},
new Codec[]{new BytesCodec(new Configuration(Endian.LITTLE)), new Crc32cCodec()},
"end")));
} catch (ZarrException e) {
throw new RuntimeException(e);
}
return this;
}
public CodecBuilder withSharding(int[] chunkShape,
Function<CodecBuilder, CodecBuilder> codecBuilder) {
return withSharding(chunkShape, codecBuilder, "end");
}
public CodecBuilder withSharding(int[] chunkShape,
Function<CodecBuilder, CodecBuilder> codecBuilder, String indexLocation) {
CodecBuilder nestedBuilder = new CodecBuilder((DataType) dataType);
try {
codecs.add(new ShardingIndexedCodec(
new ShardingIndexedCodec.Configuration(chunkShape,
codecBuilder.apply(nestedBuilder).build(),
new Codec[]{new BytesCodec(Endian.LITTLE), new Crc32cCodec()},
indexLocation)));
} catch (ZarrException e) {
throw new RuntimeException(e);
}
return this;
}
public CodecBuilder withCrc32c() {
codecs.add(new Crc32cCodec());
return this;
}
private void autoInsertBytesCodec() {
if (codecs.stream().noneMatch(c -> c instanceof ArrayBytesCodec)) {
Codec[] arrayArrayCodecs = codecs.stream().filter(c -> c instanceof ArrayArrayCodec)
.toArray(Codec[]::new);
Codec[] bytesBytesCodecs = codecs.stream().filter(c -> c instanceof BytesBytesCodec)
.toArray(Codec[]::new);
this.codecs = new ArrayList<>();
Collections.addAll(this.codecs, arrayArrayCodecs);
this.codecs.add(new BytesCodec(new BytesCodec.Configuration(Endian.LITTLE)));
Collections.addAll(this.codecs, bytesBytesCodecs);
}
}
public Codec[] build() {
autoInsertBytesCodec();
return codecs.toArray(new Codec[0]);
}
}