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.
*/
void close() {
void closeFully() {
// timer action runs on THIS thread, not timer thread
if (timer != null) {
this.timer.delay(0L);
@ -507,7 +515,7 @@ class DiskStorage implements Storage {
/**
* Save the storage to disk, immediately.
* <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
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/>
* 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
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/>
* 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
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/>
* 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
public

View File

@ -210,53 +210,86 @@ class MemoryStorage implements Storage {
return true;
}
/**
* @return null. There is no file that backs this storage
*/
@Override
public
File getFile() {
return null;
}
/**
* Gets the backing file size.
*
* @return 0. There is no file that backs this storage
*/
@Override
public
long getFileSize() {
return 0;
}
/**
* @return false. Writes to in-memory storage are immediate.
*/
@Override
public
boolean hasWriteWaiting() {
return false;
}
/**
* @return 0. There is no file that backs this storage
*/
@Override
public
long getSaveDelay() {
return 0;
}
/**
* 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
*/
@Override
public synchronized
int getVersion() {
return version;
}
/**
* Sets the version of data stored in the database
*/
@Override
public synchronized
void setVersion(final int version) {
this.version = version;
}
/**
* There is no file that backs this storage, so writes are immediate and saves do nothgin
*/
@Override
public
void save() {
// 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
public
void putAndSave(final String key, final Object object) {
@ -264,17 +297,36 @@ class MemoryStorage implements Storage {
// 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
public
void putAndSave(final byte[] key, final Object 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
public
void putAndSave(final StorageKey key, final Object 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();
/**
* Save the storage to disk, immediately.
* Adds a key/value pair to the storage, then saves the storage immediately.
* <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);
/**
* Save the storage to disk, immediately.
* Adds a key/value pair to the storage, then saves the storage immediately.
* <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);
/**
* Save the storage to disk, immediately.
* Adds a key/value pair to the storage, then saves the storage immediately.
* <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);
/**
* Closes this storage system
*/
void close();
}

View File

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