block_db/free.rs
1// Authors: Robert Lopez
2
3use super::{error::Error, BlockDB, BlockKey};
4
5impl BlockDB {
6 /// Frees the `DataBlock` (`data_block_id`) within `DataFile` (`data_file_id`)
7 /// by marking its chunks as "free".
8 ///
9 /// **Once marked as free, the `DataBlock` is unrecoverable**, making this
10 /// operation functionally equivalent to a delete.
11 ///
12 /// Freed space will be reused in future writes. To physically remove the
13 /// freed bytes from the underlying `DataFile`, use `BlockDB::compact_data_file(s)`.
14 ///
15 /// Returns the total number of bytes freed.
16 ///
17 /// ---
18 /// - **Atomic**
19 /// - **Non-corruptible**
20 ///
21 /// ---
22 /// Example
23 /// ```
24 /// let block_db = BlockDB::open("./data", None).await?;
25 ///
26 /// let block_key = block_db.write(b"Hello World").await?;
27 ///
28 /// let freed_bytes: usize = block_db.free(&block_key).await?;
29 /// ```
30 pub async fn free(
31 &mut self,
32 BlockKey {
33 data_file_id,
34 data_block_id,
35 }: &BlockKey,
36 ) -> Result<usize, Error> {
37 if let Some(data_file) = self.data_files.read().await.get(data_file_id).cloned() {
38 return data_file.write().await.free(data_block_id).await;
39 }
40
41 Ok(0)
42 }
43}