Skip to main content

EventLog

Trait EventLog 

Source
pub trait EventLog {
    // Required methods
    fn append_event(&mut self, event: MemoryEvent) -> ThingdResult<MemoryEvent>;
    fn list_events(
        &self,
        stream: Option<&str>,
        options: ListEventsOptions,
    ) -> ThingdResult<Vec<MemoryEvent>>;
    fn count_events(&self) -> ThingdResult<u64>;
    fn list_streams(&self) -> ThingdResult<Vec<String>>;

    // Provided methods
    fn is_protected_stream(&self, stream: &str) -> bool { ... }
    fn append_events_batch(
        &mut self,
        events: Vec<MemoryEvent>,
    ) -> ThingdResult<Vec<MemoryEvent>> { ... }
    fn delete_last_event(
        &mut self,
        stream: &str,
    ) -> ThingdResult<Option<MemoryEvent>> { ... }
    fn delete_stream(&mut self, stream: &str) -> ThingdResult<u64> { ... }
}
Expand description

Append-only event log operations.

§Examples

use thingd::{MemoryEngine, EventLog, MemoryEvent, ListEventsOptions};

let mut store = MemoryEngine::new();
let event = MemoryEvent::new("audit", "user.created", r#"{"user":"alice"}"#);
store.append_event(event).unwrap();

let events = store.list_events(None, ListEventsOptions::default()).unwrap();
assert_eq!(events.len(), 1);
assert_eq!(events[0].event_type, "user.created");

Required Methods§

Source

fn append_event(&mut self, event: MemoryEvent) -> ThingdResult<MemoryEvent>

Append an event to a stream.

§Errors

Returns an error when the backing store cannot append the event.

Source

fn list_events( &self, stream: Option<&str>, options: ListEventsOptions, ) -> ThingdResult<Vec<MemoryEvent>>

List events, optionally filtered by stream, with pagination.

Events are returned in ascending sequence order (oldest first).

§Errors

Returns an error when the backing store cannot read events.

Source

fn count_events(&self) -> ThingdResult<u64>

Count total events across all streams.

§Errors

Returns an error when the backing store cannot count events.

Source

fn list_streams(&self) -> ThingdResult<Vec<String>>

List all unique stream names.

§Errors

Returns an error when the backing store cannot list streams.

Provided Methods§

Source

fn is_protected_stream(&self, stream: &str) -> bool

Returns true if the stream is protected from deletion and external mutation. Protected streams (e.g. "__thingd:mcp:audit") reject delete_last_event and delete_stream calls.

Source

fn append_events_batch( &mut self, events: Vec<MemoryEvent>, ) -> ThingdResult<Vec<MemoryEvent>>

Append multiple events to a stream in a single transaction.

This is significantly faster than calling append_event in a loop.

§Errors

Returns an error when the backing store cannot append any event.

Source

fn delete_last_event( &mut self, stream: &str, ) -> ThingdResult<Option<MemoryEvent>>

Delete the most recent event from a stream.

Returns the deleted event, or None if the stream was empty or did not exist. This is useful for implementing undo patterns in event-sourced applications.

Returns ThingdError::Protected when the stream is protected.

§Errors

Returns an error when the backing store cannot delete the event.

Source

fn delete_stream(&mut self, stream: &str) -> ThingdResult<u64>

Delete all events in a stream.

Returns the number of events deleted. This is useful for cleaning up completed or expired event streams (e.g. finished game matches).

Returns ThingdError::Protected when the stream is protected.

§Errors

Returns an error when the backing store cannot delete the events.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl EventLog for MemoryEngine

Source§

impl EventLog for SqliteThingStore

Available on crate feature sqlite only.