mecha10_core/state/
manager_trait.rs1use super::{StateOptions, StateSnapshot, VersionedState};
4use crate::error::Result;
5use async_trait::async_trait;
6use serde::{de::DeserializeOwned, Serialize};
7
8#[async_trait]
9pub trait StateManager: Send + Sync {
10 async fn save<T: Serialize + Send + Sync>(&self, node_id: &str, state: &T) -> Result<()> {
12 self.save_with_options(node_id, state, StateOptions::default()).await
13 }
14
15 async fn save_with_options<T: Serialize + Send + Sync>(
17 &self,
18 node_id: &str,
19 state: &T,
20 options: StateOptions,
21 ) -> Result<()>;
22
23 async fn load<T: DeserializeOwned + Send + Sync>(&self, node_id: &str) -> Result<Option<T>> {
25 Ok(self.load_versioned::<T>(node_id).await?.map(|versioned| versioned.data))
26 }
27
28 async fn load_versioned<T: DeserializeOwned + Send + Sync>(
30 &self,
31 node_id: &str,
32 ) -> Result<Option<VersionedState<T>>>;
33
34 async fn delete(&self, node_id: &str) -> Result<()>;
36
37 async fn exists(&self, node_id: &str) -> Result<bool>;
39
40 async fn list_nodes(&self) -> Result<Vec<String>>;
42
43 async fn size(&self, node_id: &str) -> Result<Option<usize>>;
45
46 async fn create_snapshot(&self, node_id: &str, description: Option<String>) -> Result<StateSnapshot>;
48
49 async fn restore_snapshot(&self, snapshot_id: &str) -> Result<()>;
51
52 async fn list_snapshots(&self, node_id: &str) -> Result<Vec<StateSnapshot>>;
54
55 async fn delete_snapshot(&self, snapshot_id: &str) -> Result<()>;
57
58 async fn compare_and_swap<T: Serialize + DeserializeOwned + Send + Sync>(
60 &self,
61 node_id: &str,
62 expected: &T,
63 new: &T,
64 ) -> Result<bool>;
65
66 async fn clear_expired(&self) -> Result<usize>;
68}