use crate::cache::Key;
use crate::{IndexTable, IndexType};
use std::path::PathBuf;
#[cfg(feature = "write")]
use crate::transactions::Transaction;
pub struct DatabaseSettings {
pub path: Option<PathBuf>,
pub cache_size: Option<usize>,
pub index_type: IndexType,
pub create_path: bool,
}
impl Default for DatabaseSettings {
fn default() -> Self {
DatabaseSettings {
path: None,
cache_size: None,
index_type: IndexType::HashMap,
create_path: false,
}
}
}
pub trait Database: Send + Sync {
fn new(settings: DatabaseSettings) -> Self
where
Self: Sized;
fn new_default(location: PathBuf) -> Self
where
Self: Sized;
fn get(&mut self, key: &str) -> anyhow::Result<Option<Vec<u8>>>;
fn link(&mut self, old: &str, new: &str) -> anyhow::Result<()>;
fn delete(&mut self, key: &str) -> anyhow::Result<()>;
fn persist(&mut self) -> anyhow::Result<()>;
#[cfg(feature = "write")]
fn put(&mut self, key: &str, value: &[u8]) -> anyhow::Result<()>;
#[cfg(feature = "garbage-collection")]
fn gc(&mut self) -> anyhow::Result<()>;
#[cfg(feature = "write")]
fn tx(&mut self) -> anyhow::Result<Box<dyn Transaction + '_>>;
}
pub(crate) trait DatabaseTransactionsIO: Database {
fn snapshot(&self) -> Box<dyn IndexTable>;
fn merge_file(&mut self, new_content: &[u8]) -> anyhow::Result<u64>;
fn merge_index_table(
&mut self,
backup: Box<dyn IndexTable>,
new_key_values: Vec<(String, Key)>,
) -> anyhow::Result<()>;
fn rollback(&mut self, index_table: Box<dyn IndexTable>) -> anyhow::Result<()>;
}