Skip to main content

actr_platform_traits/
storage.rs

1//! Platform-agnostic key-value storage trait
2
3use std::sync::Arc;
4
5use async_trait::async_trait;
6
7use crate::PlatformError;
8
9/// Batch operation for KV store
10pub enum KvOp {
11    Set { key: String, value: Vec<u8> },
12    Delete { key: String },
13}
14
15/// Platform-agnostic key-value store trait
16///
17/// Each Actor gets an isolated KV namespace. The backing implementation
18/// may be SQLite (native), IndexedDB (web), or any other storage engine.
19#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
20#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
21pub trait KvStore: Send + Sync {
22    /// Read a key's value, returns `None` if the key does not exist
23    async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, PlatformError>;
24
25    /// Write or update a key-value pair
26    async fn set(&self, key: &str, value: &[u8]) -> Result<(), PlatformError>;
27
28    /// Delete a key, returns whether a record was actually deleted
29    async fn delete(&self, key: &str) -> Result<bool, PlatformError>;
30
31    /// List all keys, optionally filtered by prefix
32    async fn list_keys(&self, prefix: Option<&str>) -> Result<Vec<String>, PlatformError>;
33
34    /// Batch operations (atomic transaction)
35    async fn batch(&self, ops: Vec<KvOp>) -> Result<(), PlatformError>;
36}
37
38/// Extension: obtain a type-erased clone of a KvStore
39pub trait KvStoreClone: KvStore {
40    fn clone_box(&self) -> Arc<dyn KvStore>;
41}