use super::*;
use std::{collections::hash_map::RandomState, hash::Hash};
mod write;
pub use write::*;
#[cfg(test)]
mod tests;
struct Inner<K, V, S = RandomState> {
tm: Tm<K, V, HashCm<K, S>, BTreePwm<K, V>>,
map: SkipCore<K, V>,
hasher: S,
}
impl<K, V, S> Inner<K, V, S> {
fn new(name: &str, hasher: S) -> Self {
let tm = Tm::new(name, 0);
Self {
tm,
map: SkipCore::new(),
hasher,
}
}
fn version(&self) -> u64 {
self.tm.version()
}
}
pub struct OptimisticDb<K, V, S = RandomState> {
inner: Arc<Inner<K, V, S>>,
}
#[doc(hidden)]
impl<K, V, S> AsSkipCore<K, V> for OptimisticDb<K, V, S> {
#[inline]
#[allow(private_interfaces)]
fn as_inner(&self) -> &SkipCore<K, V> {
&self.inner.map
}
}
impl<K, V, S> Clone for OptimisticDb<K, V, S> {
#[inline]
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<K, V> Default for OptimisticDb<K, V> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<K, V> OptimisticDb<K, V> {
#[inline]
pub fn new() -> Self {
Self::with_hasher(Default::default())
}
}
impl<K, V, S> OptimisticDb<K, V, S> {
#[inline]
pub fn with_hasher(hasher: S) -> Self {
let inner = Arc::new(Inner::new(core::any::type_name::<Self>(), hasher));
Self { inner }
}
#[inline]
pub fn version(&self) -> u64 {
self.inner.version()
}
#[inline]
pub fn read(&self) -> ReadTransaction<K, V, OptimisticDb<K, V, S>, HashCm<K, S>> {
ReadTransaction::new(self.clone(), self.inner.tm.read())
}
}
impl<K, V, S> OptimisticDb<K, V, S>
where
K: Ord + Eq + Hash,
V: 'static,
S: BuildHasher + Clone,
{
#[inline]
pub fn write(&self) -> OptimisticTransaction<K, V, S> {
OptimisticTransaction::new(self.clone(), None)
}
#[inline]
pub fn write_with_capacity(&self, capacity: usize) -> OptimisticTransaction<K, V, S> {
OptimisticTransaction::new(self.clone(), Some(capacity))
}
}
impl<K, V, S> OptimisticDb<K, V, S>
where
K: Ord + Eq + Hash + Send + 'static,
V: Send + 'static,
S: BuildHasher + Clone,
{
#[inline]
pub fn compact(&self) {
self.inner.map.compact(self.inner.tm.discard_hint());
}
}