pub mod tables;
mod error;
pub use error::StorageError;
mod txn;
pub use txn::{MultimapTableHandle, TableHandle, Txn};
pub use redb::{
MultimapTableDefinition, ReadTransaction, ReadableMultimapTable, ReadableTable,
TableDefinition, WriteTransaction,
};
use std::path::Path;
pub struct StorageEngine {
db: redb::Database,
}
impl StorageEngine {
pub fn open_file(path: impl AsRef<Path>) -> Result<Self, StorageError> {
let db = redb::Database::create(path)?;
Self::from_db(db)
}
pub fn open_memory() -> Result<Self, StorageError> {
let backend = redb::backends::InMemoryBackend::new();
let db = redb::Database::builder().create_with_backend(backend)?;
Self::from_db(db)
}
fn from_db(db: redb::Database) -> Result<Self, StorageError> {
let write_txn = db.begin_write()?;
{
write_txn.open_table(tables::META)?;
write_txn.open_table(tables::LABEL_TO_ID)?;
write_txn.open_table(tables::ID_TO_LABEL)?;
write_txn.open_table(tables::NODES)?;
write_txn.open_table(tables::EDGES)?;
write_txn.open_multimap_table(tables::ADJ_OUT)?;
write_txn.open_multimap_table(tables::ADJ_IN)?;
write_txn.open_multimap_table(tables::NODE_LABEL_INDEX)?;
}
write_txn.commit()?;
Ok(Self { db })
}
pub fn begin_write(&self) -> Result<WriteTransaction, StorageError> {
Ok(self.db.begin_write()?)
}
pub fn begin_read(&self) -> Result<ReadTransaction, StorageError> {
Ok(self.db.begin_read()?)
}
}