From dddcc2b2f03a754aa4c241c32989c054f2854008 Mon Sep 17 00:00:00 2001 From: Aleksey Selihov <1+faumaray@noreply@void.com> Date: Thu, 3 Dec 2026 06:53:34 +0000 Subject: [PATCH] feat(Algorithms): bounded epoch-qualified revision read sets (#13) --- include/Void/Algorithms/RevisionReadSet.hpp | 156 ++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 include/Void/Algorithms/RevisionReadSet.hpp 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