Skip to main content

Sequencer

Struct Sequencer 

Source
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 assigned

Implementations§

Source§

impl Sequencer

Source

pub fn new() -> Self

Create an empty sequencer (every namespace starts at position 0).

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Sequencer

Source§

fn clone(&self) -> Sequencer

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Sequencer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Sequencer

Source§

fn default() -> Sequencer

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.