-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathZarrGroup.java
210 lines (176 loc) · 7.26 KB
/
ZarrGroup.java
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
/*
*
* MIT License
*
* Copyright (c) 2020. Brockmann Consult GmbH (info@brockmann-consult.de)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package com.bc.zarr;
import com.bc.zarr.storage.FileSystemStore;
import com.bc.zarr.storage.InMemoryStore;
import com.bc.zarr.storage.Store;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import static com.bc.zarr.ZarrConstants.*;
public class ZarrGroup {
public static ZarrGroup create() throws IOException {
return create((String) null);
}
public static ZarrGroup create(String path) throws IOException {
return create(path, null);
}
/**
* @param path
* @param attributes
* @throws IOException
*/
public static ZarrGroup create(String path, final Map<String, Object> attributes) throws IOException {
if (path == null) {
return create(new InMemoryStore(), attributes);
}
return create(Paths.get(path), attributes);
}
public static ZarrGroup create(Path fileSystemPath) throws IOException {
return create(fileSystemPath, null);
}
public static ZarrGroup create(Path fileSystemPath, final Map<String, Object> attributes) throws IOException {
return create(new FileSystemStore(fileSystemPath), attributes);
}
public static ZarrGroup create(Store store) throws IOException {
return create(store, null);
}
public static ZarrGroup create(Store store, final Map<String, Object> attributes) throws IOException {
ZarrGroup zarrGroup = new ZarrGroup(store);
zarrGroup.createHeader();
zarrGroup.writeAttributes(attributes);
return zarrGroup;
}
public static ZarrGroup open(String path) throws IOException {
if (path == null) {
return create();
}
return open(Paths.get(path));
}
public static ZarrGroup open(Path fileSystemPath) throws IOException {
if (fileSystemPath == null) {
return create();
}
ZarrUtils.ensureDirectory(fileSystemPath);
return open(new FileSystemStore(fileSystemPath));
}
public static ZarrGroup open(Store store) throws IOException {
if (store == null) {
return create();
}
validateGroupToBeOpened(store, new ZarrPath(""));
return new ZarrGroup(store);
}
private static void validateGroupToBeOpened(Store store, ZarrPath relativePath) throws IOException {
try (InputStream is = store.getInputStream(relativePath.resolve(FILENAME_DOT_ZGROUP).storeKey)) {
if (is == null) {
throw new IOException("'" + FILENAME_DOT_ZGROUP + "' expected but is not readable or missing in store.");
}
ensureZarrFormatIs2(is);
}
}
private static void ensureZarrFormatIs2(InputStream is) throws IOException {
try (
final InputStreamReader in = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(in)
) {
final ZarrFormat fromJson = ZarrUtils.fromJson(reader, ZarrFormat.class);
if (fromJson.zarr_format != 2) {
throw new IOException("Zarr format 2 expected but is '" + fromJson.zarr_format + "'");
}
}
}
public ZarrGroup createSubGroup(String subGroupName) throws IOException {
return createSubGroup(subGroupName, null);
}
public ZarrGroup createSubGroup(String subGroupName, Map<String, Object> attributes) throws IOException {
final ZarrPath relativePath = this.relativePath.resolve(subGroupName);
final ZarrGroup group = new ZarrGroup(store, relativePath);
group.createHeader();
group.writeAttributes(attributes);
return group;
}
public ZarrGroup openSubGroup(String subGroupName) throws IOException {
final ZarrPath relativePath = this.relativePath.resolve(subGroupName);
validateGroupToBeOpened(store, relativePath);
return new ZarrGroup(store, relativePath);
}
private final static class ZarrFormat {
public double zarr_format;
}
private final Store store;
private final ZarrPath relativePath;
private ZarrGroup(Store store) {
this.relativePath = new ZarrPath("");
this.store = store;
}
private ZarrGroup(Store store, ZarrPath relativePath) {
this.relativePath = relativePath;
this.store = store;
}
public ZarrArray createArray(String name, ArrayParams params) throws IOException {
return createArray(name, params, null);
}
public ZarrArray createArray(String name, ArrayParams params, final Map<String, Object> attributes) throws IOException {
final ZarrPath relativePath = this.relativePath.resolve(name);
return ZarrArray.create(relativePath, store, params, attributes);
}
public ZarrArray openArray(String name) throws IOException {
final ZarrPath relativePath = this.relativePath.resolve(name);
return ZarrArray.open(relativePath, store);
}
public Set<String> getArrayKeys() throws IOException {
return store.getArrayKeys();
}
public Set<String> getGroupKeys() throws IOException {
final Set<String> groupKeys = store.getGroupKeys();
groupKeys.remove("");
return groupKeys;
}
public void writeAttributes(Map<String, Object> attributes) throws IOException {
ZarrUtils.writeAttributes(attributes, relativePath, store);
}
public Map<String, Object> getAttributes() throws IOException {
return ZarrUtils.readAttributes(relativePath, store);
}
@Override
public String toString() {
return getClass().getCanonicalName() + "{'/" + relativePath.storeKey + "'}";
}
private void createHeader() throws IOException {
final Map<String, Integer> singletonMap = Collections.singletonMap(ZARR_FORMAT, 2);
final ZarrPath headerPath = relativePath.resolve(FILENAME_DOT_ZGROUP);
try (
final OutputStream os = store.getOutputStream(headerPath.storeKey);
final OutputStreamWriter writer = new OutputStreamWriter(os)
) {
ZarrUtils.toJson(singletonMap, writer, true);
}
}
}