Search in sources :

Example 6 with PartialFileHistory

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

the class FileHistorySqlDao method getFileHistoryWithLastVersionByPath.

/**
	 * This function returns a FileHistory, with as last version a FileVersion with
	 * the given path. 
	 * 
	 * If the last FileVersion referring to this path is not the last in the
	 * FileHistory, or no such FileVersion exists, null is returned.
	 */
public PartialFileHistory getFileHistoryWithLastVersionByPath(String path) {
    try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.findLatestFileVersionsForPath.sql")) {
        preparedStatement.setString(1, path);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            // Fetch the latest versions of all files that once existed with the given
            // path and find the most recent by comparing vector clocks
            String latestFileHistoryId = null;
            Long latestFileVersion = null;
            VectorClock latestVectorClock = null;
            while (resultSet.next()) {
                VectorClock resultSetVectorClock = VectorClock.parseVectorClock(resultSet.getString("vectorclock_serialized"));
                boolean vectorClockIsGreater = latestVectorClock == null || VectorClock.compare(resultSetVectorClock, latestVectorClock) == VectorClock.VectorClockComparison.GREATER;
                if (vectorClockIsGreater) {
                    latestVectorClock = resultSetVectorClock;
                    latestFileHistoryId = resultSet.getString("filehistory_id");
                    latestFileVersion = resultSet.getLong("version");
                }
            }
            // If no active file history exists for this path, return
            if (latestFileHistoryId == null) {
                return null;
            }
            // Get the last FileVersion of the FileHistory in the database with the largest vectorclock.
            PartialFileHistory fileHistory = getLastVersionByFileHistoryId(latestFileHistoryId);
            // history. We need to check this before returning the file.
            if (fileHistory.getLastVersion().getVersion() == latestFileVersion) {
                return fileHistory;
            } else {
                // which should be continued.
                return null;
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) VectorClock(org.syncany.database.VectorClock) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 7 with PartialFileHistory

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

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

the class FileHistorySqlDao method getFileHistoriesByChecksumSizeAndModifiedDate.

/**
	 * This function returns FileHistories with the last version for which this last version
	 * matches the given checksum, size and modified date. 
	 * 
	 * @return An empty Collection is returned if none exist.
	 */
public Collection<PartialFileHistory> getFileHistoriesByChecksumSizeAndModifiedDate(String filecontentChecksum, long size, Date modifiedDate) {
    try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.getFileHistoriesByChecksumSizeAndModifiedDate.sql")) {
        // This first query retrieves the last version for each FileHistory matching the three requested properties.
        // However, it does not guarantee that this version is indeed the last version in that particular
        // FileHistory, so we need another query to verify that.
        preparedStatement.setString(1, filecontentChecksum);
        preparedStatement.setLong(2, size);
        preparedStatement.setTimestamp(3, new Timestamp(modifiedDate.getTime()));
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            Collection<PartialFileHistory> fileHistories = new ArrayList<>();
            while (resultSet.next()) {
                String fileHistoryId = resultSet.getString("filehistory_id");
                PartialFileHistory fileHistory = getLastVersionByFileHistoryId(fileHistoryId);
                boolean resultIsLatestVersion = fileHistory.getLastVersion().getVersion() == resultSet.getLong("version");
                boolean resultIsNotDelete = fileHistory.getLastVersion().getStatus() != FileVersion.FileStatus.DELETED;
                if (resultIsLatestVersion && resultIsNotDelete) {
                    fileHistories.add(fileHistory);
                }
            }
            return fileHistories;
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 9 with PartialFileHistory

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

the class DatabaseXmlWriter method writeFileHistories.

private void writeFileHistories(IndentXmlStreamWriter xmlOut, Collection<PartialFileHistory> fileHistories) throws XMLStreamException, IOException {
    xmlOut.writeStartElement("fileHistories");
    for (PartialFileHistory fileHistory : fileHistories) {
        xmlOut.writeStartElement("fileHistory");
        xmlOut.writeAttribute("id", fileHistory.getFileHistoryId().toString());
        xmlOut.writeStartElement("fileVersions");
        Collection<FileVersion> fileVersions = fileHistory.getFileVersions().values();
        for (FileVersion fileVersion : fileVersions) {
            if (fileVersion.getVersion() == null || fileVersion.getType() == null || fileVersion.getPath() == null || fileVersion.getStatus() == null || fileVersion.getSize() == null || fileVersion.getLastModified() == null) {
                throw new IOException("Unable to write file version, because one or many mandatory fields are null (version, type, path, name, status, size, last modified): " + fileVersion);
            }
            if (fileVersion.getType() == FileType.SYMLINK && fileVersion.getLinkTarget() == null) {
                throw new IOException("Unable to write file version: All symlinks must have a target.");
            }
            xmlOut.writeEmptyElement("fileVersion");
            xmlOut.writeAttribute("version", fileVersion.getVersion());
            xmlOut.writeAttribute("type", fileVersion.getType().toString());
            xmlOut.writeAttribute("status", fileVersion.getStatus().toString());
            if (containsXmlRestrictedChars(fileVersion.getPath())) {
                xmlOut.writeAttribute("pathEncoded", encodeXmlRestrictedChars(fileVersion.getPath()));
            } else {
                xmlOut.writeAttribute("path", fileVersion.getPath());
            }
            xmlOut.writeAttribute("size", fileVersion.getSize());
            xmlOut.writeAttribute("lastModified", fileVersion.getLastModified().getTime());
            if (fileVersion.getLinkTarget() != null) {
                xmlOut.writeAttribute("linkTarget", fileVersion.getLinkTarget());
            }
            if (fileVersion.getUpdated() != null) {
                xmlOut.writeAttribute("updated", fileVersion.getUpdated().getTime());
            }
            if (fileVersion.getChecksum() != null) {
                xmlOut.writeAttribute("checksum", fileVersion.getChecksum().toString());
            }
            if (fileVersion.getDosAttributes() != null) {
                xmlOut.writeAttribute("dosattrs", fileVersion.getDosAttributes());
            }
            if (fileVersion.getPosixPermissions() != null) {
                xmlOut.writeAttribute("posixperms", fileVersion.getPosixPermissions());
            }
        }
        // </fileVersions>
        xmlOut.writeEndElement();
        // </fileHistory>	
        xmlOut.writeEndElement();
    }
    // </fileHistories>		
    xmlOut.writeEndElement();
}
Also used : FileVersion(org.syncany.database.FileVersion) IOException(java.io.IOException) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 10 with PartialFileHistory

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

the class FileSystemActionReconciliatorTest method testFileSystemActionReconDeleteNonExistingFolder.

@Test
public void testFileSystemActionReconDeleteNonExistingFolder() throws Exception {
    // Setup
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    Config testConfigA = clientA.getConfig();
    // - Create first database version
    clientA.createNewFolder("new folder/some subfolder");
    clientA.upWithForceChecksum();
    // Delete this!
    clientA.deleteFile("new folder/some subfolder");
    // - Create new version (delete folder)
    TestSqlDatabase sqlDatabaseA = new TestSqlDatabase(testConfigA);
    PartialFileHistory folderFileHistoryWithLastVersion = sqlDatabaseA.getFileHistoryWithLastVersion("new folder/some subfolder");
    FileVersion deletedFolderVersion = folderFileHistoryWithLastVersion.getLastVersion().clone();
    deletedFolderVersion.setStatus(FileStatus.DELETED);
    deletedFolderVersion.setVersion(deletedFolderVersion.getVersion() + 1);
    PartialFileHistory deletedFolderVersionHistory = new PartialFileHistory(folderFileHistoryWithLastVersion.getFileHistoryId());
    deletedFolderVersionHistory.addFileVersion(deletedFolderVersion);
    DatabaseVersion winnersDatabaseVersion = TestDatabaseUtil.createDatabaseVersion(sqlDatabaseA.getLastDatabaseVersionHeader());
    winnersDatabaseVersion.addFileHistory(deletedFolderVersionHistory);
    // - Create memory database with this version
    MemoryDatabase winnersDatabase = new MemoryDatabase();
    winnersDatabase.addDatabaseVersion(winnersDatabaseVersion);
    // Run! Finally!
    DownOperationResult outDownOperationResult = new DownOperationResult();
    FileSystemActionReconciliator fileSystemActionReconciliator = new FileSystemActionReconciliator(testConfigA, outDownOperationResult.getChangeSet());
    List<FileSystemAction> fileSystemActions = fileSystemActionReconciliator.determineFileSystemActions(winnersDatabase);
    assertNotNull(fileSystemActions);
    assertEquals(0, fileSystemActions.size());
    // Tear down
    clientA.deleteTestData();
}
Also used : Config(org.syncany.config.Config) DownOperationResult(org.syncany.operations.down.DownOperationResult) FileSystemAction(org.syncany.operations.down.actions.FileSystemAction) TestClient(org.syncany.tests.util.TestClient) FileVersion(org.syncany.database.FileVersion) MemoryDatabase(org.syncany.database.MemoryDatabase) TestSqlDatabase(org.syncany.tests.util.TestSqlDatabase) TransferSettings(org.syncany.plugins.transfer.TransferSettings) PartialFileHistory(org.syncany.database.PartialFileHistory) DatabaseVersion(org.syncany.database.DatabaseVersion) FileSystemActionReconciliator(org.syncany.operations.down.FileSystemActionReconciliator) 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