drasi_source_sdk/
memory_statestore.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::{collections::HashMap, sync::Arc};

use axum::async_trait;
use tokio::sync::Mutex;

use crate::StateStore;


pub struct MemoryStateStore {
    data: Arc<Mutex<HashMap<String, Vec<u8>>>>,
}

impl MemoryStateStore {
    pub fn new() -> Self {
        MemoryStateStore {
            data: Arc::new(Mutex::new(HashMap::new())),
        }
    }
}

#[async_trait]
impl StateStore for MemoryStateStore {
    async fn get(&self, id: &str) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error + Send + Sync>> {
        let data = self.data.lock().await;
        Ok(data.get(id).cloned())
    }

    async fn put(&self, id: &str, value: Vec<u8>) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut data = self.data.lock().await;
        data.insert(id.to_string(), value);
        Ok(())
    }

    async fn delete(&self, id: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut data = self.data.lock().await;
        data.remove(id);
        Ok(())
    }
}