Skip to main content

Storage

Trait Storage 

Source
pub trait Storage:
    Send
    + Sync
    + 'static {
    // Required methods
    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 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 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 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;

    // Provided methods
    fn namespace(&self, _prefix: &str) -> Option<Result<Arc<dyn Storage>>> { ... }
    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 storage_type(&self) -> &str { ... }
    fn is_ephemeral(&self) -> bool { ... }
    fn is_namespaced(&self) -> bool { ... }
}
Expand description

A backend for storing and retrieving raw bytes under string keys (Storage).

Keys are opaque strings and implementations round-trip the bytes they are given unchanged. The shipped backends read / as a logical separator: they collapse slash runs, strip leading and trailing slashes and reject empty keys, backslashes and .. segments. Custom backends may use their own key scheme.

Two methods are optional capabilities: namespace and search return None when the backend does not implement them, and callers then fall back to a NamespacedStorage view or a client-side KeywordSearchStrategy scan.

use std::collections::BTreeMap;
use std::sync::Mutex;

use strands::storage::Storage;

#[derive(Default)]
struct MapStorage(Mutex<BTreeMap<String, Vec<u8>>>);

#[strands::async_trait]
impl Storage for MapStorage {
    async fn write(&self, key: &str, data: &[u8]) -> strands::Result<()> {
        self.0.lock().unwrap().insert(key.to_owned(), data.to_vec());
        Ok(())
    }

    async fn read(&self, key: &str) -> strands::Result<Option<Vec<u8>>> {
        Ok(self.0.lock().unwrap().get(key).cloned())
    }

    async fn delete(&self, key: &str) -> strands::Result<()> {
        self.0.lock().unwrap().remove(key);
        Ok(())
    }

    async fn list(&self, prefix: &str) -> strands::Result<Vec<String>> {
        let map = self.0.lock().unwrap();
        Ok(map.keys().filter(|key| key.starts_with(prefix)).cloned().collect())
    }
}

assert_eq!(MapStorage::default().storage_type(), "MapStorage");

Required Methods§

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,

Stores data under key, overwriting any existing value.

§Errors

Returns a StorageError when the key is invalid or the write fails.

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,

The bytes stored under key, or None when no value exists.

§Errors

Returns a StorageError when the key is invalid or the read fails for a reason other than a missing key.

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,

Deletes the value stored under key. A no-op when the key does not exist.

§Errors

Returns a StorageError when the key is invalid or the delete fails.

Source

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,

The full keys that start with prefix, sorted ascending. An empty prefix lists every key.

§Errors

Returns a StorageError when the prefix is invalid or the listing fails.

Provided Methods§

Source

fn namespace(&self, _prefix: &str) -> Option<Result<Arc<dyn Storage>>>

A view of this storage with every key prefixed by prefix (optional namespace).

None means the backend has no namespacing of its own; wrap it in a NamespacedStorage instead. The shipped backends return an instance of their own type.

Source

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,

Searches stored content for query, best match first (optional search).

None means the backend does not search; callers fall back to a client-side scan such as KeywordSearchStrategy. How a query is interpreted (keyword scan, full-text index, vector similarity) is up to the backend.

Source

fn storage_type(&self) -> &str

The backend’s type name, recorded as the archive storageType.

Defaults to the implementing type’s name without its module path or generics. That name comes from std::any::type_name, whose output Rust does not guarantee across compiler versions, so a custom backend whose archive may be restored by another build or SDK should override it with a fixed name.

Source

fn is_ephemeral(&self) -> bool

Whether stored data is lost when the process exits.

Source

fn is_namespaced(&self) -> bool

Whether this is already a namespaced view, so constructs that scope storage under a default prefix use it as it is.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§