Skip to content

Commit

Permalink
pass bodies_segment_region to indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr committed Mar 21, 2024
1 parent ae76c3a commit abd5ca0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 21 deletions.
9 changes: 7 additions & 2 deletions silkworm/capi/silkworm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <charconv>
#include <chrono>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
Expand Down Expand Up @@ -268,10 +269,14 @@ SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle handle, struc
});

if (bodies_file < snapshots + len) {
index = std::make_shared<snapshots::IndexBuilder>(snapshots::TransactionIndex::make(bodies_segment_path, *snapshot_path));
auto bodies_segment_region = make_region(**bodies_file);

index = std::make_shared<snapshots::IndexBuilder>(snapshots::TransactionIndex::make(
bodies_segment_path, bodies_segment_region, *snapshot_path, snapshot_region));
needed_indexes.push_back(index);

index = std::make_shared<snapshots::IndexBuilder>(snapshots::TransactionToBlockIndex::make(bodies_segment_path, *snapshot_path));
index = std::make_shared<snapshots::IndexBuilder>(snapshots::TransactionToBlockIndex::make(
bodies_segment_path, bodies_segment_region, *snapshot_path, snapshot_region));
needed_indexes.push_back(index);
}
break;
Expand Down
8 changes: 3 additions & 5 deletions silkworm/db/snapshots/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MemoryMappedRegion> segment_region)
: path_(std::move(path)), decoder_{path_.path(), segment_region} {}

MemoryMappedRegion Snapshot::memory_file_region() const {
Expand Down Expand Up @@ -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<MemoryMappedRegion> 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} {}
Expand Down
5 changes: 2 additions & 3 deletions silkworm/db/snapshots/snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MemoryMappedRegion> segment_region = std::nullopt);
virtual ~Snapshot() = default;

[[nodiscard]] SnapshotPath path() const { return path_; }
Expand Down Expand Up @@ -137,7 +136,7 @@ using StoredBlockBody = BlockBodyForStorage;

class BodySnapshot : public Snapshot {
public:
explicit BodySnapshot(SnapshotPath path);
BodySnapshot(SnapshotPath path, std::optional<MemoryMappedRegion> segment_region = std::nullopt);
BodySnapshot(SnapshotPath path, MappedBodiesSnapshot mapped);
~BodySnapshot() override;

Expand Down
6 changes: 4 additions & 2 deletions silkworm/db/snapshots/txn_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ SnapshotPath TransactionIndex::bodies_segment_path(const SnapshotPath& segment_p
SnapshotType::bodies);
}

std::pair<uint64_t, uint64_t> TransactionIndex::compute_txs_amount(const SnapshotPath& bodies_segment_path) {
BodySnapshot bodies_snapshot{bodies_segment_path};
std::pair<uint64_t, uint64_t> TransactionIndex::compute_txs_amount(
SnapshotPath bodies_segment_path,
std::optional<MemoryMappedRegion> bodies_segment_region) {
BodySnapshot bodies_snapshot{std::move(bodies_segment_path), bodies_segment_region};
bodies_snapshot.reopen_segment();
return bodies_snapshot.compute_txs_amount();
}
Expand Down
20 changes: 17 additions & 3 deletions silkworm/db/snapshots/txn_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,29 @@ struct TransactionKeyFactory : IndexKeyFactory {

class TransactionIndex {
public:
static IndexBuilder make(const SnapshotPath& bodies_segment_path, SnapshotPath segment_path, std::optional<MemoryMappedRegion> 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<MemoryMappedRegion> bodies_segment_region,
SnapshotPath segment_path,
std::optional<MemoryMappedRegion> 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<DecompressorIndexInputDataQuery>(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<uint64_t, uint64_t> compute_txs_amount(const SnapshotPath& bodies_segment_path);
static std::pair<uint64_t, uint64_t> compute_txs_amount(
SnapshotPath bodies_segment_path,
std::optional<MemoryMappedRegion> bodies_segment_region);

private:
static IndexDescriptor make_descriptor(const SnapshotPath& segment_path, uint64_t first_tx_id) {
Expand Down
9 changes: 5 additions & 4 deletions silkworm/db/snapshots/txn_to_block_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ bool TransactionToBlockIndexInputDataQuery::equal_iterators(
}

IndexBuilder TransactionToBlockIndex::make(
const SnapshotPath& bodies_segment_path,
SnapshotPath bodies_segment_path,
std::optional<MemoryMappedRegion> bodies_segment_region,
SnapshotPath segment_path,
std::optional<MemoryMappedRegion> 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;

Expand All @@ -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,
};
Expand Down
13 changes: 11 additions & 2 deletions silkworm/db/snapshots/txn_to_block_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MemoryMappedRegion> bodies_segment_region,
SnapshotPath segment_path,
std::optional<MemoryMappedRegion> segment_region = std::nullopt);
std::optional<MemoryMappedRegion> segment_region);

private:
static IndexDescriptor make_descriptor(const SnapshotPath& segment_path, uint64_t first_tx_id) {
Expand Down

0 comments on commit abd5ca0

Please sign in to comment.