pub struct EntityEvents<T: EsEvent, S: EsSnapshot = NoSnapshot> {
pub entity_id: <T as EsEvent>::EntityId,
/* private fields */
}Expand description
A Vec wrapper that manages event-stream of an entity with helpers for event-sourcing operations
Provides event sourcing operations for loading, appending, and persisting events in chronological sequence. Required field for all event-sourced entities to maintain their state change history.
Fields§
§entity_id: <T as EsEvent>::EntityIdThe entity’s id
Implementations§
Source§impl<T, S> EntityEvents<T, S>where
T: EsEvent,
S: EsSnapshot,
impl<T, S> EntityEvents<T, S>where
T: EsEvent,
S: EsSnapshot,
Sourcepub fn init(
id: <T as EsEvent>::EntityId,
initial_events: impl IntoIterator<Item = T>,
) -> Self
pub fn init( id: <T as EsEvent>::EntityId, initial_events: impl IntoIterator<Item = T>, ) -> Self
Initializes a new EntityEvents instance with the given entity ID and initial events which is returned by IntoEvents method
Sourcepub fn entity_first_persisted_at(&self) -> Option<DateTime<Utc>>
pub fn entity_first_persisted_at(&self) -> Option<DateTime<Utc>>
Returns the timestamp of the first persisted event, indicating when the entity was created
Sourcepub fn entity_last_modified_at(&self) -> Option<DateTime<Utc>>
pub fn entity_last_modified_at(&self) -> Option<DateTime<Utc>>
Returns the timestamp of the last persisted event, indicating when the entity was last modified
Sourcepub fn push(&mut self, event: T)
pub fn push(&mut self, event: T)
Appends a single new event to the entity’s event stream to be persisted later
Sourcepub fn extend(&mut self, events: impl IntoIterator<Item = T>)
pub fn extend(&mut self, events: impl IntoIterator<Item = T>)
Appends multiple new events to the entity’s event stream to be persisted later
Sourcepub fn any_new(&self) -> bool
pub fn any_new(&self) -> bool
Returns true if there are any unpersisted events waiting to be saved
Sourcepub fn len_persisted(&self) -> usize
pub fn len_persisted(&self) -> usize
Returns the count of persisted events, including those folded into the snapshot. This is the OCC offset used by every write path.
Sourcepub fn tail_len(&self) -> usize
pub fn tail_len(&self) -> usize
Returns the count of events after the snapshot: the persisted tail
plus any events staged for the write in flight. This is what
HeadSnapshot::capture() thresholds on.
Sourcepub fn snapshot(&self) -> Option<&SnapshotRecord<S>>
pub fn snapshot(&self) -> Option<&SnapshotRecord<S>>
Returns the loaded snapshot, if any.
Sourcepub fn last_persisted(&self, n: usize) -> LastPersisted<'_, T>
pub fn last_persisted(&self, n: usize) -> LastPersisted<'_, T>
Returns an iterator over the last n persisted events after the
snapshot, if any. This is what post-persist hooks receive.
Sourcepub fn replay(
&self,
) -> impl DoubleEndedIterator<Item = Replay<'_, T, S>> + Clone
pub fn replay( &self, ) -> impl DoubleEndedIterator<Item = Replay<'_, T, S>> + Clone
Returns an iterator over the full replay: the snapshot (if any), first, followed by the tail and any new events, in chronological order.
Sourcepub fn replay_persisted(
&self,
) -> impl DoubleEndedIterator<Item = Replay<'_, PersistedEvent<T>, SnapshotRecord<S>>> + Clone
pub fn replay_persisted( &self, ) -> impl DoubleEndedIterator<Item = Replay<'_, PersistedEvent<T>, SnapshotRecord<S>>> + Clone
Like replay but only over persisted state: the
snapshot record (if any) followed by the persisted tail. No new
events.
Sourcepub fn load_first<E>(
events: impl IntoIterator<Item = impl Into<HydrationRow<<T as EsEvent>::EntityId>>>,
) -> Result<Option<E>, EntityHydrationError>where
E: EsEntity<Event = T, Snapshot = S>,
pub fn load_first<E>(
events: impl IntoIterator<Item = impl Into<HydrationRow<<T as EsEvent>::EntityId>>>,
) -> Result<Option<E>, EntityHydrationError>where
E: EsEntity<Event = T, Snapshot = S>,
Loads and reconstructs the first entity from a stream of hydration
rows, marking events as persisted.
Returns Ok(None) if no events are present, Ok(Some(entity)) on success.
Sourcepub fn load_n<E>(
events: impl IntoIterator<Item = impl Into<HydrationRow<<T as EsEvent>::EntityId>>>,
n: usize,
) -> Result<(Vec<E>, bool), EntityHydrationError>where
E: EsEntity<Event = T, Snapshot = S>,
pub fn load_n<E>(
events: impl IntoIterator<Item = impl Into<HydrationRow<<T as EsEvent>::EntityId>>>,
n: usize,
) -> Result<(Vec<E>, bool), EntityHydrationError>where
E: EsEntity<Event = T, Snapshot = S>,
Loads and reconstructs up to n entities from a stream of hydration
rows. Assumes the rows are grouped by id and ordered by sequence
per id.
Returns both the entities and a flag indicating whether more entities were available in the stream.
Source§impl<T: EsEvent> EntityEvents<T, NoSnapshot>
impl<T: EsEvent> EntityEvents<T, NoSnapshot>
Sourcepub fn iter_persisted(
&self,
) -> impl DoubleEndedIterator<Item = &PersistedEvent<T>> + Clone
pub fn iter_persisted( &self, ) -> impl DoubleEndedIterator<Item = &PersistedEvent<T>> + Clone
Returns an iterator over all persisted events
Sourcepub fn iter_all(&self) -> impl DoubleEndedIterator<Item = &T> + Clone
pub fn iter_all(&self) -> impl DoubleEndedIterator<Item = &T> + Clone
Returns an iterator over all events (both persisted and new) in chronological order.
Only exists for EntityEvents<T, NoSnapshot> — a switch to a real
snapshot type is a compile error at every such scan, rather than a
silently-wrong fold that skips whatever the snapshot summarised:
use es_entity::*;
use serde::{Serialize, Deserialize};
es_entity::entity_id! { IterAllMeterId }
#[derive(EsEvent, Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[es_event(id = "IterAllMeterId")]
pub enum IterAllMeterEvent {
Initialized { id: IterAllMeterId },
}
#[derive(EsSnapshot, Debug, Clone, Serialize, Deserialize)]
#[es_snapshot(version = 1)]
pub struct IterAllMeterSnapshot {
pub id: IterAllMeterId,
}
fn count_all(events: &EntityEvents<IterAllMeterEvent, IterAllMeterSnapshot>) -> usize {
// error[E0599]: no method named `iter_all` on this type — it is
// only defined for `EntityEvents<T, NoSnapshot>`. Use `replay()`,
// which forces every match to account for `Replay::Snapshot`.
events.iter_all().count()
}Trait Implementations§
Auto Trait Implementations§
impl<T, S> Freeze for EntityEvents<T, S>where
<T as EsEvent>::EntityId: Freeze,
Option<SnapshotRecord<S>>: Freeze,
Vec<PersistedEvent<T>>: Freeze,
Vec<EventWithContext<T>>: Freeze,
impl<T, S> RefUnwindSafe for EntityEvents<T, S>where
<T as EsEvent>::EntityId: RefUnwindSafe,
Option<SnapshotRecord<S>>: RefUnwindSafe,
Vec<PersistedEvent<T>>: RefUnwindSafe,
Vec<EventWithContext<T>>: RefUnwindSafe,
impl<T, S> Send for EntityEvents<T, S>where
<T as EsEvent>::EntityId: Send,
Option<SnapshotRecord<S>>: Send,
Vec<PersistedEvent<T>>: Send,
Vec<EventWithContext<T>>: Send,
impl<T, S> Sync for EntityEvents<T, S>where
<T as EsEvent>::EntityId: Sync,
Option<SnapshotRecord<S>>: Sync,
Vec<PersistedEvent<T>>: Sync,
Vec<EventWithContext<T>>: Sync,
impl<T, S> Unpin for EntityEvents<T, S>where
<T as EsEvent>::EntityId: Unpin,
Option<SnapshotRecord<S>>: Unpin,
Vec<PersistedEvent<T>>: Unpin,
Vec<EventWithContext<T>>: Unpin,
impl<T, S> UnsafeUnpin for EntityEvents<T, S>where
<T as EsEvent>::EntityId: UnsafeUnpin,
Option<SnapshotRecord<S>>: UnsafeUnpin,
Vec<PersistedEvent<T>>: UnsafeUnpin,
Vec<EventWithContext<T>>: UnsafeUnpin,
impl<T, S> UnwindSafe for EntityEvents<T, S>where
<T as EsEvent>::EntityId: UnwindSafe,
Option<SnapshotRecord<S>>: UnwindSafe,
Vec<PersistedEvent<T>>: UnwindSafe,
Vec<EventWithContext<T>>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more