pub mod impls;
pub(crate) mod helpers;
pub(crate) mod indexes;
pub(crate) mod query;
use redb::{Database, ReadTransaction, TableDefinition, WriteTransaction};
use std::path::Path;
use std::sync::Arc;
use vantage_core::{Result, error};
#[derive(Clone, Debug)]
pub struct Redb {
db: Arc<Database>,
}
impl Redb {
pub fn create<P: AsRef<Path>>(path: P) -> Result<Self> {
let db = Database::create(path)
.map_err(|e| error!("Failed to create redb database", details = e.to_string()))?;
Ok(Self { db: Arc::new(db) })
}
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
let db = Database::open(path)
.map_err(|e| error!("Failed to open redb database", details = e.to_string()))?;
Ok(Self { db: Arc::new(db) })
}
pub fn from_database(db: Database) -> Self {
Self { db: Arc::new(db) }
}
pub fn database(&self) -> &Database {
&self.db
}
pub(crate) fn begin_read(&self) -> Result<ReadTransaction> {
self.db
.begin_read()
.map_err(|e| error!("Failed to begin redb read txn", details = e.to_string()))
}
pub(crate) fn begin_write(&self) -> Result<WriteTransaction> {
self.db
.begin_write()
.map_err(|e| error!("Failed to begin redb write txn", details = e.to_string()))
}
}
pub(crate) fn main_table_def(name: &str) -> TableDefinition<'_, &'static str, &'static [u8]> {
TableDefinition::new(name)
}
pub(crate) fn index_table_name(table_name: &str, column_name: &str) -> String {
format!("{}__idx__{}", table_name, column_name)
}
pub(crate) fn index_table_def(
name: &str,
) -> TableDefinition<'_, (&'static [u8], &'static str), ()> {
TableDefinition::new(name)
}