Search in sources :

Example 1 with ChunkChecksum

use of org.syncany.database.ChunkEntry.ChunkChecksum in project syncany by syncany.

the class MultiChunkSqlDao method getMultiChunkIdsByChecksums.

/**
	 * Note: This method selects also {@link DatabaseVersionStatus#DIRTY DIRTY}.
	 */
public Map<ChunkChecksum, MultiChunkId> getMultiChunkIdsByChecksums(List<ChunkChecksum> chunkChecksums) {
    // Gather a unique array of checksum strings (required for query!)
    Set<ChunkChecksum> chunkChecksumSet = new HashSet<ChunkChecksum>(chunkChecksums);
    String[] checksums = new String[chunkChecksumSet.size()];
    int i = 0;
    for (ChunkChecksum checksum : chunkChecksumSet) {
        checksums[i] = checksum.toString();
        i++;
    }
    // Execute query
    Map<ChunkChecksum, MultiChunkId> result = new HashMap<ChunkChecksum, MultiChunkId>();
    try (PreparedStatement preparedStatement = getStatement("multichunk.select.all.getMultiChunkIdForChunks.sql")) {
        preparedStatement.setArray(1, connection.createArrayOf("varchar", checksums));
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                result.put(ChunkChecksum.parseChunkChecksum(resultSet.getString("chunk_checksum")), MultiChunkId.parseMultiChunkId(resultSet.getString("multichunk_id")));
            }
        }
        return result;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet)

Example 2 with ChunkChecksum

use of org.syncany.database.ChunkEntry.ChunkChecksum in project syncany by syncany.

the class Assembler method assembleToCache.

/**
	 * Assembles the given file version to the local cache and returns a reference
	 * to the cached file after successfully assembling the file. 
	 */
public File assembleToCache(FileVersion fileVersion) throws Exception {
    File reconstructedFileInCache = config.getCache().createTempFile("reconstructedFileVersion");
    logger.log(Level.INFO, "     - Creating file " + fileVersion.getPath() + " to " + reconstructedFileInCache + " ...");
    FileContent fileContent = localDatabase.getFileContent(fileVersion.getChecksum(), true);
    if (fileContent == null && memoryDatabase != null) {
        fileContent = memoryDatabase.getContent(fileVersion.getChecksum());
    }
    // Check consistency!
    if (fileContent == null && fileVersion.getChecksum() != null) {
        throw new Exception("Cannot determine file content for checksum " + fileVersion.getChecksum());
    }
    // Create empty file
    if (fileContent == null) {
        FileUtils.touch(reconstructedFileInCache);
        return reconstructedFileInCache;
    }
    // Create non-empty file
    Chunker chunker = config.getChunker();
    MultiChunker multiChunker = config.getMultiChunker();
    FileOutputStream reconstructedFileOutputStream = new FileOutputStream(reconstructedFileInCache);
    MessageDigest reconstructedFileChecksum = MessageDigest.getInstance(chunker.getChecksumAlgorithm());
    if (fileContent != null) {
        // File can be empty!
        Collection<ChunkChecksum> fileChunks = fileContent.getChunks();
        for (ChunkChecksum chunkChecksum : fileChunks) {
            MultiChunkId multiChunkIdForChunk = localDatabase.getMultiChunkId(chunkChecksum);
            if (multiChunkIdForChunk == null && memoryDatabase != null) {
                multiChunkIdForChunk = memoryDatabase.getMultiChunkIdForChunk(chunkChecksum);
            }
            File decryptedMultiChunkFile = config.getCache().getDecryptedMultiChunkFile(multiChunkIdForChunk);
            MultiChunk multiChunk = multiChunker.createMultiChunk(decryptedMultiChunkFile);
            InputStream chunkInputStream = multiChunk.getChunkInputStream(chunkChecksum.getBytes());
            byte[] buffer = new byte[4096];
            int read = 0;
            while (-1 != (read = chunkInputStream.read(buffer))) {
                reconstructedFileChecksum.update(buffer, 0, read);
                reconstructedFileOutputStream.write(buffer, 0, read);
            }
            chunkInputStream.close();
            multiChunk.close();
        }
    }
    reconstructedFileOutputStream.close();
    // Validate checksum
    byte[] reconstructedFileExpectedChecksum = fileContent.getChecksum().getBytes();
    byte[] reconstructedFileActualChecksum = reconstructedFileChecksum.digest();
    if (!Arrays.equals(reconstructedFileActualChecksum, reconstructedFileExpectedChecksum)) {
        throw new Exception("Checksums do not match: expected " + StringUtil.toHex(reconstructedFileExpectedChecksum) + " != actual " + StringUtil.toHex(reconstructedFileActualChecksum));
    }
    return reconstructedFileInCache;
}
Also used : MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) InputStream(java.io.InputStream) Chunker(org.syncany.chunk.Chunker) MultiChunker(org.syncany.chunk.MultiChunker) MultiChunk(org.syncany.chunk.MultiChunk) MultiChunker(org.syncany.chunk.MultiChunker) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) FileContent(org.syncany.database.FileContent) FileOutputStream(java.io.FileOutputStream) MessageDigest(java.security.MessageDigest) File(java.io.File)

