Skip to content

Commit e758d54

Browse files
authored
Merge pull request #15 from scalableminds/storage-transformers
Storage transformers
2 parents 87c04fa + 8e218e7 commit e758d54

File tree

5 files changed

+123
-5
lines changed

5 files changed

+123
-5
lines changed

src/main/java/dev/zarr/zarrjava/v3/ArrayMetadata.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public final class ArrayMetadata {
5555
@Nullable
5656
@JsonProperty("dimension_names")
5757
public String[] dimensionNames;
58+
@Nullable
59+
@JsonProperty("storage_transformers")
60+
public Map<String, Object>[] storageTransformers;
5861

5962
@JsonIgnore
6063
public CoreArrayMetadata coreArrayMetadata;
@@ -64,11 +67,12 @@ public ArrayMetadata(
6467
Object fillValue,
6568
@Nonnull Codec[] codecs,
6669
@Nullable String[] dimensionNames,
67-
@Nullable Map<String, Object> attributes
70+
@Nullable Map<String, Object> attributes,
71+
@Nullable Map<String, Object>[] storageTransformers
6872
) throws ZarrException {
6973
this(ZARR_FORMAT, NODE_TYPE, shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
7074
dimensionNames,
71-
attributes
75+
attributes, storageTransformers
7276
);
7377
}
7478

@@ -83,7 +87,8 @@ public ArrayMetadata(
8387
@JsonProperty(value = "fill_value", required = true) Object fillValue,
8488
@Nonnull @JsonProperty(value = "codecs") Codec[] codecs,
8589
@Nullable @JsonProperty(value = "dimension_names") String[] dimensionNames,
86-
@Nullable @JsonProperty(value = "attributes") Map<String, Object> attributes
90+
@Nullable @JsonProperty(value = "attributes") Map<String, Object> attributes,
91+
@Nullable @JsonProperty(value = "storage_transformers") Map<String, Object>[] storageTransformers
8792
) throws ZarrException {
8893
if (zarrFormat != this.zarrFormat) {
8994
throw new ZarrException(
@@ -93,7 +98,10 @@ public ArrayMetadata(
9398
throw new ZarrException(
9499
"Expected node type '" + this.nodeType + "', got '" + nodeType + "'.");
95100
}
96-
101+
if (storageTransformers != null && storageTransformers.length > 0) {
102+
throw new ZarrException(
103+
"Storage transformers are not supported in this version of Zarr Java.");
104+
}
97105
if (chunkGrid instanceof RegularChunkGrid) {
98106
int[] chunkShape = ((RegularChunkGrid) chunkGrid).configuration.chunkShape;
99107
if (shape.length != chunkShape.length) {
@@ -126,6 +134,7 @@ public ArrayMetadata(
126134
this.codecs = codecs;
127135
this.dimensionNames = dimensionNames;
128136
this.attributes = attributes;
137+
this.storageTransformers = storageTransformers;
129138
this.coreArrayMetadata =
130139
new CoreArrayMetadata(shape, ((RegularChunkGrid) chunkGrid).configuration.chunkShape,
131140
dataType,

src/main/java/dev/zarr/zarrjava/v3/ArrayMetadataBuilder.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class ArrayMetadataBuilder {
2929
Object fillValue = 0;
3030
Codec[] codecs = new Codec[]{new BytesCodec(Endian.LITTLE)};
3131
Map<String, Object> attributes = new HashMap<>();
32+
Map<String, Object>[] storageTransformers = new HashMap[]{};
3233
String[] dimensionNames = null;
3334

3435
protected ArrayMetadataBuilder() {
@@ -44,6 +45,7 @@ protected static ArrayMetadataBuilder fromArrayMetadata(ArrayMetadata arrayMetad
4445
builder.codecs = arrayMetadata.codecs;
4546
builder.attributes = arrayMetadata.attributes;
4647
builder.dimensionNames = arrayMetadata.dimensionNames;
48+
builder.storageTransformers = arrayMetadata.storageTransformers;
4749
return builder;
4850
}
4951

@@ -125,6 +127,10 @@ public ArrayMetadataBuilder withAttributes(Map<String, Object> attributes) {
125127
this.attributes = attributes;
126128
return this;
127129
}
130+
public ArrayMetadataBuilder withStorageTransformers(Map<String, Object>[] storageTransformers) {
131+
this.storageTransformers = storageTransformers;
132+
return this;
133+
}
128134

129135
public ArrayMetadata build() throws ZarrException {
130136
if (shape == null) {
@@ -140,7 +146,8 @@ public ArrayMetadata build() throws ZarrException {
140146

141147
return new ArrayMetadata(shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
142148
dimensionNames,
143-
attributes
149+
attributes,
150+
storageTransformers
144151
);
145152
}
146153
}

src/test/java/dev/zarr/zarrjava/ZarrTest.java

+29
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import com.amazonaws.auth.AWSStaticCredentialsProvider;
44
import com.amazonaws.auth.AnonymousAWSCredentials;
55
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
6+
import com.fasterxml.jackson.databind.JsonMappingException;
67
import com.fasterxml.jackson.databind.ObjectMapper;
78
import com.github.luben.zstd.Zstd;
89
import com.github.luben.zstd.ZstdCompressCtx;
10+
import com.google.common.collect.Maps;
911
import dev.zarr.zarrjava.store.*;
1012
import dev.zarr.zarrjava.utils.MultiArrayUtils;
1113
import dev.zarr.zarrjava.v3.*;
@@ -654,5 +656,32 @@ public void testParallel(boolean useParallel) throws IOException, ZarrException
654656
Assertions.assertArrayEquals(testData, (int[]) result.get1DJavaArray(ucar.ma2.DataType.UINT));
655657
clearTestoutputFolder();
656658
}
659+
660+
@Test
661+
public void testMetadataAcceptsEmptyStorageTransformer() throws ZarrException, IOException {
662+
// non-empty storage transformers are currently not supported
663+
664+
Map<String, Object>[] storageTransformersEmpty = Array.open(
665+
new FilesystemStore(TESTDATA).resolve("storage_transformer", "empty")
666+
).metadata.storageTransformers;
667+
assert storageTransformersEmpty.length == 0;
668+
669+
assertThrows(JsonMappingException.class, () -> Array.open(
670+
new FilesystemStore(TESTDATA).resolve("storage_transformer", "exists"))
671+
);
672+
673+
ArrayMetadataBuilder builderWithStorageTransformer = Array.metadataBuilder()
674+
.withShape(1)
675+
.withChunkShape(1)
676+
.withDataType(DataType.UINT8)
677+
.withStorageTransformers(new HashMap[]{new HashMap<String, Object>(){{
678+
put("some", "value");
679+
}}});
680+
681+
assertThrows(ZarrException.class, () -> Array.create(
682+
new FilesystemStore(TESTOUTPUT).resolve("storage_transformer"),
683+
builderWithStorageTransformer.build()
684+
));
685+
}
657686
}
658687

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"zarr_format": 3,
3+
"node_type": "array",
4+
"shape": [
5+
1
6+
],
7+
"data_type": "uint8",
8+
"chunk_grid": {
9+
"name": "regular",
10+
"configuration": {
11+
"chunk_shape": [
12+
1
13+
]
14+
}
15+
},
16+
"chunk_key_encoding": {
17+
"name": "default",
18+
"configuration": {
19+
"separator": "/"
20+
}
21+
},
22+
"fill_value": 0,
23+
"codecs": [
24+
{
25+
"name": "bytes",
26+
"configuration": {
27+
"endian": "little"
28+
}
29+
}
30+
],
31+
"attributes": {},
32+
"storage_transformers": []
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"zarr_format": 3,
3+
"node_type": "array",
4+
"shape": [
5+
1
6+
],
7+
"data_type": "uint8",
8+
"chunk_grid": {
9+
"name": "regular",
10+
"configuration": {
11+
"chunk_shape": [
12+
1
13+
]
14+
}
15+
},
16+
"chunk_key_encoding": {
17+
"name": "default",
18+
"configuration": {
19+
"separator": "/"
20+
}
21+
},
22+
"fill_value": 0,
23+
"codecs": [
24+
{
25+
"name": "bytes",
26+
"configuration": {
27+
"endian": "little"
28+
}
29+
}
30+
],
31+
"attributes": {},
32+
"storage_transformers": [
33+
{
34+
"name": "chunk-manifest-json",
35+
"configuration": {
36+
"manifest": "./manifest.json"
37+
}
38+
}
39+
]
40+
}

0 commit comments

Comments
 (0)