Search in sources :

Example 1 with FileHistoryId

use of org.syncany.database.PartialFileHistory.FileHistoryId in project syncany by syncany.

the class RestoreFileScenarioTest method testRestoreDeletedFileWithTargetFile.

@Test
public void testRestoreDeletedFileWithTargetFile() throws Exception {
    // Setup 
    File tempDir = TestFileUtil.createTempDirectoryInSystemTemp();
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    java.sql.Connection databaseConnectionA = DatabaseConnectionFactory.createConnection(clientA.getDatabaseFile(), false);
    // A new/up
    clientA.createNewFile("A-original");
    clientA.upWithForceChecksum();
    String originalFileHistoryStr = TestSqlUtil.runSqlSelect("select filehistory_id from fileversion", databaseConnectionA);
    assertNotNull(originalFileHistoryStr);
    FileHistoryId originalFileHistoryId = FileHistoryId.parseFileId(originalFileHistoryStr);
    // A "delete"
    File deletedFile = new File(tempDir, "A-original-DELETED");
    FileUtils.moveFile(clientA.getLocalFile("A-original"), deletedFile);
    clientA.upWithForceChecksum();
    // A restore
    RestoreOperationOptions operationOptions = new RestoreOperationOptions();
    operationOptions.setFileHistoryId(originalFileHistoryId);
    operationOptions.setFileVersion(1);
    operationOptions.setRelativeTargetPath("restored-file");
    clientA.restore(operationOptions);
    assertTrue(clientA.getLocalFile("restored-file").exists());
    assertEquals(StringUtil.toHex(TestFileUtil.createChecksum(deletedFile)), StringUtil.toHex(TestFileUtil.createChecksum(clientA.getLocalFile("restored-file"))));
    assertEquals(deletedFile.lastModified(), clientA.getLocalFile("restored-file").lastModified());
    assertEquals(deletedFile.length(), clientA.getLocalFile("restored-file").length());
    // Tear down
    clientA.deleteTestData();
    TestFileUtil.deleteDirectory(tempDir);
}
Also used : RestoreOperationOptions(org.syncany.operations.restore.RestoreOperationOptions) FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) TestClient(org.syncany.tests.util.TestClient) TransferSettings(org.syncany.plugins.transfer.TransferSettings) File(java.io.File) Test(org.junit.Test)

Example 2 with FileHistoryId

use of org.syncany.database.PartialFileHistory.FileHistoryId in project syncany by syncany.

the class RestoreFileScenarioTest method testRestoreDeletedFile.

@Test
public void testRestoreDeletedFile() throws Exception {
    // Setup 
    File tempDir = TestFileUtil.createTempDirectoryInSystemTemp();
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    java.sql.Connection databaseConnectionA = DatabaseConnectionFactory.createConnection(clientA.getDatabaseFile(), false);
    // A new/up
    clientA.createNewFile("A-original");
    clientA.upWithForceChecksum();
    String originalFileHistoryStr = TestSqlUtil.runSqlSelect("select filehistory_id from fileversion", databaseConnectionA);
    assertNotNull(originalFileHistoryStr);
    FileHistoryId originalFileHistoryId = FileHistoryId.parseFileId(originalFileHistoryStr);
    // A "delete"
    File deletedFile = new File(tempDir, "A-original-DELETED");
    FileUtils.moveFile(clientA.getLocalFile("A-original"), deletedFile);
    clientA.upWithForceChecksum();
    // A restore
    RestoreOperationOptions operationOptions = new RestoreOperationOptions();
    operationOptions.setFileHistoryId(originalFileHistoryId);
    operationOptions.setFileVersion(1);
    clientA.restore(operationOptions);
    assertTrue(clientA.getLocalFile("A-original (restored version 1)").exists());
    assertEquals(StringUtil.toHex(TestFileUtil.createChecksum(deletedFile)), StringUtil.toHex(TestFileUtil.createChecksum(clientA.getLocalFile("A-original (restored version 1)"))));
    assertEquals(deletedFile.lastModified(), clientA.getLocalFile("A-original (restored version 1)").lastModified());
    assertEquals(deletedFile.length(), clientA.getLocalFile("A-original (restored version 1)").length());
    // Tear down
    clientA.deleteTestData();
    TestFileUtil.deleteDirectory(tempDir);
}
Also used : RestoreOperationOptions(org.syncany.operations.restore.RestoreOperationOptions) FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) TestClient(org.syncany.tests.util.TestClient) TransferSettings(org.syncany.plugins.transfer.TransferSettings) File(java.io.File) Test(org.junit.Test)

Example 3 with FileHistoryId

use of org.syncany.database.PartialFileHistory.FileHistoryId in project syncany by syncany.

the class RestoreFileScenarioTest method testRestoreDeletedWithSubfolders.

