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();
}
}
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);
}
}
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;
}
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.");
}
}
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);
}
Aggregations