Search in sources :

Example 21 with FileHistoryId

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

the class FileVersionSqlDao method removeFileVersions.

/**
	 * Removes all file versions with versions <b>lower or equal</b> than the given file version.
	 *
	 * <p>Note that this method does not just delete the given file version, but also all of its
	 * previous versions.
	 */
public void removeFileVersions(Map<FileHistoryId, FileVersion> purgeFileVersions) throws SQLException {
    if (purgeFileVersions.size() > 0) {
        try (PreparedStatement preparedStatement = getStatement(connection, "fileversion.delete.all.removeFileVersionsByIds.sql")) {
            for (Map.Entry<FileHistoryId, FileVersion> purgeFileVersionEntry : purgeFileVersions.entrySet()) {
                FileHistoryId purgeFileHistoryId = purgeFileVersionEntry.getKey();
                FileVersion purgeFileVersion = purgeFileVersionEntry.getValue();
                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) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) TreeMap(java.util.TreeMap)

Example 22 with FileHistoryId

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

the class FileVersionSqlDao method getSingleVersionInHistory.

private Map<FileHistoryId, FileVersion> getSingleVersionInHistory(PreparedStatement preparedStatement) throws SQLException {
    try (ResultSet resultSet = preparedStatement.executeQuery()) {
        Map<FileHistoryId, FileVersion> mostRecentPurgeFileVersions = new HashMap<FileHistoryId, FileVersion>();
        while (resultSet.next()) {
            FileHistoryId fileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id"));
            FileVersion fileVersion = createFileVersionFromRow(resultSet);
            mostRecentPurgeFileVersions.put(fileHistoryId, fileVersion);
        }
        return mostRecentPurgeFileVersions;
    }
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) HashMap(java.util.HashMap) FileVersion(org.syncany.database.FileVersion) ResultSet(java.sql.ResultSet)

Example 23 with FileHistoryId

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

the class FileSystemActionReconciliator method determineFileSystemActions.

public List<FileSystemAction> determineFileSystemActions(MemoryDatabase winnersDatabase, boolean cleanupOccurred, List<PartialFileHistory> localFileHistoriesWithLastVersion) throws Exception {
    this.assembler = new Assembler(config, localDatabase, winnersDatabase);
    List<FileSystemAction> fileSystemActions = new ArrayList<FileSystemAction>();
    // Load file history cache
    logger.log(Level.INFO, "- Loading current file tree...");
    Map<FileHistoryId, FileVersion> localFileHistoryIdCache = fillFileHistoryIdCache(localFileHistoriesWithLastVersion);
    logger.log(Level.INFO, "- Determine filesystem actions ...");
    for (PartialFileHistory winningFileHistory : winnersDatabase.getFileHistories()) {
        // Get remote file version and content
        FileVersion winningLastVersion = winningFileHistory.getLastVersion();
        File winningLastFile = new File(config.getLocalDir(), winningLastVersion.getPath());
        // Get local file version and content
        FileVersion localLastVersion = localFileHistoryIdCache.get(winningFileHistory.getFileHistoryId());
        File localLastFile = (localLastVersion != null) ? new File(config.getLocalDir(), localLastVersion.getPath()) : null;
        logger.log(Level.INFO, "  + Comparing local version: " + localLastVersion);
        logger.log(Level.INFO, "    with winning version   : " + winningLastVersion);
        // No local file version in local database
        if (localLastVersion == null) {
            determineActionNoLocalLastVersion(winningLastVersion, winningLastFile, winnersDatabase, fileSystemActions);
        } else // Local version found in local database
        {
            FileVersionComparison localFileToVersionComparison = fileVersionComparator.compare(localLastVersion, localLastFile, true);
            // Local file on disk as expected
            if (localFileToVersionComparison.areEqual()) {
                determineActionWithLocalVersionAndLocalFileAsExpected(winningLastVersion, winningLastFile, localLastVersion, localLastFile, winnersDatabase, fileSystemActions);
            } else // Local file NOT what was expected
            {
                determineActionWithLocalVersionAndLocalFileDiffers(winningLastVersion, winningLastFile, localLastVersion, localLastFile, winnersDatabase, fileSystemActions, localFileToVersionComparison);
            }
        }
    }
    if (cleanupOccurred) {
        logger.log(Level.INFO, "- Determine filesystem actions (for deleted histories in winner's branch)...");
        Map<FileHistoryId, FileVersion> winnerFileHistoryIdCache = fillFileHistoryIdCache(winnersDatabase.getFileHistories());
        for (PartialFileHistory localFileHistoryWithLastVersion : localFileHistoriesWithLastVersion) {
            boolean localFileHistoryInWinnersDatabase = winnerFileHistoryIdCache.get(localFileHistoryWithLastVersion.getFileHistoryId()) != null;
            if (!localFileHistoryInWinnersDatabase) {
                FileVersion localLastVersion = localFileHistoryWithLastVersion.getLastVersion();
                File localLastFile = (localLastVersion != null) ? new File(config.getLocalDir(), localLastVersion.getPath()) : null;
                determineActionFileHistoryNotInWinnerBranch(localLastVersion, localLastFile, fileSystemActions);
            }
        }
    }
    return fileSystemActions;
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) RenameFileSystemAction(org.syncany.operations.down.actions.RenameFileSystemAction) NewFileSystemAction(org.syncany.operations.down.actions.NewFileSystemAction) ChangeFileSystemAction(org.syncany.operations.down.actions.ChangeFileSystemAction) SetAttributesFileSystemAction(org.syncany.operations.down.actions.SetAttributesFileSystemAction) FileSystemAction(org.syncany.operations.down.actions.FileSystemAction) NewSymlinkFileSystemAction(org.syncany.operations.down.actions.NewSymlinkFileSystemAction) DeleteFileSystemAction(org.syncany.operations.down.actions.DeleteFileSystemAction) FileVersion(org.syncany.database.FileVersion) ArrayList(java.util.ArrayList) FileVersionComparison(org.syncany.database.FileVersionComparator.FileVersionComparison) Assembler(org.syncany.operations.Assembler) File(java.io.File) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 24 with FileHistoryId

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

the class GetFileFolderRequestHandler method handleRequest.

@Override
public Response handleRequest(FolderRequest request) {
    GetFileFolderRequest concreteRequest = (GetFileFolderRequest) request;
    try {
        FileHistoryId fileHistoryId = FileHistoryId.parseFileId(concreteRequest.getFileHistoryId());
        long version = concreteRequest.getVersion();
        FileVersion fileVersion = localDatabase.getFileVersion(fileHistoryId, version);
        FileContent fileContent = localDatabase.getFileContent(fileVersion.getChecksum(), true);
        Map<ChunkChecksum, MultiChunkId> multiChunks = localDatabase.getMultiChunkIdsByChecksums(fileContent.getChunks());
        TransferManager transferManager = config.getTransferPlugin().createTransferManager(config.getConnection(), config);
        Downloader downloader = new Downloader(config, transferManager);
        Assembler assembler = new Assembler(config, localDatabase);
        downloader.downloadAndDecryptMultiChunks(new HashSet<MultiChunkId>(multiChunks.values()));
        File tempFile = assembler.assembleToCache(fileVersion);
        String tempFileToken = StringUtil.toHex(ObjectId.secureRandomBytes(40));
        GetFileFolderResponse fileResponse = new GetFileFolderResponse(concreteRequest.getId(), concreteRequest.getRoot(), tempFileToken);
        GetFileFolderResponseInternal fileResponseInternal = new GetFileFolderResponseInternal(fileResponse, tempFile);
        eventBus.post(fileResponseInternal);
        return null;
    } catch (Exception e) {
        logger.log(Level.WARNING, "Cannot reassemble file.", e);
        return new BadRequestResponse(concreteRequest.getId(), "Cannot reassemble file.");
    }
}
Also used : TransferManager(org.syncany.plugins.transfer.TransferManager) FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) Downloader(org.syncany.operations.Downloader) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) FileContent(org.syncany.database.FileContent) FileVersion(org.syncany.database.FileVersion) Assembler(org.syncany.operations.Assembler) File(java.io.File)

