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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 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(())
}
}