-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathstorage.rs
More file actions
117 lines (100 loc) · 2.88 KB
/
storage.rs
File metadata and controls
117 lines (100 loc) · 2.88 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use async_trait::async_trait;
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use crate::{Context, error::Result, graph::Graph};
/// Session information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Session {
pub id: String,
pub graph_id: String,
pub current_task_id: String,
/// Optional status message from the last executed task
pub status_message: Option<String>,
pub context: crate::context::Context,
}
impl Session {
pub fn new_from_task(sid: String, task_name: &str) -> Self {
Self {
id: sid,
graph_id: "default".to_string(),
current_task_id: task_name.to_string(),
status_message: None,
context: Context::new(),
}
}
}
/// Trait for storing and retrieving graphs
#[async_trait]
pub trait GraphStorage: Send + Sync {
async fn save(&self, id: String, graph: Arc<Graph>) -> Result<()>;
async fn get(&self, id: &str) -> Result<Option<Arc<Graph>>>;
async fn delete(&self, id: &str) -> Result<()>;
}
/// Trait for storing and retrieving sessions
#[async_trait]
pub trait SessionStorage: Send + Sync {
async fn save(&self, session: Session) -> Result<()>;
async fn get(&self, id: &str) -> Result<Option<Session>>;
async fn delete(&self, id: &str) -> Result<()>;
}
/// In-memory implementation of GraphStorage
pub struct InMemoryGraphStorage {
graphs: Arc<DashMap<String, Arc<Graph>>>,
}
impl Default for InMemoryGraphStorage {
fn default() -> Self {
Self::new()
}
}
impl InMemoryGraphStorage {
pub fn new() -> Self {
Self {
graphs: Arc::new(DashMap::new()),
}
}
}
#[async_trait]
impl GraphStorage for InMemoryGraphStorage {
async fn save(&self, id: String, graph: Arc<Graph>) -> Result<()> {
self.graphs.insert(id, graph);
Ok(())
}
async fn get(&self, id: &str) -> Result<Option<Arc<Graph>>> {
Ok(self.graphs.get(id).map(|entry| entry.clone()))
}
async fn delete(&self, id: &str) -> Result<()> {
self.graphs.remove(id);
Ok(())
}
}
/// In-memory implementation of SessionStorage
pub struct InMemorySessionStorage {
sessions: Arc<DashMap<String, Session>>,
}
impl Default for InMemorySessionStorage {
fn default() -> Self {
Self::new()
}
}
impl InMemorySessionStorage {
pub fn new() -> Self {
Self {
sessions: Arc::new(DashMap::new()),
}
}
}
#[async_trait]
impl SessionStorage for InMemorySessionStorage {
async fn save(&self, session: Session) -> Result<()> {
self.sessions.insert(session.id.clone(), session);
Ok(())
}
async fn get(&self, id: &str) -> Result<Option<Session>> {
Ok(self.sessions.get(id).map(|entry| entry.clone()))
}
async fn delete(&self, id: &str) -> Result<()> {
self.sessions.remove(id);
Ok(())
}
}