Skip to main content

MemoriesAdapter

Struct MemoriesAdapter 

Source
pub struct MemoriesAdapter { /* private fields */ }
Expand description

Typed wrapper around CortexAdapter<MemoriesState> that exposes domain-level operations (store, retag, pin, unpin, delete) and hides the EventMeta + postcard plumbing.

Implementations§

Source§

impl MemoriesAdapter

Source

pub async fn open( redex: &Redex, origin_hash: u64, ) -> Result<MemoriesAdapter, CortexAdapterError>

Open the memories adapter against a Redex manager.

Uses MEMORIES_CHANNEL ("cortex/memories"). Replays the full history into state on open.

async because the constructor awaits the fold task’s catch-up before returning: the inner WatermarkingFold observes every replayed event’s EventMeta and advances app_seq past any pre-existing same-origin seq_or_ts, so the first ingest_typed after open cannot collide with an already-stored event THIS ADAPTER caused.

§Single-origin invariant

app_seq is initialized by reading file.next_seq() and awaiting wait_for_seq(next_seq - 1). If another writer — typically a sibling adapter under the same origin_hash running in a different process or a crashed daemon resurrecting its old keypair — appends events between next_seq() and the user’s first ingest_typed, those events are NOT reflected in our app_seq and the next stamped seq_or_ts can collide with them.

The bus assumes exactly one writer per origin_hash per channel. Splitting writers across processes / runtimes for the same origin breaks the no-collision guarantee. Either:

  • Use distinct origins per writer (typical: derive from a unique EntityKeypair).
  • Coordinate writers out-of-band so only one is active at a time.

The pre-fix doc said “the first ingest_typed after open cannot collide with an already-stored event” full-stop; the qualifier “THIS ADAPTER caused” is the correction.

Source

pub async fn open_with_config( redex: &Redex, origin_hash: u64, redex_config: RedexFileConfig, ) -> Result<MemoriesAdapter, CortexAdapterError>

Like Self::open but with a caller-supplied RedexFileConfig.

Source

pub fn store( &self, id: u64, content: impl Into<String>, tags: impl IntoIterator<Item = String>, source: impl Into<String>, now_ns: u64, ) -> Result<u64, CortexAdapterError>

Store or update a memory.

Upsert semantics: if id is unknown, a new memory is inserted with pinned: false and created_ns = now_ns. If id already exists, the entry’s content, tags, and source are overwritten and updated_ns advances to now_ns, but the existing pinned flag and created_ns timestamp are preserved — store will not silently un-pin an entry or rewrite its creation time. Use Self::pin / Self::unpin to change the pin explicitly.

Returns the RedEX seq of the append.

Source

pub fn retag( &self, id: u64, tags: impl IntoIterator<Item = String>, now_ns: u64, ) -> Result<u64, CortexAdapterError>

Replace the tag set on an existing memory. No-op at fold time if id is unknown.

Source

pub fn pin(&self, id: u64, now_ns: u64) -> Result<u64, CortexAdapterError>

Pin a memory.

Source

pub fn unpin(&self, id: u64, now_ns: u64) -> Result<u64, CortexAdapterError>

Unpin a memory.

Source

pub fn delete(&self, id: u64) -> Result<u64, CortexAdapterError>

Delete a memory.

Source

pub fn state(&self) -> Arc<RwLock<RawRwLock, MemoriesState>>

Read-only access to the materialized state.

Source

pub fn count(&self) -> usize

Total memory count in the current state.

Source

pub async fn wait_for_seq(&self, seq: u64) -> Result<(), Option<u64>>

Block until every event up through seq has been folded. Returns Err(folded) if the fold task stopped before reaching seq; see CortexAdapter::wait_for_seq for the stop-vs-success rationale.

Source

pub async fn wait_for_token( &self, token: WriteToken, deadline: Duration, ) -> Result<(), WaitForTokenError>

Block until the fold task has processed every event up through token.seq, or deadline elapses. Read-your-writes wait scoped to this adapter’s origin — see the matching TasksAdapter::wait_for_token for the rationale on the origin guard.

Source

pub fn poll_for_token(&self, token: WriteToken) -> Result<(), WaitForTokenError>

Non-blocking RYW poll. See super::super::tasks::TasksAdapter::poll_for_token for the full contract — identical shape for Memories.

Source

pub fn close(&self) -> Result<(), CortexAdapterError>

Close the adapter. See CortexAdapter::close.

Source

pub fn is_running(&self) -> bool

True if the fold task is currently running.

Source

pub fn as_cortex(&self) -> &CortexAdapter<MemoriesState>

Access the wrapped CortexAdapter for cases that need the lower-level surface.

Source

pub fn origin_hash(&self) -> u64

Origin hash this adapter is bound to. Stamped on every outgoing EventMeta; tokens with a different origin reject at wait_for_token.

Source

pub fn watch(&self) -> MemoriesWatcher

Start building a reactive watcher.

Source

pub fn snapshot_and_watch( &self, watcher: MemoriesWatcher, ) -> (Vec<Arc<Memory>>, Pin<Box<dyn Stream<Item = Vec<Arc<Memory>>> + Send>>)

One-shot combo: a snapshot of the current filter result PLUS a stream that emits every subsequent change to that filter. The stream skips the initial emission so the caller doesn’t see the snapshot twice — the snapshot is the initial state; the stream carries deltas from there forward.

Source

pub fn snapshot(&self) -> Result<(Vec<u8>, Option<u64>), CortexAdapterError>

Capture a snapshot suitable for restore. Returns (state_bytes, last_seq) — persist both together.

Source

pub async fn open_from_snapshot( redex: &Redex, origin_hash: u64, state_bytes: &[u8], last_seq: Option<u64>, ) -> Result<MemoriesAdapter, CortexAdapterError>

Open the memories adapter from a snapshot.

See Self::open for why this is async.

Source

pub async fn open_from_snapshot_with_config( redex: &Redex, origin_hash: u64, redex_config: RedexFileConfig, state_bytes: &[u8], last_seq: Option<u64>, ) -> Result<MemoriesAdapter, CortexAdapterError>

Like Self::open_from_snapshot but with a caller-supplied RedexFileConfig.

Trait Implementations§

Source§

impl Debug for MemoriesAdapter

Source§

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

Formats the value using the given formatter. 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.

Source§

impl<T> Instrument for T

Source§

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

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

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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