Skip to main content

graphd_engine/
backend.rs

1use std::path::Path;
2
3use crate::error::GraphdError;
4
5/// LadybugDB graph database backend.
6pub struct Backend {
7    db: lbug::Database,
8}
9
10impl Backend {
11    /// Open or create a database at the given directory path with default config.
12    pub fn open(path: &Path) -> Result<Self, GraphdError> {
13        Self::open_with_config(path, lbug::SystemConfig::default())
14    }
15
16    /// Open or create a database with memory and thread limits.
17    ///
18    /// This is the preferred constructor for multi-tenant environments.
19    pub fn open_tenant(path: &Path, memory_mb: u64, max_threads: u64) -> Result<Self, GraphdError> {
20        let config = lbug::SystemConfig::default()
21            .buffer_pool_size(memory_mb * 1024 * 1024)
22            .max_num_threads(max_threads);
23        Self::open_with_config(path, config)
24    }
25
26    /// Open or create a database at the given directory path with custom config.
27    pub fn open_with_config(path: &Path, config: lbug::SystemConfig) -> Result<Self, GraphdError> {
28        let db = lbug::Database::new(path, config)
29            .map_err(|e| GraphdError::DatabaseError(format!("Failed to open database: {e}")))?;
30        Ok(Self { db })
31    }
32
33    /// Create a new connection to the database.
34    pub fn connection(&self) -> Result<lbug::Connection<'_>, GraphdError> {
35        lbug::Connection::new(&self.db)
36            .map_err(|e| GraphdError::DatabaseError(format!("Connection failed: {e}")))
37    }
38}