1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 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)
}
}