Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions silkworm/capi/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ SILKWORM_EXPORT int silkworm_init(SilkwormHandle* handle, const struct SilkwormS
.state_repository_latest = std::move(state_repository_latest),
.state_repository_historical = std::move(state_repository_historical),
.chaindata = {},
.query_caches = snapshots::QueryCaches{db::state::make_query_caches_schema(), snapshots_dir_path, settings->state_repo_index_salt},
};

// NOLINTNEXTLINE(bugprone-unhandled-exception-at-new)
Expand Down
2 changes: 2 additions & 0 deletions silkworm/capi/silkworm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ SILKWORM_EXPORT int silkworm_execute_txn(SilkwormHandle handle, MDBX_txn* mdbx_t
db_ref,
handle->db->blocks_repository,
handle->db->state_repository_latest,
handle->db->query_caches,
};
if (!handle->chain_config) {
handle->chain_config = db::read_chain_config(unmanaged_tx);
Expand Down Expand Up @@ -646,6 +647,7 @@ SILKWORM_EXPORT int silkworm_block_exec_end(SilkwormHandle handle, MDBX_txn* mdb
db_ref,
handle->db->blocks_repository,
handle->db->state_repository_latest,
handle->db->query_caches,
};

const auto log_index = handle->executions_in_block[index].log_index;
Expand Down
2 changes: 0 additions & 2 deletions silkworm/db/blocks/schema_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ snapshots::SnapshotRepository make_blocks_repository(
schema,
index_salt,
make_blocks_index_builders_factory(),
std::nullopt, // no domain caches
std::nullopt, // no inverted index caches
};
}

Expand Down
3 changes: 3 additions & 0 deletions silkworm/db/capi/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <silkworm/core/common/assert.hpp>
#include <silkworm/db/access_layer.hpp>
#include <silkworm/db/datastore/kvdb/database.hpp>
#include <silkworm/db/datastore/snapshots/query_caches.hpp>
#include <silkworm/db/datastore/snapshots/snapshot_repository.hpp>

