use dbutils::equivalent::Comparable;
use super::{DiscardLog, Fid, Iter, Keys, Values};
pub struct ImmutableDiscardLog<I = u32>(DiscardLog<I>);
unsafe impl<I: Send> Send for ImmutableDiscardLog<I> {}
unsafe impl<I: Sync> Sync for ImmutableDiscardLog<I> {}
impl<I> ImmutableDiscardLog<I> {
#[inline]
pub const fn capacity(&self) -> usize {
self.0.capacity()
}
#[inline]
pub const fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
pub fn path(&self) -> &std::rc::Rc<std::path::PathBuf> {
self.0.path().unwrap()
}
#[inline]
pub unsafe fn reserved_slice(&self) -> &[u8] {
self.0.reserved_slice()
}
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
pub fn lock_exclusive(&self) -> std::io::Result<()> {
self.0.lock_exclusive()
}
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
pub fn lock_shared(&self) -> std::io::Result<()> {
self.0.lock_shared()
}
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
pub fn unlock(&self) -> std::io::Result<()> {
self.0.unlock()
}
#[inline]
pub(crate) const fn construct(log: DiscardLog<I>) -> Self {
Self(log)
}
}
impl<I> ImmutableDiscardLog<I>
where
I: Fid,
{
#[inline]
pub const fn iter(&self) -> Iter<'_, I> {
self.0.iter()
}
#[inline]
pub const fn keys(&self) -> Keys<'_, I> {
self.0.keys()
}
#[inline]
pub const fn values(&self) -> Values<'_, I> {
self.0.values()
}
#[inline]
pub fn max_discard(&self) -> Option<(I::Ref<'_>, u64)> {
self.iter().max_by(|(_, a), (_, b)| a.cmp(b))
}
}
impl<I> ImmutableDiscardLog<I>
where
I: Fid,
for<'a> I::Ref<'a>: Ord,
{
#[inline]
pub fn get<Q>(&self, fid: &Q) -> Option<u64>
where
Q: ?Sized + for<'a> Comparable<I::Ref<'a>>,
{
self.0.get(fid)
}
}