Skip to main content

Minter

Struct Minter 

Source
pub struct Minter {
    clock_id: u64,
    last: Mutex<u64>,
}
Expand description

Mints TIDs that sort in the order they were minted.

A TID as atproto specifies it: microseconds since the Unix epoch in the low 53 bits above a 10-bit clock identifier, with the top bit clear, rendered in BASE32_SORTABLE. Two properties matter. It is timestamped, so a TID says when it was minted without a second field beside it; and it is sortable as a string, so newest-first is a reverse iteration of an ordered map rather than a sort.

One minter serves a store’s record keys and its commit revisions together. They are separate sequences to a reader and the same sequence here, which costs nothing — a revision is not required to be dense — and buys the one property both need: that nothing this store hands out ever repeats or sorts backwards, whatever the clock does.

Fields§

§clock_id: u64§last: Mutex<u64>

Implementations§

Source§

impl Minter

Source

pub fn new() -> Self

A minter with a fixed clock identifier.

The clock id is derived from the process start rather than randomly, because this store is per-process and never merges with another: its only job is to keep two processes writing the same logical repository from colliding, which cannot happen to an in-memory store anyway.

Source

pub fn next(&self) -> String

The next TID, strictly greater than every one this minter has returned.

The clock is only read, never trusted for ordering. Two writes inside one microsecond, or a clock that steps backwards, would otherwise produce a key that sorts before an earlier record and silently reorder a listing.

Source

pub fn observe(&self, minted: &str)

Notes a TID somebody else minted, so this minter never repeats it.

A restart replays keys a previous process wrote, and the clock is the only thing that would otherwise keep the new process past them. That is enough while the clock moves forward and is exactly the assumption Minter::next already refuses to make, so replay states it here instead: a key that sorted after everything when it was written must still sort after everything when the log is read back.

Trait Implementations§

Source§

impl Debug for Minter

Source§

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

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

impl Default for Minter

Source§

fn default() -> Self

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more