-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathArray.java
More file actions
177 lines (161 loc) · 6.92 KB
/
Copy pathArray.java
File metadata and controls
177 lines (161 loc) · 6.92 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
package dev.zarr.zarrjava.v2;
import com.fasterxml.jackson.databind.ObjectWriter;
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.core.codec.CodecPipeline;
import dev.zarr.zarrjava.v2.codec.Codec;
import dev.zarr.zarrjava.v2.codec.core.BytesCodec;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Collectors;
import static dev.zarr.zarrjava.v2.Node.makeObjectMapper;
import static dev.zarr.zarrjava.v2.Node.makeObjectWriter;
public class Array extends dev.zarr.zarrjava.core.Array implements Node {
private final ArrayMetadata metadata;
public ArrayMetadata metadata() {
return metadata;
}
protected Array(StoreHandle storeHandle, ArrayMetadata arrayMetadata) throws IOException, ZarrException {
super(storeHandle);
this.metadata = arrayMetadata;
this.codecPipeline = new CodecPipeline(Utils.concatArrays(
new Codec[]{},
metadata.filters == null ? new Codec[]{} : metadata.filters,
new Codec[]{new BytesCodec(arrayMetadata.endianness.toEndian())},
metadata.compressor == null ? new Codec[]{} : new Codec[]{metadata.compressor}
), metadata.coreArrayMetadata);
}
/**
* Opens an existing Zarr array at a specified storage location.
*
* @param storeHandle the storage location of the Zarr array
* @throws IOException throws IOException if the metadata cannot be read
* @throws ZarrException throws ZarrException if the Zarr array cannot be opened
*/
public static Array open(StoreHandle storeHandle) throws IOException, ZarrException {
return new Array(
storeHandle,
makeObjectMapper()
.readValue(
Utils.toArray(storeHandle.resolve(ZARRAY).readNonNull()),
ArrayMetadata.class
)
);
}
/**
* Opens an existing Zarr array at a specified storage location.
*
* @param path the storage location of the Zarr array
* @throws IOException throws IOException if the metadata cannot be read
* @throws ZarrException throws ZarrException if the Zarr array cannot be opened
*/
public static Array open(Path path) throws IOException, ZarrException {
return open(new StoreHandle(new FilesystemStore(path)));
}
/**
* Opens an existing Zarr array at a specified storage location.
*
* @param path the storage location of the Zarr array
* @throws IOException throws IOException if the metadata cannot be read
* @throws ZarrException throws ZarrException if the Zarr array cannot be opened
*/
public static Array open(String path) throws IOException, ZarrException {
return open(Paths.get(path));
}
/**
* Creates a new Zarr array with the provided metadata at a specified storage location. This
* method will raise an exception if a Zarr array already exists at the specified storage
* location.
*
* @param path the storage location of the Zarr array
* @param arrayMetadata the metadata of the Zarr array
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the Zarr array cannot be created
*/
public static Array create(Path path, ArrayMetadata arrayMetadata)
throws IOException, ZarrException {
return create(new StoreHandle(new FilesystemStore(path)), arrayMetadata);
}
/**
* Creates a new Zarr array with the provided metadata at a specified storage location. This
* method will raise an exception if a Zarr array already exists at the specified storage
* location.
*
* @param path the storage location of the Zarr array
* @param arrayMetadata the metadata of the Zarr array
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the Zarr array cannot be created
*/
public static Array create(String path, ArrayMetadata arrayMetadata)
throws IOException, ZarrException {
return create(Paths.get(path), arrayMetadata);
}
/**
* Creates a new Zarr array with the provided metadata at a specified storage location. This
* method will raise an exception if a Zarr array already exists at the specified storage
* location.
*
* @param storeHandle the storage location of the Zarr array
* @param arrayMetadata the metadata of the Zarr array
* @throws IOException if the metadata cannot be serialized
* @throws ZarrException if the Zarr array cannot be created
*/
public static Array create(StoreHandle storeHandle, ArrayMetadata arrayMetadata)
throws IOException, ZarrException {
return Array.create(storeHandle, arrayMetadata, false);
}
/**
* Creates a new Zarr array with the provided metadata at a specified storage location. If
* `existsOk` is false, this method will raise an exception if a Zarr array already exists at the
* specified storage location.
*
* @param storeHandle the storage location of the Zarr array
* @param arrayMetadata the metadata of the Zarr array
* @param existsOk if true, no exception is raised if the Zarr array already exists
* @throws IOException throws IOException if the metadata cannot be serialized
* @throws ZarrException throws ZarrException if the Zarr array cannot be created
*/
public static Array create(StoreHandle storeHandle, ArrayMetadata arrayMetadata, boolean existsOk)
throws IOException, ZarrException {
StoreHandle metadataHandle = storeHandle.resolve(ZARRAY);
if (!existsOk && metadataHandle.exists()) {
throw new RuntimeException(
"Trying to create a new array in " + storeHandle + ". But " + metadataHandle
+ " already exists.");
}
ObjectWriter objectWriter = makeObjectWriter();
ByteBuffer metadataBytes = ByteBuffer.wrap(objectWriter.writeValueAsBytes(arrayMetadata));
metadataHandle.set(metadataBytes);
return new Array(storeHandle, arrayMetadata);
}
public static Array create(StoreHandle storeHandle,
Function<ArrayMetadataBuilder, ArrayMetadataBuilder> arrayMetadataBuilderMapper,
boolean existsOk) throws IOException, ZarrException {
return create(storeHandle,
arrayMetadataBuilderMapper.apply(new ArrayMetadataBuilder()).build(), existsOk);
}
@Nonnull
public static ArrayMetadataBuilder metadataBuilder() {
return new ArrayMetadataBuilder();
}
@Nonnull
public static ArrayMetadataBuilder metadataBuilder(ArrayMetadata existingMetadata) {
return ArrayMetadataBuilder.fromArrayMetadata(existingMetadata);
}
@Override
public String toString() {
return String.format("<v2.Array {%s} (%s) %s>", storeHandle,
Arrays.stream(metadata.shape)
.mapToObj(Long::toString)
.collect(Collectors.joining(", ")),
metadata.dataType
);
}
}