-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathNode.java
More file actions
66 lines (57 loc) · 2.41 KB
/
Copy pathNode.java
File metadata and controls
66 lines (57 loc) · 2.41 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
package dev.zarr.zarrjava.v3;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import dev.zarr.zarrjava.ZarrException;
import dev.zarr.zarrjava.store.FilesystemStore;
import dev.zarr.zarrjava.store.StoreHandle;
import dev.zarr.zarrjava.utils.Utils;
import dev.zarr.zarrjava.v3.codec.CodecRegistry;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
public interface Node extends dev.zarr.zarrjava.core.Node{
static ObjectMapper makeObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new Jdk8Module());
objectMapper.registerSubtypes(CodecRegistry.getNamedTypes());
objectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
return objectMapper;
}
static ObjectWriter makeObjectWriter() {
return makeObjectMapper().writerWithDefaultPrettyPrinter();
}
/**
* Opens an existing Zarr array or group at a specified storage location.
*
* @param storeHandle the storage location of the Zarr array or group
* @throws IOException throws IOException if the metadata cannot be read
* @throws ZarrException throws ZarrException if the Zarr array or group cannot be opened
*/
static Node open(StoreHandle storeHandle) throws IOException, ZarrException {
ObjectMapper objectMapper = makeObjectMapper();
ByteBuffer metadataBytes = storeHandle.resolve(ZARR_JSON).readNonNull();
byte[] metadataBytearray = Utils.toArray(metadataBytes);
String nodeType = objectMapper.readTree(metadataBytearray)
.get("node_type")
.asText();
switch (nodeType) {
case ArrayMetadata.NODE_TYPE:
return new Array(storeHandle,
objectMapper.readValue(metadataBytearray, ArrayMetadata.class));
case GroupMetadata.NODE_TYPE:
return new Group(storeHandle,
objectMapper.readValue(metadataBytearray, GroupMetadata.class));
default:
throw new ZarrException("Unsupported node_type '" + nodeType + "' at " + storeHandle);
}
}
static Node open(Path path) throws IOException, ZarrException {
return open(new StoreHandle(new FilesystemStore(path)));
}
static Node open(String path) throws IOException, ZarrException {
return open(Paths.get(path));
}
}