Search in sources :

Example 16 with ChunkChecksum

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

the class DatabaseXmlWriter method writeMultiChunks.

private void writeMultiChunks(IndentXmlStreamWriter xmlOut, Collection<MultiChunkEntry> multiChunks) throws XMLStreamException {
    if (multiChunks.size() > 0) {
        xmlOut.writeStartElement("multiChunks");
        for (MultiChunkEntry multiChunk : multiChunks) {
            xmlOut.writeStartElement("multiChunk");
            xmlOut.writeAttribute("id", multiChunk.getId().toString());
            xmlOut.writeAttribute("size", multiChunk.getSize());
            xmlOut.writeStartElement("chunkRefs");
            Collection<ChunkChecksum> multiChunkChunks = multiChunk.getChunks();
            for (ChunkChecksum chunkChecksum : multiChunkChunks) {
                xmlOut.writeEmptyElement("chunkRef");
                xmlOut.writeAttribute("ref", chunkChecksum.toString());
            }
            // </chunkRefs>
            xmlOut.writeEndElement();
            // </multiChunk>			
            xmlOut.writeEndElement();
        }
        // </multiChunks>
        xmlOut.writeEndElement();
    }
}
Also used : ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) MultiChunkEntry(org.syncany.database.MultiChunkEntry)

Example 17 with ChunkChecksum

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

the class FileContentSqlDao method getFileContentWithChunkChecksums.

private FileContent getFileContentWithChunkChecksums(FileChecksum fileChecksum) {
    try (PreparedStatement preparedStatement = getStatement("filecontent.select.all.getFileContentByChecksumWithChunkChecksums.sql")) {
        preparedStatement.setString(1, fileChecksum.toString());
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            FileContent fileContent = null;
            while (resultSet.next()) {
                if (fileContent == null) {
                    fileContent = new FileContent();
                    fileContent.setChecksum(FileChecksum.parseFileChecksum(resultSet.getString("checksum")));
                    fileContent.setSize(resultSet.getLong("size"));
                }
                // Add chunk references
                ChunkChecksum chunkChecksum = ChunkChecksum.parseChunkChecksum(resultSet.getString("chunk_checksum"));
                fileContent.addChunk(chunkChecksum);
            }
            return fileContent;
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileContent(org.syncany.database.FileContent) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum)

Example 18 with ChunkChecksum

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

the class ApplyChangesOperation method determineMultiChunksToDownload.

/**
	 * Finds the multichunks that need to be downloaded for the given file version -- using the local 
	 * database and given winners database. Returns a set of multichunk identifiers.
	 */
private Collection<MultiChunkId> determineMultiChunksToDownload(FileVersion fileVersion, MemoryDatabase winnersDatabase) {
    Set<MultiChunkId> multiChunksToDownload = new HashSet<MultiChunkId>();
    // First: Check if we know this file locally!
    List<MultiChunkId> multiChunkIds = localDatabase.getMultiChunkIds(fileVersion.getChecksum());
    if (multiChunkIds.size() > 0) {
        multiChunksToDownload.addAll(multiChunkIds);
    } else {
        // Second: We don't know it locally; must be from the winners database
        FileContent winningFileContent = winnersDatabase.getContent(fileVersion.getChecksum());
        boolean winningFileHasContent = winningFileContent != null;
        if (winningFileHasContent) {
            // File can be empty!
            List<ChunkChecksum> fileChunks = winningFileContent.getChunks();
            // TODO [medium] Instead of just looking for multichunks to download here, we should look for chunks in local files as well
            // and return the chunk positions in the local files ChunkPosition (chunk123 at file12, offset 200, size 250)
            Map<ChunkChecksum, MultiChunkId> checksumsWithMultiChunkIds = localDatabase.getMultiChunkIdsByChecksums(fileChunks);
            for (ChunkChecksum chunkChecksum : fileChunks) {
                MultiChunkId multiChunkIdForChunk = checksumsWithMultiChunkIds.get(chunkChecksum);
                if (multiChunkIdForChunk == null) {
                    multiChunkIdForChunk = winnersDatabase.getMultiChunkIdForChunk(chunkChecksum);
                    if (multiChunkIdForChunk == null) {
                        throw new RuntimeException("Cannot find multichunk for chunk " + chunkChecksum);
                    }
                }
                if (!multiChunksToDownload.contains(multiChunkIdForChunk)) {
                    logger.log(Level.INFO, "  + Adding multichunk " + multiChunkIdForChunk + " to download list ...");
                    multiChunksToDownload.add(multiChunkIdForChunk);
                }
            }
        }
    }
    return multiChunksToDownload;
}
Also used : FileContent(org.syncany.database.FileContent) MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) HashSet(java.util.HashSet)

Example 19 with ChunkChecksum

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

the class GetFileFolderRequestHandler method handleRequest.

