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
impl MemoriesAdapter
Sourcepub async fn open(
redex: &Redex,
origin_hash: u64,
) -> Result<MemoriesAdapter, CortexAdapterError>
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.
Sourcepub async fn open_with_config(
redex: &Redex,
origin_hash: u64,
redex_config: RedexFileConfig,
) -> Result<MemoriesAdapter, CortexAdapterError>
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.
Sourcepub fn store(
&self,
id: u64,
content: impl Into<String>,
tags: impl IntoIterator<Item = String>,
source: impl Into<String>,
now_ns: u64,
) -> Result<u64, CortexAdapterError>
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.
Sourcepub fn retag(
&self,
id: u64,
tags: impl IntoIterator<Item = String>,
now_ns: u64,
) -> Result<u64, CortexAdapterError>
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.
Sourcepub fn state(&self) -> Arc<RwLock<RawRwLock, MemoriesState>> ⓘ
pub fn state(&self) -> Arc<RwLock<RawRwLock, MemoriesState>> ⓘ
Read-only access to the materialized state.
Sourcepub async fn wait_for_seq(&self, seq: u64) -> Result<(), Option<u64>>
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.
Sourcepub async fn wait_for_token(
&self,
token: WriteToken,
deadline: Duration,
) -> Result<(), WaitForTokenError>
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.
Sourcepub fn poll_for_token(&self, token: WriteToken) -> Result<(), WaitForTokenError>
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.
Sourcepub fn close(&self) -> Result<(), CortexAdapterError>
pub fn close(&self) -> Result<(), CortexAdapterError>
Close the adapter. See CortexAdapter::close.
Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
True if the fold task is currently running.
Sourcepub fn as_cortex(&self) -> &CortexAdapter<MemoriesState>
pub fn as_cortex(&self) -> &CortexAdapter<MemoriesState>
Access the wrapped CortexAdapter for cases that need the
lower-level surface.
Sourcepub fn origin_hash(&self) -> u64
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.
Sourcepub fn watch(&self) -> MemoriesWatcher
pub fn watch(&self) -> MemoriesWatcher
Start building a reactive watcher.
Sourcepub fn snapshot_and_watch(
&self,
watcher: MemoriesWatcher,
) -> (Vec<Arc<Memory>>, Pin<Box<dyn Stream<Item = Vec<Arc<Memory>>> + Send>>)
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.
Sourcepub fn snapshot(&self) -> Result<(Vec<u8>, Option<u64>), CortexAdapterError>
pub fn snapshot(&self) -> Result<(Vec<u8>, Option<u64>), CortexAdapterError>
Capture a snapshot suitable for restore. Returns
(state_bytes, last_seq) — persist both together.
Sourcepub async fn open_from_snapshot(
redex: &Redex,
origin_hash: u64,
state_bytes: &[u8],
last_seq: Option<u64>,
) -> Result<MemoriesAdapter, CortexAdapterError>
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.
Sourcepub 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>
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.