Skip to content

Commit

Permalink
Merge pull request #669 from alvasw/persistable_store_file_manager_im…
Browse files Browse the repository at this point in the history
…plement_restore_backup

PerStoreFileManager: Support restore backup
  • Loading branch information
alvasw authored Mar 2, 2023
2 parents 36b0436 + 2a23f0a commit 3b4d31b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,33 @@

package bisq.persistence;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;

@Slf4j
public class PersistableStoreFileManager {

public static final String BACKUP_FILE_PREFIX = "backup_";
public static final String TEMP_FILE_PREFIX = "temp_";

@Getter
private final Path storeFilePath;
private final Path parentDirectoryPath;

private final Path backupFilePath;
private final Path tempFilePath;

public PersistableStoreFileManager(Path storeFilePath) {
this.storeFilePath = storeFilePath;
this.parentDirectoryPath = storeFilePath.getParent();
this.backupFilePath = createBackupFilePath();
this.tempFilePath = createTempFilePath();
}

public void createParentDirectoriesIfNotExisting() {
Expand All @@ -47,15 +57,12 @@ public void createParentDirectoriesIfNotExisting() {
}

public void tryToBackupCurrentStoreFile() throws IOException {
String backupFileName = BACKUP_FILE_PREFIX + storeFilePath.getFileName();
Path backupFilePath = parentDirectoryPath.resolve(backupFileName);
File backupFile = backupFilePath.toFile();

File storeFile = storeFilePath.toFile();
if (!storeFile.exists()) {
return;
}

File backupFile = backupFilePath.toFile();
if (backupFile.exists()) {
Files.delete(backupFilePath);
}
Expand All @@ -66,15 +73,25 @@ public void tryToBackupCurrentStoreFile() throws IOException {
}
}

public void restoreBackupFileIfCurrentFileNotExisting() {
File storeFile = storeFilePath.toFile();
if (!storeFile.exists()) {
File backupFile = backupFilePath.toFile();
boolean isSuccess = backupFile.renameTo(storeFile);

if (!isSuccess) {
log.error("Couldn't rename " + backupFile + " to " + storeFilePath);
}
}
}

public void renameTempFileToCurrentFile() throws IOException {
File storeFile = storeFilePath.toFile();
if (storeFile.exists()) {
throw new IOException(storeFilePath + " does already exist.");
}

String tempFileName = TEMP_FILE_PREFIX + storeFilePath.getFileName();
File tempFile = parentDirectoryPath.resolve(tempFileName).toFile();

File tempFile = tempFilePath.toFile();
if (!tempFile.exists()) {
throw new NoSuchFileException(tempFile.getAbsolutePath() + " does not exist. Cannot rename not existing file.");
}
Expand All @@ -84,4 +101,14 @@ public void renameTempFileToCurrentFile() throws IOException {
throw new IOException("Couldn't rename " + tempFile + " to " + storeFilePath);
}
}

private Path createBackupFilePath() {
String backupFileName = BACKUP_FILE_PREFIX + storeFilePath.getFileName();
return parentDirectoryPath.resolve(backupFileName);
}

private Path createTempFilePath() {
String tempFileName = TEMP_FILE_PREFIX + storeFilePath.getFileName();
return parentDirectoryPath.resolve(tempFileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,35 @@ void backupNotExistingStore(@TempDir Path tempDir) throws IOException {
assertThat(storePath).doesNotExist();
}

@Test
void restoreBackupIfCurrentFileNotExisting(@TempDir Path tempDir) throws IOException {
Path storePath = tempDir.resolve("store");
var storeFileManager = new PersistableStoreFileManager(storePath);

Path backupFilePath = tempDir.resolve(PersistableStoreFileManager.BACKUP_FILE_PREFIX + "store");
boolean isSuccess = backupFilePath.toFile().createNewFile();
assertThat(isSuccess).isTrue();

storeFileManager.restoreBackupFileIfCurrentFileNotExisting();
assertThat(storePath).exists();
assertThat(backupFilePath).doesNotExist();
}

@Test
void restoreBackupIfCurrentFileExists(@TempDir Path tempDir) throws IOException {
Path storePath = tempDir.resolve("store");
createEmptyFile(storePath);
var storeFileManager = new PersistableStoreFileManager(storePath);

Path backupFilePath = tempDir.resolve(PersistableStoreFileManager.BACKUP_FILE_PREFIX + "store");
boolean isSuccess = backupFilePath.toFile().createNewFile();
assertThat(isSuccess).isTrue();

storeFileManager.restoreBackupFileIfCurrentFileNotExisting();
assertThat(storePath).exists();
assertThat(backupFilePath).exists();
}

@Test
void renameTempFileToCurrentFileIfCurrentNotExisting(@TempDir Path tempDir) throws IOException {
Path tmpFilePath = tempDir.resolve(PersistableStoreFileManager.TEMP_FILE_PREFIX + "store");
Expand Down

0 comments on commit 3b4d31b

Please sign in to comment.