-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathGroup.java
More file actions
300 lines (272 loc) · 11.7 KB
/
Copy pathGroup.java
File metadata and controls
300 lines (272 loc) · 11.7 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package dev.zarr.zarrjava.v2;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import dev.zarr.zarrjava.ZarrException;
import dev.zarr.zarrjava.core.Attributes;
import dev.zarr.zarrjava.store.FilesystemStore;
import dev.zarr.zarrjava.store.MemoryStore;
import dev.zarr.zarrjava.store.StoreHandle;
import dev.zarr.zarrjava.utils.Utils;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;
import static dev.zarr.zarrjava.v2.Node.makeObjectMapper;
import static dev.zarr.zarrjava.v2.Node.makeObjectWriter;
public class Group extends dev.zarr.zarrjava.core.Group implements Node {
public GroupMetadata metadata;
protected Group(@Nonnull StoreHandle storeHandle, @Nonnull GroupMetadata groupMetadata) throws IOException {
super(storeHandle);
this.metadata = groupMetadata;
}
/**
* Opens an existing Zarr group at a specified storage location.
*
* @param storeHandle the storage location of the Zarr group
* @throws IOException if the metadata cannot be read
*/
public static Group open(@Nonnull StoreHandle storeHandle) throws IOException {
ObjectMapper mapper = makeObjectMapper();
GroupMetadata metadata = mapper.readValue(
Utils.toArray(storeHandle.resolve(ZGROUP).readNonNull()),
GroupMetadata.class
);
if (storeHandle.resolve(ZATTRS).exists())
metadata.attributes = mapper.readValue(
Utils.toArray(storeHandle.resolve(ZATTRS).readNonNull()),
dev.zarr.zarrjava.core.Attributes.class
);
return new Group(storeHandle, metadata);
}
/**
* Opens an existing Zarr group at a specified storage location.
*
* @param path the storage location of the Zarr group
* @throws IOException if the metadata cannot be read
*/
public static Group open(Path path) throws IOException {
return open(new StoreHandle(new FilesystemStore(path)));
}
/**
* Opens an existing Zarr group at a specified storage location.
*
* @param path the storage location of the Zarr group
* @throws IOException if the metadata cannot be read
*/
public static Group open(String path) throws IOException {
return open(Paths.get(path));
}
/**
* Creates a new Zarr group with the provided metadata in an in-memory store.
*
* @param groupMetadata the metadata of the Zarr group
* @throws IOException if the metadata cannot be serialized
*/
public static Group create(@Nonnull GroupMetadata groupMetadata) throws IOException {
return new Group(new MemoryStore().resolve(), groupMetadata).writeMetadata();
}
/**
* Creates a new Zarr group with the provided metadata at a specified storage location.
*
* @param storeHandle the storage location of the Zarr group
* @param groupMetadata the metadata of the Zarr group
* @throws IOException if the metadata cannot be serialized
*/
public static Group create(
@Nonnull StoreHandle storeHandle, @Nonnull GroupMetadata groupMetadata
) throws IOException {
return new Group(storeHandle, groupMetadata).writeMetadata();
}
/**
* Creates a new Zarr group with default metadata at a specified storage location.
*
* @param storeHandle the storage location of the Zarr group
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the metadata is invalid
*/
public static Group create(@Nonnull StoreHandle storeHandle) throws IOException, ZarrException {
return create(storeHandle, new GroupMetadata());
}
/**
* Creates a new Zarr group with the provided attributes at a specified storage location.
*
* @param storeHandle the storage location of the Zarr group
* @param attributes the attributes of the Zarr group
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the attributes are invalid
*/
public static Group create(@Nonnull StoreHandle storeHandle, Attributes attributes) throws IOException, ZarrException {
return create(storeHandle, new GroupMetadata(attributes));
}
/**
* Creates a new Zarr group with default metadata at a specified storage location.
*
* @param path the storage location of the Zarr group
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the metadata is invalid
*/
public static Group create(Path path) throws IOException, ZarrException {
return create(new StoreHandle(new FilesystemStore(path)));
}
/**
* Creates a new Zarr group with the provided attributes at a specified storage location.
*
* @param path the storage location of the Zarr group
* @param attributes the attributes of the Zarr group
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the attributes are invalid
*/
public static Group create(Path path, Attributes attributes) throws IOException, ZarrException {
return create(new StoreHandle(new FilesystemStore(path)), attributes);
}
/**
* Creates a new Zarr group with default metadata at a specified storage location.
*
* @param path the storage location of the Zarr group
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the metadata is invalid
*/
public static Group create(String path) throws IOException, ZarrException {
return create(Paths.get(path));
}
/**
* Creates a new Zarr group with the provided attributes at a specified storage location.
*
* @param path the storage location of the Zarr group
* @param attributes the attributes of the Zarr group
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the attributes are invalid
*/
public static Group create(String path, Attributes attributes) throws IOException, ZarrException {
return create(Paths.get(path), attributes);
}
/**
* Retrieves a node (group or array) at the specified key within the current group.
*
* @param key the key of the node to retrieve
* @return the node at the specified key, or null if it does not exist
* @throws ZarrException if the node cannot be opened
* @throws IOException if there is an error accessing the storage
*/
@Nullable
public Node get(String[] key) throws ZarrException, IOException {
StoreHandle keyHandle = storeHandle.resolve(key);
try {
return Node.open(keyHandle);
} catch (NoSuchFileException e) {
return null;
}
}
@Override
public Stream<dev.zarr.zarrjava.core.Node> list() {
return storeHandle.list().map(key -> {
if (key.length <= 1) return null; // exclude root from list
String fileName = key[key.length - 1];
StoreHandle parent = storeHandle.resolve(Arrays.copyOf(key, key.length - 1));
try {
if (fileName.equals(ZARRAY)) {
return Array.open(parent);
}
if (fileName.equals(ZGROUP)) {
return (dev.zarr.zarrjava.core.Node) Group.open(parent);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}).filter(Objects::nonNull);
}
/**
* Creates a new subgroup with default metadata at the specified key.
*
* @param key the key of the new Zarr group within the current group
* @return the created subgroup
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the group cannot be created
*/
public Group createGroup(String key) throws IOException, ZarrException {
return Group.create(storeHandle.resolve(key));
}
/**
* Creates a new array with the provided metadata at the specified key.
*
* @param key the key of the new Zarr array within the current group
* @param arrayMetadata the metadata of the Zarr array
* @return the created array
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the array cannot be created
*/
public Array createArray(String key, ArrayMetadata arrayMetadata)
throws IOException, ZarrException {
return Array.create(storeHandle.resolve(key), arrayMetadata);
}
/**
* Creates a new array with the provided metadata at the specified key.
*
* @param key the key of the new Zarr array within the current group
* @param arrayMetadataBuilderMapper a function that modifies the array metadata
* @return the created array
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the array cannot be created
*/
public Array createArray(String key, Function<ArrayMetadataBuilder, ArrayMetadataBuilder> arrayMetadataBuilderMapper)
throws IOException, ZarrException {
return Array.create(storeHandle.resolve(key), arrayMetadataBuilderMapper, false);
}
private Group writeMetadata() throws IOException {
return writeMetadata(this.metadata);
}
private Group writeMetadata(GroupMetadata newGroupMetadata) throws IOException {
ObjectWriter objectWriter = makeObjectWriter();
ByteBuffer metadataBytes = ByteBuffer.wrap(objectWriter.writeValueAsBytes(newGroupMetadata));
storeHandle.resolve(ZGROUP).set(metadataBytes);
if (newGroupMetadata.attributes != null) {
StoreHandle attrsHandle = storeHandle.resolve(ZATTRS);
ByteBuffer attrsBytes = ByteBuffer.wrap(
objectWriter.writeValueAsBytes(newGroupMetadata.attributes));
attrsHandle.set(attrsBytes);
}
this.metadata = newGroupMetadata;
return this;
}
/**
* Sets new attributes for the group, replacing any existing attributes.
*
* @param newAttributes the new attributes to set
* @return the updated group
* @throws ZarrException if the new attributes are invalid
* @throws IOException if the metadata cannot be serialized
*/
public Group setAttributes(Attributes newAttributes) throws ZarrException, IOException {
GroupMetadata newGroupMetadata = new GroupMetadata(newAttributes);
return writeMetadata(newGroupMetadata);
}
/**
* Updates the attributes of the group using a mapper function.
*
* @param attributeMapper a function that takes the current attributes and returns the updated attributes
* @return the updated group
* @throws ZarrException if the new attributes are invalid
* @throws IOException if the metadata cannot be serialized
*/
public Group updateAttributes(Function<Attributes, Attributes> attributeMapper)
throws ZarrException, IOException {
Attributes currentAttributes = metadata.attributes != null ? new Attributes(metadata.attributes) : new Attributes();
return setAttributes(attributeMapper.apply(currentAttributes));
}
@Override
public String toString() {
return String.format("<v2.Group {%s}>", storeHandle);
}
@Override
public GroupMetadata metadata() {
return metadata;
}
}