diff --git a/CMakeLists.txt b/CMakeLists.txt index 42082ab..10205af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,9 @@ add_library(solanaceae_sha1_ngcft1 ./solanaceae/ngc_ft1_sha1/ft1_sha1_info.hpp ./solanaceae/ngc_ft1_sha1/ft1_sha1_info.cpp + ./solanaceae/ngc_ft1_sha1/components.hpp + ./solanaceae/ngc_ft1_sha1/components.cpp + ./solanaceae/ngc_ft1_sha1/sha1_ngcft1.hpp ./solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp ) diff --git a/solanaceae/ngc_ft1_sha1/components.cpp b/solanaceae/ngc_ft1_sha1/components.cpp new file mode 100644 index 0000000..5fbf038 --- /dev/null +++ b/solanaceae/ngc_ft1_sha1/components.cpp @@ -0,0 +1,25 @@ +#include "./components.hpp" + +std::vector Components::FT1ChunkSHA1Cache::chunkIndices(const SHA1Digest& hash) const { + const auto it = chunk_hash_to_index.find(hash); + if (it != chunk_hash_to_index.cend()) { + return it->second; + } else { + return {}; + } +} + +bool Components::FT1ChunkSHA1Cache::haveChunk(const SHA1Digest& hash) const { + if (have_all) { // short cut + return true; + } + + if (auto i_vec = chunkIndices(hash); !i_vec.empty()) { + // TODO: should i test all? + return have_chunk[i_vec.front()]; + } + + // not part of this file + return false; +} + diff --git a/solanaceae/ngc_ft1_sha1/components.hpp b/solanaceae/ngc_ft1_sha1/components.hpp new file mode 100644 index 0000000..ea8b455 --- /dev/null +++ b/solanaceae/ngc_ft1_sha1/components.hpp @@ -0,0 +1,66 @@ +#pragma once + +#include +#include +#include + +#include + +#include "./ft1_sha1_info.hpp" +#include "./hash_utils.hpp" + + +#include + + +// TODO: rename to object components +namespace Components { + + struct Messages { + std::vector messages; + }; + + using FT1InfoSHA1 = FT1InfoSHA1; + + struct FT1InfoSHA1Data { + std::vector data; + }; + + struct FT1InfoSHA1Hash { + std::vector hash; + }; + + struct FT1ChunkSHA1Cache { + std::vector have_chunk; + bool have_all {false}; + size_t have_count {0}; + entt::dense_map> chunk_hash_to_index; + + std::vector chunkIndices(const SHA1Digest& hash) const; + bool haveChunk(const SHA1Digest& hash) const; + }; + + struct FT1ChunkSHA1Requested { + // requested chunks with a timer since last request + entt::dense_map chunks; + }; + + // TODO: once announce is shipped, remove the "Suspected" + struct SuspectedParticipants { + entt::dense_set participants; + }; + + struct ReRequestInfoTimer { + float timer {0.f}; + }; + + struct ReadHeadHint { + // points to the first byte we want + // this is just a hint, that can be set from outside + // to guide the sequential "piece picker" strategy + // the strategy *should* set this to the first byte we dont yet have + uint64_t offset_into_file {0u}; + }; + +} // Components + diff --git a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp index 83ea9af..03b7cb3 100644 --- a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp +++ b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp @@ -16,6 +16,8 @@ #include "./file_rw_mapped.hpp" +#include "./components.hpp" + #include #include #include @@ -29,80 +31,6 @@ namespace Message::Components { } // Message::Components -// TODO: rename to object components -namespace Components { - - struct Messages { - std::vector messages; - }; - - using FT1InfoSHA1 = FT1InfoSHA1; - - struct FT1InfoSHA1Data { - std::vector data; - }; - - struct FT1InfoSHA1Hash { - std::vector hash; - }; - - struct FT1ChunkSHA1Cache { - std::vector have_chunk; - bool have_all {false}; - size_t have_count {0}; - entt::dense_map> chunk_hash_to_index; - - std::vector chunkIndices(const SHA1Digest& hash) const; - bool haveChunk(const SHA1Digest& hash) const; - }; - - struct FT1ChunkSHA1Requested { - // requested chunks with a timer since last request - entt::dense_map chunks; - }; - - // TODO: once announce is shipped, remove the "Suspected" - struct SuspectedParticipants { - entt::dense_set participants; - }; - - struct ReRequestInfoTimer { - float timer {0.f}; - }; - - struct ReadHeadHint { - // points to the first byte we want - // this is just a hint, that can be set from outside - // to guide the sequential "piece picker" strategy - // the strategy *should* set this to the first byte we dont yet have - uint64_t offset_into_file {0u}; - }; - -} // Components - -std::vector Components::FT1ChunkSHA1Cache::chunkIndices(const SHA1Digest& hash) const { - const auto it = chunk_hash_to_index.find(hash); - if (it != chunk_hash_to_index.cend()) { - return it->second; - } else { - return {}; - } -} - -bool Components::FT1ChunkSHA1Cache::haveChunk(const SHA1Digest& hash) const { - if (have_all) { // short cut - return true; - } - - if (auto i_vec = chunkIndices(hash); !i_vec.empty()) { - // TODO: should i test all? - return have_chunk[i_vec.front()]; - } - - // not part of this file - return false; -} - static size_t chunkSize(const FT1InfoSHA1& sha1_info, size_t chunk_index) { if (chunk_index+1 == sha1_info.chunks.size()) { // last chunk