Skip to main content

FilePersistenceBackend

Struct FilePersistenceBackend 

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

Persists data as files in a directory on disk.

Each key maps to a file named <key>.bin inside the base directory. The base directory must exist before calling any methods.

§Key Sanitization

Characters that are invalid in file names on common operating systems (/, \, :, *, ?, ", <, >, |) are replaced with _ before the path is constructed. This prevents path-traversal attacks and ensures portability across Linux, macOS, and Windows.

§Concurrency

FilePersistenceBackend is Clone and Send + Sync. Each clone shares the same base_dir via Arc and can be used from multiple async tasks simultaneously.

Writes are atomic at the OS level: data is written to a uniquely-named temporary file in the same directory and then renamed into place, so a reader never observes a half-written file even if the process crashes. Concurrent writes to the same key from multiple tasks are safe but are not serialized — the final file content is determined by whichever rename completes last.

Implementations§

Source§

impl FilePersistenceBackend

Source

pub fn new(base_dir: impl Into<PathBuf>) -> Self

Create a new backend that stores files in base_dir.

The directory is not created automatically — it must already exist.

Source

pub fn base_dir(&self) -> &Path

Return the base directory that this backend stores files in.

Source§

impl FilePersistenceBackend

Source

pub async fn list_keys(&self) -> Result<Vec<String>, AgentRuntimeError>

List all keys currently stored in the backend.

Returns the readable prefix portion of stored filenames (the part before the hash suffix). Useful for backup/restore tooling and debugging.

Source

pub async fn exists(&self, key: &str) -> Result<bool, AgentRuntimeError>

Check whether a key exists in the backend without loading its value.

Returns Ok(true) if the file for key is present in the store.

Source

pub async fn key_count(&self) -> Result<usize, AgentRuntimeError>

Return the number of keys currently stored in the backend.

More efficient than list_keys().await?.len() for callers that only need the count, as it avoids building the string list.

Trait Implementations§

Source§

impl Clone for FilePersistenceBackend

Source§

fn clone(&self) -> FilePersistenceBackend

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FilePersistenceBackend

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PersistenceBackend for FilePersistenceBackend

Source§

fn batch_save<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, items: &'life1 [(&'life2 str, &'life3 [u8])], ) -> Pin<Box<dyn Future<Output = Result<(), AgentRuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Save multiple key-value pairs concurrently.

All writes are issued simultaneously using try_join_all instead of the default sequential loop, so throughput scales with disk/OS concurrency.

Source§

fn batch_load<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = Result<Vec<Option<Vec<u8>>>, AgentRuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Load multiple keys concurrently.

All reads are issued simultaneously using try_join_all. Returns results in the same order as keys.

Source§

fn save<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, value: &'life2 [u8], ) -> Pin<Box<dyn Future<Output = Result<(), AgentRuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Persist raw bytes under the given key.
Source§

fn load<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>, AgentRuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Load raw bytes for the given key. Read more
Source§

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), AgentRuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete the entry for the given key. No-op if the key does not exist.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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