@Override
public Response handleRequest(FolderRequest request) {
    GetFileFolderRequest concreteRequest = (GetFileFolderRequest) request;
    try {
        FileHistoryId fileHistoryId = FileHistoryId.parseFileId(concreteRequest.getFileHistoryId());
        long version = concreteRequest.getVersion();
        FileVersion fileVersion = localDatabase.getFileVersion(fileHistoryId, version);
        FileContent fileContent = localDatabase.getFileContent(fileVersion.getChecksum(), true);
        Map<ChunkChecksum, MultiChunkId> multiChunks = localDatabase.getMultiChunkIdsByChecksums(fileContent.getChunks());
        TransferManager transferManager = config.getTransferPlugin().createTransferManager(config.getConnection(), config);
        Downloader downloader = new Downloader(config, transferManager);
        Assembler assembler = new Assembler(config, localDatabase);
        downloader.downloadAndDecryptMultiChunks(new HashSet<MultiChunkId>(multiChunks.values()));
        File tempFile = assembler.assembleToCache(fileVersion);
        String tempFileToken = StringUtil.toHex(ObjectId.secureRandomBytes(40));
        GetFileFolderResponse fileResponse = new GetFileFolderResponse(concreteRequest.getId(), concreteRequest.getRoot(), tempFileToken);
        GetFileFolderResponseInternal fileResponseInternal = new GetFileFolderResponseInternal(fileResponse, tempFile);
        eventBus.post(fileResponseInternal);
        return null;
    } catch (Exception e) {
        logger.log(Level.WARNING, "Cannot reassemble file.", e);
        return new BadRequestResponse(concreteRequest.getId(), "Cannot reassemble file.");
    }
}
Also used : TransferManager(org.syncany.plugins.transfer.TransferManager) FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) Downloader(org.syncany.operations.Downloader) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) FileContent(org.syncany.database.FileContent) FileVersion(org.syncany.database.FileVersion) Assembler(org.syncany.operations.Assembler) File(java.io.File)

Example 20 with ChunkChecksum

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

the class MultiChunkDaoTest method testGetMultiChunkIds.

@Test
public void testGetMultiChunkIds() throws Exception {
    // Setup
    Config testConfig = TestConfigUtil.createTestLocalConfig();
    Connection databaseConnection = testConfig.createDatabaseConnection();
    // Run
    TestSqlUtil.runSqlFromResource(databaseConnection, "test.insert.set3.sql");
    MultiChunkSqlDao multiChunkDao = new MultiChunkSqlDao(databaseConnection);
    List<ChunkChecksum> checksums = new ArrayList<ChunkChecksum>();
    checksums.add(ChunkChecksum.parseChunkChecksum("eba69a8e359ce3258520138a50ed9860127ab6e0"));
    checksums.add(ChunkChecksum.parseChunkChecksum("0fecbac8ac8a5f8b7aa12b2741a4ef5db88c5dea"));
    checksums.add(ChunkChecksum.parseChunkChecksum("38a18897e94a901b833e750e8604d9616a02ca84"));
    checksums.add(ChunkChecksum.parseChunkChecksum("beefbeefbeefbeefbeefbeefbeefbeefbeefbeef"));
    //Ensure that uniqueness is not required
    checksums.add(ChunkChecksum.parseChunkChecksum("38a18897e94a901b833e750e8604d9616a02ca84"));
    Map<ChunkChecksum, MultiChunkId> multiChunkIds = multiChunkDao.getMultiChunkIdsByChecksums(checksums);
    // Test
    assertNotNull(multiChunkIds.get(checksums.get(0)));
    assertEquals("0d79eed3fd8ac866b5872ea3f3f079c46dd15ac9", multiChunkIds.get(checksums.get(0)).toString());
    assertNotNull(multiChunkIds.get(checksums.get(1)));
    assertEquals("51aaca5c1280b1cf95cff8a3266a6bb44b482ad4", multiChunkIds.get(checksums.get(1)).toString());
    assertNotNull(multiChunkIds.get(checksums.get(2)));
    assertEquals("51aaca5c1280b1cf95cff8a3266a6bb44b482ad4", multiChunkIds.get(checksums.get(2)).toString());
    assertEquals(multiChunkIds.get(checksums.get(1)), multiChunkIds.get(checksums.get(2)));
    assertNull(multiChunkIds.get(checksums.get(3)));
    Map<ChunkChecksum, MultiChunkId> multiChunkIdsEmpty = multiChunkDao.getMultiChunkIdsByChecksums(new ArrayList<ChunkChecksum>());
    assertTrue(multiChunkIdsEmpty.isEmpty());
    // Tear down
    databaseConnection.close();
    TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
Also used : MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) Config(org.syncany.config.Config) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) MultiChunkSqlDao(org.syncany.database.dao.MultiChunkSqlDao) Test(org.junit.Test)

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