Skip to main content

grafeo_adapters/storage/
memory.rs

1//! Pure in-memory storage backend.
2
3use grafeo_common::memory::arena::AllocError;
4use grafeo_core::graph::lpg::LpgStore;
5use std::sync::Arc;
6
7/// In-memory storage backend.
8///
9/// This is the default storage backend that keeps all data in memory.
10/// Data is lost when the process exits unless WAL is enabled.
11pub struct MemoryBackend {
12    /// The underlying LPG store.
13    store: Arc<LpgStore>,
14}
15
16impl MemoryBackend {
17    /// Creates a new in-memory backend.
18    ///
19    /// # Errors
20    ///
21    /// Returns [`AllocError`] if arena allocation fails.
22    pub fn new() -> Result<Self, AllocError> {
23        Ok(Self {
24            store: Arc::new(LpgStore::new()?),
25        })
26    }
27
28    /// Returns a reference to the underlying store.
29    #[must_use]
30    pub fn store(&self) -> &Arc<LpgStore> {
31        &self.store
32    }
33}
34
35impl Default for MemoryBackend {
36    fn default() -> Self {
37        Self::new().expect("arena allocation for default MemoryBackend")
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_memory_backend() {
47        let backend = MemoryBackend::new().unwrap();
48        let store = backend.store();
49
50        let id = store.create_node(&["Test"]);
51        assert!(id.is_valid());
52    }
53}