feat(Core): instance-owned restorable id sequences with checked exhaustion (#7)

- IdSequence<Tag, Rep> is per-instance, copyable and restorable, so two
  worlds in one process allocate independently and a saved high-water
  mark replays exactly.
- next()/next_batch() check exhaustion before mutating; failed requests
  leave the sequence and application records untouched. Batches are
  all-or-nothing.
- High-water only moves forward: erased records keep their ids retired.
- Fixed-width Rep (default uint64_t) plus an explicit exhausted flag make
  saved state canonical and portable; restore() rejects impossible state.
- 10 standalone tests cover independence, erase/restore, copy replay,
  atomic batches, exhaustion, final-boundary batches and invalid state.
This commit is contained in:
selikhov committed 2026-12-03 09:41:50 +03:00
1 parent 7b0fea6095
commit f2b002eb26
2 files changed
+331

No files matched your search

+183
View File
@@ -0,0 +1,183 @@
#pragma once
#include <compare>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <limits>
#include <optional>
namespace Void::Core
{
enum class SequenceErrorKind : std::uint8_t {
/// The requested id or batch does not fit in the remaining range.
exhausted = 0,
/// A batch of zero ids was requested.
zero_count,
/// The supplied saved state is not a valid sequence state.
invalid_state,
};
struct SequenceError {
SequenceErrorKind kind{SequenceErrorKind::exhausted};
/// How many ids were still available when the request failed.
std::uint64_t available{0};
};
/// Instance-owned, restorable id sequence with checked exhaustion.
///
/// Unlike `Void::Counted::SeqId<Tag>` (one process-wide atomic per tag), each
/// `IdSequence` owns its own state. It can be copied at a completed boundary,
/// saved and restored, and several instances (e.g. two worlds in one process)
/// allocate independently.
///
/// Ids start at 1. A removed record never gives its id back: the high-water
/// mark only moves forward, including across save/restore. `next()` and
/// `next_batch()` return `exhausted` *before* changing state when the request
/// does not fit, so a failed allocation leaves both the sequence and any
/// application record untouched.
///
/// `Rep` is the fixed-width representation used in saved state (default
/// `std::uint64_t`), making cross-platform saves reproducible. `Tag` keeps
/// unrelated id domains from being confused at compile time.
///
/// The class is not thread-safe; callers synchronize concurrent access to one
/// instance.
template <typename Tag = void, typename Rep = std::uint64_t> class IdSequence
{
public:
using value_type = Rep;
/// Serializable position. `exhausted` distinguishes "max is still
/// available" (`next == max`) from "max was already handed out".
struct State {
value_type next{1};
bool exhausted{false};
[[nodiscard]] auto
operator<=>(const State&) const noexcept = default;
[[nodiscard]] bool
operator==(const State&) const noexcept = default;
};
struct Batch {
value_type first{0};
value_type count{0};
};
constexpr IdSequence() noexcept = default;
explicit constexpr IdSequence(value_type first) noexcept
: state_{.next = first == 0 ? value_type{1} : first}
{
}
/// Allocate one id.
[[nodiscard]] std::expected<value_type, SequenceError> next()
{
if (state_.exhausted) {
return fail(SequenceErrorKind::exhausted);
}
const value_type id = state_.next;
if (id == maximum()) {
state_.exhausted = true;
} else {
state_.next = static_cast<value_type>(id + 1);
}
return id;
}
/// Reserve a contiguous batch atomically: either all `count` ids are
/// reserved or none are.
[[nodiscard]] std::expected<Batch, SequenceError>
next_batch(std::size_t count)
{
if (count == 0) {
return fail(SequenceErrorKind::zero_count);
}
if (state_.exhausted) {
return fail(SequenceErrorKind::exhausted);
}
const auto remaining = static_cast<std::uintmax_t>(available());
if (static_cast<std::uintmax_t>(count) > remaining) {
return fail(SequenceErrorKind::exhausted);
}
const value_type first = state_.next;
const auto step = static_cast<value_type>(count);
if (static_cast<std::uintmax_t>(step) == remaining) {
state_.exhausted = true;
} else {
state_.next = static_cast<value_type>(first + step);
}
return Batch{.first = first, .count = step};
}
/// How many ids can still be allocated.
[[nodiscard]] value_type available() const noexcept
{
if (state_.exhausted) {
return 0;
}
return static_cast<value_type>(maximum() - state_.next + 1);
}
[[nodiscard]] bool exhausted() const noexcept
{
return state_.exhausted;
}
/// The next id that would be handed out (or max once exhausted).
[[nodiscard]] value_type peek() const noexcept { return state_.next; }
/// Highest id ever handed out (0 before the first allocation).
[[nodiscard]] value_type high_water() const noexcept
{
if (state_.exhausted) {
return maximum();
}
return state_.next == 1 ? value_type{0}
: static_cast<value_type>(state_.next - 1);
}
[[nodiscard]] State state() const noexcept { return state_; }
/// Restore a saved state. Rejects non-canonical or impossible states;
/// on success the sequence continues exactly after the saved position.
[[nodiscard]] static std::expected<IdSequence, SequenceError>
restore(State state)
{
if (state.next == 0) {
return std::unexpected{SequenceError{
.kind = SequenceErrorKind::invalid_state, .available = 0}};
}
if (state.exhausted && state.next != maximum()) {
return std::unexpected{SequenceError{
.kind = SequenceErrorKind::invalid_state,
.available = 0}};
}
IdSequence sequence;
sequence.state_ = state;
return sequence;
}
[[nodiscard]] static constexpr value_type maximum() noexcept
{
return std::numeric_limits<value_type>::max();
}
private:
[[nodiscard]] std::unexpected<SequenceError>
fail(SequenceErrorKind kind) const
{
return std::unexpected{SequenceError{
.kind = kind,
.available = static_cast<std::uint64_t>(available())}};
}
State state_{};
};
} // namespace Void::Core