use std::sync::Arc;
use crate::entry::Entry;
pub trait Store: Clone + core::fmt::Debug + Default + Send + Sync {
fn get(&self, key: &str) -> Option<&Arc<Entry>>;
fn insert(&self, key: String, value: Arc<Entry>) -> Self;
fn remove(&self, key: &str) -> Self;
fn insert_mut(&mut self, key: String, value: Arc<Entry>);
fn remove_mut(&mut self, key: &str) -> bool;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn iter(&self) -> impl Iterator<Item = (&String, &Arc<Entry>)>;
fn range_prefix<'a>(
&'a self,
prefix: &'a str,
) -> impl Iterator<Item = (&'a String, &'a Arc<Entry>)>;
fn shares_allocation_with(&self, other: &Self) -> bool;
}
pub type Root = rpds::RedBlackTreeMapSync<String, Arc<Entry>>;
impl Store for Root {
fn get(&self, key: &str) -> Option<&Arc<Entry>> {
Root::get(self, key)
}
fn insert(&self, key: String, value: Arc<Entry>) -> Self {
Root::insert(self, key, value)
}
fn remove(&self, key: &str) -> Self {
Root::remove(self, key)
}
fn insert_mut(&mut self, key: String, value: Arc<Entry>) {
Root::insert_mut(self, key, value);
}
fn remove_mut(&mut self, key: &str) -> bool {
Root::remove_mut(self, key)
}
fn len(&self) -> usize {
self.size()
}
fn iter(&self) -> impl Iterator<Item = (&String, &Arc<Entry>)> {
Root::iter(self)
}
fn range_prefix<'a>(
&'a self,
prefix: &'a str,
) -> impl Iterator<Item = (&'a String, &'a Arc<Entry>)> {
self.range::<String, _>(prefix.to_owned()..)
.take_while(move |(k, _)| k.starts_with(prefix))
}
fn shares_allocation_with(&self, other: &Self) -> bool {
self.ptr_eq(other)
}
}