Search in sources :

Example 21 with PartialFileHistory

use of org.syncany.database.PartialFileHistory 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 22 with PartialFileHistory

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

the class LogOperation method createLightweightDatabaseVersion.

private LightweightDatabaseVersion createLightweightDatabaseVersion(DatabaseVersion databaseVersion) {
    // Create changeset
    ChangeSet changedFiles = new ChangeSet();
    for (PartialFileHistory fileHistory : databaseVersion.getFileHistories()) {
        FileVersion fileVersion = fileHistory.getLastVersion();
        switch(fileVersion.getStatus()) {
            case NEW:
                changedFiles.getNewFiles().add(fileVersion.getPath());
                break;
            case CHANGED:
            case RENAMED:
                changedFiles.getChangedFiles().add(fileVersion.getPath());
                break;
            case DELETED:
                changedFiles.getDeletedFiles().add(fileVersion.getPath());
                break;
        }
    }
    // Create lightweight database version
    LightweightDatabaseVersion lightweightDatabaseVersion = new LightweightDatabaseVersion();
    lightweightDatabaseVersion.setClient(databaseVersion.getHeader().getClient());
    lightweightDatabaseVersion.setDate(databaseVersion.getHeader().getDate());
    lightweightDatabaseVersion.setChangeSet(changedFiles);
    return lightweightDatabaseVersion;
}
Also used : FileVersion(org.syncany.database.FileVersion) ChangeSet(org.syncany.operations.ChangeSet) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 23 with PartialFileHistory

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

the class Indexer method removeDeletedFiles.

private void removeDeletedFiles(DatabaseVersion newDatabaseVersion, List<File> deletedFiles) {
    logger.log(Level.FINER, "- Looking for deleted files ...");
    for (File deletedFile : deletedFiles) {
        String path = FileUtil.getRelativeDatabasePath(config.getLocalDir(), deletedFile);
        PartialFileHistory fileHistory = localDatabase.getFileHistoriesWithLastVersionByPath(path);
        // Ignore this file history if it has been updated in this database version before (file probably renamed!)
        if (newDatabaseVersion.getFileHistory(fileHistory.getFileHistoryId()) != null) {
            continue;
        }
        // Check if file exists, remove if it doesn't
        FileVersion lastLocalVersion = fileHistory.getLastVersion();
        File lastLocalVersionOnDisk = new File(config.getLocalDir() + File.separator + lastLocalVersion.getPath());
        // Ignore this file history if the last version is marked "DELETED"
        if (lastLocalVersion.getStatus() == FileStatus.DELETED) {
            continue;
        }
        // Add this file history if a new file with this name has been added (file type change)
        PartialFileHistory newFileWithSameName = getFileHistoryByPathFromDatabaseVersion(newDatabaseVersion, fileHistory.getLastVersion().getPath());
        // If file has VANISHED, mark as DELETED
        if (!FileUtil.exists(lastLocalVersionOnDisk) || newFileWithSameName != null) {
            PartialFileHistory fileHistoryForDeletion = createFileHistoryForDeletion(fileHistory, lastLocalVersion);
            newDatabaseVersion.addFileHistory(fileHistoryForDeletion);
            logger.log(Level.FINER, "  + Deleted: Adding DELETED version: {0}", fileHistoryForDeletion.getLastVersion());
            logger.log(Level.FINER, "                           based on: {0}", lastLocalVersion);
        }
    }
}
Also used : FileVersion(org.syncany.database.FileVersion) File(java.io.File) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 24 with PartialFileHistory

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

the class DatabaseVersionDaoTest method testPersistDatabaseVersion.

