Skip to main content

bpmn_engine/repository/
memory.rs

1//! Memory Repository
2//!
3//! In-memory implementation of the repository for process instances.
4//!
5//! This is the initial implementation that stores process instances in memory.
6//! Designed to be replaced with a database-backed implementation in the future.
7
8use crate::engine::instance::ProcessInstance;
9use crate::repository::{Repository, RepositoryError};
10use async_trait::async_trait;
11use std::collections::HashMap;
12use std::sync::Arc;
13use tokio::sync::RwLock;
14
15/// Memory Repository
16///
17/// In-memory storage for process instances.
18#[derive(Debug, Clone)]
19pub struct MemoryRepository {
20    instances: Arc<RwLock<HashMap<String, Arc<ProcessInstance>>>>,
21}
22
23impl MemoryRepository {
24    /// Create a new memory repository
25    pub fn new() -> Self {
26        Self {
27            instances: Arc::new(RwLock::new(HashMap::new())),
28        }
29    }
30}
31
32#[async_trait]
33impl Repository for MemoryRepository {
34    async fn save(&self, instance: Arc<ProcessInstance>) -> Result<(), RepositoryError> {
35        let mut instances = self.instances.write().await;
36        instances.insert(instance.id().to_string(), instance);
37        Ok(())
38    }
39
40    async fn get(&self, instance_id: &str) -> Result<Option<Arc<ProcessInstance>>, RepositoryError> {
41        let instances = self.instances.read().await;
42        Ok(instances.get(instance_id).cloned())
43    }
44
45    async fn delete(&self, instance_id: &str) -> Result<(), RepositoryError> {
46        let mut instances = self.instances.write().await;
47        instances.remove(instance_id);
48        Ok(())
49    }
50
51    async fn list_ids(&self) -> Result<Vec<String>, RepositoryError> {
52        let instances = self.instances.read().await;
53        Ok(instances.keys().cloned().collect())
54    }
55}
56
57impl Default for MemoryRepository {
58    fn default() -> Self {
59        Self::new()
60    }
61}
62