Example 25 with FileHistoryId

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

the class MemoryDatabaseCacheTest method testFilenameCacheDeleteAndNewOfSameFileInOneDatabaseVersion.

@Test
public void testFilenameCacheDeleteAndNewOfSameFileInOneDatabaseVersion() throws IOException {
    MemoryDatabase database = new MemoryDatabase();
    // Round 1: Add file history & version
    DatabaseVersion databaseVersion1 = TestDatabaseUtil.createDatabaseVersion();
    FileVersion fileVersion1 = TestDatabaseUtil.createFileVersion("file1.jpg");
    FileHistoryId idFile1 = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory1 = new PartialFileHistory(idFile1);
    fileHistory1.addFileVersion(fileVersion1);
    databaseVersion1.addFileHistory(fileHistory1);
    database.addDatabaseVersion(databaseVersion1);
    assertEquals(fileHistory1, database.getFileHistory("file1.jpg"));
    // Round 2: Add new version
    DatabaseVersion databaseVersion2 = TestDatabaseUtil.createDatabaseVersion(databaseVersion1);
    // - delete file1.jpg
    FileVersion fileVersion2 = TestDatabaseUtil.createFileVersion("file1.jpg", fileVersion1);
    fileVersion2.setStatus(FileStatus.DELETED);
    // same ID
    FileHistoryId idFile2 = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory2 = new PartialFileHistory(idFile2);
    fileHistory2.addFileVersion(fileVersion2);
    databaseVersion2.addFileHistory(fileHistory2);
    // - add file1.jpg (as FOLDER!)
    // new file!
    FileVersion fileVersion3 = TestDatabaseUtil.createFileVersion("file1.jpg");
    fileVersion3.setType(FileType.FOLDER);
    // different ID
    FileHistoryId idFile3 = FileHistoryId.parseFileId("1111111111111112");
    // same ID
    PartialFileHistory fileHistory3 = new PartialFileHistory(idFile3);
    fileHistory3.addFileVersion(fileVersion3);
    databaseVersion2.addFileHistory(fileHistory3);
    // - add database version
    database.addDatabaseVersion(databaseVersion2);
    assertNotNull(database.getFileHistory("file1.jpg"));
    assertEquals(1, database.getFileHistory("file1.jpg").getFileVersions().size());
    assertEquals(fileHistory3, database.getFileHistory("file1.jpg"));
}
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)

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