use crate::{DB,Tree,Storage,Point,Value,Error,RA};
use async_std::sync::{Arc,Mutex};
#[derive(Debug,Clone)]
pub struct SetupFields {
pub branch_factor: usize,
pub max_depth: usize,
pub max_records: usize,
pub inline: usize,
pub tree_cache_size: usize,
pub rebuild_depth: usize,
}
impl SetupFields {
pub fn default() -> Self {
Self {
branch_factor: 6,
max_depth: 8,
max_records: 20_000,
inline: 500,
tree_cache_size: 1000,
rebuild_depth: 2,
}
}
}
#[derive(Clone)]
pub struct Setup<S> where S: RA {
pub storage: Arc<Mutex<Box<dyn Storage<S>>>>,
pub fields: SetupFields
}
impl<S> Setup<S> where S: RA {
pub fn from_storage(storage: Box<dyn Storage<S>>) -> Self {
Self {
storage: Arc::new(Mutex::new(storage)),
fields: SetupFields::default()
}
}
pub fn branch_factor(mut self, bf: usize) -> Self {
self.fields.branch_factor = bf;
self
}
pub fn max_depth(mut self, md: usize) -> Self {
self.fields.max_depth = md;
self
}
pub fn max_records(mut self, mr: usize) -> Self {
self.fields.max_records = mr;
self
}
pub fn inline(mut self, n: usize) -> Self {
self.fields.inline = n;
self
}
pub fn tree_cache_size(mut self, n: usize) -> Self {
self.fields.tree_cache_size = n;
self
}
pub fn rebuild_depth(mut self, n: usize) -> Self {
self.fields.rebuild_depth = n;
self
}
pub async fn build<T,P,V> (self) -> Result<DB<S,T,P,V>,Error>
where P: Point, V: Value, T: Tree<P,V> {
DB::open_from_setup(self).await
}
}