Further tweaks to storage

This commit is contained in:
nathan 2014-09-17 17:40:16 +02:00
parent c2cb5d5725
commit e2cfd990bb
3 changed files with 16 additions and 9 deletions

View File

@ -59,9 +59,7 @@ public class Storage {
storage.increaseReference();
} else {
try {
StorageBase storageBase = new StorageBase(file);
storage = new Storage(storageBase);
storage = new Storage(file);
storages.put(file, storage);
} catch (IOException e) {
logger.error("Unable to open storage", e);
@ -270,8 +268,8 @@ public class Storage {
/**
* Creates or opens a new database file.
*/
private Storage(StorageBase _storage) {
this.storage = _storage;
private Storage(File storageFile) throws IOException {
this.storage = new StorageBase(storageFile);
this.defaultKey = wrap("");
this.timer = new DelayTimer("Storage Writer", false, new DelayTimer.Callback() {

View File

@ -93,6 +93,8 @@ public class StorageBase {
* Creates or opens a new database file.
*/
StorageBase(File filePath) throws IOException {
this.logger.info("Opening storage file: '{}'", filePath.getAbsolutePath());
this.baseFile = filePath;
File parentFile = this.baseFile.getParentFile();
@ -119,6 +121,14 @@ public class StorageBase {
this.file.setLength(indexPointer);
}
if (this.file.length() < this.dataPosition) {
this.logger.error("Corrupted storage file!");
throw new IllegalArgumentException("Unable to parse header information from storage. Maybe it's corrupted?");
}
this.logger.info("Storage version: {}", this.databaseVersion);
this.kryo = new Kryo();
this.kryo.setRegistrationRequired(false);

View File

@ -10,14 +10,13 @@ import java.util.Arrays;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import dorkbox.util.storage.Storage;
/**
* Simple test class for the RecordsFile example. To run the test, set you CLASSPATH and then type
* "java hamner.dbtest.TestRecords"
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class StorageTest {
private static final String TEST_DB = "sampleFile.records";