use super::*;
mod write;
pub use write::*;
#[cfg(test)]
mod tests;
struct Inner<K, V, SP, S = std::hash::RandomState> {
tm: AsyncTm<K, V, HashCm<K, S>, PendingMap<K, V>, SP>,
map: SkipCore<K, V>,
hasher: S,
max_batch_size: u64,
max_batch_entries: u64,
}
impl<K, V, SP: AsyncSpawner, S> Inner<K, V, SP, S> {
async fn new(name: &str, max_batch_size: u64, max_batch_entries: u64, hasher: S) -> Self {
let tm = AsyncTm::<_, _, _, _, SP>::new(name, 0).await;
Self {
tm,
map: SkipCore::new(),
hasher,
max_batch_size,
max_batch_entries,
}
}
async fn version(&self) -> u64 {
self.tm.version().await
}
}
pub struct EquivalentDB<K, V, SP, S = std::hash::RandomState> {
inner: Arc<Inner<K, V, SP, S>>,
}
impl<K, V, S> AsSkipCore<K, V> for EquivalentDB<K, V, S> {
#[inline]
fn as_inner(&self) -> &SkipCore<K, V> {
&self.inner.map
}
}
impl<K, V, SP, S> Clone for EquivalentDB<K, V, SP, S> {
#[inline]
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<K, V, SP: AsyncSpawner> EquivalentDB<K, V, SP> {
#[inline]
pub async fn new() -> Self {
Self::with_options_and_hasher(Default::default(), Default::default()).await
}
}
impl<K, V, SP: AsyncSpawner, S> EquivalentDB<K, V, SP, S> {
#[inline]
pub async fn with_hasher(hasher: S) -> Self {
Self::with_options_and_hasher(Default::default(), hasher).await
}
#[inline]
pub async fn with_options_and_hasher(opts: Options, hasher: S) -> Self {
let inner = Arc::new(
Inner::<_, _, SP, _>::new(
core::any::type_name::<Self>(),
opts.max_batch_size(),
opts.max_batch_entries(),
hasher,
)
.await,
);
Self { inner }
}
#[inline]
pub async fn version(&self) -> u64 {
self.inner.version().await
}
#[inline]
pub async fn read(&self) -> ReadTransaction<K, V, EquivalentDB<K, V, SP, S>, HashCm<K, S>, SP> {
ReadTransaction::new(self.clone(), self.inner.tm.read().await)
}
}
impl<K, V, SP, S> EquivalentDB<K, V, SP, S>
where
K: Ord + core::hash::Hash + Eq,
S: BuildHasher + Clone,
SP: AsyncSpawner,
{
#[inline]
pub async fn write(&self) -> WriteTransaction<K, V, SP, S> {
WriteTransaction::new(self.clone(), None).await
}
#[inline]
pub async fn write_with_capacity(&self, capacity: usize) -> WriteTransaction<K, V, SP, S> {
WriteTransaction::new(self.clone(), Some(capacity)).await
}
}
impl<K, V, SP, S> EquivalentDB<K, V, SP, S>
where
K: Ord + Eq + core::hash::Hash + Send + 'static,
V: Send + 'static,
SP: AsyncSpawner,
{
#[inline]
pub fn compact(&self) {
self.inner.map.compact(self.inner.tm.discard_hint());
}
}