-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBufferedZipStore.java
More file actions
333 lines (289 loc) · 12 KB
/
Copy pathBufferedZipStore.java
File metadata and controls
333 lines (289 loc) · 12 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package dev.zarr.zarrjava.store;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.stream.Stream;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
/**
* A Store implementation that buffers reads and writes and flushes them to an underlying Store as a zip file.
*/
public class BufferedZipStore extends ZipStore implements AutoCloseable {
private final Store.ListableStore bufferStore;
private final boolean flushOnWrite;
private final Comparator<String[]> zipEntryComparator = (a, b) -> {
boolean aIsZarr = a.length > 0 && a[a.length - 1].equals("zarr.json");
boolean bIsZarr = b.length > 0 && b[b.length - 1].equals("zarr.json");
// first all zarr.json files
if (aIsZarr && !bIsZarr) {
return -1;
} else if (!aIsZarr && bIsZarr) {
return 1;
} else if (aIsZarr && bIsZarr) {
// sort zarr.json in BFS order within same depth by lexicographical order
if (a.length != b.length) {
return Integer.compare(a.length, b.length);
} else {
return String.join("/", a).compareTo(String.join("/", b));
}
} else {
// then all other files in lexicographical order
return String.join("/", a).compareTo(String.join("/", b));
}
};
private String archiveComment;
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, @Nullable String archiveComment, boolean flushOnWrite) {
super(underlyingStore);
this.bufferStore = bufferStore;
this.archiveComment = archiveComment;
this.flushOnWrite = flushOnWrite;
try {
loadBuffer();
} catch (IOException e) {
throw StoreException.readFailed(
underlyingStore.toString(),
new String[]{},
new IOException("Failed to load ZIP buffer from underlying store: " + underlyingStore, e));
}
}
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, @Nullable String archiveComment) {
this(underlyingStore, bufferStore, archiveComment, false);
}
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore) {
this(underlyingStore, bufferStore, null);
}
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, String archiveComment) {
this(underlyingStore, new MemoryStore(), archiveComment);
}
public BufferedZipStore(@Nonnull StoreHandle underlyingStore) {
this(underlyingStore, (String) null);
}
public BufferedZipStore(@Nonnull Path underlyingStore, String archiveComment) {
this(new FilesystemStore(underlyingStore.toAbsolutePath().getParent()).resolve(underlyingStore.getFileName().toString()), archiveComment);
}
public BufferedZipStore(@Nonnull Path underlyingStore) {
this(underlyingStore, null);
}
public BufferedZipStore(@Nonnull String underlyingStorePath, String archiveComment) {
this(Paths.get(underlyingStorePath), archiveComment);
}
public BufferedZipStore(@Nonnull String underlyingStorePath) {
this(underlyingStorePath, null);
}
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, boolean flushOnWrite) {
this(underlyingStore, bufferStore, null, flushOnWrite);
}
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, String archiveComment, boolean flushOnWrite) {
this(underlyingStore, new MemoryStore(), archiveComment, flushOnWrite);
}
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, boolean flushOnWrite) {
this(underlyingStore, (String) null, flushOnWrite);
}
public BufferedZipStore(@Nonnull Path underlyingStore, String archiveComment, boolean flushOnWrite) {
this(new FilesystemStore(underlyingStore.toAbsolutePath().getParent()).resolve(underlyingStore.getFileName().toString()), archiveComment, flushOnWrite);
}
public BufferedZipStore(@Nonnull Path underlyingStore, boolean flushOnWrite) {
this(underlyingStore, null, flushOnWrite);
}
public BufferedZipStore(@Nonnull String underlyingStorePath, String archiveComment, boolean flushOnWrite) {
this(Paths.get(underlyingStorePath), archiveComment, flushOnWrite);
}
public BufferedZipStore(@Nonnull String underlyingStorePath, boolean flushOnWrite) {
this(underlyingStorePath, null, flushOnWrite);
}
private void writeBuffer() throws IOException {
// create zip file bytes from buffer store and write to underlying store
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(baos)) {
zos.setUseZip64(Zip64Mode.AsNeeded);
if (archiveComment != null) {
zos.setComment(archiveComment);
}
bufferStore.list().sorted(zipEntryComparator).forEach(keys -> {
try {
if (keys == null || keys.length == 0) {
// skip root entry
return;
}
String entryName = String.join("/", keys);
ByteBuffer bb = bufferStore.get(keys);
if (bb == null) {
// directory entry: ensure trailing slash
if (!entryName.endsWith("/")) {
entryName = entryName + "/";
}
ZipArchiveEntry dirEntry = new ZipArchiveEntry(entryName);
dirEntry.setMethod(ZipEntry.STORED);
dirEntry.setSize(0);
dirEntry.setCrc(0);
zos.putArchiveEntry(dirEntry);
zos.closeArchiveEntry();
} else {
// read bytes from ByteBuffer without modifying original
ByteBuffer dup = bb.duplicate();
int len = dup.remaining();
byte[] bytes = new byte[len];
dup.get(bytes);
// compute CRC and set size for STORED (no compression)
CRC32 crc = new CRC32();
crc.update(bytes, 0, bytes.length);
ZipArchiveEntry fileEntry = new ZipArchiveEntry(entryName);
fileEntry.setMethod(ZipEntry.STORED);
fileEntry.setSize(bytes.length);
fileEntry.setCrc(crc.getValue());
zos.putArchiveEntry(fileEntry);
zos.write(bytes);
zos.closeArchiveEntry();
}
} catch (IOException e) {
// wrap checked exception so it can be rethrown from stream for handling below
throw new RuntimeException(e);
}
});
zos.finish();
} catch (RuntimeException e) {
// unwrap and rethrow IOExceptions thrown inside the lambda
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
}
throw e;
}
byte[] zipBytes = baos.toByteArray();
// write zip bytes back to underlying store
underlyingStore.set(ByteBuffer.wrap(zipBytes));
}
public void deleteArchiveComment() throws IOException {
this.setArchiveComment(null);
}
/**
* Loads the buffer from the underlying store zip file.
*/
private void loadBuffer() throws IOException {
String loadedArchiveComment = super.getArchiveComment();
if (loadedArchiveComment != null && this.archiveComment == null) {
// don't overwrite existing archiveComment
this.archiveComment = loadedArchiveComment;
}
InputStream inputStream = underlyingStore.getInputStream();
if (inputStream == null) {
return;
}
try (ZipArchiveInputStream zis = new ZipArchiveInputStream(inputStream)) {
ZipArchiveEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] tmp = new byte[8192];
int read;
while ((read = zis.read(tmp)) != -1) {
baos.write(tmp, 0, read);
}
byte[] bytes = baos.toByteArray();
bufferStore.set(new String[]{entry.getName()}, ByteBuffer.wrap(bytes));
}
}
}
/**
* Flushes the buffer and archiveComment to the underlying store as a zip file.
*/
public void flush() throws IOException {
writeBuffer();
}
@Override
public void close() throws IOException {
flush();
}
@Override
public String getArchiveComment() {
return archiveComment;
}
public void setArchiveComment(@Nullable String archiveComment) throws IOException {
this.archiveComment = archiveComment;
if (flushOnWrite) {
writeBuffer();
}
}
@Override
public Stream<String[]> list(String[] keys) {
return bufferStore.list(keys);
}
@Override
public Stream<String> listChildren(String[] prefix) {
return bufferStore.listChildren(prefix);
}
@Override
public boolean exists(String[] keys) {
return bufferStore.exists(keys);
}
@Nullable
@Override
public ByteBuffer get(String[] keys) {
return bufferStore.get(keys);
}
@Nullable
@Override
public ByteBuffer get(String[] keys, long start) {
return bufferStore.get(keys, start);
}
@Nullable
@Override
public ByteBuffer get(String[] keys, long start, long end) {
return bufferStore.get(keys, start, end);
}
@Override
public void set(String[] keys, ByteBuffer bytes) {
bufferStore.set(keys, bytes);
if (flushOnWrite) {
try {
writeBuffer();
} catch (IOException e) {
throw StoreException.writeFailed(
underlyingStore.toString(),
keys,
new IOException("Failed to flush ZIP buffer to underlying store after set operation", e));
}
}
}
@Override
public void delete(String[] keys) {
bufferStore.delete(keys);
if (flushOnWrite) {
try {
writeBuffer();
} catch (IOException e) {
throw StoreException.deleteFailed(
underlyingStore.toString(),
keys,
new IOException("Failed to flush ZIP buffer to underlying store after delete operation", e));
}
}
}
@Nonnull
@Override
public StoreHandle resolve(String... keys) {
return new StoreHandle(this, keys);
}
@Override
public InputStream getInputStream(String[] keys, long start, long end) {
return bufferStore.getInputStream(keys, start, end);
}
public long getSize(String[] keys) {
return bufferStore.getSize(keys);
}
@Override
public String toString() {
return "BufferedZipStore(" + underlyingStore.toString() + ")";
}
}