Example 3 with ChunkChecksum

use of org.syncany.database.ChunkEntry.ChunkChecksum in project syncany by syncany.

the class DatabaseXmlWriter method writeFileContents.

private void writeFileContents(IndentXmlStreamWriter xmlOut, Collection<FileContent> fileContents) throws XMLStreamException {
    if (fileContents.size() > 0) {
        xmlOut.writeStartElement("fileContents");
        for (FileContent fileContent : fileContents) {
            xmlOut.writeStartElement("fileContent");
            xmlOut.writeAttribute("checksum", fileContent.getChecksum().toString());
            xmlOut.writeAttribute("size", fileContent.getSize());
            xmlOut.writeStartElement("chunkRefs");
            Collection<ChunkChecksum> fileContentChunkChunks = fileContent.getChunks();
            for (ChunkChecksum chunkChecksum : fileContentChunkChunks) {
                xmlOut.writeEmptyElement("chunkRef");
                xmlOut.writeAttribute("ref", chunkChecksum.toString());
            }
            // </chunkRefs>
            xmlOut.writeEndElement();
            // </fileContent>			
            xmlOut.writeEndElement();
        }
        // </fileContents>					
        xmlOut.writeEndElement();
    }
}
Also used : FileContent(org.syncany.database.FileContent) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum)

Example 4 with ChunkChecksum

use of org.syncany.database.ChunkEntry.ChunkChecksum in project syncany by syncany.

the class FileContentSqlDao method writeFileContentChunkRefs.

private void writeFileContentChunkRefs(Connection connection, FileContent fileContent) throws SQLException {
    PreparedStatement preparedStatement = getStatement(connection, "filecontent.insert.all.writeFileContentChunkRefs.sql");
    int order = 0;
    for (ChunkChecksum chunkChecksum : fileContent.getChunks()) {
        preparedStatement.setString(1, fileContent.getChecksum().toString());
        preparedStatement.setString(2, chunkChecksum.toString());
        preparedStatement.setInt(3, order);
        preparedStatement.addBatch();
        order++;
    }
    preparedStatement.executeBatch();
    preparedStatement.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum)

Example 5 with ChunkChecksum

use of org.syncany.database.ChunkEntry.ChunkChecksum in project syncany by syncany.

the class FileContentSqlDao method createFileContents.

private Map<FileChecksum, FileContent> createFileContents(ResultSet resultSet) throws SQLException {
    Map<FileChecksum, FileContent> fileContents = new HashMap<FileChecksum, FileContent>();
    FileChecksum currentFileChecksum = null;
    while (resultSet.next()) {
        FileChecksum fileChecksum = FileChecksum.parseFileChecksum(resultSet.getString("checksum"));
        FileContent fileContent = null;
        if (currentFileChecksum != null && currentFileChecksum.equals(fileChecksum)) {
            fileContent = fileContents.get(fileChecksum);
        } else {
            fileContent = new FileContent();
            fileContent.setChecksum(fileChecksum);
            fileContent.setSize(resultSet.getLong("size"));
        }
        ChunkChecksum chunkChecksum = ChunkChecksum.parseChunkChecksum(resultSet.getString("chunk_checksum"));
        fileContent.addChunk(chunkChecksum);
        fileContents.put(fileChecksum, fileContent);
        currentFileChecksum = fileChecksum;
    }
    return fileContents;
}
Also used : FileContent(org.syncany.database.FileContent) HashMap(java.util.HashMap) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) FileChecksum(org.syncany.database.FileContent.FileChecksum)

Aggregations

ChunkChecksum (org.syncany.database.ChunkEntry.ChunkChecksum)26 ChunkEntry (org.syncany.database.ChunkEntry)11 MultiChunkEntry (org.syncany.database.MultiChunkEntry)10 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)10 Test (org.junit.Test)9 DatabaseVersion (org.syncany.database.DatabaseVersion)9 FileContent (org.syncany.database.FileContent)9 File (java.io.File)7 MemoryDatabase (org.syncany.database.MemoryDatabase)7 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 PreparedStatement (java.sql.PreparedStatement)4 MultiChunk (org.syncany.chunk.MultiChunk)3 FileChecksum (org.syncany.database.FileContent.FileChecksum)3 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2