feat(Algorithms): bounded epoch-qualified revision read sets (#13)

This commit is contained in:
faumaray committed 2026-12-03 06:53:34 +00:00
1 parent 7b0fea6095
commit dddcc2b2f0
1 file changed
+156
+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