Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

datastore: refactor kvdb query tests #2770

Merged
merged 1 commit into from
Mar 7, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@
limitations under the License.
*/

#include "domain_put_latest_query.hpp"

#include <functional>
#include "domain_get_latest_query.hpp"

#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>

#include <silkworm/infra/common/directories.hpp>

#include "big_endian_codec.hpp"
#include "database.hpp"
#include "domain_get_latest_query.hpp"
#include "domain_put_latest_query.hpp"
#include "query_test.hpp"

namespace silkworm::datastore::kvdb {

Expand All @@ -42,46 +38,17 @@ bool operator==(const Result& lhs, const Result& rhs) {
return (lhs.value == rhs.value) && (lhs.step == rhs.step);
};

// by default has_large_values = false, is_multi_value = true
using DomainDefault = std::identity;
TEMPLATE_TEST_CASE("DomainGetLatestQuery", "", DomainDefault, DomainWithLargeValues) {
QueryTest test = QueryTest::make<TestType>();

struct DomainWithLargeValues {
Schema::DomainDef& operator()(Schema::DomainDef& domain) const {
domain.enable_large_values().values_disable_multi_value();
return domain;
}
};

TEMPLATE_TEST_CASE("DomainPutLatestQuery", "", DomainDefault, DomainWithLargeValues) {
const TemporaryDirectory tmp_dir;
::mdbx::env_managed env = open_env(EnvConfig{.path = tmp_dir.path().string(), .create = true, .in_memory = true});

EntityName name{"Test"};
Schema::DatabaseDef schema;
TestType domain_config;
[[maybe_unused]] auto _ = domain_config(schema.domain(name));

Database db{std::move(env), schema};
db.create_tables();
Domain entity = db.domain(name);
RWAccess db_access = db.access_rw();

auto find_in = [&db_access, &entity](const std::vector<DomainPutEntry>& data, uint64_t key) -> std::optional<Result> {
{
RWTxnManaged tx = db_access.start_rw_tx();
DomainPutLatestQuery<BigEndianU64Codec, BigEndianU64Codec> query{tx, entity};
for (auto& entry : data) {
query.exec(entry.key, entry.value, entry.step);
}
tx.commit_and_stop();
}

ROTxnManaged tx = db_access.start_ro_tx();
DomainGetQuery query{tx, entity};
return query.exec(key);
auto find_in = [&test](const std::vector<DomainPutEntry>& data, uint64_t key) -> std::optional<Result> {
return test.find_in<EntityKind::kDomain, DomainPutEntry, DomainPutLatestQuery<BigEndianU64Codec, BigEndianU64Codec>, DomainGetQuery>(data, key);
};

auto count = [&db_access, &entity]() -> uint64_t {
auto count = [&test]() -> uint64_t {
Domain entity = test.domain();
RWAccess db_access = test.access_rw();

ROTxnManaged tx = db_access.start_ro_tx();
PooledCursor cursor{tx, entity.values_table};
return cursor.get_map_stat().ms_entries;
Expand Down
45 changes: 4 additions & 41 deletions silkworm/db/datastore/kvdb/domain_range_latest_query_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@

#include "domain_range_latest_query.hpp"

#include <functional>
#include <utility>

#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>

#include <silkworm/infra/common/directories.hpp>

#include "../common/ranges/vector_from_range.hpp"
#include "big_endian_codec.hpp"
#include "database.hpp"
#include "domain_put_latest_query.hpp"
#include "query_test.hpp"

namespace silkworm::datastore::kvdb {

Expand All @@ -40,44 +36,11 @@ struct DomainPutEntry {
using DomainRangeQuery = DomainRangeLatestQuery<BigEndianU64Codec, BigEndianU64Codec, BigEndianU64Codec>;
using Result = std::vector<std::pair<uint64_t, uint64_t>>;

// by default has_large_values = false, is_multi_value = true
using DomainDefault = std::identity;

struct DomainWithLargeValues {
Schema::DomainDef& operator()(Schema::DomainDef& domain) const {
domain.enable_large_values().values_disable_multi_value();
return domain;
}
};

TEMPLATE_TEST_CASE("DomainRangeLatestQuery", "", DomainDefault, DomainWithLargeValues) {
const TemporaryDirectory tmp_dir;
::mdbx::env_managed env = open_env(EnvConfig{.path = tmp_dir.path().string(), .create = true, .in_memory = true});

EntityName name{"Test"};
Schema::DatabaseDef schema;
TestType domain_config;
[[maybe_unused]] auto _ = domain_config(schema.domain(name));

Database db{std::move(env), schema};
db.create_tables();
Domain entity = db.domain(name);
RWAccess db_access = db.access_rw();

auto find_in = [&db_access, &entity](const std::vector<DomainPutEntry>& data, uint64_t key_start, uint64_t key_end) -> Result {
{
RWTxnManaged tx = db_access.start_rw_tx();
DomainPutLatestQuery<BigEndianU64Codec, BigEndianU64Codec> query{tx, entity};
for (auto& entry : data) {
query.exec(entry.key, entry.value, entry.step);
}
tx.commit_and_stop();
}
QueryTest test = QueryTest::make<TestType>();

ROTxnManaged tx = db_access.start_ro_tx();
DomainRangeQuery query{tx, entity};
auto results = vector_from_range(query.exec(key_start, key_end, /* ascending = */ true));
return results;
auto find_in = [&test](const std::vector<DomainPutEntry>& data, uint64_t key_start, uint64_t key_end) -> Result {
return test.find_in<EntityKind::kDomain, DomainPutEntry, DomainPutLatestQuery<BigEndianU64Codec, BigEndianU64Codec>, DomainRangeQuery>(data, key_start, key_end, /* ascending = */ true);
};

SECTION("single entry - correct key") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@
limitations under the License.
*/

#include "history_put_query.hpp"

#include <functional>
#include "history_get_query.hpp"

#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>

#include <silkworm/infra/common/directories.hpp>

#include "big_endian_codec.hpp"
#include "database.hpp"
#include "history_get_query.hpp"
#include "history_put_query.hpp"
#include "query_test.hpp"

namespace silkworm::datastore::kvdb {

Expand All @@ -38,44 +34,11 @@ using Entry = HistoryPutEntry;

using Result = tl::expected<uint64_t, HistoryGetQuery<BigEndianU64Codec, BigEndianU64Codec>::NoValueReason>;

// by default has_large_values = false, is_multi_value = true
using DomainDefault = std::identity;

struct DomainWithLargeValues {
Schema::DomainDef& operator()(Schema::DomainDef& domain) const {
domain.enable_large_values().values_disable_multi_value();
return domain;
}
};

TEMPLATE_TEST_CASE("HistoryPutQuery", "", DomainDefault, DomainWithLargeValues) {
const TemporaryDirectory tmp_dir;
::mdbx::env_managed env = open_env(EnvConfig{.path = tmp_dir.path().string(), .create = true, .in_memory = true});

EntityName name{"Test"};
Schema::DatabaseDef schema;
TestType domain_config;
[[maybe_unused]] auto _ = domain_config(schema.domain(name));

Database db{std::move(env), schema};
db.create_tables();
Domain domain = db.domain(name);
History& entity = *domain.history;
RWAccess db_access = db.access_rw();

auto find_in = [&db_access, &entity](const std::vector<Entry>& data, uint64_t key, Timestamp timestamp) -> Result {
{
RWTxnManaged tx = db_access.start_rw_tx();
HistoryPutQuery<BigEndianU64Codec, BigEndianU64Codec> query{tx, entity};
for (auto& entry : data) {
query.exec(entry.key, entry.value, entry.timestamp);
}
tx.commit_and_stop();
}
TEMPLATE_TEST_CASE("HistoryGetQuery", "", DomainDefault, DomainWithLargeValues) {
QueryTest test = QueryTest::make<TestType>();

ROTxnManaged tx = db_access.start_ro_tx();
HistoryGetQuery<BigEndianU64Codec, BigEndianU64Codec> query{tx, entity};
return query.exec(key, timestamp);
auto find_in = [&test](const std::vector<Entry>& data, uint64_t key, Timestamp timestamp) -> Result {
return test.find_in<EntityKind::kHistory, Entry, HistoryPutQuery<BigEndianU64Codec, BigEndianU64Codec>, HistoryGetQuery<BigEndianU64Codec, BigEndianU64Codec>>(data, key, timestamp);
};

SECTION("single entry - correct key") {
Expand Down
46 changes: 4 additions & 42 deletions silkworm/db/datastore/kvdb/history_range_by_keys_query_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@

#include "history_range_by_keys_query.hpp"

#include <functional>
#include <utility>

#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>

#include <silkworm/infra/common/directories.hpp>

#include "../common/ranges/vector_from_range.hpp"
#include "big_endian_codec.hpp"
#include "database.hpp"
#include "history_put_query.hpp"
#include "query_test.hpp"

namespace silkworm::datastore::kvdb {

Expand All @@ -40,45 +36,11 @@ struct Entry {
using HistoryRangeQuery = HistoryRangeByKeysQuery<BigEndianU64Codec, BigEndianU64Codec, BigEndianU64Codec>;
using Result = std::vector<std::pair<uint64_t, uint64_t>>;

// by default has_large_values = false, is_multi_value = true
using DomainDefault = std::identity;

struct DomainWithLargeValues {
Schema::DomainDef& operator()(Schema::DomainDef& domain) const {
domain.enable_large_values().values_disable_multi_value();
return domain;
}
};

TEMPLATE_TEST_CASE("HistoryRangeByKeysQuery", "", DomainDefault, DomainWithLargeValues) {
const TemporaryDirectory tmp_dir;
::mdbx::env_managed env = open_env(EnvConfig{.path = tmp_dir.path().string(), .create = true, .in_memory = true});

EntityName name{"Test"};
Schema::DatabaseDef schema;
TestType domain_config;
[[maybe_unused]] auto _ = domain_config(schema.domain(name));

Database db{std::move(env), schema};
db.create_tables();
Domain domain = db.domain(name);
History& entity = *domain.history;
RWAccess db_access = db.access_rw();

auto find_in = [&db_access, &entity](const std::vector<Entry>& data, uint64_t key_start, uint64_t key_end, Timestamp timestamp) -> Result {
{
RWTxnManaged tx = db_access.start_rw_tx();
HistoryPutQuery<BigEndianU64Codec, BigEndianU64Codec> query{tx, entity};
for (auto& entry : data) {
query.exec(entry.key, entry.value, entry.timestamp);
}
tx.commit_and_stop();
}
QueryTest test = QueryTest::make<TestType>();

ROTxnManaged tx = db_access.start_ro_tx();
HistoryRangeQuery query{tx, entity};
auto results = vector_from_range(query.exec(key_start, key_end, timestamp, /* ascending = */ true));
return results;
auto find_in = [&test](const std::vector<Entry>& data, uint64_t key_start, uint64_t key_end, Timestamp timestamp) -> Result {
return test.find_in<EntityKind::kHistory, Entry, HistoryPutQuery<BigEndianU64Codec, BigEndianU64Codec>, HistoryRangeQuery>(data, key_start, key_end, timestamp, /* ascending = */ true);
};

SECTION("single entry - correct key") {
Expand Down
47 changes: 5 additions & 42 deletions silkworm/db/datastore/kvdb/history_range_in_period_query_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@

#include "history_range_in_period_query.hpp"

#include <functional>

#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>

#include <silkworm/infra/common/directories.hpp>

#include "../common/ranges/vector_from_range.hpp"
#include "big_endian_codec.hpp"
#include "database.hpp"
#include "history_put_query.hpp"
#include "query_test.hpp"

namespace silkworm::datastore::kvdb {

Expand All @@ -37,46 +32,14 @@ struct HistoryPutEntry {
};
using Entry = HistoryPutEntry;

using HistoryRangeQuery = HistoryRangeInPeriodQuery<BigEndianU64Codec, BigEndianU64Codec>;
using Result = std::vector<std::pair<uint64_t, uint64_t>>;

// by default has_large_values = false, is_multi_value = true
using DomainDefault = std::identity;

struct DomainWithLargeValues {
Schema::DomainDef& operator()(Schema::DomainDef& domain) const {
domain.enable_large_values().values_disable_multi_value();
return domain;
}
};

TEMPLATE_TEST_CASE("HistoryRangeInPeriodQuery", "", DomainDefault, DomainWithLargeValues) {
const TemporaryDirectory tmp_dir;
::mdbx::env_managed env = open_env(EnvConfig{.path = tmp_dir.path().string(), .create = true, .in_memory = true});

EntityName name{"Test"};
Schema::DatabaseDef schema;
TestType domain_config;
[[maybe_unused]] auto _ = domain_config(schema.domain(name));

Database db{std::move(env), schema};
db.create_tables();
Domain domain = db.domain(name);
History& entity = *domain.history;
RWAccess db_access = db.access_rw();

auto find_in = [&db_access, &entity](const std::vector<Entry>& data, TimestampRange ts_range) -> Result {
{
RWTxnManaged tx = db_access.start_rw_tx();
HistoryPutQuery<BigEndianU64Codec, BigEndianU64Codec> query{tx, entity};
for (auto& entry : data) {
query.exec(entry.key, entry.value, entry.timestamp);
}
tx.commit_and_stop();
}
QueryTest test = QueryTest::make<TestType>();

ROTxnManaged tx = db_access.start_ro_tx();
HistoryRangeInPeriodQuery<BigEndianU64Codec, BigEndianU64Codec> query{tx, entity};
return vector_from_range(query.exec(ts_range, true));
auto find_in = [&test](const std::vector<Entry>& data, TimestampRange ts_range) -> Result {
return test.find_in<EntityKind::kHistory, Entry, HistoryPutQuery<BigEndianU64Codec, BigEndianU64Codec>, HistoryRangeQuery>(data, ts_range, /* ascending = */ true);
};

SECTION("find all") {
Expand Down
Loading
Loading