@Test
public void testPersistDatabaseVersion() throws Exception {
    // Setup
    Config testConfig = TestConfigUtil.createTestLocalConfig();
    Connection databaseConnection = testConfig.createDatabaseConnection();
    // Run
    TestSqlUtil.runSqlFromResource(databaseConnection, "test.insert.set1.sql");
    ChunkSqlDao chunkDao = new ChunkSqlDao(databaseConnection);
    MultiChunkSqlDao multiChunkDao = new MultiChunkSqlDao(databaseConnection);
    FileVersionSqlDao fileVersionDao = new FileVersionSqlDao(databaseConnection);
    FileHistorySqlDao fileHistoryDao = new FileHistorySqlDao(databaseConnection, fileVersionDao);
    FileContentSqlDao fileContentDao = new FileContentSqlDao(databaseConnection);
    DatabaseVersionSqlDao databaseVersionDao = new DatabaseVersionSqlDao(databaseConnection, chunkDao, fileContentDao, fileVersionDao, fileHistoryDao, multiChunkDao);
    // a. Capture database version header (now)
    DatabaseVersionHeader lastDatabaseVersionHeaderBefore = databaseVersionDao.getLastDatabaseVersionHeader();
    // b. Add new database header (with one file history)
    DatabaseVersion newDatabaseVersion = new DatabaseVersion();
    DatabaseVersionHeader newDatabaseVersionHeader = new DatabaseVersionHeader();
    newDatabaseVersionHeader.setClient("C");
    newDatabaseVersionHeader.setDate(new Date(1489977288000L));
    newDatabaseVersionHeader.setVectorClock(TestDatabaseUtil.createVectorClock("A5,C1"));
    newDatabaseVersion.setHeader(newDatabaseVersionHeader);
    PartialFileHistory newFileHistory = new PartialFileHistory(FileHistoryId.secureRandomFileId());
    FileVersion newFileVersion = new FileVersion();
    newFileVersion.setVersion(1L);
    newFileVersion.setPath("newfile");
    newFileVersion.setChecksum(FileChecksum.parseFileChecksum("aaaaaaaaaaaaaaaaaaaaab2b263ffa4cc48e282f"));
    newFileVersion.setLinkTarget(null);
    newFileVersion.setPosixPermissions("rwxrwxrwx");
    newFileVersion.setDosAttributes(null);
    newFileVersion.setStatus(FileStatus.NEW);
    newFileVersion.setLastModified(new Date());
    newFileVersion.setUpdated(new Date());
    newFileVersion.setSize(1L);
    newFileVersion.setType(FileType.FILE);
    newFileHistory.addFileVersion(newFileVersion);
    newDatabaseVersion.addFileHistory(newFileHistory);
    ChunkEntry newChunkEntry = new ChunkEntry(ChunkChecksum.parseChunkChecksum("aaaaaaaaaaaaaaaaaaaaab2b263ffa4cc48e282f"), 1);
    newDatabaseVersion.addChunk(newChunkEntry);
    MultiChunkEntry newMultiChunkEntry = new MultiChunkEntry(MultiChunkId.parseMultiChunkId("1234567890987654321234567876543456555555"), 10);
    newMultiChunkEntry.addChunk(newChunkEntry.getChecksum());
    newDatabaseVersion.addMultiChunk(newMultiChunkEntry);
    FileContent newFileContent = new FileContent();
    newFileContent.setChecksum(FileChecksum.parseFileChecksum("aaaaaaaaaaaaaaaaaaaaab2b263ffa4cc48e282f"));
    newFileContent.setSize(1L);
    newFileContent.addChunk(newChunkEntry.getChecksum());
    newDatabaseVersion.addFileContent(newFileContent);
    // c. Persist database version
    databaseVersionDao.writeDatabaseVersion(newDatabaseVersion);
    // d. Capture new last database version header
    DatabaseVersionHeader lastDatabaseVersionHeaderAfter = databaseVersionDao.getLastDatabaseVersionHeader();
    // Test
    assertNotNull(lastDatabaseVersionHeaderBefore);
    assertEquals("A/(A5)/T=1388935689000", lastDatabaseVersionHeaderBefore.toString());
    assertNotNull(lastDatabaseVersionHeaderAfter);
    assertEquals("C/(A5,C1)/T=1489977288000", lastDatabaseVersionHeaderAfter.toString());
    assertEquals(newDatabaseVersionHeader.getVectorClock(), lastDatabaseVersionHeaderAfter.getVectorClock());
    assertEquals(newChunkEntry, chunkDao.getChunk(ChunkChecksum.parseChunkChecksum("aaaaaaaaaaaaaaaaaaaaab2b263ffa4cc48e282f")));
    assertEquals(newFileContent, fileContentDao.getFileContent(FileChecksum.parseFileChecksum("aaaaaaaaaaaaaaaaaaaaab2b263ffa4cc48e282f"), true));
    Map<MultiChunkId, MultiChunkEntry> multiChunkIds = multiChunkDao.getMultiChunks(newDatabaseVersionHeader.getVectorClock());
    assertNotNull(multiChunkIds);
    assertEquals(1, multiChunkIds.size());
    MultiChunkEntry actualNewMultiChunkEntry = multiChunkIds.get(MultiChunkId.parseMultiChunkId("1234567890987654321234567876543456555555"));
    assertNotNull(actualNewMultiChunkEntry);
    assertEquals(newMultiChunkEntry.getId(), actualNewMultiChunkEntry.getId());
    // Tear down
    databaseConnection.close();
    TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
Also used : MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) Config(org.syncany.config.Config) Connection(java.sql.Connection) DatabaseVersionSqlDao(org.syncany.database.dao.DatabaseVersionSqlDao) MultiChunkSqlDao(org.syncany.database.dao.MultiChunkSqlDao) Date(java.util.Date) PartialFileHistory(org.syncany.database.PartialFileHistory) FileContent(org.syncany.database.FileContent) ChunkSqlDao(org.syncany.database.dao.ChunkSqlDao) MultiChunkSqlDao(org.syncany.database.dao.MultiChunkSqlDao) FileVersionSqlDao(org.syncany.database.dao.FileVersionSqlDao) ChunkEntry(org.syncany.database.ChunkEntry) MultiChunkEntry(org.syncany.database.MultiChunkEntry) FileContentSqlDao(org.syncany.database.dao.FileContentSqlDao) FileVersion(org.syncany.database.FileVersion) DatabaseVersionHeader(org.syncany.database.DatabaseVersionHeader) MultiChunkEntry(org.syncany.database.MultiChunkEntry) FileHistorySqlDao(org.syncany.database.dao.FileHistorySqlDao) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 25 with PartialFileHistory

use of org.syncany.database.PartialFileHistory 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

PartialFileHistory (org.syncany.database.PartialFileHistory)31 FileVersion (org.syncany.database.FileVersion)23 Test (org.junit.Test)17 DatabaseVersion (org.syncany.database.DatabaseVersion)12 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)12 MemoryDatabase (org.syncany.database.MemoryDatabase)8 PreparedStatement (java.sql.PreparedStatement)5 SQLException (java.sql.SQLException)5 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 File (java.io.File)3 Config (org.syncany.config.Config)3 ChunkEntry (org.syncany.database.ChunkEntry)3 FileContent (org.syncany.database.FileContent)3 FileChecksum (org.syncany.database.FileContent.FileChecksum)3 MultiChunkEntry (org.syncany.database.MultiChunkEntry)3 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)3 TransferSettings (org.syncany.plugins.transfer.TransferSettings)3 TestClient (org.syncany.tests.util.TestClient)3