Skip to main content

SearchStrategy

Trait SearchStrategy 

Source
pub trait SearchStrategy:
    Send
    + Sync
    + 'static {
    // Required method
    fn search<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        storage: &'life1 dyn Storage,
        query: &'life2 str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<StorageSearchResult>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;

    // Provided methods
    fn index<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        _storage: &'life1 dyn Storage,
        _key: &'life2 str,
        _data: &'life3 [u8],
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait { ... }
    fn requires_host_fs(&self) -> bool { ... }
    fn name(&self) -> &str { ... }
}
Expand description

A pluggable way to search a storage backend (SearchStrategy).

Backends configured with a strategy call index on every write and delegate Storage::search to search.

use strands::storage::search::{SearchStrategy, StorageSearchResult};
use strands::storage::{InMemoryStorage, InMemoryStorageConfig, Storage};
use std::sync::Arc;

struct PrefixMatch;

#[strands::async_trait]
impl SearchStrategy for PrefixMatch {
    async fn search(&self, storage: &dyn Storage, query: &str) -> strands::Result<Vec<StorageSearchResult>> {
        let keys = storage.list(query).await?;
        Ok(keys.into_iter().map(|key| StorageSearchResult::new(key, 1.0)).collect())
    }
}

let config = InMemoryStorageConfig::builder().search_strategy(Arc::new(PrefixMatch)).build();
let storage = InMemoryStorage::with_config(config);
storage.write("notes/a.md", b"x").await?;
assert_eq!(storage.search("notes/").await?[0].key, "notes/a.md");

Required Methods§

Source

fn search<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, storage: &'life1 dyn Storage, query: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<StorageSearchResult>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Searches storage for query, best match first.

§Errors

Returns an error when the search fails.

Provided Methods§

Source

fn index<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _storage: &'life1 dyn Storage, _key: &'life2 str, _data: &'life3 [u8], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Indexes one entry for later searches; called on each write. Strategies that search on the fly (keyword) or manage their own index keep the default no-op.

key is relative to storage. Namespaced instances of a backend share its strategy and pass keys relative to their own prefix, so two entries can arrive with the same key and one entry can arrive under different keys. A strategy that keeps its own index should key it by where the entry lives (such as LocalFileStorage::base_dir plus the key), or use a separate storage per index.

§Errors

An error fails the write that triggered it.

Source

fn requires_host_fs(&self) -> bool

Whether the strategy reads or writes the host filesystem itself, so it cannot work on storage routed through a sandbox. false by default.

Source

fn name(&self) -> &str

The strategy’s name for error messages.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§