use async_txn::{AsyncSpawner, BTreeCm};
pub use cheap_clone::CheapClone;
use super::*;
mod write;
pub use write::*;
#[cfg(test)]
mod tests;
struct Inner<K, V, S> {
tm: AsyncTm<K, V, BTreeCm<K>, PendingMap<K, V>, S>,
map: SkipCore<K, V>,
max_batch_size: u64,
max_batch_entries: u64,
}
impl<K, V, S: AsyncSpawner> Inner<K, V, S> {
async fn new(name: &str, max_batch_size: u64, max_batch_entries: u64) -> Self {
let tm = AsyncTm::new(name, 0).await;
Self {
tm,
map: SkipCore::new(),
max_batch_size,
max_batch_entries,
}
}
async fn version(&self) -> u64 {
self.tm.version().await
}
}
pub struct ComparableDB<K, V, S> {
inner: Arc<Inner<K, V, S>>,
}
impl<K, V, S> AsSkipCore<K, V> for ComparableDB<K, V, S> {
#[inline]
#[allow(private_interfaces)]
fn as_inner(&self) -> &SkipCore<K, V> {
&self.inner.map
}
}
impl<K, V, S> Clone for ComparableDB<K, V, S> {
#[inline]
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<K, V, S: AsyncSpawner> ComparableDB<K, V, S> {
#[inline]
pub async fn new() -> Self {
Self::with_options(Default::default()).await
}
}
impl<K, V, S: AsyncSpawner> ComparableDB<K, V, S> {
#[inline]
pub async fn with_options(opts: Options) -> Self {
Self {
inner: Arc::new(
Inner::new(
core::any::type_name::<Self>(),
opts.max_batch_size(),
opts.max_batch_entries(),
)
.await,
),
}
}
#[inline]
pub async fn version(&self) -> u64 {
self.inner.version().await
}
#[inline]
pub async fn read(&self) -> ReadTransaction<K, V, ComparableDB<K, V, S>, BTreeCm<K>, S> {
ReadTransaction::new(self.clone(), self.inner.tm.read().await)
}
}
impl<K, V, S> ComparableDB<K, V, S>
where
K: CheapClone + Ord,
S: AsyncSpawner,
{
#[inline]
pub async fn write(&self) -> WriteTransaction<K, V, S> {
WriteTransaction::new(self.clone(), None).await
}
#[inline]
pub async fn write_with_capacity(&self, cap: usize) -> WriteTransaction<K, V, S> {
WriteTransaction::new(self.clone(), Some(cap)).await
}
}
impl<K, V, S> ComparableDB<K, V, S>
where
K: CheapClone + Ord + Send + 'static,
V: Send + 'static,
S: AsyncSpawner,
{
#[inline]
pub fn compact(&self) {
self.inner.map.compact(self.inner.tm.discard_hint());
}
}