Skip to main content

MemoryIngestStore

Struct MemoryIngestStore 

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

An in-memory EventStore that is also an IngestStore.

It answers every event-store question as MemoryEventStore does over the same rows, and ingests as the port’s documentation says: foreign events at the tail with their origin identity and recorded time unchanged, a re-delivery skipped by identity alone, compensation minted here only for a group that landed something, and an unheld claim on this store’s own identity refusing the whole call (SY-38).

Send + Sync, with one RwLock over the log and the set of identities it holds, so the two never disagree. Every check runs before anything is written, and the write is one critical section.

Positions are dense and start at 1. The specification permits gaps, so nothing may depend on that.

§Examples

A foreign event lands with the identity and the time its origin gave it, including a time before 1970:

use happenstance_core::{
    Event, EventId, EventStore, Query, ReadOptions, RecordedAt, SequencePosition, StoreId,
    collect,
};
use happenstance_sync::{IngestGroup, IngestStore, MemoryIngestStore, ReplicatedEvent};

let store = MemoryIngestStore::new();
let origin = StoreId::from_bytes([7; 16]);
let shipped = ReplicatedEvent::new(
    EventId::new(origin, SequencePosition::new(4).ok_or("position zero")?),
    RecordedAt::from_millis(-86_400_000),
    Event::new("Shipped", &b"opaque"[..])?,
);

let ingested = store
    .ingest(&[IngestGroup::new(core::slice::from_ref(&shipped), &[])])
    .await?;
assert_eq!(ingested.appended, 1);

let log = collect(store.read(&Query::all(), ReadOptions::new())).await?;
let landed = log.first().ok_or("the log is empty")?;
assert_eq!(landed.id, shipped.id);
assert_eq!(landed.recorded_at, shipped.recorded_at);

Implementations§

§

impl MemoryIngestStore

pub fn new() -> Self

An empty store with a fresh incarnation identifier.

pub fn with_store_id(store_id: StoreId) -> Self

An empty store with a caller-chosen incarnation, as MemoryEventStore::with_store_id is.

pub const fn store_id(&self) -> StoreId

The incarnation this store mints identities under.

pub fn snapshot(&self) -> Vec<SequencedEvent>

Every event held, in position order.

Owned, because the lock is released before the caller looks at it.

Trait Implementations§

§

impl Debug for MemoryIngestStore

§

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

Formats the value using the given formatter. Read more
§

impl Default for MemoryIngestStore

§

fn default() -> Self

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

impl SendEventStore for MemoryIngestStore

§

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
§

impl SendIngestStore for MemoryIngestStore

§

async fn store_id(&self) -> Result<StoreId, Self::Error>

Never fails: the identity is held in memory.

§

type Error = MemoryStoreError

How this store fails, stating a class so a runner never infers one.
§

async fn ingest( &self, groups: &[IngestGroup<'_>], ) -> Result<Ingested, IngestError<Self::Error>>

Records events other stores minted, preserving their identity, together with any compensation this side writes for them. Read more
§

async fn watermark(&self) -> Result<Watermark, Self::Error>

The highest origin position this store holds from each origin store. Read more
§

impl SendWriteGroupStore for MemoryIngestStore

Writers are serialised under the lock, so a group’s rows are contiguous and a scan in position order meets each group whole, in ascending order of their first positions.

Its error is the one the shared GroupPageBuilder can raise, and the only one: the builder checks the order rather than trusting it, and this store states that check in its type rather than claiming it never fails.

§

type Error = GroupOrderError

How this store fails, stating a class so a runner never infers one.
§

async fn read_groups( &self, after: Option<SequencePosition>, max_events: NonZeroUsize, ) -> Result<GroupPage, Self::Error>

The write groups whose first position is above after (every group when None), in ascending first order, each whole. 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<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.

§

impl<TraitVariantBlanketType> IngestStore for TraitVariantBlanketType
where TraitVariantBlanketType: SendIngestStore,

§

type Error = <TraitVariantBlanketType as SendIngestStore>::Error

How this store fails, stating a class so a runner never infers one.
§

async fn store_id( &self, ) -> Result<StoreId, <TraitVariantBlanketType as IngestStore>::Error>

This store’s own incarnation identifier. Read more
§

async fn ingest( &self, groups: &[IngestGroup<'_>], ) -> Result<Ingested, IngestError<<TraitVariantBlanketType as IngestStore>::Error>>

Records events other stores minted, preserving their identity, together with any compensation this side writes for them. Read more
§

async fn watermark( &self, ) -> Result<Watermark, <TraitVariantBlanketType as IngestStore>::Error>

The highest origin position this store holds from each origin store. 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, 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<TraitVariantBlanketType> WriteGroupStore for TraitVariantBlanketType
where TraitVariantBlanketType: SendWriteGroupStore,

§

type Error = <TraitVariantBlanketType as SendWriteGroupStore>::Error

How this store fails, stating a class so a runner never infers one.
§

async fn read_groups( &self, after: Option<SequencePosition>, max_events: NonZero<usize>, ) -> Result<GroupPage, <TraitVariantBlanketType as WriteGroupStore>::Error>

The write groups whose first position is above after (every group when None), in ascending first order, each whole. Read more