use crate::storage::Storage;
use crate::table::Table;
use crate::types::RadbKey;
use crate::Error;
use memmap2::MmapMut;
use std::fs::OpenOptions;
use std::path::Path;
pub struct Database {
storage: Storage,
}
impl Database {
pub unsafe fn open(path: &Path) -> Result<Database, Error> {
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(path)?;
let mut db_size = 16 * 1024 * 1024 * 1024;
db_size -= db_size % page_size::get();
file.set_len(db_size as u64)?;
let mmap = MmapMut::map_mut(&file)?;
let storage = Storage::new(mmap)?;
Ok(Database { storage })
}
pub fn open_table<K: RadbKey + ?Sized>(&self, name: &[u8]) -> Result<Table<K>, Error> {
assert!(!name.is_empty());
let id = self.storage.get_or_create_table(name)?;
Table::new(id, &self.storage)
}
}