use core::mem;
use dbutils::equivalentor::{Ascend, BytesComparator};
use crate::{
allocator::Sealed,
error::Error,
options::{CompressionPolicy, Freelist},
traits::Constructable,
types::{Height, KeySize},
Arena, Options,
};
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
mod memmap;
#[derive(Debug, Clone)]
pub struct Builder<C = Ascend> {
options: Options,
cmp: C,
}
impl<C> Default for Builder<C>
where
C: Default,
{
#[inline]
fn default() -> Self {
Self {
options: Options::new(),
cmp: C::default(),
}
}
}
impl<C> From<Options> for Builder<C>
where
C: Default,
{
#[inline]
fn from(options: Options) -> Self {
Self {
options,
cmp: C::default(),
}
}
}
impl<C> From<Builder<C>> for Options {
#[inline]
fn from(builder: Builder<C>) -> Self {
builder.options
}
}
impl Builder {
#[inline]
pub const fn new() -> Self {
Self {
options: Options::new(),
cmp: Ascend::new(),
}
}
}
impl<C> Builder<C> {
#[inline]
pub const fn with(cmp: C) -> Self {
Self {
options: Options::new(),
cmp,
}
}
#[inline]
pub const fn options(&self) -> &Options {
&self.options
}
#[inline]
pub fn with_options(self, options: Options) -> Self {
Self {
options,
cmp: self.cmp,
}
}
#[inline]
pub const fn comparator(&self) -> &C {
&self.cmp
}
#[inline]
pub fn with_comparator<NC>(self, cmp: NC) -> Builder<NC> {
Builder {
options: self.options,
cmp,
}
}
crate::__builder_opts!(dynamic::Builder);
}
impl<C: BytesComparator> Builder<C> {
#[inline]
pub fn alloc<T>(self) -> Result<T, Error>
where
T: Arena,
T::Constructable: Constructable<Comparator = C>,
{
let node_align =
mem::align_of::<<<T::Constructable as Constructable>::Allocator as Sealed>::Node>();
let Self { options, cmp } = self;
options
.to_arena_options()
.with_maximum_alignment(node_align)
.alloc::<<<T::Constructable as Constructable>::Allocator as Sealed>::Allocator>()
.map_err(Into::into)
.and_then(|arena| T::construct(arena, options, false, cmp))
}
}