Make num backups editable per storage, reduce for closed trades to 3

This commit is contained in:
Manfred Karrer 2016-08-07 16:41:14 +02:00
parent 96fea0c98c
commit c9349d2935
4 changed files with 12 additions and 8 deletions

View File

@ -152,8 +152,8 @@ public class FileManager<T> {
renameTempFileToFile(storageFile, corruptedFile);
}
public synchronized void backupFile(String fileName) throws IOException {
FileUtil.rollingBackup(dir, fileName);
public synchronized void backupFile(String fileName, int numMaxBackupFiles) throws IOException {
FileUtil.rollingBackup(dir, fileName, numMaxBackupFiles);
}
///////////////////////////////////////////////////////////////////////////////////////////

View File

@ -15,10 +15,6 @@ import java.util.List;
public class FileUtil {
private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
public static void rollingBackup(File dir, String fileName) {
rollingBackup(dir, fileName, 10);
}
public static void rollingBackup(File dir, String fileName, int numMaxBackupFiles) {
if (dir.exists()) {
File backupDir = new File(Paths.get(dir.getAbsolutePath(), "backup").toString());

View File

@ -65,6 +65,7 @@ public class Storage<T extends Serializable> {
private File storageFile;
private T serializable;
private String fileName;
private int numMaxBackupFiles = 10;
///////////////////////////////////////////////////////////////////////////////////////////
@ -115,6 +116,10 @@ public class Storage<T extends Serializable> {
queueUpForSave(serializable, delayInMilli);
}
public void setNumMaxBackupFiles(int numMaxBackupFiles) {
this.numMaxBackupFiles = numMaxBackupFiles;
}
// Save delayed and on a background thread
private void queueUpForSave(T serializable) {
if (serializable != null) {
@ -159,7 +164,7 @@ public class Storage<T extends Serializable> {
// If we did not get any exception we can be sure the data are consistent so we make a backup
now = System.currentTimeMillis();
fileManager.backupFile(fileName);
fileManager.backupFile(fileName, numMaxBackupFiles);
log.trace("Backup {} completed in {}msec", storageFile, System.currentTimeMillis() - now);
return persistedObject;

View File

@ -40,7 +40,10 @@ public class ClosedTradableManager {
@Inject
public ClosedTradableManager(KeyRing keyRing, PriceFeedService priceFeedService, @Named(Storage.DIR_KEY) File storageDir) {
this.keyRing = keyRing;
this.closedTrades = new TradableList<>(new Storage<>(storageDir), "ClosedTrades");
final Storage<TradableList<Tradable>> tradableListStorage = new Storage<>(storageDir);
// The ClosedTrades object can become a few MB so we don't keep so many backups
tradableListStorage.setNumMaxBackupFiles(3);
this.closedTrades = new TradableList<>(tradableListStorage, "ClosedTrades");
closedTrades.forEach(e -> e.getOffer().setPriceFeedService(priceFeedService));
}