detect and reuse existing data
This commit is contained in:
parent
845586c2db
commit
a0ac08df86
@ -519,32 +519,61 @@ 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
|
||||||
for (size_t i = 0; i < info.chunks.size(); i++) {
|
|
||||||
cc.have_chunk.push_back(false);
|
if (file_exists) {
|
||||||
_chunks[info.chunks[i]] = ce;
|
// iterate existing file
|
||||||
cc.chunk_hash_to_index[info.chunks[i]].push_back(i);
|
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++) {
|
||||||
|
cc.have_chunk.push_back(false);
|
||||||
|
_chunks[info.chunks[i]] = ce;
|
||||||
|
cc.chunk_hash_to_index[info.chunks[i]].push_back(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cc.have_all) {
|
||||||
|
// now, enque
|
||||||
|
_queue_content_want_chunk.push_back(ce);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// now, enque
|
ce.emplace<Message::Components::Transfer::File>(std::move(file_impl));
|
||||||
_queue_content_want_chunk.push_back(ce);
|
|
||||||
|
|
||||||
ce.remove<Message::Components::Transfer::TagPaused>();
|
ce.remove<Message::Components::Transfer::TagPaused>();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user