Skip to main content

GappyMemoryStore

Struct GappyMemoryStore 

pub struct GappyMemoryStore { /* private fields */ }
Available on crate feature memory only.
Expand description

A conformant store whose positions advance by a stride, never by one.

Rejects: a read-model handler that computes its next position by adding one. Positions are an opaque ordering key and the specification permits gaps, but every store an application author has met assigns them densely, so the input separating a correct handler from an incorrect one never occurs in-process. This is that input, and it is conformant: it runs the full conformance suite, because gaps are a freedom VT-11 grants rather than a defect.

A real adapter reaches the same shape by allocating from a sequence with CACHE 7, by encoding a shard id in the low bits, or by using a transaction id as the position.

§Panics

append panics if the position space is exhausted — if previous + stride would overflow u64. Wrapping instead would break the strict monotonicity this store exists to demonstrate it keeps, and there is no error to report it through: Error is MemoryStoreError, which is uninhabited because this store has no failure modes of its own. Reaching it takes about 2^64 / stride appends.

§Examples

use core::num::NonZeroU64;
use happenstance_core::{AppendError, Event, EventStore};
use happenstance_core::MemoryStoreError;
use happenstance_testkit::{GappyMemoryStore, block_on};

let stride = NonZeroU64::new(7).ok_or("seven is not zero")?;
let store = GappyMemoryStore::with_stride(stride);
let seat = Event::new("Seated", &b"{}"[..])?;

let (first, second) = block_on(async {
    let first = store.append(&[seat.clone()], None).await?;
    let second = store.append(&[seat], None).await?;
    Ok::<_, AppendError<MemoryStoreError>>((first, second))
})?;

// Strictly increasing, and never the next integer.
assert!(second > first);
assert_ne!(second.get(), first.get() + 1);

Implementations§

§

impl GappyMemoryStore

pub fn with_stride(stride: NonZeroU64) -> Self

Creates an empty store whose positions advance by stride.

The first position assigned is stride itself, and each one after it is previous + stride. There is deliberately no Default and no implicit stride: a default here would be a number nobody chose, and the gap it left would be an accident rather than an instrument.

A stride of one is legal and makes the store dense, which is a caller asking for a plain in-memory store; anything above one leaves a hole between every pair of consecutive events.

pub const fn store_id(&self) -> StoreId

The incarnation this store mints identities under.

Trait Implementations§

§

impl Debug for GappyMemoryStore

§

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

Formats the value using the given formatter. Read more
§

impl SendEventStore for GappyMemoryStore

§

type Error = MemoryStoreError

How this adapter fails for its own reasons. Read more
§

fn read( &self, query: &Query, options: ReadOptions, ) -> impl Stream<Item = Result<SequencedEvent, Self::Error>> + Send

Reads the events matching query, in the order options asks for. Read more
§

async fn append( &self, events: &[Event], condition: Option<&AppendCondition>, ) -> Result<SequencePosition, AppendError<Self::Error>>

Atomically appends events, rejecting the write if condition matches. Read more
§

async fn head(&self) -> Result<Option<SequencePosition>, Self::Error>

The highest position currently visible in the store, or None when it holds nothing. Read more
§

async fn contains_event_id(&self, id: EventId) -> Result<bool, Self::Error>

Whether this store holds an event with id. 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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<TraitVariantBlanketType> EventStore for TraitVariantBlanketType
where TraitVariantBlanketType: SendEventStore,

Source§

type Error = <TraitVariantBlanketType as SendEventStore>::Error

How this adapter fails for its own reasons. Read more
Source§

fn read( &self, query: &Query, options: ReadOptions, ) -> impl Stream<Item = Result<SequencedEvent, <TraitVariantBlanketType as EventStore>::Error>>

Reads the events matching query, in the order options asks for. Read more
Source§

async fn append( &self, events: &[Event], condition: Option<&AppendCondition>, ) -> Result<SequencePosition, AppendError<<TraitVariantBlanketType as EventStore>::Error>>

Atomically appends events, rejecting the write if condition matches. Read more
Source§

async fn head( &self, ) -> Result<Option<SequencePosition>, <TraitVariantBlanketType as EventStore>::Error>

The highest position currently visible in the store, or None when it holds nothing. Read more
Source§

async fn contains_event_id( &self, id: EventId, ) -> Result<bool, <TraitVariantBlanketType as EventStore>::Error>

Whether this store holds an event with id. 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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V