bpmn_engine/repository/
memory.rs1use 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#[derive(Debug, Clone)]
19pub struct MemoryRepository {
20 instances: Arc<RwLock<HashMap<String, Arc<ProcessInstance>>>>,
21}
22
23impl MemoryRepository {
24 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