Search in sources :

Example 31 with FileVersion

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

the class FileVersionSqlDao method getFileHistory.

public List<FileVersion> getFileHistory(FileHistoryId fileHistoryId) {
    try (PreparedStatement preparedStatement = getStatement("fileversion.select.master.getFileHistoryById.sql")) {
        preparedStatement.setString(1, fileHistoryId.toString());
        List<FileVersion> fileTree = new ArrayList<FileVersion>();
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                FileVersion fileVersion = createFileVersionFromRow(resultSet);
                fileTree.add(fileVersion);
            }
            return fileTree;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) FileVersion(org.syncany.database.FileVersion) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 32 with FileVersion

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

the class FileVersionSqlDao method getCurrentFileTree.

/**
 * Queries the database for the currently active {@link FileVersion}s and returns it
 * as a map. If the current file tree (on the disk) has not changed, the result will
 * match the files on the disk.
 *
 * <p>Keys in the returned map correspond to the file version's relative file path,
 * and values to the actual {@link FileVersion} object.
 *
 * @return Returns the current file tree as a map of relative paths to {@link FileVersion} objects
 */
public Map<String, FileVersion> getCurrentFileTree() {
    try (PreparedStatement preparedStatement = getStatement("fileversion.select.master.getCurrentFileTree.sql")) {
        Map<String, FileVersion> fileTree = new TreeMap<>();
        List<FileVersion> fileList = getFileTree(preparedStatement);
        for (FileVersion fileVersion : fileList) {
            fileTree.put(fileVersion.getPath(), fileVersion);
        }
        return fileTree;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) FileVersion(org.syncany.database.FileVersion) PreparedStatement(java.sql.PreparedStatement) TreeMap(java.util.TreeMap)

Example 33 with FileVersion

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

the class FileVersionSqlDao method writeFileVersions.

/**
 * Writes a list of {@link FileVersion} to the database table <i>fileversion</i> using <tt>INSERT</tt>s
 * and the given connection.
 *
 * <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 fileHistoryId References the {@link PartialFileHistory} to which the list of file versions belongs
 * @param databaseVersionId References the {@link PartialFileHistory} to which the list of file versions belongs
 * @param fileVersions List of {@link FileVersion}s to be written to the database
 * @throws SQLException If the SQL statement fails
 */
public void writeFileVersions(Connection connection, FileHistoryId fileHistoryId, long databaseVersionId, Collection<FileVersion> fileVersions) throws SQLException {
    PreparedStatement preparedStatement = getStatement(connection, "fileversion.insert.writeFileVersions.sql");
    for (FileVersion fileVersion : fileVersions) {
        String fileContentChecksumStr = (fileVersion.getChecksum() != null) ? fileVersion.getChecksum().toString() : null;
        preparedStatement.setString(1, fileHistoryId.toString());
        preparedStatement.setInt(2, Integer.parseInt("" + fileVersion.getVersion()));
        preparedStatement.setLong(3, databaseVersionId);
        preparedStatement.setString(4, fileVersion.getPath());
        preparedStatement.setString(5, fileVersion.getType().toString());
        preparedStatement.setString(6, fileVersion.getStatus().toString());
        preparedStatement.setLong(7, fileVersion.getSize());
        preparedStatement.setTimestamp(8, new Timestamp(fileVersion.getLastModified().getTime()));
        preparedStatement.setString(9, fileVersion.getLinkTarget());
        preparedStatement.setString(10, fileContentChecksumStr);
        preparedStatement.setTimestamp(11, new Timestamp(fileVersion.getUpdated().getTime()));
        preparedStatement.setString(12, fileVersion.getPosixPermissions());
        preparedStatement.setString(13, fileVersion.getDosAttributes());
        preparedStatement.addBatch();
    }
    preparedStatement.executeBatch();
    preparedStatement.close();
}
Also used : FileVersion(org.syncany.database.FileVersion) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 34 with FileVersion

use of org.syncany.database.FileVersion 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 35 with FileVersion

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

the class FileVersionSqlDao method getFileTree.

private List<FileVersion> getFileTree(PreparedStatement preparedStatement) {
    List<FileVersion> fileTree = new ArrayList<>();
    try (ResultSet resultSet = preparedStatement.executeQuery()) {
        while (resultSet.next()) {
            FileVersion fileVersion = createFileVersionFromRow(resultSet);
            fileTree.add(fileVersion);
        }
        return fileTree;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) FileVersion(org.syncany.database.FileVersion) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet)

Aggregations

FileVersion (org.syncany.database.FileVersion)52 PartialFileHistory (org.syncany.database.PartialFileHistory)25 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)23 Test (org.junit.Test)19 DatabaseVersion (org.syncany.database.DatabaseVersion)12 ArrayList (java.util.ArrayList)10 Date (java.util.Date)10 FileChecksum (org.syncany.database.FileContent.FileChecksum)8 MemoryDatabase (org.syncany.database.MemoryDatabase)8 PreparedStatement (java.sql.PreparedStatement)7 Config (org.syncany.config.Config)7 File (java.io.File)6 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)5 HashMap (java.util.HashMap)5 List (java.util.List)5 FileContent (org.syncany.database.FileContent)5 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)5 Connection (java.sql.Connection)4 Map (java.util.Map)4