Skip to content

Commit aeb2302

Browse files
authored
datastore: query caches refactoring (#2880)
1 parent 41185c3 commit aeb2302

36 files changed

+469
-260
lines changed

silkworm/capi/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ SILKWORM_EXPORT int silkworm_init(SilkwormHandle* handle, const struct SilkwormS
8383
.state_repository_latest = std::move(state_repository_latest),
8484
.state_repository_historical = std::move(state_repository_historical),
8585
.chaindata = {},
86+
.query_caches = snapshots::QueryCaches{db::state::make_query_caches_schema(), snapshots_dir_path, settings->state_repo_index_salt},
8687
};
8788

8889
// NOLINTNEXTLINE(bugprone-unhandled-exception-at-new)

silkworm/capi/silkworm.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ SILKWORM_EXPORT int silkworm_execute_txn(SilkwormHandle handle, MDBX_txn* mdbx_t
500500
db_ref,
501501
handle->db->blocks_repository,
502502
handle->db->state_repository_latest,
503+
handle->db->query_caches,
503504
};
504505
if (!handle->chain_config) {
505506
handle->chain_config = db::read_chain_config(unmanaged_tx);
@@ -646,6 +647,7 @@ SILKWORM_EXPORT int silkworm_block_exec_end(SilkwormHandle handle, MDBX_txn* mdb
646647
db_ref,
647648
handle->db->blocks_repository,
648649
handle->db->state_repository_latest,
650+
handle->db->query_caches,
649651
};
650652

651653
const auto log_index = handle->executions_in_block[index].log_index;

silkworm/db/blocks/schema_config.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ snapshots::SnapshotRepository make_blocks_repository(
5757
schema,
5858
index_salt,
5959
make_blocks_index_builders_factory(),
60-
std::nullopt, // no domain caches
61-
std::nullopt, // no inverted index caches
6260
};
6361
}
6462

silkworm/db/capi/component.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <silkworm/core/common/assert.hpp>
99
#include <silkworm/db/access_layer.hpp>
1010
#include <silkworm/db/datastore/kvdb/database.hpp>
11+
#include <silkworm/db/datastore/snapshots/query_caches.hpp>
1112
#include <silkworm/db/datastore/snapshots/snapshot_repository.hpp>
1213

1314
namespace silkworm::db::capi {
@@ -17,6 +18,7 @@ struct Component {
1718
silkworm::snapshots::SnapshotRepository state_repository_latest;
1819
silkworm::snapshots::SnapshotRepository state_repository_historical;
1920
std::unique_ptr<silkworm::datastore::kvdb::DatabaseUnmanaged> chaindata;
21+
silkworm::snapshots::QueryCaches query_caches;
2022

2123
DataStoreRef data_store() {
2224
SILKWORM_ASSERT(chaindata);
@@ -25,6 +27,7 @@ struct Component {
2527
blocks_repository,
2628
state_repository_latest,
2729
state_repository_historical,
30+
query_caches,
2831
};
2932
}
3033

silkworm/db/data_store.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ datastore::Schema DataStore::make_schema() {
1313
snapshots.repository(blocks::kBlocksRepositoryName) = blocks::make_blocks_repository_schema();
1414
snapshots.repository(state::kStateRepositoryNameLatest) = state::make_state_repository_schema_latest();
1515
snapshots.repository(state::kStateRepositoryNameHistorical) = state::make_state_repository_schema_historical();
16+
snapshots.query_caches_schema() = state::make_query_caches_schema();
1617

1718
return {
1819
std::move(kvdb),

silkworm/db/data_store.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct DataStoreRef {
1717
snapshots::SnapshotRepository& blocks_repository;
1818
snapshots::SnapshotRepository& state_repository_latest;
1919
snapshots::SnapshotRepository& state_repository_historical;
20+
const snapshots::QueryCaches& query_caches;
2021
};
2122

2223
class DataStore {
@@ -32,6 +33,7 @@ class DataStore {
3233
std::move(blocks_repository),
3334
std::move(state_repository_latest),
3435
std::move(state_repository_historical)),
36+
blocks_repository.path(),
3537
} {}
3638

3739
public:
@@ -61,6 +63,7 @@ class DataStore {
6163
blocks_repository(),
6264
state_repository_latest(),
6365
state_repository_historical(),
66+
store_.query_caches(),
6467
};
6568
}
6669

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

7983
static datastore::kvdb::Schema::DatabaseDef make_chaindata_database_schema();
8084
static datastore::kvdb::Database make_chaindata_database(mdbx::env_managed chaindata_env);

silkworm/db/datastore/data_store.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "common/entity_name.hpp"
99
#include "kvdb/database.hpp"
1010
#include "schema.hpp"
11+
#include "snapshots/query_caches.hpp"
1112
#include "snapshots/snapshot_repository.hpp"
1213

1314
namespace silkworm::datastore {
@@ -17,21 +18,25 @@ class DataStore {
1718
DataStore(
1819
Schema schema,
1920
EntityMap<std::unique_ptr<kvdb::Database>> databases,
20-
EntityMap<std::unique_ptr<snapshots::SnapshotRepository>> repositories)
21+
EntityMap<std::unique_ptr<snapshots::SnapshotRepository>> repositories,
22+
const std::filesystem::path& snapshots_path)
2123
: schema_{std::move(schema)},
2224
databases_{std::move(databases)},
23-
repositories_{std::move(repositories)} {}
25+
repositories_{std::move(repositories)},
26+
query_caches_{schema_.snapshots.query_caches_schema(), snapshots_path} {}
2427

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

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

3135
private:
3236
Schema schema_;
3337
EntityMap<std::unique_ptr<kvdb::Database>> databases_;
3438
EntityMap<std::unique_ptr<snapshots::SnapshotRepository>> repositories_;
39+
snapshots::QueryCaches query_caches_;
3540
};
3641

3742
} // namespace silkworm::datastore

silkworm/db/datastore/domain_get_as_of_query.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ struct DomainGetAsOfQuery {
1717
kvdb::Domain kvdb_entity,
1818
kvdb::ROTxn& tx,
1919
const snapshots::SnapshotRepositoryROAccess& repository_latest,
20-
const snapshots::SnapshotRepositoryROAccess& repository_historical)
21-
: query1_{*kvdb_entity.history, tx, repository_historical},
22-
query2_{history_segment_names.front(), kvdb_entity, tx, repository_latest} {}
20+
const snapshots::SnapshotRepositoryROAccess& repository_historical,
21+
const snapshots::QueryCaches& query_caches)
22+
: query1_{*kvdb_entity.history, tx, repository_historical, query_caches},
23+
query2_{history_segment_names.front(), kvdb_entity, tx, repository_latest, query_caches} {}
2324

2425
DomainGetAsOfQuery(
2526
const kvdb::DatabaseRef& database,
2627
kvdb::ROTxn& tx,
2728
const snapshots::SnapshotRepositoryROAccess& repository_latest,
28-
const snapshots::SnapshotRepositoryROAccess& repository_historical)
29-
: query1_{database, tx, repository_historical},
30-
query2_{history_segment_names.front(), database, tx, repository_latest} {}
29+
const snapshots::SnapshotRepositoryROAccess& repository_historical,
30+
const snapshots::QueryCaches& query_caches)
31+
: query1_{database, tx, repository_historical, query_caches},
32+
query2_{history_segment_names.front(), database, tx, repository_latest, query_caches} {}
3133

3234
using Key = decltype(TKeyEncoder1::value);
3335
using Value = decltype(TValueDecoder1::value);

silkworm/db/datastore/domain_get_latest_query.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,23 @@ struct DomainGetLatestQuery {
1717
datastore::EntityName entity_name,
1818
kvdb::Domain kvdb_entity,
1919
kvdb::ROTxn& tx,
20-
const snapshots::SnapshotRepositoryROAccess& repository)
20+
const snapshots::SnapshotRepositoryROAccess& repository,
21+
const snapshots::QueryCaches& query_caches)
2122
: query1_{tx, kvdb_entity},
22-
query2_{repository, entity_name} {}
23+
query2_{repository, query_caches, entity_name} {}
2324

