From abd5ca02e9078d30097f8fcfa6663617674388c7 Mon Sep 17 00:00:00 2001 From: battlmonstr Date: Thu, 21 Mar 2024 17:56:39 +0100 Subject: [PATCH] pass bodies_segment_region to indexes --- silkworm/capi/silkworm.cpp | 9 +++++++-- silkworm/db/snapshots/snapshot.cpp | 8 +++----- silkworm/db/snapshots/snapshot.hpp | 5 ++--- silkworm/db/snapshots/txn_index.cpp | 6 ++++-- silkworm/db/snapshots/txn_index.hpp | 20 +++++++++++++++++--- silkworm/db/snapshots/txn_to_block_index.cpp | 9 +++++---- silkworm/db/snapshots/txn_to_block_index.hpp | 13 +++++++++++-- 7 files changed, 49 insertions(+), 21 deletions(-) diff --git a/silkworm/capi/silkworm.cpp b/silkworm/capi/silkworm.cpp index 3d0329219b..e3cc1141ba 100644 --- a/silkworm/capi/silkworm.cpp +++ b/silkworm/capi/silkworm.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -268,10 +269,14 @@ SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle handle, struc }); if (bodies_file < snapshots + len) { - index = std::make_shared(snapshots::TransactionIndex::make(bodies_segment_path, *snapshot_path)); + auto bodies_segment_region = make_region(**bodies_file); + + index = std::make_shared(snapshots::TransactionIndex::make( + bodies_segment_path, bodies_segment_region, *snapshot_path, snapshot_region)); needed_indexes.push_back(index); - index = std::make_shared(snapshots::TransactionToBlockIndex::make(bodies_segment_path, *snapshot_path)); + index = std::make_shared(snapshots::TransactionToBlockIndex::make( + bodies_segment_path, bodies_segment_region, *snapshot_path, snapshot_region)); needed_indexes.push_back(index); } break; diff --git a/silkworm/db/snapshots/snapshot.cpp b/silkworm/db/snapshots/snapshot.cpp index 3ff116d928..47bc9dbb44 100644 --- a/silkworm/db/snapshots/snapshot.cpp +++ b/silkworm/db/snapshots/snapshot.cpp @@ -37,10 +37,7 @@ inline std::string to_string(DecodingResult result) { return s; } -Snapshot::Snapshot(SnapshotPath path) - : path_(std::move(path)), decoder_{path_.path()} {} - -Snapshot::Snapshot(SnapshotPath path, MemoryMappedRegion segment_region) +Snapshot::Snapshot(SnapshotPath path, std::optional segment_region) : path_(std::move(path)), decoder_{path_.path(), segment_region} {} MemoryMappedRegion Snapshot::memory_file_region() const { @@ -215,7 +212,8 @@ void HeaderSnapshot::close_index() { idx_header_hash_.reset(); } -BodySnapshot::BodySnapshot(SnapshotPath path) : Snapshot(std::move(path)) {} +BodySnapshot::BodySnapshot(SnapshotPath path, std::optional segment_region) + : Snapshot(std::move(path), segment_region) {} BodySnapshot::BodySnapshot(SnapshotPath path, MappedBodiesSnapshot mapped) : Snapshot(std::move(path), mapped.segment), idx_body_number_region_{mapped.block_num_index} {} diff --git a/silkworm/db/snapshots/snapshot.hpp b/silkworm/db/snapshots/snapshot.hpp index 22ba0cb16e..e691e046d7 100644 --- a/silkworm/db/snapshots/snapshot.hpp +++ b/silkworm/db/snapshots/snapshot.hpp @@ -60,8 +60,7 @@ class Snapshot { public: static inline const auto kPageSize{os::page_size()}; - explicit Snapshot(SnapshotPath path); - Snapshot(SnapshotPath path, MemoryMappedRegion segment_region); + explicit Snapshot(SnapshotPath path, std::optional segment_region = std::nullopt); virtual ~Snapshot() = default; [[nodiscard]] SnapshotPath path() const { return path_; } @@ -137,7 +136,7 @@ using StoredBlockBody = BlockBodyForStorage; class BodySnapshot : public Snapshot { public: - explicit BodySnapshot(SnapshotPath path); + BodySnapshot(SnapshotPath path, std::optional segment_region = std::nullopt); BodySnapshot(SnapshotPath path, MappedBodiesSnapshot mapped); ~BodySnapshot() override; diff --git a/silkworm/db/snapshots/txn_index.cpp b/silkworm/db/snapshots/txn_index.cpp index 6ed47f18a3..187af04dd7 100644 --- a/silkworm/db/snapshots/txn_index.cpp +++ b/silkworm/db/snapshots/txn_index.cpp @@ -34,8 +34,10 @@ SnapshotPath TransactionIndex::bodies_segment_path(const SnapshotPath& segment_p SnapshotType::bodies); } -std::pair TransactionIndex::compute_txs_amount(const SnapshotPath& bodies_segment_path) { - BodySnapshot bodies_snapshot{bodies_segment_path}; +std::pair TransactionIndex::compute_txs_amount( + SnapshotPath bodies_segment_path, + std::optional bodies_segment_region) { + BodySnapshot bodies_snapshot{std::move(bodies_segment_path), bodies_segment_region}; bodies_snapshot.reopen_segment(); return bodies_snapshot.compute_txs_amount(); } diff --git a/silkworm/db/snapshots/txn_index.hpp b/silkworm/db/snapshots/txn_index.hpp index bcc87e53ae..2a35c9b7ed 100644 --- a/silkworm/db/snapshots/txn_index.hpp +++ b/silkworm/db/snapshots/txn_index.hpp @@ -41,15 +41,29 @@ struct TransactionKeyFactory : IndexKeyFactory { class TransactionIndex { public: - static IndexBuilder make(const SnapshotPath& bodies_segment_path, SnapshotPath segment_path, std::optional segment_region = std::nullopt) { - auto txs_amount = compute_txs_amount(bodies_segment_path); + static IndexBuilder make( + SnapshotPath bodies_segment_path, + SnapshotPath segment_path) { + return make( + std::move(bodies_segment_path), std::nullopt, + std::move(segment_path), std::nullopt); + } + + static IndexBuilder make( + SnapshotPath bodies_segment_path, + std::optional bodies_segment_region, + SnapshotPath segment_path, + std::optional segment_region) { + auto txs_amount = compute_txs_amount(std::move(bodies_segment_path), bodies_segment_region); auto descriptor = make_descriptor(segment_path, txs_amount.first); auto query = std::make_unique(std::move(segment_path), segment_region); return IndexBuilder{std::move(descriptor), std::move(query)}; } static SnapshotPath bodies_segment_path(const SnapshotPath& segment_path); - static std::pair compute_txs_amount(const SnapshotPath& bodies_segment_path); + static std::pair compute_txs_amount( + SnapshotPath bodies_segment_path, + std::optional bodies_segment_region); private: static IndexDescriptor make_descriptor(const SnapshotPath& segment_path, uint64_t first_tx_id) { diff --git a/silkworm/db/snapshots/txn_to_block_index.cpp b/silkworm/db/snapshots/txn_to_block_index.cpp index f48e8f5589..0ef1cff32e 100644 --- a/silkworm/db/snapshots/txn_to_block_index.cpp +++ b/silkworm/db/snapshots/txn_to_block_index.cpp @@ -57,10 +57,11 @@ bool TransactionToBlockIndexInputDataQuery::equal_iterators( } IndexBuilder TransactionToBlockIndex::make( - const SnapshotPath& bodies_segment_path, + SnapshotPath bodies_segment_path, + std::optional bodies_segment_region, SnapshotPath segment_path, std::optional segment_region) { - auto txs_amount = TransactionIndex::compute_txs_amount(bodies_segment_path); + auto txs_amount = TransactionIndex::compute_txs_amount(bodies_segment_path, bodies_segment_region); const uint64_t first_tx_id = txs_amount.first; const uint64_t expected_tx_count = txs_amount.second; @@ -69,8 +70,8 @@ IndexBuilder TransactionToBlockIndex::make( TxsAndBodiesQuery data_query{ std::move(segment_path), segment_region, - bodies_segment_path, - std::nullopt, + std::move(bodies_segment_path), + bodies_segment_region, first_tx_id, expected_tx_count, }; diff --git a/silkworm/db/snapshots/txn_to_block_index.hpp b/silkworm/db/snapshots/txn_to_block_index.hpp index 56287200be..e5fa12da5c 100644 --- a/silkworm/db/snapshots/txn_to_block_index.hpp +++ b/silkworm/db/snapshots/txn_to_block_index.hpp @@ -48,9 +48,18 @@ class TransactionToBlockIndexInputDataQuery : public IndexInputDataQuery { class TransactionToBlockIndex { public: static IndexBuilder make( - const SnapshotPath& bodies_segment_path, + SnapshotPath bodies_segment_path, + SnapshotPath segment_path) { + return make( + std::move(bodies_segment_path), std::nullopt, + std::move(segment_path), std::nullopt); + } + + static IndexBuilder make( + SnapshotPath bodies_segment_path, + std::optional bodies_segment_region, SnapshotPath segment_path, - std::optional segment_region = std::nullopt); + std::optional segment_region); private: static IndexDescriptor make_descriptor(const SnapshotPath& segment_path, uint64_t first_tx_id) {