pub struct LocalFileStorage { /* private fields */ }Expand description
Storage that keeps each key as a file under a base directory (LocalFileStorage).
The key’s / segments map onto directories: key a/b.json is the file <base_dir>/a/b.json.
Writes are atomic: the bytes go to a scratch sibling that is then renamed over the target, so
a crash mid-write never leaves a partial file. Missing directories are created on write. Bound
to a Sandbox (see for_sandbox), all I/O goes through the sandbox
instead, and atomicity is the sandbox’s.
The harness keeps its sessions in LocalFileStorage::new("./.agent").namespace("sessions"),
the on-disk layout shared with the other Strands harness SDKs.
use strands::storage::{LocalFileStorage, Storage};
let sessions = LocalFileStorage::new(&root).namespace("sessions")?;
sessions.write("abc/scopes/agent/default/snapshots/snapshot_latest.json", b"{}").await?;
assert!(root.join("sessions/abc/scopes/agent/default/snapshots/snapshot_latest.json").is_file());Implementations§
Source§impl LocalFileStorage
impl LocalFileStorage
Sourcepub fn new(base_dir: impl AsRef<Path>) -> Self
pub fn new(base_dir: impl AsRef<Path>) -> Self
Storage rooted at base_dir, using the default keyword search.
Sourcepub fn with_config(
base_dir: impl AsRef<Path>,
config: LocalFileStorageConfig,
) -> Result<Self>
pub fn with_config( base_dir: impl AsRef<Path>, config: LocalFileStorageConfig, ) -> Result<Self>
Storage rooted at base_dir, with options.
§Errors
When config combines a sandbox with a search strategy that requires host filesystem
access.
Sourcepub fn for_sandbox(&self, sandbox: Arc<dyn Sandbox>) -> Self
pub fn for_sandbox(&self, sandbox: Arc<dyn Sandbox>) -> Self
A storage with the same base directory whose I/O goes through sandbox.
Storage already bound to a sandbox comes back unchanged. A search strategy that requires host filesystem access is dropped with a warning, falling back to the keyword scan, which reads through the sandbox.
Sourcepub fn base_dir(&self) -> &Path
pub fn base_dir(&self) -> &Path
The root directory of this storage, with repeated and trailing separators removed.
Sourcepub fn namespace(&self, prefix: &str) -> Result<Self>
pub fn namespace(&self, prefix: &str) -> Result<Self>
A storage rooted at the prefix subdirectory, sharing this storage’s search strategy.
§Errors
Returns a StorageError when the prefix contains a backslash, a .. segment, or a
segment the platform does not read as one plain name (a Windows drive prefix).
Sourcepub async fn search(&self, query: &str) -> Result<Vec<StorageSearchResult>>
pub async fn search(&self, query: &str) -> Result<Vec<StorageSearchResult>>
Searches the stored files with the configured strategy or the default keyword scan.
§Errors
Returns the strategy’s error, or a StorageError when the keyword scan cannot list.
Trait Implementations§
Source§impl Clone for LocalFileStorage
impl Clone for LocalFileStorage
Source§impl Debug for LocalFileStorage
impl Debug for LocalFileStorage
Source§impl Storage for LocalFileStorage
impl Storage for LocalFileStorage
Source§fn write<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 str,
data: &'life2 [u8],
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn write<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 str,
data: &'life2 [u8],
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Source§fn read<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn read<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn delete<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn delete<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
key. A no-op when the key does not exist. Read moreSource§fn list<'life0, 'life1, 'async_trait>(
&'life0 self,
prefix: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Vec<String>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn list<'life0, 'life1, 'async_trait>(
&'life0 self,
prefix: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Vec<String>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
prefix, sorted ascending. An empty prefix lists every key. Read moreSource§fn search<'life0, 'life1, 'async_trait>(
&'life0 self,
query: &'life1 str,
) -> Pin<Box<dyn Future<Output = Option<Result<Vec<StorageSearchResult>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn search<'life0, 'life1, 'async_trait>(
&'life0 self,
query: &'life1 str,
) -> Pin<Box<dyn Future<Output = Option<Result<Vec<StorageSearchResult>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn storage_type(&self) -> &str
fn storage_type(&self) -> &str
storageType. Read moreSource§fn is_namespaced(&self) -> bool
fn is_namespaced(&self) -> bool
Source§fn is_ephemeral(&self) -> bool
fn is_ephemeral(&self) -> bool
Auto Trait Implementations§
impl !RefUnwindSafe for LocalFileStorage
impl !UnwindSafe for LocalFileStorage
impl Freeze for LocalFileStorage
impl Send for LocalFileStorage
impl Sync for LocalFileStorage
impl Unpin for LocalFileStorage
impl UnsafeUnpin for LocalFileStorage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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