Skip to content

Commit 8b1e1ca

Browse files
committed
add BufferedZipStore parameter flushOnWrite
1 parent 6eb760c commit 8b1e1ca

File tree

2 files changed

+74
-19
lines changed

2 files changed

+74
-19
lines changed

src/main/java/dev/zarr/zarrjava/store/BufferedZipStore.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class BufferedZipStore implements Store, Store.ListableStore {
2525
private final StoreHandle underlyingStore;
2626
private final Store.ListableStore bufferStore;
2727
private String archiveComment;
28+
private boolean flushOnWrite;
2829

2930
private void writeBuffer() throws IOException{
3031
// create zip file bytes from buffer store and write to underlying store
@@ -147,17 +148,22 @@ private void loadBuffer() throws IOException{
147148
}
148149
}
149150

150-
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, @Nullable String archiveComment) {
151+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, @Nullable String archiveComment, boolean flushOnWrite) {
151152
this.underlyingStore = underlyingStore;
152153
this.bufferStore = bufferStore;
153154
this.archiveComment = archiveComment;
155+
this.flushOnWrite = flushOnWrite;
154156
try {
155157
loadBuffer();
156158
} catch (IOException e) {
157159
throw new RuntimeException("Failed to load buffer from underlying store", e);
158160
}
159161
}
160162

163+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, @Nullable String archiveComment) {
164+
this(underlyingStore, bufferStore, archiveComment, true);
165+
}
166+
161167
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore) {
162168
this(underlyingStore, bufferStore, null);
163169
}
@@ -186,6 +192,35 @@ public BufferedZipStore(@Nonnull String underlyingStorePath) {
186192
this(underlyingStorePath, null);
187193
}
188194

