diff --git a/include/Void/Algorithms/RevisionReadSet.hpp b/include/Void/Algorithms/RevisionReadSet.hpp new file mode 100644 index 0000000..10cf6d1 --- /dev/null +++ b/include/Void/Algorithms/RevisionReadSet.hpp @@ -0,0 +1,156 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +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 + 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& + 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 + [[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 entries_; + bool conflicted_{false}; + bool incomplete_{false}; +}; + +} // namespace Void::Algorithms diff --git a/tests/standalone/Algorithms/RevisionReadSet.cpp b/tests/standalone/Algorithms/RevisionReadSet.cpp new file mode 100644 index 0000000..eaa52cd --- /dev/null +++ b/tests/standalone/Algorithms/RevisionReadSet.cpp @@ -0,0 +1,135 @@ +#include +#include +#include + +#include +#include +#include +#include + +namespace +{ + +using Void::Algorithms::ReadSetStatus; +using Void::Algorithms::ReadSetValidation; +using Void::Algorithms::RevisionReadSet; + +using View = std::map; + +[[nodiscard]] auto lookup(const View& view) +{ + return [&view](const std::string& key) -> std::optional { + 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"}); +}