Merge pull request 'Bounded epoch-qualified revision read sets (#13)' (#29) from issue/13-revision-read-set into main

This commit was merged in pull request #29.
This commit is contained in:
faumaray committed 2026-12-03 08:08:31 +00:00
commit 8c576fdcbf
2 files changed
+291

No files matched your search

+156
View File
@@ -0,0 +1,156 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
namespace Void::Algorithms
{
enum class ReadSetStatus : std::uint8_t {
/// A new key/revision pair was recorded.
recorded = 0,
/// The same key with the same revision was already recorded.
unchanged,
/// The same key was observed with a different revision.
conflicting,
/// The entry bound is reached; nothing was recorded.
capacity_exceeded,
/// The key is empty.
invalid_key,
};
enum class ReadSetValidation : std::uint8_t {
valid = 0,
/// A recorded key is missing or has a different revision.
invalidated,
/// The validating view belongs to another epoch.
epoch_changed,
/// A conflicting observation was reported; the set cannot be published.
conflicted,
/// An observation was rejected for capacity; the set is incomplete.
incomplete,
};
struct ReadSetLimits {
std::size_t max_entries{256};
};
/// Bounded set of distinct key/revision pairs consulted while computing a
/// result, qualified by an epoch.
///
/// Repeated reads of one unchanged key consume one entry. Observing the same
/// key with a different revision is reported as a conflict, and an entry over
/// the bound as capacity exhaustion; `validate` refuses to report a conflicted
/// or incomplete set as valid so a result cannot be published from it.
///
/// Validation compares the recorded set against a stable read view supplied by
/// the caller (a revision lookup per key plus the view's epoch). An epoch
/// change invalidates the set even when every numeric revision matches. Keys
/// not in the set (unrelated edits) do not affect the result.
class RevisionReadSet
{
public:
using Limits = ReadSetLimits;
explicit RevisionReadSet(std::uint64_t epoch, Limits limits = {})
: epoch_(epoch)
, limits_(limits)
{
}
[[nodiscard]] std::uint64_t epoch() const noexcept { return epoch_; }
[[nodiscard]] ReadSetStatus observe(std::string key,
std::uint64_t revision)
{
if (key.empty()) {
return ReadSetStatus::invalid_key;
}
if (const auto found = entries_.find(key);
found != entries_.end()) {
if (found->second == revision) {
return ReadSetStatus::unchanged;
}
conflicted_ = true;
return ReadSetStatus::conflicting;
}
if (entries_.size() >= limits_.max_entries) {
incomplete_ = true;
return ReadSetStatus::capacity_exceeded;
}
entries_.emplace(std::move(key), revision);
return ReadSetStatus::recorded;
}
[[nodiscard]] bool contains(std::string_view key) const
{
return entries_.contains(std::string{key});
}
[[nodiscard]] std::optional<std::uint64_t>
revision_of(std::string_view key) const
{
const auto found = entries_.find(std::string{key});
if (found == entries_.end()) {
return std::nullopt;
}
return found->second;
}
[[nodiscard]] std::size_t size() const noexcept
{
return entries_.size();
}
[[nodiscard]] bool conflicted() const noexcept { return conflicted_; }
[[nodiscard]] bool incomplete() const noexcept { return incomplete_; }
/// Recorded observations, sorted by key.
[[nodiscard]] const std::map<std::string, std::uint64_t>&
entries() const noexcept
{
return entries_;
}
/// Validate against a stable read view.
///
/// `lookup(key)` returns the current revision of `key`, or `nullopt`
/// when it no longer exists. `view_epoch` is the epoch of the view.
template <typename Lookup>
[[nodiscard]] ReadSetValidation
validate(std::uint64_t view_epoch, Lookup&& lookup) const
{
if (view_epoch != epoch_) {
return ReadSetValidation::epoch_changed;
}
if (conflicted_) {
return ReadSetValidation::conflicted;
}
if (incomplete_) {
return ReadSetValidation::incomplete;
}
for (const auto& [key, revision] : entries_) {
const auto current = lookup(key);
if (!current.has_value() || *current != revision) {
return ReadSetValidation::invalidated;
}
}
return ReadSetValidation::valid;
}
private:
std::uint64_t epoch_{0};
Limits limits_{};
std::map<std::string, std::uint64_t> entries_;
bool conflicted_{false};
bool incomplete_{false};
};
} // namespace Void::Algorithms
@@ -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"});
}