195+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, boolean flushOnWrite) {
196+
this(underlyingStore, bufferStore, null, flushOnWrite);
197+
}
198+
199+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, String archiveComment, boolean flushOnWrite) {
200+
this(underlyingStore, new MemoryStore(), archiveComment, flushOnWrite);
201+
}
202+
203+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, boolean flushOnWrite) {
204+
this(underlyingStore, (String) null, flushOnWrite);
205+
}
206+
207+
public BufferedZipStore(@Nonnull Path underlyingStore, String archiveComment, boolean flushOnWrite) {
208+
this(new FilesystemStore(underlyingStore.getParent()).resolve(underlyingStore.getFileName().toString()), archiveComment, flushOnWrite);
209+
}
210+
211+
public BufferedZipStore(@Nonnull Path underlyingStore, boolean flushOnWrite) {
212+
this(underlyingStore, null, flushOnWrite);
213+
}
214+
215+
public BufferedZipStore(@Nonnull String underlyingStorePath, String archiveComment, boolean flushOnWrite) {
216+
this(Paths.get(underlyingStorePath), archiveComment, flushOnWrite);
217+
}
218+
219+
public BufferedZipStore(@Nonnull String underlyingStorePath, boolean flushOnWrite) {
220+
this(underlyingStorePath, null, flushOnWrite);
221+
}
222+
223+
189224
/**
190225
* Flushes the buffer and archiveComment to the underlying store as a zip file.
191226
*/
@@ -228,11 +263,25 @@ public ByteBuffer get(String[] keys, long start, long end) {
228263
@Override
229264
public void set(String[] keys, ByteBuffer bytes) {
230265
bufferStore.set(keys, bytes);
266+
if (flushOnWrite) {
267+
try {
268+
writeBuffer();
269+
} catch (IOException e) {
270+
throw new RuntimeException("Failed to flush buffer to underlying store after set operation", e);
271+
}
272+
}
231273
}
232274

233275
@Override
234276
public void delete(String[] keys) {
235277
bufferStore.delete(keys);
278+
if (flushOnWrite) {
279+
try {
280+
writeBuffer();
281+
} catch (IOException e) {
282+
throw new RuntimeException("Failed to flush buffer to underlying store after delete operation", e);
283+
}
284+
}
236285
}
237286

238287
@Nonnull

src/test/java/dev/zarr/zarrjava/ZarrStoreTest.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,37 @@ public void testOpenZipStore() throws ZarrException, IOException {
156156

157157
BufferedZipStore zipStore = new BufferedZipStore(targetDir);
158158
assertIsTestGroupV3(Group.open(zipStore.resolve()), true);
159+
160+
ReadOnlyZipStore readOnlyZipStore = new ReadOnlyZipStore(targetDir);
161+
assertIsTestGroupV3(Group.open(readOnlyZipStore.resolve()), true);
159162
}
160163

161-
@Test
162-
public void testWriteZipStore() throws ZarrException, IOException {
163-
Path path = TESTOUTPUT.resolve("testWriteZipStore.zip");
164-
BufferedZipStore zipStore = new BufferedZipStore(path);
164+
@ParameterizedTest
165+
@CsvSource({"false", "true",})
166+
public void testWriteZipStore(boolean flushOnWrite) throws ZarrException, IOException {
167+
Path path = TESTOUTPUT.resolve("testWriteZipStore" + (flushOnWrite ? "Flush" : "NoFlush") + ".zip");
168+
BufferedZipStore zipStore = new BufferedZipStore(path, flushOnWrite);
165169
writeTestGroupV3(zipStore, true);
166-
zipStore.flush();
170+
if(!flushOnWrite) zipStore.flush();
167171

168172
BufferedZipStore zipStoreRead = new BufferedZipStore(path);
169173
assertIsTestGroupV3(Group.open(zipStoreRead.resolve()), true);
170174

171-
Path unzippedPath = TESTOUTPUT.resolve("testWriteZipStoreUnzipped");
175+
Path unzippedPath = TESTOUTPUT.resolve("testWriteZipStoreUnzipped" + (flushOnWrite ? "Flush" : "NoFlush"));
172176

173177
unzipFile(path, unzippedPath);
174178
FilesystemStore fsStore = new FilesystemStore(unzippedPath);
175179
assertIsTestGroupV3(Group.open(fsStore.resolve()), true);
176180
}
177181

178-
@Test
179-
public void testZipStoreWithComment() throws ZarrException, IOException {
180-
Path path = TESTOUTPUT.resolve("testZipStoreWithComment.zip");
182+
@ParameterizedTest
183+
@CsvSource({"false", "true",})
184+
public void testZipStoreWithComment(boolean flushOnWrite) throws ZarrException, IOException {
185+
Path path = TESTOUTPUT.resolve("testZipStoreWithComment"+ (flushOnWrite ? "Flush" : "NoFlush") + ".zip");
181186
String comment = "{\"ome\": { \"version\": \"XX.YY\" }}";
182-
BufferedZipStore zipStore = new BufferedZipStore(path, comment);
187+
BufferedZipStore zipStore = new BufferedZipStore(path, comment, flushOnWrite);
183188
writeTestGroupV3(zipStore, true);
184-
zipStore.flush();
189+
if(!flushOnWrite) zipStore.flush();
185190

186191
try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(path.toFile())) {
187192
String retrievedComment = zipFile.getComment();
@@ -246,12 +251,13 @@ public void testZipStoreRequirements() throws ZarrException, IOException {
246251
}
247252

248253

249-
@Test
250-
public void testZipStoreV2() throws ZarrException, IOException {
251-
Path path = TESTOUTPUT.resolve("testZipStoreV2.zip");
252-
BufferedZipStore zipStore = new BufferedZipStore(path);
254+
@ParameterizedTest
255+
@CsvSource({"false", "true",})
256+
public void testZipStoreV2(boolean flushOnWrite) throws ZarrException, IOException {
257+
Path path = TESTOUTPUT.resolve("testZipStoreV2" + (flushOnWrite ? "Flush" : "NoFlush") + ".zip");
258+
BufferedZipStore zipStore = new BufferedZipStore(path, flushOnWrite);
253259
writeTestGroupV2(zipStore, true);
254-
zipStore.flush();
260+
if(!flushOnWrite) zipStore.flush();
255261

256262
BufferedZipStore zipStoreRead = new BufferedZipStore(path);
257263
assertIsTestGroupV2(dev.zarr.zarrjava.core.Group.open(zipStoreRead.resolve()), true);
@@ -280,8 +286,8 @@ public void testReadOnlyZipStore() throws ZarrException, IOException {
280286
static Stream<Store> localStores() {
281287
return Stream.of(
282288
new MemoryStore(),
283-
new FilesystemStore(TESTOUTPUT.resolve("testLocalStoresFS"))
284-
// new BufferedZipStore(TESTOUTPUT.resolve("testLocalStoresZIP.zip"))
289+
new FilesystemStore(TESTOUTPUT.resolve("testLocalStoresFS")),
290+
new BufferedZipStore(TESTOUTPUT.resolve("testLocalStoresZIP.zip"))
285291
);
286292
}
287293

0 commit comments

Comments
 (0)