Skip to main content

mecha10_core/state/
manager_trait.rs

1//! The `StateManager` trait: persistent state storage abstraction.
2
3use 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    /// Save state for a node
11    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    /// Save state with options
16    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    /// Load state for a node
24    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    /// Load versioned state with migration support
29    async fn load_versioned<T: DeserializeOwned + Send + Sync>(
30        &self,
31        node_id: &str,
32    ) -> Result<Option<VersionedState<T>>>;
33
34    /// Delete state for a node
35    async fn delete(&self, node_id: &str) -> Result<()>;
36
37    /// Check if state exists for a node
38    async fn exists(&self, node_id: &str) -> Result<bool>;
39
40    /// List all node IDs with stored state
41    async fn list_nodes(&self) -> Result<Vec<String>>;
42
43    /// Get state size in bytes
44    async fn size(&self, node_id: &str) -> Result<Option<usize>>;
45
46    /// Create a snapshot of current state
47    async fn create_snapshot(&self, node_id: &str, description: Option<String>) -> Result<StateSnapshot>;
48
49    /// Restore from a snapshot
50    async fn restore_snapshot(&self, snapshot_id: &str) -> Result<()>;
51
52    /// List snapshots for a node
53    async fn list_snapshots(&self, node_id: &str) -> Result<Vec<StateSnapshot>>;
54
55    /// Delete a snapshot
56    async fn delete_snapshot(&self, snapshot_id: &str) -> Result<()>;
57
58    /// Atomic compare-and-swap operation
59    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    /// Clear all expired states (based on TTL)
67    async fn clear_expired(&self) -> Result<usize>;
68}