//! Pure in-memory storage backend.

use grafeo_common::memory::arena::AllocError;
use grafeo_core::graph::lpg::LpgStore;
use std::sync::Arc;

/// In-memory storage backend.
///
/// This is the default storage backend that keeps all data in memory.
/// Data is lost when the process exits unless WAL is enabled.
pub struct MemoryBackend {
    /// The underlying LPG store.
    store: Arc<LpgStore>,
}

impl MemoryBackend {
    /// Creates a new in-memory backend.
    ///
    /// # Errors
    ///
    /// Returns [`AllocError`] if arena allocation fails.
    pub fn new() -> Result<Self, AllocError> {
        Ok(Self {
            store: Arc::new(LpgStore::new()?),
        })
    }

    /// Returns a reference to the underlying store.
    #[must_use]
    pub fn store(&self) -> &Arc<LpgStore> {
        &self.store
    }
}

impl Default for MemoryBackend {
    fn default() -> Self {
        Self::new().expect("arena allocation for default MemoryBackend")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_memory_backend() {
        let backend = MemoryBackend::new().unwrap();
        let store = backend.store();

        let id = store.create_node(&["Test"]);
        assert!(id.is_valid());
    }
}
