Added comments, renamed to StorageSystem

This commit is contained in:
nathan 2017-02-18 14:41:22 +01:00
parent f5ae7fb32d
commit b6272112e8
2 changed files with 196 additions and 138 deletions

View File

@ -22,12 +22,13 @@ import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.helpers.NOPLogger;
import dorkbox.util.FileUtil;
import dorkbox.util.SerializationManager;
public
class StorageType {
class StorageSystem {
@SuppressWarnings("SpellCheckingInspection")
private static final Map<File, Storage> storages = new HashMap<File, Storage>(1);
@ -36,7 +37,7 @@ class StorageType {
@Override
public
void run() {
StorageType.shutdown();
StorageSystem.shutdown();
}
});
@ -46,21 +47,28 @@ class StorageType {
.addShutdownHook(shutdownHook);
}
/**
* Creates a persistent, on-disk storage system. Writes to disk are queued, so it is recommended to NOT edit/change an object after
* it has been put into storage, or whenever it does changes, make sure to put it back into storage (to update the saved record)
*/
public static
DiskMaker Disk() {
return new DiskMaker();
}
/**
* Creates an in-memory only storage system
*/
public static
MemoryMaker Memory() {
return new MemoryMaker();
}
/**
* Closes the storage.
* Closes the specified storage system based on the file used
*/
public static
void close(File file) {
void close(final File file) {
synchronized (storages) {
Storage storage = storages.get(file);
if (storage != null) {
@ -77,24 +85,27 @@ class StorageType {
}
/**
* Closes the storage.
* Closes the specified storage system
*/
public static
void close(Storage _storage) {
void close(final Storage storage) {
synchronized (storages) {
File file = _storage.getFile();
File file = storage.getFile();
close(file);
}
}
/**
* Saves and closes all open storage systems
*/
public static
void shutdown() {
synchronized (storages) {
Collection<Storage> values = storages.values();
for (Storage storage : values) {
if (storage instanceof DiskStorage) {
//noinspection StatementWithEmptyBody
final DiskStorage diskStorage = (DiskStorage) storage;
//noinspection StatementWithEmptyBody
while (!diskStorage.decrementReference()) {
}
diskStorage.close();
@ -104,6 +115,11 @@ class StorageType {
}
}
/**
* Closes (if in use) and deletes the specified storage file.
* <p>
* The file is checked to see if it is in use by the storage system first, and closes if so.
*/
public static
void delete(File file) {
synchronized (storages) {
@ -116,13 +132,20 @@ class StorageType {
}
}
/**
* Closes (if in use) and deletes the specified storage.
*/
public static
void delete(Storage storage) {
File file = storage.getFile();
delete(file);
}
/**
* Creates a persistent, on-disk storage system. Writes to disk are queued, so it is recommended to NOT edit/change an object after
* it has been put into storage, or whenever it does changes, make sure to put it back into storage (to update the saved record)
*/
@SuppressWarnings("unused")
public static
class DiskMaker {
private File file;
@ -130,24 +153,64 @@ class StorageType {
private boolean readOnly = false;
private Logger logger = null;
/**
* Specify the file to write to on disk when saving objects
*/
public
DiskMaker file(File file) {
this.file = FileUtil.normalize(file);
return this;
}
/**
* Specify the file to write to on disk when saving objects
*/
public
DiskMaker file(String file) {
this.file = FileUtil.normalize(file);
return this;
}
/**
* Specify the serialization manager to use. This is what serializes the files (which are then saved to disk)
*/
public
DiskMaker serializer(SerializationManager serializationManager) {
this.serializationManager = serializationManager;
return this;
}
/**
* Mark this storage system as read only
*/
public
DiskMaker readOnly() {
this.readOnly = true;
return this;
}
/**
* Assigns a logger to use for the storage system. If null, then only errors will be logged to the error console.
*/
public
DiskMaker logger(final Logger logger) {
this.logger = logger;
return this;
}
/**
* Assigns a No Operation (NOP) logger which will ignore everything. This is not recommended for normal use, as it will also
* suppress serialization errors.
*/
public
DiskMaker nologger() {
this.logger = NOPLogger.NOP_LOGGER;
return this;
}
/**
* Makes the storage system
*/
public
Storage make() {
if (this.file == null) {
@ -188,23 +251,18 @@ class StorageType {
return storage;
}
}
public
DiskMaker readOnly() {
this.readOnly = true;
return this;
}
public
DiskMaker logger(final Logger logger) {
this.logger = logger;
return this;
}
}
/**
* Creates an in-memory only storage system
*/
public static
class MemoryMaker {
/**
* Makes the storage system
*/
public
MemoryStorage make() throws IOException {
return new MemoryStorage();

View File

@ -33,7 +33,7 @@ import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import dorkbox.util.storage.Storage;
import dorkbox.util.storage.StorageType;
import dorkbox.util.storage.StorageSystem;
import io.netty.buffer.ByteBuf;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ -121,20 +121,20 @@ class StorageTest {
@Before
public
void deleteDB() {
StorageType.delete(TEST_DB);
StorageSystem.delete(TEST_DB);
}
@After
public
void delete2DB() {
StorageType.delete(TEST_DB);
StorageSystem.delete(TEST_DB);
}
@Test
public
void testCreateDB() throws IOException {
Storage storage = StorageType.Disk()
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -145,9 +145,9 @@ class StorageTest {
Assert.assertEquals("count is not correct", numberOfRecords1, 0);
Assert.assertEquals("size is not correct", size1, initialSize);
StorageType.close(storage);
StorageSystem.close(storage);
storage = StorageType.Disk()
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -158,7 +158,7 @@ class StorageTest {
Assert.assertEquals("Record count is not the same", numberOfRecords1, numberOfRecords2);
Assert.assertEquals("size is not the same", size1, size2);
StorageType.close(storage);
StorageSystem.close(storage);
}
@ -166,7 +166,7 @@ class StorageTest {
public
void testAddAsOne() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageType.Disk()
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -175,8 +175,8 @@ class StorageTest {
add(storage, i);
}
StorageType.close(storage);
storage = StorageType.Disk()
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -188,7 +188,7 @@ class StorageTest {
Assert.assertEquals("Object is not the same", record1Data, readRecord);
}
StorageType.close(storage);
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
@ -204,7 +204,7 @@ class StorageTest {
public
void testAddNoKeyRecords() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageType.Disk()
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -219,9 +219,9 @@ class StorageTest {
Assert.assertEquals("Object is not the same", addRecord, readData);
}
StorageType.close(storage);
StorageSystem.close(storage);
storage = StorageType.Disk()
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -240,7 +240,7 @@ class StorageTest {
Assert.assertEquals("size is not correct", size1, initialSize + sizePerRecord);
StorageType.close(storage);
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
@ -251,7 +251,7 @@ class StorageTest {
public
void testAddRecords_DelaySaveA() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageType.Disk()
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -272,9 +272,9 @@ class StorageTest {
Assert.assertEquals("Object is not the same", record1Data, readRecord);
}
StorageType.close(storage);
StorageSystem.close(storage);
storage = StorageType.Disk()
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -285,7 +285,7 @@ class StorageTest {
Assert.assertEquals("Object is not the same", dataCheck, readRecord);
}
StorageType.close(storage);
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
@ -296,7 +296,7 @@ class StorageTest {
public
void testAddRecords_DelaySaveB() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageType.Disk()
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -312,9 +312,9 @@ class StorageTest {
Assert.assertEquals("Object is not the same", record1Data, readRecord);
}
StorageType.close(storage);
StorageSystem.close(storage);
storage = StorageType.Disk()
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -326,7 +326,7 @@ class StorageTest {
Assert.assertEquals("Object is not the same", dataCheck, readRecord);
}
StorageType.close(storage);
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
@ -337,7 +337,7 @@ class StorageTest {
public
void testLoadRecords() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageType.Disk()
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -348,9 +348,9 @@ class StorageTest {
Assert.assertEquals("Object is not the same", addRecord, readRecord);
}
StorageType.close(storage);
StorageSystem.close(storage);
storage = StorageType.Disk()
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -373,8 +373,8 @@ class StorageTest {
data2 = storage.getAndPut(createKey, new Data());
Assert.assertEquals("Object is not the same", data, data2);
StorageType.close(storage);
storage = StorageType.Disk()
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -382,7 +382,7 @@ class StorageTest {
data2 = storage.getAndPut(createKey, new Data());
Assert.assertEquals("Object is not the same", data, data2);
StorageType.close(storage);
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
@ -398,7 +398,7 @@ class StorageTest {
}
try {
Storage storage = StorageType.Disk()
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -409,9 +409,9 @@ class StorageTest {
Assert.assertEquals("Object is not the same", addRecord, readRecord);
}
StorageType.close(storage);
StorageSystem.close(storage);
storage = StorageType.Disk()
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -442,9 +442,9 @@ class StorageTest {
Assert.assertEquals("Object is not the same", dataCheck, addRecord);
StorageType.close(storage);
StorageSystem.close(storage);
storage = StorageType.Disk()
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -469,7 +469,7 @@ class StorageTest {
public
void testUpdateRecords() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageType.Disk()
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -480,9 +480,9 @@ class StorageTest {
Assert.assertEquals("Object is not the same", addRecord, readRecord);
}
StorageType.close(storage);
StorageSystem.close(storage);
storage = StorageType.Disk()
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -491,8 +491,8 @@ class StorageTest {
String readRecord = readRecord(storage, 3);
Assert.assertEquals("Object is not the same", updateRecord, readRecord);
StorageType.close(storage);
storage = StorageType.Disk()
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -502,8 +502,8 @@ class StorageTest {
updateRecord = updateRecord(storage, 3, createData(3));
StorageType.close(storage);
storage = StorageType.Disk()
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -511,8 +511,8 @@ class StorageTest {
readRecord = readRecord(storage, 3);
Assert.assertEquals("Object is not the same", updateRecord, readRecord);
StorageType.close(storage);
storage = StorageType.Disk()
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -521,7 +521,7 @@ class StorageTest {
readRecord = readRecord(storage, 0);
Assert.assertEquals("Object is not the same", updateRecord, readRecord);
StorageType.close(storage);
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
@ -533,7 +533,7 @@ class StorageTest {
public
void testSaveAllRecords() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageType.Disk()
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -545,12 +545,12 @@ class StorageTest {
storage.put(createKey, data);
}
StorageType.close(storage);
StorageSystem.close(storage);
Data data = new Data();
makeData(data);
storage = StorageType.Disk()
storage = StorageSystem.Disk()
.file(TEST_DB)
.serializer(manager)
.make();
@ -561,7 +561,7 @@ class StorageTest {
data2 = storage.getAndPut(createKey, new Data());
Assert.assertEquals("Object is not the same", data, data2);
}
StorageType.close(storage);
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");