block-db 0.2.0

Local, multi-threaded, durable byte DB.
Documentation
// Authors: Robert Lopez

pub(crate) mod store;

use serde::{Deserialize, Serialize};
use std::num::{NonZero, NonZeroUsize};

/// `chunk_size`: Defines the size (in bytes) of a single chunk
/// within a `DataBlock`. This **cannot** be changed after the
/// `BlockDB` is opened for the first time.
/// _(Default: 4,096)_
///
/// `max_file_size`: Sets the **soft** maximum size (in bytes) of a `DataFile`.
/// "Soft" means that it may be exceeded once per file (e.g., for large writes).
/// This **can** be changed after initialization, but it will not retroactively
/// affect the size of existing `DataFile`s.
/// _(Default: 4,096 * 1,000,000)_
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockDBOptions {
    chunk_size: NonZeroUsize,
    max_file_size: NonZeroUsize,
}

impl Default for BlockDBOptions {
    fn default() -> Self {
        unsafe {
            Self {
                chunk_size: NonZero::new_unchecked(4_096),
                max_file_size: NonZero::new_unchecked(4_096 * 1_000_000),
            }
        }
    }
}

impl BlockDBOptions {
    /// Get `chunk_size` as `usize`
    pub fn chunk_size(&self) -> usize {
        usize::from(self.chunk_size)
    }

    /// Get `max_file_size` as `usize`
    pub fn max_file_size(&self) -> usize {
        usize::from(self.max_file_size)
    }
}

#[derive(Debug, Default)]
pub struct BlockDBOptionsBuilder {
    pub chunk_size: Option<NonZeroUsize>,
    pub max_file_size: Option<NonZeroUsize>,
}

impl BlockDBOptionsBuilder {
    /// Defines the size (in bytes) of a single chunk
    /// within a `DataBlock`. This **cannot** be changed after the
    /// `BlockDB` is opened for the first time.
    /// _(Default: 4,096)_
    pub fn chunk_size(mut self, chunk_size: NonZeroUsize) -> Self {
        self.chunk_size = Some(chunk_size);

        self
    }

    /// Sets the **soft** maximum size (in bytes) of a `DataFile`.
    /// "Soft" means that it may be exceeded once per file (e.g., for large writes).
    /// This **can** be changed after initialization, but it will not retroactively
    /// affect the size of existing `DataFile`s.
    /// _(Default: 4,096 * 1,000,000)_
    pub fn max_file_size(mut self, max_file_size: NonZeroUsize) -> Self {
        self.max_file_size = Some(max_file_size);

        self
    }

    pub fn build(self) -> BlockDBOptions {
        unsafe {
            BlockDBOptions {
                chunk_size: self.chunk_size.unwrap_or(NonZero::new_unchecked(4_096)),
                max_file_size: self
                    .max_file_size
                    .unwrap_or(NonZero::new_unchecked(4_096 * 1_000_000)),
            }
        }
    }
}