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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Authors: Robert Lopez
use super::{error::Error, wal::BlockDBLog, BlockDB, ConfirmDestructiveAction};
use crate::{uncorrupt::UncorruptAction, util::fs::delete_directory};
impl BlockDB {
/// ⚠️ **DELETES a `DataFile`and _all_ of its data. This action is irreversible.**
///
/// Requires explicitly passing `ConfirmDestructiveAction::IKnowWhatImDoing`
/// to confirm intent.
///
/// Use with extreme caution.
///
/// ---
/// - **Atomic**
/// - **Corruptible**
///
/// ---
/// Example
/// ```
/// let block_db = BlockDB::open("./data", None).await?;
///
/// block_db.delete_data_file("3f-6hf", ConfirmDestructiveAction::IKnowWhatImDoing).await?;
/// ```
pub async fn delete_data_file<S: AsRef<str>>(
&self,
data_file_id: S,
_: ConfirmDestructiveAction,
) -> Result<(), Error> {
let mut wal_writer = self.wal.write().await;
self.wal_checkpoint_handler(&mut wal_writer).await?;
wal_writer
.log(&BlockDBLog::DeleteDataFile(
data_file_id.as_ref().to_string(),
))
.await?;
if let Some(data_file) = self.data_files.write().await.remove(data_file_id.as_ref()) {
data_file
.write()
.await
.delete()
.await
.map_err(|err| Error::Corrupted {
err: Box::new(err),
action: UncorruptAction::Recover,
})?;
}
Ok(())
}
/// ⚠️ **DELETES _all_ `DataFile`s and _all_ of their data. This action is irreversible.**
///
/// Requires explicitly passing `ConfirmDestructiveAction::IKnowWhatImDoing`
/// to confirm intent.
///
/// Use with extreme caution.
///
/// ---
/// - **Atomic**
/// - **Corruptible**
///
/// ---
/// Example
/// ```
/// let block_db = BlockDB::open("./data", None).await?;
///
/// block_db.delete_data_files(ConfirmDestructiveAction::IKnowWhatImDoing).await?;
/// ```
pub async fn delete_data_files(&self, _: ConfirmDestructiveAction) -> Result<(), Error> {
let mut wal_writer = self.wal.write().await;
self.wal_checkpoint_handler(&mut wal_writer).await?;
wal_writer.log(&BlockDBLog::DeleteDataFiles).await?;
delete_directory(&self.path.join("data_files"))
.await
.map_err(|err| Error::Corrupted {
err: Box::new(err),
action: UncorruptAction::Recover,
})?;
Ok(())
}
}