Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PerStoreFileManager: Support restore backup #669

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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