Search in sources :

Example 1 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 2 with PartialFileHistory

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

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

the class FileHistorySqlDao method writeFileHistories.

/**
 * Writes a list of {@link PartialFileHistory}s to the database table <i>filehistory</i> using <tt>INSERT</tt>s
 * and the given connection. In addition, this method also writes the corresponding {@link FileVersion}s of
 * each file history to the database using
 * {@link FileVersionSqlDao#writeFileVersions(Connection, FileHistoryId, long, Collection) FileVersionSqlDao#writeFileVersions}.
 *
 * <p><b>Note:</b> This method executes, but <b>does not commit</b> the queries.
 *
 * @param connection The connection used to execute the statements
 * @param databaseVersionId References the {@link PartialFileHistory} to which the list of file versions belongs
 * @param fileHistories List of {@link PartialFileHistory}s to be written to the database
 * @throws SQLException If the SQL statement fails
 */
public void writeFileHistories(Connection connection, long databaseVersionId, Collection<PartialFileHistory> fileHistories) throws SQLException {
    for (PartialFileHistory fileHistory : fileHistories) {
        PreparedStatement preparedStatement = getStatement(connection, "filehistory.insert.all.writeFileHistories.sql");
        preparedStatement.setString(1, fileHistory.getFileHistoryId().toString());
        preparedStatement.setLong(2, databaseVersionId);
        int affectedRows = preparedStatement.executeUpdate();
        if (affectedRows == 0) {
            throw new SQLException("Cannot add database version header. Affected rows is zero.");
        }
        preparedStatement.close();
        fileVersionDao.writeFileVersions(connection, fileHistory.getFileHistoryId(), databaseVersionId, fileHistory.getFileVersions().values());
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 5 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)

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