use crate::error::WriteError;
#[derive(Clone, Debug)]
pub enum NodeSizing {
Fanout(usize),
TargetBytes(usize),
}
#[derive(Clone, Debug)]
pub enum PageAlignment {
None,
Aligned { page_size: usize },
}
#[derive(Clone, Debug)]
pub struct BuildPolicy {
pub sizing: NodeSizing,
pub align: PageAlignment,
}
impl BuildPolicy {
pub const MIN_TARGET_BYTES: usize = 64;
pub fn compact(fanout: usize) -> Self {
Self {
sizing: NodeSizing::Fanout(fanout),
align: PageAlignment::None,
}
}
pub fn disk_aligned(page_size: usize) -> Self {
Self {
sizing: NodeSizing::TargetBytes(page_size),
align: PageAlignment::Aligned { page_size },
}
}
pub(crate) fn validate(&self) -> Result<(), WriteError> {
match self.sizing {
NodeSizing::Fanout(f) if f < 2 => {
return Err(WriteError::InvalidOption("fanout must be >= 2"));
}
NodeSizing::TargetBytes(t) if t < Self::MIN_TARGET_BYTES => {
return Err(WriteError::InvalidOption("target_node_bytes must be >= 64"));
}
_ => {}
}
if let PageAlignment::Aligned { page_size } = self.align {
if !page_size.is_power_of_two() || page_size < 64 {
return Err(WriteError::InvalidOption(
"page_size must be a power of two and >= 64",
));
}
}
Ok(())
}
}
impl Default for BuildPolicy {
fn default() -> Self {
Self::compact(128)
}
}
#[derive(Clone, Debug)]
pub struct WriterOptions {
pub object_sort_window: usize,
pub policy: BuildPolicy,
}
impl Default for WriterOptions {
fn default() -> Self {
Self {
object_sort_window: 16_384,
policy: BuildPolicy::default(),
}
}
}