From fe788c82c138fd750ce462a16655bf8ab04f4dc7 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 4 Aug 2017 17:45:17 +0200 Subject: [PATCH] DiskStorage save delay is now part of the builder. --- src/dorkbox/util/storage/DiskStorage.java | 22 +++++++-------------- src/dorkbox/util/storage/MemoryStorage.java | 11 +---------- src/dorkbox/util/storage/Storage.java | 5 ----- src/dorkbox/util/storage/StorageSystem.java | 12 ++++++++++- 4 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/dorkbox/util/storage/DiskStorage.java b/src/dorkbox/util/storage/DiskStorage.java index 7a2071c..d40d66d 100644 --- a/src/dorkbox/util/storage/DiskStorage.java +++ b/src/dorkbox/util/storage/DiskStorage.java @@ -53,14 +53,19 @@ class DiskStorage implements Storage { private final AtomicInteger references = new AtomicInteger(1); private final AtomicBoolean isOpen = new AtomicBoolean(false); - private volatile long milliSeconds = 3000L; + private final long milliSeconds; /** * Creates or opens a new database file. */ - DiskStorage(File storageFile, SerializationManager serializationManager, final boolean readOnly, final Logger logger) throws IOException { + DiskStorage(File storageFile, + SerializationManager serializationManager, + final boolean readOnly, + final long saveDelayInMilliseconds, + final Logger logger) throws IOException { this.storage = new StorageBase(storageFile, serializationManager, logger); + this.milliSeconds = saveDelayInMilliseconds; if (readOnly) { this.timer = null; @@ -328,19 +333,6 @@ class DiskStorage implements Storage { return this.milliSeconds; } - /** - * @param milliSeconds milliseconds to wait - */ - @Override - public final - void setSaveDelay(long milliSeconds) { - if (!this.isOpen.get()) { - throw new RuntimeException("Unable to act on closed storage"); - } - - this.milliSeconds = milliSeconds; - } - /** * @return the version of data stored in the database */ diff --git a/src/dorkbox/util/storage/MemoryStorage.java b/src/dorkbox/util/storage/MemoryStorage.java index a086be1..267d606 100644 --- a/src/dorkbox/util/storage/MemoryStorage.java +++ b/src/dorkbox/util/storage/MemoryStorage.java @@ -158,19 +158,10 @@ class MemoryStorage implements Storage { @Override public long getSaveDelay() { - return 0; + return 0L; } - /** - * There is no file that backs this storage, so saves/writes are immediate - */ - @Override - public - void setSaveDelay(final long milliSeconds) { - // no-op - } - /** * @return the version of data stored in the database */ diff --git a/src/dorkbox/util/storage/Storage.java b/src/dorkbox/util/storage/Storage.java index a9922cc..537dd71 100644 --- a/src/dorkbox/util/storage/Storage.java +++ b/src/dorkbox/util/storage/Storage.java @@ -83,11 +83,6 @@ interface Storage { */ long getSaveDelay(); - /** - * @param milliSeconds milliseconds to wait - */ - void setSaveDelay(long milliSeconds); - /** * @return the version of data stored in the database */ diff --git a/src/dorkbox/util/storage/StorageSystem.java b/src/dorkbox/util/storage/StorageSystem.java index eac2305..ec7e34e 100644 --- a/src/dorkbox/util/storage/StorageSystem.java +++ b/src/dorkbox/util/storage/StorageSystem.java @@ -154,6 +154,7 @@ class StorageSystem { private SerializationManager serializationManager; private boolean readOnly = false; private Logger logger = null; + private long saveDelayInMilliseconds = 3000L; // default /** * Specify the file to write to on disk when saving objects @@ -191,6 +192,15 @@ class StorageSystem { return this; } + /** + * Mark this storage system as read only + */ + public + DiskMaker setSaveDelay(long saveDelayInMilliseconds) { + this.saveDelayInMilliseconds = saveDelayInMilliseconds; + return this; + } + /** * Assigns a logger to use for the storage system. If null, then only errors will be logged to the error console. */ @@ -243,7 +253,7 @@ class StorageSystem { } else { try { - storage = new DiskStorage(this.file, this.serializationManager, this.readOnly, this.logger); + storage = new DiskStorage(this.file, this.serializationManager, this.readOnly, this.saveDelayInMilliseconds, this.logger); storages.put(this.file, storage); } catch (IOException e) { String message = e.getMessage().substring(0,e.getMessage().indexOf(OS.LINE_SEPARATOR));