block_db/compact_data_file.rs
1// Authors: Robert Lopez
2
3use super::{
4 error::Error,
5 util::map_data_file::{map_data_file, map_data_files},
6 BlockDB,
7};
8
9impl BlockDB {
10 /// Compacts a `DataFile` by removing all free chunks.
11 ///
12 /// A temporary file is created and all non-free `DataBlock`s are written
13 /// into it. The temporary file is then swapped in to replace the original,
14 /// effectively removing unused space and reducing the file size.
15 ///
16 /// **Note:** This is a relatively heavy operation. It’s recommended to run
17 /// it during initialization (warm-up) or before shutdown (clean-up),
18 /// as freed chunks are already reused automatically during writes.
19 ///
20 /// ---
21 /// - **Atomic**
22 /// - **Corruptible**
23 ///
24 /// ---
25 /// Example
26 /// ```
27 /// let block_db = BlockDB::open("./data", None).await?;
28 ///
29 /// block_db.compact_data_file("3f-6hf").await?;
30 /// ```
31 pub async fn compact_data_file<S: AsRef<str>>(&self, data_file_id: S) -> Result<(), Error> {
32 map_data_file!(self, data_file_id.as_ref(), compact);
33
34 Ok(())
35 }
36
37 /// Compacts each `DataFile` by removing all free chunks.
38 ///
39 /// For every `DataFile`, a temporary file is created and all non-free
40 /// `DataBlock`s are written into it. The temporary file is then swapped
41 /// in to replace the original, effectively removing unused space and
42 /// reducing the file size.
43 ///
44 /// **Note:** This is a relatively heavy operation. It’s recommended to run
45 /// it during initialization (warm-up) or before shutdown (clean-up),
46 /// as freed chunks are already reused automatically during writes.
47 ///
48 /// ---
49 /// - **Atomic**
50 /// - **Corruptible**
51 ///
52 /// ---
53 /// Example
54 /// ```
55 /// let block_db = BlockDB::open("./data", None).await?;
56 ///
57 /// block_db.compact_data_files().await?;
58 /// ```
59 pub async fn compact_data_files(&self) -> Result<(), Error> {
60 map_data_files!(self, compact);
61
62 Ok(())
63 }
64}