Removed default storageKey get() methods (they were confusing)
This commit is contained in:
parent
973d6d99c2
commit
ec84231b1b
@ -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> 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.
|
||||
* <p/>
|
||||
* 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> 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> T getAndPut(String key, T data) {
|
||||
<T> 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> T getAndPut(byte[] key, T data) {
|
||||
return getAndPut(new StorageKey(key), data);
|
||||
<T> 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> T getAndPut(StorageKey key, T data) {
|
||||
<T> 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
|
||||
* <p/>
|
||||
* 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.
|
||||
*
|
||||
|
@ -23,13 +23,11 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
*/
|
||||
class MemoryStorage implements Storage {
|
||||
private final ConcurrentHashMap<StorageKey, Object> storage;
|
||||
private final StorageKey defaultKey;
|
||||
private int version;
|
||||
|
||||
|
||||
MemoryStorage() {
|
||||
this.storage = new ConcurrentHashMap<StorageKey, Object>();
|
||||
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> 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.
|
||||
* <p/>
|
||||
* 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> 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> T getAndPut(String key, T data) {
|
||||
<T> 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> T getAndPut(byte[] key, T data) {
|
||||
return getAndPut(new StorageKey(key), data);
|
||||
<T> T get(byte[] key, T data) {
|
||||
return get(new StorageKey(key), data);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public
|
||||
<T> T getAndPut(final StorageKey key, final T data) {
|
||||
<T> 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.
|
||||
* <p/>
|
||||
* 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.
|
||||
*
|
||||
|
@ -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> T get();
|
||||
|
||||
/**
|
||||
* Reads a object using the specific key, and casts it to the expected class.
|
||||
*/
|
||||
@ -54,13 +49,12 @@ interface Storage {
|
||||
<T> T get(StorageKey key);
|
||||
|
||||
/**
|
||||
* Uses the DEFAULT key ("") to return saved data.
|
||||
* <p/>
|
||||
* 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> T getAndPut(T data);
|
||||
<T> 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> T getAndPut(String key, T data);
|
||||
<T> 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> 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> T getAndPut(StorageKey key, T data);
|
||||
<T> 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
|
||||
* <p/>
|
||||
* 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.
|
||||
*
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user