Skip to main content

ClawTransaction

Struct ClawTransaction 

Source
pub struct ClawTransaction<'c> { /* private fields */ }
Expand description

A wrapper around a SQLite transaction with deferred cache operations.

Obtain a ClawTransaction via ClawTransaction::begin or ClawEngine::transaction. Call ClawTransaction::commit to persist changes or allow the value to be dropped to trigger an implicit rollback.

Cache mutations that should be applied after a successful commit can be registered with ClawTransaction::stage and retrieved afterwards with ClawTransaction::pending_cache_ops.

§Example

let mut tx = engine.transaction().await?;
let r = MemoryRecord::new("hello", MemoryType::Semantic, vec![], None);
tx.insert_memory(&r).await?;
tx.commit().await?;

Implementations§

Source§

impl<'c> ClawTransaction<'c>

Source

pub async fn begin(engine: &'c ClawEngine) -> ClawResult<ClawTransaction<'c>>

Begin a new transaction against the engine’s connection pool.

§Errors

Returns ClawError::Transaction if the underlying pool cannot start a new transaction.

Source

pub fn stage(&mut self, op: CacheOp)

Stage a CacheOp to be applied to an in-memory cache after a successful commit.

Staged operations are available via ClawTransaction::pending_cache_ops.

Source

pub fn pending_cache_ops(&self) -> &[CacheOp]

Return a slice of the cache operations staged so far.

Source

pub async fn commit(self) -> ClawResult<()>

Commit the transaction, making all changes permanent.

All operations staged via ClawTransaction::insert_memory are written to the database — including their FTS5 and tag-index rows — inside the open transaction before the final commit, guaranteeing atomicity.

§Errors

Returns ClawError::Transaction if the commit fails.

Source

pub async fn rollback(self) -> ClawResult<()>

Explicitly roll back the transaction, discarding all changes.

All operations in the staging buffer are discarded without any database writes. Any writes made directly via ClawTransaction::inner_mut are rolled back by the underlying SQLite transaction.

§Errors

Returns ClawError::Transaction if the rollback fails.

Source

pub fn inner_mut(&mut self) -> &mut Transaction<'c, Sqlite>

Return a mutable reference to the inner SQLx transaction for use with raw SQLx queries.

Source

pub async fn insert_memory(&mut self, record: &MemoryRecord) -> ClawResult<Uuid>

Insert a crate::store::memory::MemoryRecord within this transaction.

Both the memories table and the memories_fts FTS5 index are updated atomically as part of the same transaction.

§Errors

Returns a ClawError if the SQL execution fails or serialization fails.

§Example
let mut tx = engine.transaction().await?;
let r = MemoryRecord::new("transactional", MemoryType::Episodic, vec![], None);
let id = tx.insert_memory(&r).await?;
tx.commit().await?;

Trait Implementations§

Source§

impl<'c> Debug for ClawTransaction<'c>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'c> Freeze for ClawTransaction<'c>

§

impl<'c> !RefUnwindSafe for ClawTransaction<'c>

§

impl<'c> Send for ClawTransaction<'c>

§

impl<'c> Sync for ClawTransaction<'c>

§

impl<'c> Unpin for ClawTransaction<'c>

§

impl<'c> UnsafeUnpin for ClawTransaction<'c>

§

impl<'c> !UnwindSafe for ClawTransaction<'c>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

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

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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