namespace silkworm::db::capi {
Expand All @@ -17,6 +18,7 @@ struct Component {
silkworm::snapshots::SnapshotRepository state_repository_latest;
silkworm::snapshots::SnapshotRepository state_repository_historical;
std::unique_ptr<silkworm::datastore::kvdb::DatabaseUnmanaged> chaindata;
silkworm::snapshots::QueryCaches query_caches;

DataStoreRef data_store() {
SILKWORM_ASSERT(chaindata);
Expand All @@ -25,6 +27,7 @@ struct Component {
blocks_repository,
state_repository_latest,
state_repository_historical,
query_caches,
};
}

Expand Down
1 change: 1 addition & 0 deletions silkworm/db/data_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ datastore::Schema DataStore::make_schema() {
snapshots.repository(blocks::kBlocksRepositoryName) = blocks::make_blocks_repository_schema();
snapshots.repository(state::kStateRepositoryNameLatest) = state::make_state_repository_schema_latest();
snapshots.repository(state::kStateRepositoryNameHistorical) = state::make_state_repository_schema_historical();
snapshots.query_caches_schema() = state::make_query_caches_schema();

return {
std::move(kvdb),
Expand Down
4 changes: 4 additions & 0 deletions silkworm/db/data_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct DataStoreRef {
snapshots::SnapshotRepository& blocks_repository;
snapshots::SnapshotRepository& state_repository_latest;
snapshots::SnapshotRepository& state_repository_historical;
const snapshots::QueryCaches& query_caches;
};

class DataStore {
Expand All @@ -32,6 +33,7 @@ class DataStore {
std::move(blocks_repository),
std::move(state_repository_latest),
std::move(state_repository_historical)),
blocks_repository.path(),
} {}

public:
Expand Down Expand Up @@ -61,6 +63,7 @@ class DataStore {
blocks_repository(),
state_repository_latest(),
state_repository_historical(),
store_.query_caches(),
};
}

Expand All @@ -75,6 +78,7 @@ class DataStore {
snapshots::SnapshotRepository& state_repository_historical() const {
return store_.repository(state::kStateRepositoryNameHistorical);
}
const snapshots::QueryCaches& query_caches() const { return store_.query_caches(); }

static datastore::kvdb::Schema::DatabaseDef make_chaindata_database_schema();
static datastore::kvdb::Database make_chaindata_database(mdbx::env_managed chaindata_env);
Expand Down
9 changes: 7 additions & 2 deletions silkworm/db/datastore/data_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "common/entity_name.hpp"
#include "kvdb/database.hpp"
#include "schema.hpp"
#include "snapshots/query_caches.hpp"
#include "snapshots/snapshot_repository.hpp"

namespace silkworm::datastore {
Expand All @@ -17,21 +18,25 @@ class DataStore {
DataStore(
Schema schema,
EntityMap<std::unique_ptr<kvdb::Database>> databases,
EntityMap<std::unique_ptr<snapshots::SnapshotRepository>> repositories)
EntityMap<std::unique_ptr<snapshots::SnapshotRepository>> repositories,
const std::filesystem::path& snapshots_path)
: schema_{std::move(schema)},
databases_{std::move(databases)},
repositories_{std::move(repositories)} {}
repositories_{std::move(repositories)},
query_caches_{schema_.snapshots.query_caches_schema(), snapshots_path} {}

const Schema& schema() const { return schema_; }

kvdb::Database& default_database() const { return database(kvdb::Schema::kDefaultEntityName); }
kvdb::Database& database(const EntityName& name) const { return *databases_.at(name); }
snapshots::SnapshotRepository& repository(const EntityName& name) const { return *repositories_.at(name); }
const snapshots::QueryCaches& query_caches() const { return query_caches_; }

private:
Schema schema_;
EntityMap<std::unique_ptr<kvdb::Database>> databases_;
EntityMap<std::unique_ptr<snapshots::SnapshotRepository>> repositories_;
snapshots::QueryCaches query_caches_;
};

} // namespace silkworm::datastore
14 changes: 8 additions & 6 deletions silkworm/db/datastore/domain_get_as_of_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ struct DomainGetAsOfQuery {
kvdb::Domain kvdb_entity,
kvdb::ROTxn& tx,
const snapshots::SnapshotRepositoryROAccess& repository_latest,
const snapshots::SnapshotRepositoryROAccess& repository_historical)
: query1_{*kvdb_entity.history, tx, repository_historical},
query2_{history_segment_names.front(), kvdb_entity, tx, repository_latest} {}
const snapshots::SnapshotRepositoryROAccess& repository_historical,
const snapshots::QueryCaches& query_caches)
: query1_{*kvdb_entity.history, tx, repository_historical, query_caches},
query2_{history_segment_names.front(), kvdb_entity, tx, repository_latest, query_caches} {}

DomainGetAsOfQuery(
const kvdb::DatabaseRef& database,
kvdb::ROTxn& tx,
const snapshots::SnapshotRepositoryROAccess& repository_latest,
const snapshots::SnapshotRepositoryROAccess& repository_historical)
: query1_{database, tx, repository_historical},
query2_{history_segment_names.front(), database, tx, repository_latest} {}
const snapshots::SnapshotRepositoryROAccess& repository_historical,
const snapshots::QueryCaches& query_caches)
: query1_{database, tx, repository_historical, query_caches},
query2_{history_segment_names.front(), database, tx, repository_latest, query_caches} {}

using Key = decltype(TKeyEncoder1::value);
using Value = decltype(TValueDecoder1::value);
Expand Down
9 changes: 6 additions & 3 deletions silkworm/db/datastore/domain_get_latest_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@ struct DomainGetLatestQuery {
datastore::EntityName entity_name,
kvdb::Domain kvdb_entity,
kvdb::ROTxn& tx,
const snapshots::SnapshotRepositoryROAccess& repository)
const snapshots::SnapshotRepositoryROAccess& repository,
const snapshots::QueryCaches& query_caches)
: query1_{tx, kvdb_entity},
query2_{repository, entity_name} {}
query2_{repository, query_caches, entity_name} {}

