feat(Algorithms): bounded epoch-qualified revision read sets (#13)
This commit is contained in:
1 parent
dddcc2b2f0
commit
b062e759ba
1 file changed
+135
@@ -0,0 +1,135 @@
|
||||
#include <Void/Algorithms/RevisionReadSet.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using Void::Algorithms::ReadSetStatus;
|
||||
using Void::Algorithms::ReadSetValidation;
|
||||
using Void::Algorithms::RevisionReadSet;
|
||||
|
||||
using View = std::map<std::string, std::uint64_t>;
|
||||
|
||||
[[nodiscard]] auto lookup(const View& view)
|
||||
{
|
||||
return [&view](const std::string& key) -> std::optional<std::uint64_t> {
|
||||
const auto found = view.find(key);
|
||||
if (found == view.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return found->second;
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test read_set_deduplicates_unchanged_reads()
|
||||
{
|
||||
RevisionReadSet set{1};
|
||||
Void::Testing::expect(set.observe("chunk", 3) == ReadSetStatus::recorded);
|
||||
Void::Testing::expect(set.observe("chunk", 3) == ReadSetStatus::unchanged);
|
||||
Void::Testing::expect_eq(set.size(), std::size_t{1});
|
||||
Void::Testing::expect_eq(set.revision_of("chunk"), std::uint64_t{3});
|
||||
}
|
||||
|
||||
Void::Testing::Test read_set_detects_conflicting_observations()
|
||||
{
|
||||
RevisionReadSet set{1};
|
||||
Void::Testing::expect(set.observe("chunk", 3) == ReadSetStatus::recorded);
|
||||
Void::Testing::expect(set.observe("chunk", 4) ==
|
||||
ReadSetStatus::conflicting);
|
||||
Void::Testing::expect(set.conflicted());
|
||||
|
||||
// The first observation is retained, but the set cannot be published.
|
||||
Void::Testing::expect_eq(set.revision_of("chunk"), std::uint64_t{3});
|
||||
const View view{{"chunk", 3}};
|
||||
Void::Testing::expect(set.validate(1, lookup(view)) ==
|
||||
ReadSetValidation::conflicted);
|
||||
}
|
||||
|
||||
Void::Testing::Test read_set_reports_capacity_exhaustion()
|
||||
{
|
||||
RevisionReadSet set{1, RevisionReadSet::Limits{.max_entries = 2}};
|
||||
Void::Testing::expect(set.observe("a", 1) == ReadSetStatus::recorded);
|
||||
Void::Testing::expect(set.observe("b", 1) == ReadSetStatus::recorded);
|
||||
Void::Testing::expect(set.observe("c", 1) ==
|
||||
ReadSetStatus::capacity_exceeded);
|
||||
Void::Testing::expect(set.incomplete());
|
||||
Void::Testing::expect_eq(set.size(), std::size_t{2});
|
||||
|
||||
const View view{{"a", 1}, {"b", 1}, {"c", 1}};
|
||||
Void::Testing::expect(set.validate(1, lookup(view)) ==
|
||||
ReadSetValidation::incomplete);
|
||||
}
|
||||
|
||||
Void::Testing::Test read_set_validates_against_stable_view()
|
||||
{
|
||||
RevisionReadSet set{1};
|
||||
Void::Testing::expect(set.observe("route", 10) == ReadSetStatus::recorded);
|
||||
Void::Testing::expect(set.observe("support", 20) ==
|
||||
ReadSetStatus::recorded);
|
||||
|
||||
View view{{"route", 10}, {"support", 20}};
|
||||
Void::Testing::expect(set.validate(1, lookup(view)) ==
|
||||
ReadSetValidation::valid);
|
||||
|
||||
// An unrelated edit does not invalidate the cached result.
|
||||
view["unrelated"] = 99;
|
||||
Void::Testing::expect(set.validate(1, lookup(view)) ==
|
||||
ReadSetValidation::valid);
|
||||
|
||||
// A change to a consulted revision does.
|
||||
view["support"] = 21;
|
||||
Void::Testing::expect(set.validate(1, lookup(view)) ==
|
||||
ReadSetValidation::invalidated);
|
||||
|
||||
// A missing consulted key does too.
|
||||
view["support"] = 20;
|
||||
view.erase("route");
|
||||
Void::Testing::expect(set.validate(1, lookup(view)) ==
|
||||
ReadSetValidation::invalidated);
|
||||
}
|
||||
|
||||
Void::Testing::Test read_set_rejects_epoch_change_even_with_matching_revisions()
|
||||
{
|
||||
RevisionReadSet set{1};
|
||||
Void::Testing::expect(set.observe("route", 10) == ReadSetStatus::recorded);
|
||||
const View view{{"route", 10}};
|
||||
Void::Testing::expect(set.validate(2, lookup(view)) ==
|
||||
ReadSetValidation::epoch_changed);
|
||||
}
|
||||
|
||||
Void::Testing::Test read_set_failed_search_frontier_opening()
|
||||
{
|
||||
// A failed search consulted a closed frontier at revision 0.
|
||||
RevisionReadSet set{7};
|
||||
Void::Testing::expect(set.observe("frontier", 0) ==
|
||||
ReadSetStatus::recorded);
|
||||
View view{{"frontier", 0}};
|
||||
Void::Testing::expect(set.validate(7, lookup(view)) ==
|
||||
ReadSetValidation::valid);
|
||||
|
||||
// The frontier opens: the cached failure must not be reused.
|
||||
view["frontier"] = 1;
|
||||
Void::Testing::expect(set.validate(7, lookup(view)) ==
|
||||
ReadSetValidation::invalidated);
|
||||
}
|
||||
|
||||
Void::Testing::Test read_set_rejects_empty_key_and_sorts_entries()
|
||||
{
|
||||
RevisionReadSet set{1};
|
||||
Void::Testing::expect(set.observe("", 1) == ReadSetStatus::invalid_key);
|
||||
Void::Testing::expect_eq(set.size(), std::size_t{0});
|
||||
|
||||
Void::Testing::expect(set.observe("b", 2) == ReadSetStatus::recorded);
|
||||
Void::Testing::expect(set.observe("a", 1) == ReadSetStatus::recorded);
|
||||
const auto& entries = set.entries();
|
||||
Void::Testing::require(entries.size() == 2);
|
||||
Void::Testing::expect_eq(entries.begin()->first, std::string{"a"});
|
||||
}
|
||||
Reference in new issue
Block a user