bothan_lib/store/
worker.rs

1use crate::store::Store;
2use crate::types::AssetInfo;
3
4/// A store wrapper for worker-specific operations with namespace isolation.
5///
6/// `WorkerStore` provides a simplified interface for workers to interact with the main store,
7/// automatically applying a consistent namespace prefix to all operations. This ensures that
8/// different workers can operate on isolated sections of the store without conflicts.
9#[derive(Clone)]
10pub struct WorkerStore<S: Store> {
11    store: S,
12    prefix: String,
13}
14
15impl<S: Store> WorkerStore<S> {
16    /// Creates a new WorkerStore with the specified store and unique prefix key.
17    ///
18    /// The prefix serves as a namespace that is automatically applied to all store operations,
19    /// isolating this worker's data from other parts of the system.
20    pub fn new<T: Into<String>>(store: &S, prefix: T) -> Self {
21        Self {
22            store: store.clone(),
23            prefix: prefix.into(),
24        }
25    }
26
27    /// Sets the asset information for a specific asset.
28    ///
29    /// This method automatically applies the worker's namespace prefix when
30    /// storing the asset information in the underlying store.
31    ///
32    /// # Errors
33    ///
34    /// Returns the underlying store's error type if the operation fails
35    pub async fn set_asset_info(&self, asset: AssetInfo) -> Result<(), S::Error> {
36        self.store.insert_asset_info(&self.prefix, asset).await
37    }
38
39    /// Sets multiple asset information entries in a single batch operation.
40    ///
41    /// This method provides an optimized way to store multiple asset entries at once,
42    /// while automatically applying the worker's namespace prefix.
43    ///
44    /// # Errors
45    ///
46    /// Returns the underlying store's error type if the operation fails
47    pub async fn set_batch_asset_info(&self, assets: Vec<AssetInfo>) -> Result<(), S::Error> {
48        self.store
49            .insert_batch_asset_info(&self.prefix, assets)
50            .await
51    }
52}