DomainGetLatestQuery(
datastore::EntityName entity_name,
const kvdb::DatabaseRef& database,
kvdb::ROTxn& tx,
const snapshots::SnapshotRepositoryROAccess& repository)
const snapshots::SnapshotRepositoryROAccess& repository,
const snapshots::QueryCaches& query_caches)
: DomainGetLatestQuery{
entity_name,
database.domain(entity_name),
tx,
repository,
query_caches,
} {}

using Key1 = decltype(TKeyEncoder1::value);
Expand Down
9 changes: 6 additions & 3 deletions silkworm/db/datastore/history_get_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ struct HistoryGetQuery {
HistoryGetQuery(
kvdb::History kvdb_entity,
kvdb::ROTxn& tx,
const snapshots::SnapshotRepositoryROAccess& repository)
const snapshots::SnapshotRepositoryROAccess& repository,
const snapshots::QueryCaches& query_caches)
: query1_{tx, kvdb_entity},
query2_{repository} {}
query2_{repository, query_caches} {}

HistoryGetQuery(
const kvdb::DatabaseRef& database,
kvdb::ROTxn& tx,
const snapshots::SnapshotRepositoryROAccess& repository)
const snapshots::SnapshotRepositoryROAccess& repository,
const snapshots::QueryCaches& query_caches)
: HistoryGetQuery{
database.domain(segment_names.front()).history.value(),
tx,
repository,
query_caches,
} {}

using Key1 = decltype(TKeyEncoder1::value);
Expand Down
25 changes: 0 additions & 25 deletions silkworm/db/datastore/snapshots/domain_cache.hpp

This file was deleted.

105 changes: 71 additions & 34 deletions silkworm/db/datastore/snapshots/domain_get_latest_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include "common/codec.hpp"
#include "common/raw_codec.hpp"
#include "domain.hpp"
#include "domain_cache.hpp"
#include "query_cache.hpp"
#include "query_caches.hpp"
#include "segment/kv_segment_reader.hpp"
#include "snapshot_bundle.hpp"
#include "snapshot_repository_ro_access.hpp"
Expand Down Expand Up @@ -54,11 +55,70 @@ struct DomainGetLatestSegmentQuery {
Domain entity_;
};

