Skip to content

Commit 0ab59f6

Browse files
committed
implement setAttributes and updateAttributes for v2
1 parent 832ef4e commit 0ab59f6

File tree

8 files changed

+264
-160
lines changed

8 files changed

+264
-160
lines changed

src/main/java/dev/zarr/zarrjava/core/Attributes.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public Attributes(Map<String, Object> attributes) {
1717
super(attributes);
1818
}
1919

20+
public Attributes add(String s, Object o){
21+
this.put(s, o);
22+
return this;
23+
}
24+
25+
public Attributes delete(String s){
26+
this.remove(s);
27+
return this;
28+
}
29+
2030
public boolean getBoolean(String key) {
2131
Object value = this.get(key);
2232
if (value instanceof Boolean) {

src/main/java/dev/zarr/zarrjava/v2/Array.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,62 @@ public static ArrayMetadataBuilder metadataBuilder(ArrayMetadata existingMetadat
177177
return ArrayMetadataBuilder.fromArrayMetadata(existingMetadata);
178178
}
179179

180+
private Array writeMetadata(ArrayMetadata newArrayMetadata) throws ZarrException, IOException {
181+
return Array.create(storeHandle, newArrayMetadata, true);
182+
}
183+
184+
/**
185+
* Sets a new shape for the Zarr array. It only changes the metadata, no array data is modified or
186+
* deleted. This method returns a new instance of the Zarr array class and the old instance
187+
* becomes invalid.
188+
*
189+
* @param newShape the new shape of the Zarr array
190+
* @throws ZarrException if the new metadata is invalid
191+
* @throws IOException throws IOException if the new metadata cannot be serialized
192+
*/
193+
public Array resize(long[] newShape) throws ZarrException, IOException {
194+
//TODO: test
195+
if (newShape.length != metadata.ndim()) {
196+
throw new IllegalArgumentException(
197+
"'newShape' needs to have rank '" + metadata.ndim() + "'.");
198+
}
199+
200+
ArrayMetadata newArrayMetadata = ArrayMetadataBuilder.fromArrayMetadata(metadata)
201+
.withShape(newShape)
202+
.build();
203+
return writeMetadata(newArrayMetadata);
204+
}
205+
206+
/**
207+
* Sets the attributes of the Zarr array. It overwrites and removes any existing attributes. This
208+
* method returns a new instance of the Zarr array class and the old instance becomes invalid.
209+
*
210+
* @param newAttributes the new attributes of the Zarr array
211+
* @throws ZarrException throws ZarrException if the new metadata is invalid
212+
* @throws IOException throws IOException if the new metadata cannot be serialized
213+
*/
214+
public Array setAttributes(Attributes newAttributes) throws ZarrException, IOException {
215+
ArrayMetadata newArrayMetadata =
216+
ArrayMetadataBuilder.fromArrayMetadata(metadata)
217+
.withAttributes(newAttributes)
218+
.build();
219+
return writeMetadata(newArrayMetadata);
220+
}
221+
222+
/**
223+
* Updates the attributes of the Zarr array. It provides a callback that gets the current
224+
* attributes as input and needs to return the new set of attributes. The attributes in the
225+
* callback may be mutated. This method overwrites and removes any existing attributes. This
226+
* method returns a new instance of the Zarr array class and the old instance becomes invalid.
227+
*
228+
* @param attributeMapper the callback that is used to construct the new attributes
229+
* @throws ZarrException throws ZarrException if the new metadata is invalid
230+
* @throws IOException throws IOException if the new metadata cannot be serialized
231+
*/
232+
public Array updateAttributes(Function<Attributes, Attributes> attributeMapper) throws ZarrException, IOException {
233+
return setAttributes(attributeMapper.apply(metadata.attributes));
234+
}
235+
180236
@Override
181237
public String toString() {
182238
return String.format("<v2.Array {%s} (%s) %s>", storeHandle,

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,7 @@ public static ArrayMetadataBuilder metadataBuilder(ArrayMetadata existingMetadat
183183
}
184184

185185
private Array writeMetadata(ArrayMetadata newArrayMetadata) throws ZarrException, IOException {
186-
ObjectMapper objectMapper = makeObjectMapper();
187-
ByteBuffer metadataBytes = ByteBuffer.wrap(objectMapper.writeValueAsBytes(newArrayMetadata));
188-
storeHandle.resolve(ZARR_JSON)
189-
.set(metadataBytes);
190-
return new Array(storeHandle, newArrayMetadata);
186+
return Array.create(storeHandle, newArrayMetadata, true);
191187
}
192188

193189
/**
@@ -238,8 +234,7 @@ public Array setAttributes(Attributes newAttributes) throws ZarrException, IOExc
238234
* @throws IOException throws IOException if the new metadata cannot be serialized
239235
*/
240236
public Array updateAttributes(Function<Attributes, Attributes> attributeMapper) throws ZarrException, IOException {
241-
return setAttributes(attributeMapper.apply(new Attributes(metadata.attributes) {
242-
}));
237+
return setAttributes(attributeMapper.apply(metadata.attributes));
243238
}
244239

245240
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public Attributes attributes() throws ZarrException {
150150
if (attributes == null) {
151151
throw new ZarrException("Array attributes have not been set.");
152152
}
153-
return new Attributes(attributes); //todo change attributes to Attributes type
153+
return attributes;
154154
}
155155

156156
public static Optional<Codec> getShardingIndexedCodec(Codec[] codecs) {

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

Lines changed: 0 additions & 139 deletions
This file was deleted.

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

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package dev.zarr.zarrjava;
22

3+
import dev.zarr.zarrjava.core.Attributes;
4+
import org.junit.jupiter.api.Assertions;
35
import org.junit.jupiter.api.BeforeAll;
46

5-
import java.io.File;
67
import java.io.IOException;
78
import java.nio.file.Files;
89
import java.nio.file.Path;
910
import java.nio.file.Paths;
11+
import java.util.ArrayList;
1012
import java.util.Comparator;
13+
import java.util.List;
1114
import java.util.stream.Stream;
1215

1316
public class ZarrTest {
@@ -30,4 +33,81 @@ public static void clearTestoutputFolder() throws IOException {
3033
}
3134
Files.createDirectory(TESTOUTPUT);
3235
}
36+
37+
protected void assertListEquals(List<Object> a, List<Object> b) {
38+
Assertions.assertEquals(a.size(), b.size());
39+
for (int i = 0; i < a.size(); i++) {
40+
Object aval = a.get(i);
41+
Object bval = b.get(i);
42+
if (aval instanceof List && bval instanceof List) {
43+
assertListEquals((List<Object>) aval, (List<Object>) bval);
44+
} else {
45+
Assertions.assertEquals(aval, bval);
46+
}
47+
}
48+
}
49+
50+
protected Attributes defaultTestAttributes() {
51+
return new Attributes() {{
52+
put("string", "stringvalue");
53+
put("int", 42);
54+
put("float", 0.5f);
55+
put("double", 3.14);
56+
put("boolean", true);
57+
put("list", new ArrayList<Object>() {
58+
{
59+
add(1);
60+
add(2.0d);
61+
add("string");
62+
}
63+
});
64+
put("int_array", new int[]{1, 2, 3});
65+
put("long_array", new long[]{1, 2, 3});
66+
put("double_array", new double[]{1.0, 2.0, 3.0});
67+
put("float_array", new float[]{1.0f, 2.0f, 3.0f});
68+
put("nested", new Attributes() {{
69+
put("element", "value");
70+
}});
71+
put("array_of_attributes", new Attributes[]{
72+
new Attributes() {{
73+
put("a", 1);
74+
}},
75+
new Attributes() {{
76+
put("b", 2);
77+
}}
78+
});
79+
}};
80+
}
81+
82+
protected void assertContainsTestAttributes(Attributes attributes) throws ZarrException {
83+
Assertions.assertEquals("stringvalue", attributes.getString("string"));
84+
Assertions.assertEquals(42, attributes.getInt("int"));
85+
Assertions.assertEquals(0.5, attributes.getFloat("float"));
86+
Assertions.assertEquals(3.14, attributes.getDouble("double"));
87+
Assertions.assertTrue(attributes.getBoolean("boolean"));
88+
assertListEquals(new ArrayList<Object>() {
89+
{
90+
add(1);
91+
add(2.0d);
92+
add("string");
93+
}
94+
}, attributes.getList("list"));
95+
Assertions.assertArrayEquals(new int[]{1, 2, 3}, attributes.getIntArray("int_array"));
96+
Assertions.assertArrayEquals(new long[]{1, 2, 3}, attributes.getLongArray("long_array"));
97+
Assertions.assertArrayEquals(new double[]{1, 2, 3}, attributes.getDoubleArray("double_array"));
98+
Assertions.assertArrayEquals(new float[]{1, 2, 3}, attributes.getFloatArray("float_array"));
99+
Assertions.assertEquals("value", attributes.getAttributes("nested").getString("element"));
100+
Assertions.assertArrayEquals(
101+
new Attributes[]{
102+
new Attributes() {{
103+
put("a", 1);
104+
}},
105+
new Attributes() {{
106+
put("b", 2);
107+
}}
108+
},
109+
attributes.getArray("array_of_attributes", Attributes.class)
110+
);
111+
}
112+
33113
}

0 commit comments

Comments
 (0)