Can now specify the logger to use for Disk Storage.
This commit is contained in:
parent
001e84b073
commit
7627f487d4
|
@ -15,10 +15,6 @@
|
||||||
*/
|
*/
|
||||||
package dorkbox.util.storage;
|
package dorkbox.util.storage;
|
||||||
|
|
||||||
import dorkbox.util.DelayTimer;
|
|
||||||
import dorkbox.util.SerializationManager;
|
|
||||||
import dorkbox.util.bytes.ByteArrayWrapper;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -27,6 +23,12 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import dorkbox.util.DelayTimer;
|
||||||
|
import dorkbox.util.SerializationManager;
|
||||||
|
import dorkbox.util.bytes.ByteArrayWrapper;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Nothing spectacular about this storage -- it allows for persistent storage of objects to disk.
|
* Nothing spectacular about this storage -- it allows for persistent storage of objects to disk.
|
||||||
|
@ -48,9 +50,8 @@ class DiskStorage implements Storage {
|
||||||
/**
|
/**
|
||||||
* Creates or opens a new database file.
|
* Creates or opens a new database file.
|
||||||
*/
|
*/
|
||||||
protected
|
DiskStorage(File storageFile, SerializationManager serializationManager, final boolean readOnly, final Logger logger) throws IOException {
|
||||||
DiskStorage(File storageFile, SerializationManager serializationManager, final boolean readOnly) throws IOException {
|
this.storage = new StorageBase(storageFile, serializationManager, logger);
|
||||||
this.storage = new StorageBase(storageFile, serializationManager);
|
|
||||||
this.defaultKey = ByteArrayWrapper.wrap("");
|
this.defaultKey = ByteArrayWrapper.wrap("");
|
||||||
|
|
||||||
if (readOnly) {
|
if (readOnly) {
|
||||||
|
|
|
@ -15,12 +15,12 @@
|
||||||
*/
|
*/
|
||||||
package dorkbox.util.storage;
|
package dorkbox.util.storage;
|
||||||
|
|
||||||
import dorkbox.util.bytes.ByteArrayWrapper;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import dorkbox.util.bytes.ByteArrayWrapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Storage that is in memory only (and is not persisted to disk)
|
* Storage that is in memory only (and is not persisted to disk)
|
||||||
*/
|
*/
|
||||||
|
@ -30,7 +30,6 @@ class MemoryStorage implements Storage {
|
||||||
private int version;
|
private int version;
|
||||||
|
|
||||||
|
|
||||||
public
|
|
||||||
MemoryStorage() {
|
MemoryStorage() {
|
||||||
this.storage = new ConcurrentHashMap<ByteArrayWrapper, Object>();
|
this.storage = new ConcurrentHashMap<ByteArrayWrapper, Object>();
|
||||||
this.defaultKey = ByteArrayWrapper.wrap("");
|
this.defaultKey = ByteArrayWrapper.wrap("");
|
||||||
|
|
|
@ -32,7 +32,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import com.esotericsoftware.kryo.KryoException;
|
import com.esotericsoftware.kryo.KryoException;
|
||||||
import com.esotericsoftware.kryo.io.Input;
|
import com.esotericsoftware.kryo.io.Input;
|
||||||
|
@ -48,7 +47,7 @@ import dorkbox.util.bytes.ByteArrayWrapper;
|
||||||
|
|
||||||
|
|
||||||
class StorageBase {
|
class StorageBase {
|
||||||
private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());
|
private final Logger logger;
|
||||||
|
|
||||||
|
|
||||||
// File pointer to the data start pointer header.
|
// File pointer to the data start pointer header.
|
||||||
|
@ -108,8 +107,9 @@ class StorageBase {
|
||||||
/**
|
/**
|
||||||
* Creates or opens a new database file.
|
* Creates or opens a new database file.
|
||||||
*/
|
*/
|
||||||
StorageBase(File filePath, SerializationManager serializationManager) throws IOException {
|
StorageBase(final File filePath, final SerializationManager serializationManager, final Logger logger) throws IOException {
|
||||||
this.serializationManager = serializationManager;
|
this.serializationManager = serializationManager;
|
||||||
|
this.logger = logger;
|
||||||
|
|
||||||
this.logger.info("Opening storage file: '{}'", filePath.getAbsolutePath());
|
this.logger.info("Opening storage file: '{}'", filePath.getAbsolutePath());
|
||||||
|
|
||||||
|
@ -152,7 +152,7 @@ class StorageBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
//noinspection AutoBoxing
|
//noinspection AutoBoxing
|
||||||
this.logger.info("Storage version: {}", this.databaseVersion);
|
logger.info("Storage version: {}", this.databaseVersion);
|
||||||
|
|
||||||
|
|
||||||
// If we want to use compression (no need really, since this file is small already),
|
// If we want to use compression (no need really, since this file is small already),
|
||||||
|
@ -291,7 +291,7 @@ class StorageBase {
|
||||||
|
|
||||||
return readRecordData;
|
return readRecordData;
|
||||||
} catch (KryoException e) {
|
} catch (KryoException e) {
|
||||||
this.logger.error("Error while getting data from disk. Ignoring previous value.");
|
this.logger.error("Error while getting data from disk. Ignoring previous value.", e);
|
||||||
return null;
|
return null;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
this.logger.error("Error while getting data from disk", e);
|
this.logger.error("Error while getting data from disk", e);
|
||||||
|
|
|
@ -22,15 +22,13 @@ import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.helpers.NOPLogger;
|
||||||
|
|
||||||
import dorkbox.util.FileUtil;
|
import dorkbox.util.FileUtil;
|
||||||
import dorkbox.util.SerializationManager;
|
import dorkbox.util.SerializationManager;
|
||||||
|
|
||||||
public
|
public
|
||||||
class StorageType {
|
class StorageType {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(DiskStorage.class);
|
|
||||||
|
|
||||||
@SuppressWarnings("SpellCheckingInspection")
|
@SuppressWarnings("SpellCheckingInspection")
|
||||||
private static final Map<File, Storage> storages = new HashMap<File, Storage>(1);
|
private static final Map<File, Storage> storages = new HashMap<File, Storage>(1);
|
||||||
|
|
||||||
|
@ -86,15 +84,7 @@ class StorageType {
|
||||||
void close(Storage _storage) {
|
void close(Storage _storage) {
|
||||||
synchronized (storages) {
|
synchronized (storages) {
|
||||||
File file = _storage.getFile();
|
File file = _storage.getFile();
|
||||||
Storage storage = storages.get(file);
|
close(file);
|
||||||
if (storage instanceof DiskStorage) {
|
|
||||||
final DiskStorage diskStorage = (DiskStorage) storage;
|
|
||||||
boolean isLastOne = diskStorage.decrementReference();
|
|
||||||
if (isLastOne) {
|
|
||||||
diskStorage.close();
|
|
||||||
storages.remove(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,7 +128,8 @@ class StorageType {
|
||||||
class DiskMaker {
|
class DiskMaker {
|
||||||
private File file;
|
private File file;
|
||||||
private SerializationManager serializationManager;
|
private SerializationManager serializationManager;
|
||||||
private boolean readOnly;
|
private boolean readOnly = false;
|
||||||
|
private Logger logger = null;
|
||||||
|
|
||||||
public
|
public
|
||||||
DiskMaker file(File file) {
|
DiskMaker file(File file) {
|
||||||
|
@ -168,6 +159,10 @@ class StorageType {
|
||||||
throw new IllegalArgumentException("serializer cannot be null!");
|
throw new IllegalArgumentException("serializer cannot be null!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.logger == null) {
|
||||||
|
this.logger = NOPLogger.NOP_LOGGER;
|
||||||
|
}
|
||||||
|
|
||||||
// if we load from a NEW storage at the same location as an ALREADY EXISTING storage,
|
// if we load from a NEW storage at the same location as an ALREADY EXISTING storage,
|
||||||
// without saving the existing storage first --- whoops!
|
// without saving the existing storage first --- whoops!
|
||||||
synchronized (storages) {
|
synchronized (storages) {
|
||||||
|
@ -188,7 +183,7 @@ class StorageType {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
try {
|
try {
|
||||||
storage = new DiskStorage(this.file, this.serializationManager, this.readOnly);
|
storage = new DiskStorage(this.file, this.serializationManager, this.readOnly, this.logger);
|
||||||
storages.put(this.file, storage);
|
storages.put(this.file, storage);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("Unable to open storage", e);
|
logger.error("Unable to open storage", e);
|
||||||
|
@ -204,6 +199,12 @@ class StorageType {
|
||||||
this.readOnly = true;
|
this.readOnly = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
DiskMaker logger(final Logger logger) {
|
||||||
|
this.logger = logger;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user