pub use cheap_clone::CheapClone;
use txn::BTreeCm;
use super::*;
mod optimistic;
pub use optimistic::*;
#[allow(clippy::module_inception)]
mod serializable;
pub use serializable::*;
struct Inner<K, V> {
tm: Tm<K, V, BTreeCm<K>, BTreePwm<K, V>>,
map: SkipCore<K, V>,
}
impl<K, V> Inner<K, V> {
fn new(name: &str) -> Self {
let tm = Tm::new(name, 0);
Self {
tm,
map: SkipCore::new(),
}
}
fn version(&self) -> u64 {
self.tm.version()
}
}
#[repr(transparent)]
pub struct SerializableDb<K, V> {
inner: Arc<Inner<K, V>>,
}
#[doc(hidden)]
impl<K, V> AsSkipCore<K, V> for SerializableDb<K, V> {
#[inline]
fn as_inner(&self) -> &SkipCore<K, V> {
&self.inner.map
}
}
impl<K, V> Clone for SerializableDb<K, V> {
#[inline]
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<K, V> Default for SerializableDb<K, V> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<K, V> SerializableDb<K, V> {
#[inline]
pub fn new() -> Self {
Self {
inner: Arc::new(Inner::new(core::any::type_name::<Self>())),
}
}
}
impl<K, V> SerializableDb<K, V> {
#[inline]
pub fn version(&self) -> u64 {
self.inner.version()
}
#[inline]
pub fn read(&self) -> ReadTransaction<K, V, SerializableDb<K, V>, BTreeCm<K>> {
ReadTransaction::new(self.clone(), self.inner.tm.read())
}
}
impl<K, V> SerializableDb<K, V>
where
K: CheapClone + Ord + 'static,
V: 'static,
{
#[inline]
pub fn optimistic_write(&self) -> OptimisticTransaction<K, V> {
OptimisticTransaction::new(self.clone())
}
#[inline]
pub fn serializable_write(&self) -> SerializableTransaction<K, V> {
SerializableTransaction::new(self.clone())
}
}
impl<K, V> SerializableDb<K, V>
where
K: CheapClone + Ord + Send + 'static,
V: Send + 'static,
{
#[inline]
pub fn compact(&self) {
self.inner.map.compact(self.inner.tm.discard_hint());
}
}