From ec84231b1b6dcfa3452a9789b6c724b2d29febf6 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 4 Aug 2017 16:04:30 +0200 Subject: [PATCH] Removed default storageKey get() methods (they were confusing) --- src/dorkbox/util/storage/DiskStorage.java | 55 ++------------------- src/dorkbox/util/storage/MemoryStorage.java | 47 ++---------------- src/dorkbox/util/storage/Storage.java | 32 ++---------- test/dorkbox/util/StorageTest.java | 18 ++++--- 4 files changed, 26 insertions(+), 126 deletions(-) diff --git a/src/dorkbox/util/storage/DiskStorage.java b/src/dorkbox/util/storage/DiskStorage.java index 6c70b78..fbf5bbc 100644 --- a/src/dorkbox/util/storage/DiskStorage.java +++ b/src/dorkbox/util/storage/DiskStorage.java @@ -37,7 +37,6 @@ import dorkbox.util.SerializationManager; @SuppressWarnings({"Convert2Diamond", "Convert2Lambda"}) class DiskStorage implements Storage { private final DelayTimer timer; - private final StorageKey defaultKey; private final StorageBase storage; private final AtomicInteger references = new AtomicInteger(1); @@ -52,7 +51,6 @@ class DiskStorage implements Storage { */ DiskStorage(File storageFile, SerializationManager serializationManager, final boolean readOnly, final Logger logger) throws IOException { this.storage = new StorageBase(storageFile, serializationManager, logger); - this.defaultKey = new StorageKey(""); if (readOnly) { this.timer = null; @@ -120,15 +118,6 @@ class DiskStorage implements Storage { return this.actionMap.containsKey(wrap) || this.storage.contains(wrap); } - /** - * Reads a object using the default (blank) key, and casts it to the expected class - */ - @Override - public final - T get() { - return get0(this.defaultKey); - } - /** * Reads a object using the specific key, and casts it to the expected class */ @@ -156,19 +145,6 @@ class DiskStorage implements Storage { return get0(key); } - /** - * Uses the DEFAULT key ("") to return saved data. Also saves the data. - *

- * This will check to see if there is an associated key for that data, if not - it will use data as the default - * - * @param data The data that will hold the copy of the data from disk - */ - @Override - public - T getAndPut(T data) { - return getAndPut(this.defaultKey, data); - } - /** * Returns the saved data for the specified key. Also saves the data. * @@ -176,10 +152,10 @@ class DiskStorage implements Storage { */ @Override public - T getAndPut(String key, T data) { + T get(String key, T data) { StorageKey wrap = new StorageKey(key); - return getAndPut(wrap, data); + return get(wrap, data); } /** @@ -189,8 +165,8 @@ class DiskStorage implements Storage { */ @Override public - T getAndPut(byte[] key, T data) { - return getAndPut(new StorageKey(key), data); + T get(byte[] key, T data) { + return get(new StorageKey(key), data); } /** @@ -203,7 +179,7 @@ class DiskStorage implements Storage { @Override @SuppressWarnings("unchecked") public - T getAndPut(StorageKey key, T data) { + T get(StorageKey key, T data) { Object source = get0(key); if (source == null) { @@ -305,27 +281,6 @@ class DiskStorage implements Storage { } } - /** - * Adds the given object to the storage using a default (blank) key, OR -- if it has been registered, using it's registered key - *

- * Also will update existing data. If the new contents do not fit in the original space, then the update is handled by - * deleting the old data and adding the new. - */ - @Override - public final - void put(Object object) { - if (!this.isOpen.get()) { - throw new RuntimeException("Unable to act on closed storage"); - } - - if (timer != null) { - action(this.defaultKey, object); - - // timer action runs on TIMER thread, not this thread - this.timer.delay(this.milliSeconds); - } - } - /** * Deletes an object from storage. * diff --git a/src/dorkbox/util/storage/MemoryStorage.java b/src/dorkbox/util/storage/MemoryStorage.java index e8fb87e..6513bd3 100644 --- a/src/dorkbox/util/storage/MemoryStorage.java +++ b/src/dorkbox/util/storage/MemoryStorage.java @@ -23,13 +23,11 @@ import java.util.concurrent.ConcurrentHashMap; */ class MemoryStorage implements Storage { private final ConcurrentHashMap storage; - private final StorageKey defaultKey; private int version; MemoryStorage() { this.storage = new ConcurrentHashMap(); - this.defaultKey = new StorageKey(""); } @@ -51,16 +49,6 @@ class MemoryStorage implements Storage { return storage.containsKey(new StorageKey(key)); } - /** - * Reads a object using the default (blank) key, and casts it to the expected class - */ - @SuppressWarnings("unchecked") - @Override - public - T get() { - return (T) storage.get(defaultKey); - } - /** * Reads a object using the specific key, and casts it to the expected class */ @@ -89,19 +77,6 @@ class MemoryStorage implements Storage { return (T) storage.get(key); } - /** - * Uses the DEFAULT key ("") to return saved data. - *

- * This will check to see if there is an associated key for that data, if not - it will use data as the default - * - * @param data The data that will hold the copy of the data from disk - */ - @Override - public - T getAndPut(T data) { - return getAndPut(this.defaultKey, data); - } - /** * Returns the saved data for the specified key. * @@ -109,10 +84,10 @@ class MemoryStorage implements Storage { */ @Override public - T getAndPut(String key, T data) { + T get(String key, T data) { StorageKey wrap = new StorageKey(key); - return getAndPut(wrap, data); + return get(wrap, data); } /** @@ -122,14 +97,14 @@ class MemoryStorage implements Storage { */ @Override public - T getAndPut(byte[] key, T data) { - return getAndPut(new StorageKey(key), data); + T get(byte[] key, T data) { + return get(new StorageKey(key), data); } @SuppressWarnings("unchecked") @Override public - T getAndPut(final StorageKey key, final T data) { + T get(final StorageKey key, final T data) { final Object o = storage.get(key); if (o == null) { storage.put(key, data); @@ -174,18 +149,6 @@ class MemoryStorage implements Storage { storage.put(key, object); } - /** - * Saves the given data to storage with the associated key. - *

- * Also will update existing data. If the new contents do not fit in the original space, then the update is handled by - * deleting the old data and adding the new. - */ - @Override - public - void put(final Object data) { - put(defaultKey, data); - } - /** * Deletes an object from storage. * diff --git a/src/dorkbox/util/storage/Storage.java b/src/dorkbox/util/storage/Storage.java index 705b534..f6ab8b0 100644 --- a/src/dorkbox/util/storage/Storage.java +++ b/src/dorkbox/util/storage/Storage.java @@ -33,11 +33,6 @@ interface Storage { */ boolean contains(String key); - /** - * Reads a object using the DEFAULT key ("") key, and casts it to the expected class - */ - T get(); - /** * Reads a object using the specific key, and casts it to the expected class. */ @@ -54,13 +49,12 @@ interface Storage { T get(StorageKey key); /** - * Uses the DEFAULT key ("") to return saved data. - *

- * This will check to see if there is an associated key for that data, if not - it will use data as the default + * Returns the saved data for the specified key. * + * @param key The key used to check if data already exists. * @param data This is the default value, and if there is no value with the key in the DB this default value will be saved. */ - T getAndPut(T data); + T get(String key, T data); /** * Returns the saved data for the specified key. @@ -68,7 +62,7 @@ interface Storage { * @param key The key used to check if data already exists. * @param data This is the default value, and if there is no value with the key in the DB this default value will be saved. */ - T getAndPut(String key, T data); + T get(byte[] key, T data); /** * Returns the saved data for the specified key. @@ -76,15 +70,7 @@ interface Storage { * @param key The key used to check if data already exists. * @param data This is the default value, and if there is no value with the key in the DB this default value will be saved. */ - T getAndPut(byte[] key, T data); - - /** - * Returns the saved data for the specified key. - * - * @param key The key used to check if data already exists. - * @param data This is the default value, and if there is no value with the key in the DB this default value will be saved. - */ - T getAndPut(StorageKey key, T data); + T get(StorageKey key, T data); /** * Saves the given data to storage with the associated key. @@ -110,14 +96,6 @@ interface Storage { */ void put(StorageKey key, Object data); - /** - * Adds the given object to the storage using a default (blank) key, OR -- if it has been registered, using it's registered key - *

- * Also will update existing data. If the new contents do not fit in the original space, then the update is handled by - * deleting the old data and adding the new. - */ - void put(Object data); - /** * Deletes an object from storage. * diff --git a/test/dorkbox/util/StorageTest.java b/test/dorkbox/util/StorageTest.java index 5f34c63..e174f31 100644 --- a/test/dorkbox/util/StorageTest.java +++ b/test/dorkbox/util/StorageTest.java @@ -27,6 +27,7 @@ import org.junit.Test; import org.junit.runners.MethodSorters; import dorkbox.util.storage.Storage; +import dorkbox.util.storage.StorageKey; import dorkbox.util.storage.StorageSystem; @FixMethodOrder(MethodSorters.NAME_ASCENDING) @@ -63,7 +64,8 @@ class StorageTest { @Test public void testCreateDB() throws IOException { - TEST_DB.delete(); + StorageSystem.shutdown(); + StorageSystem.delete(TEST_DB); Storage storage = StorageSystem.Disk() .file(TEST_DB) .build(); @@ -134,13 +136,15 @@ class StorageTest { .file(TEST_DB) .build(); + StorageKey storageKey = new StorageKey("foobar!"); + for (int i = 0; i < total; i++) { log("adding record " + i + "..."); String addRecord = createData(i); - storage.put(addRecord); + storage.put(storageKey, addRecord); log("reading record " + i + "..."); - String readData = storage.get(); + String readData = storage.get(storageKey); Assert.assertEquals("Object is not the same", addRecord, readData); } @@ -152,7 +156,7 @@ class StorageTest { String dataCheck = createData(total - 1); log("reading record " + (total - 1) + "..."); - String readData = storage.get(); + String readData = storage.get(storageKey); // the ONLY entry in storage should be the last one that we added Assert.assertEquals("Object is not the same", dataCheck, readData); @@ -288,7 +292,7 @@ class StorageTest { storage.put(createKey, data); Data data2; - data2 = storage.getAndPut(createKey, new Data()); + data2 = storage.get(createKey, new Data()); Assert.assertEquals("Object is not the same", data, data2); StorageSystem.close(storage); @@ -296,7 +300,7 @@ class StorageTest { .file(TEST_DB) .build(); - data2 = storage.getAndPut(createKey, new Data()); + data2 = storage.get(createKey, new Data()); Assert.assertEquals("Object is not the same", data, data2); StorageSystem.close(storage); @@ -465,7 +469,7 @@ class StorageTest { String createKey = createKey(i); Data data2; - data2 = storage.getAndPut(createKey, new Data()); + data2 = storage.get(createKey, new Data()); Assert.assertEquals("Object is not the same", data, data2); } StorageSystem.close(storage);