@Test
public void testRestoreDeletedWithSubfolders() throws Exception {
    // Setup 
    File tempDir = TestFileUtil.createTempDirectoryInSystemTemp();
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    java.sql.Connection databaseConnectionA = DatabaseConnectionFactory.createConnection(clientA.getDatabaseFile(), false);
    // A new/up
    clientA.createNewFolder("folder/subfolder");
    clientA.createNewFile("folder/subfolder/A-original");
    clientA.upWithForceChecksum();
    String originalFileHistoryStr = TestSqlUtil.runSqlSelect("select filehistory_id from fileversion where path='folder/subfolder/A-original'", databaseConnectionA);
    assertNotNull(originalFileHistoryStr);
    FileHistoryId originalFileHistoryId = FileHistoryId.parseFileId(originalFileHistoryStr);
    // A "delete"
    FileUtils.deleteDirectory(clientA.getLocalFile("folder"));
    clientA.upWithForceChecksum();
    assertFalse(clientA.getLocalFile("folder").exists());
    // A restore
    RestoreOperationOptions operationOptions = new RestoreOperationOptions();
    operationOptions.setFileHistoryId(originalFileHistoryId);
    operationOptions.setFileVersion(1);
    clientA.restore(operationOptions);
    assertTrue(clientA.getLocalFile("folder/subfolder").exists());
    assertTrue(clientA.getLocalFile("folder/subfolder/A-original (restored version 1)").exists());
    // Tear down
    clientA.deleteTestData();
    TestFileUtil.deleteDirectory(tempDir);
}
Also used : RestoreOperationOptions(org.syncany.operations.restore.RestoreOperationOptions) FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) TestClient(org.syncany.tests.util.TestClient) TransferSettings(org.syncany.plugins.transfer.TransferSettings) File(java.io.File) Test(org.junit.Test)

Example 4 with FileHistoryId

use of org.syncany.database.PartialFileHistory.FileHistoryId in project syncany by syncany.

the class FileHistorySqlDao method createFileHistoriesFromResult.

protected Map<FileHistoryId, PartialFileHistory> createFileHistoriesFromResult(ResultSet resultSet) throws SQLException {
    Map<FileHistoryId, PartialFileHistory> fileHistories = new HashMap<FileHistoryId, PartialFileHistory>();
    PartialFileHistory fileHistory = null;
    while (resultSet.next()) {
        FileVersion lastFileVersion = fileVersionDao.createFileVersionFromRow(resultSet);
        FileHistoryId fileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id"));
        // Old history (= same filehistory identifier)
        if (fileHistory != null && fileHistory.getFileHistoryId().equals(fileHistoryId)) {
            // Same history!
            fileHistory.addFileVersion(lastFileVersion);
        } else // New history!
        {
            // Add the old history
            if (fileHistory != null) {
                fileHistories.put(fileHistory.getFileHistoryId(), fileHistory);
            }
            // Create a new one
            fileHistory = new PartialFileHistory(fileHistoryId);
            fileHistory.addFileVersion(lastFileVersion);
        }
    }
    // Add the last history
    if (fileHistory != null) {
        fileHistories.put(fileHistory.getFileHistoryId(), fileHistory);
    }
    return fileHistories;
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) HashMap(java.util.HashMap) FileVersion(org.syncany.database.FileVersion) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 5 with FileHistoryId

use of org.syncany.database.PartialFileHistory.FileHistoryId in project syncany by syncany.

the class FileHistorySqlDao method getFileHistoriesWithLastVersion.

public List<PartialFileHistory> getFileHistoriesWithLastVersion() {
    List<PartialFileHistory> fileHistories = new ArrayList<PartialFileHistory>();
    try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.getFileHistoriesWithLastVersion.sql")) {
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                FileHistoryId fileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id"));
                FileVersion lastFileVersion = fileVersionDao.createFileVersionFromRow(resultSet);
                PartialFileHistory fileHistory = new PartialFileHistory(fileHistoryId);
                fileHistory.addFileVersion(lastFileVersion);
                fileHistories.add(fileHistory);
            }
        }
        return fileHistories;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) SQLException(java.sql.SQLException) FileVersion(org.syncany.database.FileVersion) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PartialFileHistory(org.syncany.database.PartialFileHistory)

Aggregations

FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)28 FileVersion (org.syncany.database.FileVersion)21 PartialFileHistory (org.syncany.database.PartialFileHistory)12 Test (org.junit.Test)10 DatabaseVersion (org.syncany.database.DatabaseVersion)8 File (java.io.File)6 ArrayList (java.util.ArrayList)6 MemoryDatabase (org.syncany.database.MemoryDatabase)6 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)4 SQLException (java.sql.SQLException)3 List (java.util.List)3 TreeMap (java.util.TreeMap)3 ChunkChecksum (org.syncany.database.ChunkEntry.ChunkChecksum)3 FileContent (org.syncany.database.FileContent)3 FileChecksum (org.syncany.database.FileContent.FileChecksum)3 MultiChunkEntry (org.syncany.database.MultiChunkEntry)3 RestoreOperationOptions (org.syncany.operations.restore.RestoreOperationOptions)3