block_db/
delete_data_file.rs

1// Authors: Robert Lopez
2
3use super::{error::Error, wal::BlockDBLog, BlockDB, ConfirmDestructiveAction};
4use crate::{uncorrupt::UncorruptAction, util::fs::delete_directory};
5
6impl BlockDB {
7    /// ⚠️ **DELETES a `DataFile`and _all_ of its data. This action is irreversible.**
8    ///
9    /// Requires explicitly passing `ConfirmDestructiveAction::IKnowWhatImDoing`
10    /// to confirm intent.
11    ///
12    /// Use with extreme caution.
13    ///
14    /// ---
15    /// - **Atomic**
16    /// - **Corruptible**
17    ///
18    /// ---
19    /// Example
20    /// ```
21    /// let block_db = BlockDB::open("./data", None).await?;
22    ///
23    /// block_db.delete_data_file("3f-6hf", ConfirmDestructiveAction::IKnowWhatImDoing).await?;
24    /// ```
25    pub async fn delete_data_file<S: AsRef<str>>(
26        &self,
27        data_file_id: S,
28        _: ConfirmDestructiveAction,
29    ) -> Result<(), Error> {
30        let mut wal_writer = self.wal.write().await;
31        self.wal_checkpoint_handler(&mut wal_writer).await?;
32        wal_writer
33            .log(&BlockDBLog::DeleteDataFile(
34                data_file_id.as_ref().to_string(),
35            ))
36            .await?;
37
38        if let Some(data_file) = self.data_files.write().await.remove(data_file_id.as_ref()) {
39            data_file
40                .write()
41                .await
42                .delete()
43                .await
44                .map_err(|err| Error::Corrupted {
45                    err: Box::new(err),
46                    action: UncorruptAction::Recover,
47                })?;
48        }
49
50        Ok(())
51    }
52
53    /// ⚠️ **DELETES _all_ `DataFile`s and _all_ of their data. This action is irreversible.**
54    ///
55    /// Requires explicitly passing `ConfirmDestructiveAction::IKnowWhatImDoing`
56    /// to confirm intent.
57    ///
58    /// Use with extreme caution.
59    ///
60    /// ---
61    /// - **Atomic**
62    /// - **Corruptible**
63    ///
64    /// ---
65    /// Example
66    /// ```
67    /// let block_db = BlockDB::open("./data", None).await?;
68    ///
69    /// block_db.delete_data_files(ConfirmDestructiveAction::IKnowWhatImDoing).await?;
70    /// ```
71    pub async fn delete_data_files(&self, _: ConfirmDestructiveAction) -> Result<(), Error> {
72        let mut wal_writer = self.wal.write().await;
73        self.wal_checkpoint_handler(&mut wal_writer).await?;
74        wal_writer.log(&BlockDBLog::DeleteDataFiles).await?;
75
76        delete_directory(&self.path.join("data_files"))
77            .await
78            .map_err(|err| Error::Corrupted {
79                err: Box::new(err),
80                action: UncorruptAction::Recover,
81            })?;
82
83        Ok(())
84    }
85}