Search in sources :

Example 6 with FileHistoryId

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

the class FileHistorySqlDao method getLastVersionByFileHistoryId.

private PartialFileHistory getLastVersionByFileHistoryId(String fileHistoryId) {
    try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.getLastVersionByFileHistoryId.sql")) {
        preparedStatement.setString(1, fileHistoryId);
        preparedStatement.setString(2, fileHistoryId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            if (resultSet.next()) {
                FileVersion lastFileVersion = fileVersionDao.createFileVersionFromRow(resultSet);
                FileHistoryId fileHistoryIdData = FileHistoryId.parseFileId(resultSet.getString("filehistory_id"));
                PartialFileHistory fileHistory = new PartialFileHistory(fileHistoryIdData);
                fileHistory.addFileVersion(lastFileVersion);
                return fileHistory;
            } else {
                return null;
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) SQLException(java.sql.SQLException) FileVersion(org.syncany.database.FileVersion) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 7 with FileHistoryId

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

the class FileVersionSqlDao method removeSpecificFileVersions.

public void removeSpecificFileVersions(Map<FileHistoryId, List<FileVersion>> purgeFileVersions) throws SQLException {
    if (purgeFileVersions.size() > 0) {
        try (PreparedStatement preparedStatement = getStatement(connection, "fileversion.delete.all.removeSpecificFileVersionsByIds.sql")) {
            for (FileHistoryId purgeFileHistoryId : purgeFileVersions.keySet()) {
                for (FileVersion purgeFileVersion : purgeFileVersions.get(purgeFileHistoryId)) {
                    preparedStatement.setString(1, purgeFileHistoryId.toString());
                    preparedStatement.setLong(2, purgeFileVersion.getVersion());
                    preparedStatement.addBatch();
                }
            }
            preparedStatement.executeBatch();
        }
    }
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) FileVersion(org.syncany.database.FileVersion) PreparedStatement(java.sql.PreparedStatement)

Example 8 with FileHistoryId

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

the class GetFileHistoryFolderRequestHandler method handleRequest.

@Override
public Response handleRequest(FolderRequest request) {
    GetFileHistoryFolderRequest concreteRequest = (GetFileHistoryFolderRequest) request;
    FileHistoryId fileHistoryId = FileHistoryId.parseFileId(concreteRequest.getFileHistoryId());
    List<FileVersion> fileHistory = localDatabase.getFileHistory(fileHistoryId);
    return new GetFileHistoryFolderResponse(concreteRequest.getId(), concreteRequest.getRoot(), fileHistory);
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) FileVersion(org.syncany.database.FileVersion)

Example 9 with FileHistoryId

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

the class MemoryDatabaseCacheTest method testRemoveDatabaseVersion.

@Test
public void testRemoveDatabaseVersion() {
    MemoryDatabase database = new MemoryDatabase();
    // Round 1: Add file history & version
    DatabaseVersion databaseVersion1 = TestDatabaseUtil.createDatabaseVersion();
    // - history 1, version 1
    FileVersion fileVersion1 = TestDatabaseUtil.createFileVersion("file.jpg");
    FileHistoryId idFile1 = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory1 = new PartialFileHistory(idFile1);
    fileHistory1.addFileVersion(fileVersion1);
    databaseVersion1.addFileHistory(fileHistory1);
    database.addDatabaseVersion(databaseVersion1);
    // - history 1, version 2		
    DatabaseVersion databaseVersion2 = TestDatabaseUtil.createDatabaseVersion(databaseVersion1);
    FileVersion fileVersion2 = TestDatabaseUtil.createFileVersion("file.jpg", fileVersion1);
    FileHistoryId idFile1b = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory1b = new PartialFileHistory(idFile1b);
    fileHistory1b.addFileVersion(fileVersion2);
    databaseVersion2.addFileHistory(fileHistory1b);
    database.addDatabaseVersion(databaseVersion2);
    // Tests: Database should have 2 versions of file
    assertEquals(2, database.getFileHistory(idFile1).getFileVersions().size());
    assertEquals(2, database.getFileHistory(idFile1b).getFileVersions().size());
    // Round 2: Remove second database version
    database.removeDatabaseVersion(databaseVersion2);
    // Tests: Second version removed, 1 version left
    assertEquals(1, database.getFileHistory(idFile1).getFileVersions().size());
    assertEquals(1, database.getFileHistory(idFile1b).getFileVersions().size());
    assertEquals(fileVersion1, database.getFileHistory(idFile1).getLastVersion());
    // Round 3: Add database version again
    database.addDatabaseVersion(databaseVersion2);
    // Tests: Second version added, 2 versions of file
    assertEquals(2, database.getFileHistory(idFile1).getFileVersions().size());
    assertEquals(2, database.getFileHistory(idFile1b).getFileVersions().size());
    // Round 4: Remove FIRST database version		
    database.removeDatabaseVersion(databaseVersion1);
    // Tests: First version removed, 1 version left
    assertEquals(1, database.getFileHistory(idFile1).getFileVersions().size());
    assertEquals(1, database.getFileHistory(idFile1b).getFileVersions().size());
    assertEquals(fileVersion2, database.getFileHistory(idFile1).getLastVersion());
    // Round 5: Remove second database version		
    database.removeDatabaseVersion(databaseVersion2);
    // Tests: Second version removed, none left
    assertNull(database.getFileHistory(idFile1));
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) FileVersion(org.syncany.database.FileVersion) MemoryDatabase(org.syncany.database.MemoryDatabase) PartialFileHistory(org.syncany.database.PartialFileHistory) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 10 with FileHistoryId

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

the class RestoreOperation method execute.

@Override
public RestoreOperationResult execute() throws Exception {
    logger.log(Level.INFO, "");
    logger.log(Level.INFO, "Running 'Restore' at client " + config.getMachineName() + " ...");
    logger.log(Level.INFO, "--------------------------------------------");
    // Find file history
    FileHistoryId restoreFileHistoryId = findFileHistoryId();
    if (restoreFileHistoryId == null) {
        return new RestoreOperationResult(RestoreResultCode.NACK_NO_FILE);
    }
    // Find file version
    FileVersion restoreFileVersion = findRestoreFileVersion(restoreFileHistoryId);
    if (restoreFileVersion == null) {
        return new RestoreOperationResult(RestoreResultCode.NACK_NO_FILE);
    } else if (restoreFileVersion.getType() == FileType.FOLDER) {
        return new RestoreOperationResult(RestoreResultCode.NACK_INVALID_FILE);
    }
    logger.log(Level.INFO, "Restore file identified: " + restoreFileVersion);
    // Download multichunks
    downloadMultiChunks(restoreFileVersion);
    // Restore file
    logger.log(Level.INFO, "- Restoring: " + restoreFileVersion);
    RestoreFileSystemAction restoreAction = new RestoreFileSystemAction(config, assembler, restoreFileVersion, options.getRelativeTargetPath());
    RestoreFileSystemActionResult restoreResult = restoreAction.execute();
    return new RestoreOperationResult(RestoreResultCode.ACK, restoreResult.getTargetFile());
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) FileVersion(org.syncany.database.FileVersion)

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