pub struct Sequencer { /* private fields */ }Expand description
A per-namespace monotonic sequencer.
Assigns strictly-increasing, gap-free u64 positions within each
namespace, starting at 0. Namespaces are fully independent: assigning in
one never affects another. The sequencer is pure in-memory state and holds
no I/O — durability is the operator’s concern. After a restart the operator
rebuilds state with Sequencer::resume_from (typically last_committed + 1), which refuses to rewind so monotonicity survives crashes.
use metamorphic_log::ingest::{Namespace, Sequencer};
let ns = Namespace::parse("acme").unwrap();
let mut seq = Sequencer::new();
assert_eq!(seq.next(&ns), 0);
assert_eq!(seq.next(&ns), 1);
assert_eq!(seq.peek(&ns), 2); // next position that would be assignedImplementations§
Source§impl Sequencer
impl Sequencer
Sourcepub fn peek(&self, namespace: &Namespace) -> u64
pub fn peek(&self, namespace: &Namespace) -> u64
The next position that Sequencer::next would assign for namespace
(without assigning it). A never-seen namespace peeks at 0.
Sourcepub fn next(&mut self, namespace: &Namespace) -> u64
pub fn next(&mut self, namespace: &Namespace) -> u64
Assign and return the next monotonic position for namespace.
§Panics
Panics only on u64 position overflow (after 2^64 appends in a single
namespace), which is not reachable in practice.
Sourcepub fn reserve(
&mut self,
namespace: &Namespace,
count: u64,
) -> Result<Range<u64>>
pub fn reserve( &mut self, namespace: &Namespace, count: u64, ) -> Result<Range<u64>>
Reserve a contiguous block of count positions for namespace, returning
the half-open range [start, start + count). Useful for batch ingest: the
operator assigns the whole batch in one step while preserving order and
gap-freeness. A count of 0 returns an empty range at the current
position and does not advance the counter.
§Errors
Returns Error::SequenceOverflow if the block would overflow u64.
Sourcepub fn resume_from(&mut self, namespace: &Namespace, next: u64) -> Result<()>
pub fn resume_from(&mut self, namespace: &Namespace, next: u64) -> Result<()>
Re-seat the next position for namespace from durable storage (e.g. on
restart, to last_committed_position + 1).
This is monotonic-safe: it may only move the counter forward (or leave it unchanged). An attempt to set it below the current in-memory position is rejected rather than silently rewinding, because rewinding would re-issue an already-assigned position and break append-only ordering.
§Errors
Returns Error::SequenceRegression if next is below the current
position for namespace.