detect and reuse existing data

This commit is contained in:
Green Sky 2023-08-19 21:39:12 +02:00
parent 845586c2db
commit a0ac08df86
No known key found for this signature in database

View File

@ -519,23 +519,47 @@ bool SHA1_NGCFT1::onEvent(const Message::Events::MessageUpdated& e) {
ce.emplace<Message::Components::Transfer::FileInfoLocal>(std::vector{full_file_path}); ce.emplace<Message::Components::Transfer::FileInfoLocal>(std::vector{full_file_path});
// HACK truncate std::unique_ptr<FileRWFile> file_impl;
auto file_impl = std::make_unique<FileRWFile>(full_file_path, info.file_size, true); const bool file_exists = std::filesystem::exists(full_file_path);
{
const bool truncate = !file_exists;
file_impl = std::make_unique<FileRWFile>(full_file_path, info.file_size, truncate);
}
if (!file_impl->isGood()) { if (!file_impl->isGood()) {
std::cerr << "SHA1_NGCFT1 error: failed opening file '" << full_file_path << "'!\n"; std::cerr << "SHA1_NGCFT1 error: failed opening file '" << full_file_path << "'!\n";
//e.e.remove<Message::Components::Transfer::ActionAccept>(); // stop //e.e.remove<Message::Components::Transfer::ActionAccept>(); // stop
return false; return false;
} }
ce.emplace<Message::Components::Transfer::File>(std::move(file_impl));
// next, create chuck cache and check for existing data { // next, create chuck cache and check for existing data
// TODO: check existing and dont truncate
{
auto& cc = ce.emplace<Components::FT1ChunkSHA1Cache>(); auto& cc = ce.emplace<Components::FT1ChunkSHA1Cache>();
cc.have_all = false; cc.have_all = false;
cc.have_count = 0; cc.have_count = 0;
cc.chunk_hash_to_index.clear(); // if copy pasta cc.chunk_hash_to_index.clear(); // if copy pasta
if (file_exists) {
// iterate existing file
for (size_t i = 0; i < info.chunks.size(); i++) {
auto existing_data = file_impl->read(i*info.chunk_size, info.chunkSize(i));
// TODO: avoid copy
cc.have_chunk.push_back(
SHA1Digest{hash_sha1(existing_data.data(), existing_data.size())} == info.chunks.at(i)
);
if (cc.have_chunk.back()) {
cc.have_count += 1;
}
_chunks[info.chunks[i]] = ce;
cc.chunk_hash_to_index[info.chunks[i]].push_back(i);
}
if (cc.have_count == info.chunks.size()) {
cc.have_all = true;
}
} else {
for (size_t i = 0; i < info.chunks.size(); i++) { for (size_t i = 0; i < info.chunks.size(); i++) {
cc.have_chunk.push_back(false); cc.have_chunk.push_back(false);
_chunks[info.chunks[i]] = ce; _chunks[info.chunks[i]] = ce;
@ -543,8 +567,13 @@ bool SHA1_NGCFT1::onEvent(const Message::Events::MessageUpdated& e) {
} }
} }
if (!cc.have_all) {
// now, enque // now, enque
_queue_content_want_chunk.push_back(ce); _queue_content_want_chunk.push_back(ce);
}
}
ce.emplace<Message::Components::Transfer::File>(std::move(file_impl));
ce.remove<Message::Components::Transfer::TagPaused>(); ce.remove<Message::Components::Transfer::TagPaused>();