Updated storage comments and methods. Added storage.close() as a convenience method.

This commit is contained in:
nathan 2017-08-01 10:22:50 +02:00
parent 7c2fc36d57
commit a61f3522b0
4 changed files with 85 additions and 20 deletions

View File

@ -371,10 +371,18 @@ class DiskStorage implements Storage {
} }
} }
/**
* Closes and removes this storage from the storage system. This is the same as calling {@link StorageSystem#close(Storage)}
*/
public
void close() {
StorageSystem.close(this);
}
/** /**
* Closes the database and file. * Closes the database and file.
*/ */
void close() { void closeFully() {
// timer action runs on THIS thread, not timer thread // timer action runs on THIS thread, not timer thread
if (timer != null) { if (timer != null) {
this.timer.delay(0L); this.timer.delay(0L);
@ -507,7 +515,7 @@ class DiskStorage implements Storage {
/** /**
* Save the storage to disk, immediately. * Save the storage to disk, immediately.
* <p/> * <p/>
* This will save the ALL of the pending save actions to the file * This will save ALL of the pending save actions to the file
*/ */
@Override @Override
public final public final
@ -523,9 +531,9 @@ class DiskStorage implements Storage {
} }
/** /**
* Save the storage to disk, immediately. * Adds a key/value pair to the storage, then saves the storage to disk, immediately.
* <p/> * <p/>
* This will save the ALL of the pending save actions to the file * This will save ALL of the pending save actions to the file
*/ */
@Override @Override
public public
@ -542,9 +550,9 @@ class DiskStorage implements Storage {
} }
/** /**
* Save the storage to disk, immediately. * Adds a key/value pair to the storage, then save the storage to disk, immediately.
* <p/> * <p/>
* This will save the ALL of the pending save actions to the file * This will save ALL of the pending save actions to the file
*/ */
@Override @Override
public public
@ -563,9 +571,9 @@ class DiskStorage implements Storage {
} }
/** /**
* Save the storage to disk, immediately. * Adds a key/value pair to the storage, then save the storage to disk, immediately.
* <p/> * <p/>
* This will save the ALL of the pending save actions to the file * This will save ALL of the pending save actions to the file
*/ */
@Override @Override
public public

View File

@ -210,53 +210,86 @@ class MemoryStorage implements Storage {
return true; return true;
} }
/**
* @return null. There is no file that backs this storage
*/
@Override @Override
public public
File getFile() { File getFile() {
return null; return null;
} }
/**
* Gets the backing file size.
*
* @return 0. There is no file that backs this storage
*/
@Override @Override
public public
long getFileSize() { long getFileSize() {
return 0; return 0;
} }
/**
* @return false. Writes to in-memory storage are immediate.
*/
@Override @Override
public public
boolean hasWriteWaiting() { boolean hasWriteWaiting() {
return false; return false;
} }
/**
* @return 0. There is no file that backs this storage
*/
@Override @Override
public public
long getSaveDelay() { long getSaveDelay() {
return 0; return 0;
} }
/**
* There is no file that backs this storage, so saves/writes are immediate
*/
@Override @Override
public public
void setSaveDelay(final long milliSeconds) { void setSaveDelay(final long milliSeconds) {
// no-op
} }
/**
* @return the version of data stored in the database
*/
@Override @Override
public synchronized public synchronized
int getVersion() { int getVersion() {
return version; return version;
} }
/**
* Sets the version of data stored in the database
*/
@Override @Override
public synchronized public synchronized
void setVersion(final int version) { void setVersion(final int version) {
this.version = version; this.version = version;
} }
/**
* There is no file that backs this storage, so writes are immediate and saves do nothgin
*/
@Override @Override
public public
void save() { void save() {
// no-op // no-op
} }
/**
* Adds a key/value pair to the storage.
* <p/>
* There is no file that backs this storage, so writes are immediate and saves do nothing
*/
@Override @Override
public public
void putAndSave(final String key, final Object object) { void putAndSave(final String key, final Object object) {
@ -264,17 +297,36 @@ class MemoryStorage implements Storage {
// no-save! // no-save!
} }
/**
/**
* Adds a key/value pair to the storage.
* <p/>
* There is no file that backs this storage, so writes are immediate and saves do nothing
*/
@Override @Override
public public
void putAndSave(final byte[] key, final Object object) { void putAndSave(final byte[] key, final Object object) {
put(key, object); put(key, object);
// no-save! // no save because we are in memory!
} }
/**
* Adds a key/value pair to the storage.
* <p/>
* There is no file that backs this storage, so writes are immediate and saves do nothing
*/
@Override @Override
public public
void putAndSave(final StorageKey key, final Object object) { void putAndSave(final StorageKey key, final Object object) {
put(key, object); put(key, object);
// no-save! // no save because we are in memory!
}
/**
* In-memory storage systems do not have a backing file, so there is nothing to close
*/
public
void close() {
StorageSystem.close(this);
} }
} }

View File

@ -177,23 +177,28 @@ interface Storage {
void save(); void save();
/** /**
* Save the storage to disk, immediately. * Adds a key/value pair to the storage, then saves the storage immediately.
* <p/> * <p/>
* This will save the ALL of the pending save actions to the file * This will save ALL of the pending save actions to the file
*/ */
void putAndSave(String key, Object object); void putAndSave(String key, Object object);
/** /**
* Save the storage to disk, immediately. * Adds a key/value pair to the storage, then saves the storage immediately.
* <p/> * <p/>
* This will save the ALL of the pending save actions to the file * This will save ALL of the pending save actions to the file
*/ */
void putAndSave(byte[] key, Object object); void putAndSave(byte[] key, Object object);
/** /**
* Save the storage to disk, immediately. * Adds a key/value pair to the storage, then saves the storage immediately.
* <p/> * <p/>
* This will save the ALL of the pending save actions to the file * This will save ALL of the pending save actions to the file
*/ */
void putAndSave(StorageKey key, Object object); void putAndSave(StorageKey key, Object object);
/**
* Closes this storage system
*/
void close();
} }

View File

@ -76,7 +76,7 @@ class StorageSystem {
final DiskStorage diskStorage = (DiskStorage) storage; final DiskStorage diskStorage = (DiskStorage) storage;
boolean isLastOne = diskStorage.decrementReference(); boolean isLastOne = diskStorage.decrementReference();
if (isLastOne) { if (isLastOne) {
diskStorage.close(); diskStorage.closeFully();
storages.remove(file); storages.remove(file);
} }
} }
@ -108,7 +108,7 @@ class StorageSystem {
//noinspection StatementWithEmptyBody //noinspection StatementWithEmptyBody
while (!diskStorage.decrementReference()) { while (!diskStorage.decrementReference()) {
} }
diskStorage.close(); diskStorage.closeFully();
} }
} }
storages.clear(); storages.clear();
@ -125,7 +125,7 @@ class StorageSystem {
synchronized (storages) { synchronized (storages) {
Storage remove = storages.remove(file); Storage remove = storages.remove(file);
if (remove != null && remove instanceof DiskStorage) { if (remove != null && remove instanceof DiskStorage) {
((DiskStorage) remove).close(); ((DiskStorage) remove).closeFully();
} }
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
file.delete(); file.delete();
@ -203,7 +203,7 @@ class StorageSystem {
* suppress serialization errors. * suppress serialization errors.
*/ */
public public
DiskMaker nologger() { DiskMaker noLogger() {
this.logger = NOPLogger.NOP_LOGGER; this.logger = NOPLogger.NOP_LOGGER;
return this; return this;
} }