mecha10-core 0.6.2

Mecha10 core runtime and foundational types
Documentation
//! The `StateManager` trait: persistent state storage abstraction.

use super::{StateOptions, StateSnapshot, VersionedState};
use crate::error::Result;
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Serialize};

/// Persistent state storage abstraction
#[async_trait]
pub trait StateManager: Send + Sync {
    /// Save state for a node
    async fn save<T: Serialize + Send + Sync>(&self, node_id: &str, state: &T) -> Result<()>;

    /// Save state with options
    async fn save_with_options<T: Serialize + Send + Sync>(
        &self,
        node_id: &str,
        state: &T,
        options: StateOptions,
    ) -> Result<()>;

    /// Load state for a node
    async fn load<T: DeserializeOwned + Send + Sync>(&self, node_id: &str) -> Result<Option<T>>;

    /// Load versioned state with migration support
    async fn load_versioned<T: DeserializeOwned + Send + Sync>(
        &self,
        node_id: &str,
    ) -> Result<Option<VersionedState<T>>>;

    /// Delete state for a node
    async fn delete(&self, node_id: &str) -> Result<()>;

    /// Check if state exists for a node
    async fn exists(&self, node_id: &str) -> Result<bool>;

    /// List all node IDs with stored state
    async fn list_nodes(&self) -> Result<Vec<String>>;

    /// Get state size in bytes
    async fn size(&self, node_id: &str) -> Result<Option<usize>>;

    /// Create a snapshot of current state
    async fn create_snapshot(&self, node_id: &str, description: Option<String>) -> Result<StateSnapshot>;

    /// Restore from a snapshot
    async fn restore_snapshot(&self, snapshot_id: &str) -> Result<()>;

    /// List snapshots for a node
    async fn list_snapshots(&self, node_id: &str) -> Result<Vec<StateSnapshot>>;

    /// Delete a snapshot
    async fn delete_snapshot(&self, snapshot_id: &str) -> Result<()>;

    /// Atomic compare-and-swap operation
    async fn compare_and_swap<T: Serialize + DeserializeOwned + Send + Sync>(
        &self,
        node_id: &str,
        expected: &T,
        new: &T,
    ) -> Result<bool>;

    /// Clear all expired states (based on TTL)
    async fn clear_expired(&self) -> Result<usize>;
}