block-db 0.2.0

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

use super::{error::Error, BlockDB, BlockKey};

impl BlockDB {
    /// Frees the `DataBlock` (`data_block_id`) within `DataFile` (`data_file_id`)
    /// by marking its chunks as "free".
    ///
    /// **Once marked as free, the `DataBlock` is unrecoverable**, making this
    /// operation functionally equivalent to a delete.
    ///
    /// Freed space will be reused in future writes. To physically remove the
    /// freed bytes from the underlying `DataFile`, use `BlockDB::compact_data_file(s)`.
    ///
    /// Returns the total number of bytes freed.
    ///
    /// ---
    /// - **Atomic**
    /// - **Non-corruptible**
    ///
    /// ---
    /// Example
    /// ```
    /// let block_db = BlockDB::open("./data", None).await?;
    ///
    /// let block_key = block_db.write(b"Hello World").await?;
    ///
    /// let freed_bytes: usize = block_db.free(&block_key).await?;
    /// ```
    pub async fn free(
        &mut self,
        BlockKey {
            data_file_id,
            data_block_id,
        }: &BlockKey,
    ) -> Result<usize, Error> {
        if let Some(data_file) = self.data_files.read().await.get(data_file_id).cloned() {
            return data_file.write().await.free(data_block_id).await;
        }

        Ok(0)
    }
}