@@ -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
0 commit comments