2425
DomainGetLatestQuery(
2526
datastore::EntityName entity_name,
2627
const kvdb::DatabaseRef& database,
2728
kvdb::ROTxn& tx,
28-
const snapshots::SnapshotRepositoryROAccess& repository)
29+
const snapshots::SnapshotRepositoryROAccess& repository,
30+
const snapshots::QueryCaches& query_caches)
2931
: DomainGetLatestQuery{
3032
entity_name,
3133
database.domain(entity_name),
3234
tx,
3335
repository,
36+
query_caches,
3437
} {}
3538

3639
using Key1 = decltype(TKeyEncoder1::value);

silkworm/db/datastore/history_get_query.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@ struct HistoryGetQuery {
1717
HistoryGetQuery(
1818
kvdb::History kvdb_entity,
1919
kvdb::ROTxn& tx,
20-
const snapshots::SnapshotRepositoryROAccess& repository)
20+
const snapshots::SnapshotRepositoryROAccess& repository,
21+
const snapshots::QueryCaches& query_caches)
2122
: query1_{tx, kvdb_entity},
22-
query2_{repository} {}
23+
query2_{repository, query_caches} {}
2324

2425
HistoryGetQuery(
2526
const kvdb::DatabaseRef& database,
2627
kvdb::ROTxn& tx,
27-
const snapshots::SnapshotRepositoryROAccess& repository)
28+
const snapshots::SnapshotRepositoryROAccess& repository,
29+
const snapshots::QueryCaches& query_caches)
2830
: HistoryGetQuery{
2931
database.domain(segment_names.front()).history.value(),
3032
tx,
3133
repository,
34+
query_caches,
3235
} {}
3336

3437
using Key1 = decltype(TKeyEncoder1::value);

0 commit comments

Comments
 (0)