use crate::codec::Codec;
use crate::error::DbResult;
use crate::hook::TypedWriteHook;
use crate::lsm::LsmTree;
use crate::{CollectionMeta, Key};
pub struct LsmShard<'tree, K, T, C, H>
where
K: Key + Ord,
T: CollectionMeta<SelfId = K> + Send + Sync,
C: Codec<T> + Clone,
H: TypedWriteHook<K, T>,
{
pub(crate) tree: &'tree LsmTree<K, T, C, H>,
pub(crate) stripe: usize,
}
impl<K, T, C, H> LsmShard<'_, K, T, C, H>
where
K: Key + Ord,
T: CollectionMeta<SelfId = K> + Send + Sync,
C: Codec<T> + Clone,
H: TypedWriteHook<K, T>,
{
fn check_stripe(&self, key: &K) -> DbResult<()> {
if self.tree.shard_for(key) != self.stripe {
return Err(crate::error::DbError::ShardMismatch);
}
Ok(())
}
pub fn get(&self, key: &K) -> Option<T> {
self.tree.get(key)
}
pub fn try_get(&self, key: &K) -> DbResult<Option<T>> {
self.tree.try_get(key)
}
pub fn get_or_err(&self, key: &K) -> DbResult<T> {
self.tree.get_or_err(key)
}
pub fn contains(&self, key: &K) -> bool {
self.tree.contains(key)
}
pub fn try_contains(&self, key: &K) -> DbResult<bool> {
self.tree.try_contains(key)
}
pub fn put(&mut self, key: &K, value: &T) -> DbResult<()> {
self.check_stripe(key)?;
self.tree.put_locked(key, value)
}
pub fn insert(&mut self, key: &K, value: &T) -> DbResult<()> {
self.check_stripe(key)?;
self.tree.insert_locked(key, value)
}
pub fn delete(&mut self, key: &K) -> DbResult<bool> {
self.check_stripe(key)?;
self.tree.delete_locked(key)
}
pub fn upsert<U: crate::IntoUpsert<T>>(
&mut self,
key: &K,
f: impl FnOnce(Option<&T>) -> U,
) -> crate::DbResult<U::Out> {
self.check_stripe(key)?;
crate::upsert::upsert_via_held(self, key, f)
}
}