ainl_runtime/
graph_cell.rs1use std::ops::Deref;
11#[cfg(feature = "async")]
12use std::sync::{Arc, Mutex, MutexGuard};
13
14use ainl_memory::{GraphMemory, RuntimeStateNode, SqliteGraphStore};
15
16pub struct SqliteStoreRef<'a> {
18 #[cfg(not(feature = "async"))]
19 inner: &'a SqliteGraphStore,
20 #[cfg(feature = "async")]
21 _guard: MutexGuard<'a, GraphMemory>,
22}
23
24impl<'a> SqliteStoreRef<'a> {
25 #[cfg(not(feature = "async"))]
26 pub(crate) fn borrowed(store: &'a SqliteGraphStore) -> Self {
27 Self { inner: store }
28 }
29
30 #[cfg(feature = "async")]
31 pub(crate) fn from_guard(guard: MutexGuard<'a, GraphMemory>) -> Self {
32 Self { _guard: guard }
33 }
34}
35
36impl Deref for SqliteStoreRef<'_> {
37 type Target = SqliteGraphStore;
38
39 fn deref(&self) -> &Self::Target {
40 #[cfg(not(feature = "async"))]
41 {
42 self.inner
43 }
44 #[cfg(feature = "async")]
45 {
46 self._guard.sqlite_store()
47 }
48 }
49}
50
51pub(crate) struct GraphCell {
52 #[cfg(not(feature = "async"))]
53 inner: GraphMemory,
54 #[cfg(feature = "async")]
55 inner: Arc<Mutex<GraphMemory>>,
56}
57
58impl GraphCell {
59 pub(crate) fn new(store: SqliteGraphStore) -> Self {
60 let memory = GraphMemory::from_sqlite_store(store);
61 #[cfg(not(feature = "async"))]
62 {
63 Self { inner: memory }
64 }
65 #[cfg(feature = "async")]
66 {
67 Self {
68 inner: Arc::new(Mutex::new(memory)),
69 }
70 }
71 }
72
73 pub(crate) fn read_runtime_state(
74 &self,
75 agent_id: &str,
76 ) -> Result<Option<RuntimeStateNode>, String> {
77 self.with(|m| m.read_runtime_state(agent_id))
78 }
79
80 #[cfg(not(feature = "async"))]
81 pub(crate) fn with<R, F: FnOnce(&GraphMemory) -> R>(&self, f: F) -> R {
82 f(&self.inner)
83 }
84
85 #[cfg(feature = "async")]
86 pub(crate) fn with<R, F: FnOnce(&GraphMemory) -> R>(&self, f: F) -> R {
87 let g = self.inner.lock().expect("graph mutex poisoned");
88 f(&g)
89 }
90
91 pub(crate) fn sqlite_ref(&self) -> SqliteStoreRef<'_> {
92 #[cfg(not(feature = "async"))]
93 {
94 SqliteStoreRef::borrowed(self.inner.sqlite_store())
95 }
96 #[cfg(feature = "async")]
97 {
98 SqliteStoreRef::from_guard(self.inner.lock().expect("graph mutex"))
99 }
100 }
101
102 #[cfg(feature = "async")]
103 pub(crate) fn shared_arc(&self) -> Arc<Mutex<GraphMemory>> {
104 Arc::clone(&self.inner)
105 }
106}