block-db 0.2.0

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

use super::{
    error::Error,
    util::map_data_file::{map_data_file, map_data_files},
    BlockDB,
};

impl BlockDB {
    /// Compacts a `DataFile` by removing all free chunks.
    ///
    /// A temporary file is created and all non-free `DataBlock`s are written
    /// into it. The temporary file is then swapped in to replace the original,
    /// effectively removing unused space and reducing the file size.
    ///
    /// **Note:** This is a relatively heavy operation. It’s recommended to run
    /// it during initialization (warm-up) or before shutdown (clean-up),
    /// as freed chunks are already reused automatically during writes.
    ///
    /// ---
    /// - **Atomic**
    /// - **Corruptible**
    ///
    /// ---
    /// Example
    /// ```
    /// let block_db = BlockDB::open("./data", None).await?;
    ///
    /// block_db.compact_data_file("3f-6hf").await?;
    /// ```
    pub async fn compact_data_file<S: AsRef<str>>(&self, data_file_id: S) -> Result<(), Error> {
        map_data_file!(self, data_file_id.as_ref(), compact);

        Ok(())
    }

    /// Compacts each `DataFile` by removing all free chunks.
    ///
    /// For every `DataFile`, a temporary file is created and all non-free
    /// `DataBlock`s are written into it. The temporary file is then swapped
    /// in to replace the original, effectively removing unused space and
    /// reducing the file size.
    ///
    /// **Note:** This is a relatively heavy operation. It’s recommended to run
    /// it during initialization (warm-up) or before shutdown (clean-up),
    /// as freed chunks are already reused automatically during writes.
    ///
    /// ---
    /// - **Atomic**
    /// - **Corruptible**
    ///
    /// ---
    /// Example
    /// ```
    /// let block_db = BlockDB::open("./data", None).await?;
    ///
    /// block_db.compact_data_files().await?;
    /// ```
    pub async fn compact_data_files(&self) -> Result<(), Error> {
        map_data_files!(self, compact);

        Ok(())
    }
}