Skip to content

Storage transformers #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/main/java/dev/zarr/zarrjava/v3/ArrayMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public final class ArrayMetadata {
@Nullable
@JsonProperty("dimension_names")
public String[] dimensionNames;
@Nullable
@JsonProperty("storage_transformers")
public Map<String, Object>[] storageTransformers;

@JsonIgnore
public CoreArrayMetadata coreArrayMetadata;
Expand All @@ -64,11 +67,12 @@ public ArrayMetadata(
Object fillValue,
@Nonnull Codec[] codecs,
@Nullable String[] dimensionNames,
@Nullable Map<String, Object> attributes
@Nullable Map<String, Object> attributes,
@Nullable Map<String, Object>[] storageTransformers
) throws ZarrException {
this(ZARR_FORMAT, NODE_TYPE, shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
dimensionNames,
attributes
attributes, storageTransformers
);
}

Expand All @@ -83,7 +87,8 @@ public ArrayMetadata(
@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") Map<String, Object> attributes
@Nullable @JsonProperty(value = "attributes") Map<String, Object> attributes,
@Nullable @JsonProperty(value = "storage_transformers") Map<String, Object>[] storageTransformers
) throws ZarrException {
if (zarrFormat != this.zarrFormat) {
throw new ZarrException(
Expand Down Expand Up @@ -126,6 +131,7 @@ public ArrayMetadata(
this.codecs = codecs;
this.dimensionNames = dimensionNames;
this.attributes = attributes;
this.storageTransformers = storageTransformers;
this.coreArrayMetadata =
new CoreArrayMetadata(shape, ((RegularChunkGrid) chunkGrid).configuration.chunkShape,
dataType,
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/dev/zarr/zarrjava/v3/ArrayMetadataBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ArrayMetadataBuilder {
Object fillValue = 0;
Codec[] codecs = new Codec[]{new BytesCodec(Endian.LITTLE)};
Map<String, Object> attributes = new HashMap<>();
Map<String, Object>[] storageTransformers = new HashMap[]{};
String[] dimensionNames = null;

protected ArrayMetadataBuilder() {
Expand All @@ -44,6 +45,7 @@ protected static ArrayMetadataBuilder fromArrayMetadata(ArrayMetadata arrayMetad
builder.codecs = arrayMetadata.codecs;
builder.attributes = arrayMetadata.attributes;
builder.dimensionNames = arrayMetadata.dimensionNames;
builder.storageTransformers = arrayMetadata.storageTransformers;
return builder;
}

Expand Down Expand Up @@ -125,6 +127,10 @@ public ArrayMetadataBuilder withAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
return this;
}
public ArrayMetadataBuilder withStorageTransformers(Map<String, Object>[] storageTransformers) {
this.storageTransformers = storageTransformers;
return this;
}

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

return new ArrayMetadata(shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
dimensionNames,
attributes
attributes,
storageTransformers
);
}
}
28 changes: 28 additions & 0 deletions src/test/java/dev/zarr/zarrjava/ZarrTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.luben.zstd.Zstd;
import com.github.luben.zstd.ZstdCompressCtx;
import com.google.common.collect.Maps;
import dev.zarr.zarrjava.store.*;
import dev.zarr.zarrjava.utils.MultiArrayUtils;
import dev.zarr.zarrjava.v3.*;
Expand Down Expand Up @@ -631,4 +632,31 @@ public void testReadL4Sample(String mag) throws IOException, ZarrException {

assert MultiArrayUtils.allValuesEqual(httpData2, localData2);
}

@Test
public void testMetadataAcceptsStorageTransformer() throws ZarrException, IOException {
StoreHandle localStoreHandle = new FilesystemStore(TESTDATA).resolve("storage_transformer");
Map<String, Object>[] storageTransformers1 = Array.open(localStoreHandle).metadata.storageTransformers;

Array array2 = Array.create(
new FilesystemStore(TESTOUTPUT).resolve("storage_transformer"),
Array.metadataBuilder()
.withShape(1)
.withChunkShape(1)
.withDataType(DataType.UINT8)
.withStorageTransformers(new HashMap[]{new HashMap<String, Object>(){
{
put("name", "chunk-manifest-json");
put("configuration", new HashMap<String, Object>(){
{
put("manifest", "./manifest.json");
}
});
}
}})
.build()
);
Map<String, Object>[] storageTransformers2 = array2.metadata.storageTransformers;
assert Maps.difference(storageTransformers1[0], storageTransformers2[0]).areEqual();
}
}
40 changes: 40 additions & 0 deletions testdata/storage_transformer/zarr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"zarr_format": 3,
"node_type": "array",
"shape": [
1
],
"data_type": "uint8",
"chunk_grid": {
"name": "regular",
"configuration": {
"chunk_shape": [
1
]
}
},
"chunk_key_encoding": {
"name": "default",
"configuration": {
"separator": "/"
}
},
"fill_value": 0,
"codecs": [
{
"name": "bytes",
"configuration": {
"endian": "little"
}
}
],
"attributes": {},
"storage_transformers": [
{
"name": "chunk-manifest-json",
"configuration": {
"manifest": "./manifest.json"
}
}
]
}