Use PersistenceManager for StoreService (not yet supported for subclasses)

This commit is contained in:
chimp1984 2020-10-01 17:20:50 -05:00
parent 3a348d5a0f
commit 2e55251bcf
No known key found for this signature in database
GPG key ID: 9801B4EC591F90E3

View file

@ -19,13 +19,12 @@ package bisq.network.p2p.storage.persistence;
import bisq.common.file.FileUtil;
import bisq.common.file.ResourceNotFoundException;
import bisq.common.persistence.PersistenceManager;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.storage.Storage;
import java.nio.file.Paths;
import java.io.File;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
@ -43,7 +42,7 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public abstract class StoreService<T extends PersistableEnvelope> {
protected final Storage<T> storage;
protected final PersistenceManager<T> persistenceManager;
protected final String absolutePathOfStorageDir;
protected T store;
@ -52,12 +51,9 @@ public abstract class StoreService<T extends PersistableEnvelope> {
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
public StoreService(File storageDir,
Storage<T> storage) {
this.storage = storage;
public StoreService(File storageDir, PersistenceManager<T> persistenceManager) {
this.persistenceManager = persistenceManager;
absolutePathOfStorageDir = storageDir.getAbsolutePath();
storage.setNumMaxBackupFiles(1);
}
@ -65,8 +61,8 @@ public abstract class StoreService<T extends PersistableEnvelope> {
// API
///////////////////////////////////////////////////////////////////////////////////////////
protected void persist() {
storage.queueUpForSave(store, 200);
protected void requestPersistence() {
persistenceManager.requestPersistence();
}
protected T getStore() {
@ -86,11 +82,6 @@ public abstract class StoreService<T extends PersistableEnvelope> {
try {
readStore();
} catch (Throwable t) {
try {
storage.removeAndBackupFile(fileName);
} catch (IOException e) {
log.error(e.toString());
}
makeFileFromResourceFile(fileName, postFix);
readStore();
}
@ -122,11 +113,16 @@ public abstract class StoreService<T extends PersistableEnvelope> {
}
protected T getStore(String fileName) {
T store = storage.initAndGetPersistedWithFileName(fileName, 100);
if (store != null) {
log.info("{}: size of {}: {} MB", this.getClass().getSimpleName(),
storage.getClass().getSimpleName(),
store.toProtoMessage().toByteArray().length / 1_000_000D);
T store;
T persisted = persistenceManager.getPersisted(fileName);
if (persisted != null) {
store = persisted;
int length = store.toProtoMessage().toByteArray().length;
double size = length > 1_000_000D ? length / 1_000_000D : length / 1_000D;
String unit = length > 1_000_000D ? "MB" : "KB";
log.info("{}: size of {}: {} {}", this.getClass().getSimpleName(),
persisted.getClass().getSimpleName(), size, unit);
} else {
store = createStore();
}
@ -135,6 +131,11 @@ public abstract class StoreService<T extends PersistableEnvelope> {
protected void readStore() {
store = getStore(getFileName());
initializePersistenceManager();
}
protected void initializePersistenceManager() {
persistenceManager.initialize(store);
}
protected abstract T createStore();