template <EncoderConcept TKeyEncoder, DecoderConcept TValueDecoder>
struct DomainGetLatestQuery {
struct DomainGetLatestQueryRawNoCache {
const SnapshotRepositoryROAccess& repository;
datastore::EntityName entity_name;

struct Result {
Decoder::Word value;
datastore::Step step{0};
};

std::optional<Result> exec(ByteView key_data) {
for (auto& bundle_ptr : repository.view_bundles_reverse()) {
const SnapshotBundle& bundle = *bundle_ptr;
DomainGetLatestSegmentQuery<RawEncoder<ByteView>, RawDecoder<Decoder::Word>> query{bundle, entity_name};
std::optional<Decoder::Word> value_data = query.exec_raw(key_data);
if (value_data) {
return Result{std::move(*value_data), bundle.step_range().end};
}
}
return std::nullopt;
}
};

struct DomainGetLatestQueryRawWithCache {
using Result = DomainGetLatestQueryRawNoCache::Result;
using CacheType = QueryCache<std::optional<Result>>;
static inline const datastore::EntityName kName{"DomainGetLatestQueryRawWithCache"};

DomainGetLatestQueryRawWithCache(
const SnapshotRepositoryROAccess& repository,
const QueryCaches& query_caches,
datastore::EntityName entity_name)
: query_{repository, entity_name},
cache_{query_caches.cache<CacheType>(kName, entity_name).get()} {}

std::optional<Result> exec(ByteView key_data) {
if (!cache_) {
return query_.exec(key_data);
}

std::optional<std::optional<Result>> cached_result;
uint64_t cache_key{0};
std::tie(cached_result, cache_key) = cache_->get(key_data);
if (cached_result) {
return std::move(*cached_result);
}

std::optional<Result> result = query_.exec(key_data);
cache_->put(cache_key, result);
return result;
}

private:
DomainGetLatestQueryRawNoCache query_;
CacheType* cache_;
};

template <EncoderConcept TKeyEncoder, DecoderConcept TValueDecoder>
struct DomainGetLatestQuery {
DomainGetLatestQuery(
const SnapshotRepositoryROAccess& repository,
const QueryCaches& query_caches,
datastore::EntityName entity_name)
: query_{repository, query_caches, entity_name} {}

using Key = decltype(TKeyEncoder::value);
using Value = decltype(TValueDecoder::value);

Expand All @@ -68,43 +128,20 @@ struct DomainGetLatestQuery {
};

std::optional<Result> exec(const Key& key) {
DomainGetLatestCache* cache = repository.domain_get_latest_cache(entity_name);

TKeyEncoder key_encoder;
key_encoder.value = key;
ByteView key_data = key_encoder.encode_word();

uint64_t key_hash_hi{0};
if (cache) {
std::optional<DomainGetLatestCacheData> cached_data;
if (std::tie(cached_data, key_hash_hi) = cache->get(key_data); cached_data) {
if (!cached_data->range_end) { // hit in cache but value not found
return std::nullopt;
}
TValueDecoder value_decoder;
value_decoder.decode_word(cached_data->value);
return Result{std::move(value_decoder.value), *cached_data->range_end};
}
}
auto value_data = query_.exec(key_data);
if (!value_data) return std::nullopt;

for (auto& bundle_ptr : repository.view_bundles_reverse()) {
const SnapshotBundle& bundle = *bundle_ptr;
DomainGetLatestSegmentQuery<TKeyEncoder, TValueDecoder> query{bundle, entity_name};
std::optional<Decoder::Word> value_data = query.exec_raw(key_data);
if (value_data) {
if (cache) {
cache->put(key_hash_hi, {*value_data, bundle.step_range().end});
}
TValueDecoder value_decoder;
value_decoder.decode_word(*value_data);
return Result{std::move(value_decoder.value), bundle.step_range().end};
}
}
if (cache) {
cache->put(key_hash_hi, {});
}
return std::nullopt;
TValueDecoder value_decoder;
value_decoder.decode_word(value_data->value);
return Result{std::move(value_decoder.value), value_data->step};
}

private:
DomainGetLatestQueryRawWithCache query_;
};

} // namespace silkworm::snapshots
7 changes: 5 additions & 2 deletions silkworm/db/datastore/snapshots/history_get_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "common/codec.hpp"
#include "common/raw_codec.hpp"
#include "inverted_index_seek_query.hpp"
#include "query_caches.hpp"
#include "snapshot_repository_ro_access.hpp"

namespace silkworm::snapshots {
Expand All @@ -17,11 +18,13 @@ template <
DecoderConcept TValueDecoder,
const SegmentAndAccessorIndexNames& segment_names>
struct HistoryGetQuery {
explicit HistoryGetQuery(const SnapshotRepositoryROAccess& repository)
HistoryGetQuery(
const SnapshotRepositoryROAccess& repository,
const QueryCaches& query_caches)
: timestamp_query_{
repository,
[](const SnapshotBundle& bundle) { return bundle.history(segment_names.front()).inverted_index; },
[&]() { return repository.inverted_index_seek_cache(segment_names.front()); },
query_caches.cache<InvertedIndexSeekQueryRawWithCache::CacheType>(InvertedIndexSeekQueryRawWithCache::kName, segment_names.front()).get(),
},
value_query_{repository} {}

Expand Down
Loading
Loading