private void handleGetFileRequest(GetFileRequest fileRequest) { try { FileHistoryId fileHistoryId = FileHistoryId.parseFileId(fileRequest.getFileHistoryId()); long version = fileRequest.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()); 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)); GetFileResponse fileResponse = new GetFileResponse(fileRequest.getId(), fileRequest.getRoot(), tempFileToken); GetFileResponseInternal fileResponseInternal = new GetFileResponseInternal(fileResponse, tempFile); eventBus.post(fileResponseInternal); } catch (Exception e) { logger.log(Level.WARNING, "Cannot reassemble file.", e); eventBus.post(new BadRequestResponse(fileRequest.getId(), "Cannot reassemble file.")); } }
/** * 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; }
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(); }
private Collection<MultiChunkEntry> determineMultiChunksToDownload( FileVersion fileVersion, Database localDatabase, Database winnersDatabase) { Set<MultiChunkEntry> multiChunksToDownload = new HashSet<MultiChunkEntry>(); FileContent winningFileContent = localDatabase.getContent(fileVersion.getChecksum()); if (winningFileContent == null) { winningFileContent = winnersDatabase.getContent(fileVersion.getChecksum()); } boolean winningFileHasContent = winningFileContent != null; if (winningFileHasContent) { // File can be empty! Collection<ChunkEntryId> 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) for (ChunkEntryId chunkChecksum : fileChunks) { MultiChunkEntry multiChunkForChunk = localDatabase.getMultiChunkForChunk(chunkChecksum); if (multiChunkForChunk == null) { multiChunkForChunk = winnersDatabase.getMultiChunkForChunk(chunkChecksum); } if (!multiChunksToDownload.contains(multiChunkForChunk)) { logger.log( Level.INFO, " + Adding multichunk " + StringUtil.toHex(multiChunkForChunk.getId()) + " to download list ..."); multiChunksToDownload.add(multiChunkForChunk); } } } return multiChunksToDownload; }