Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
radicle/node/db: Directly represent SQLite pragmas
Yorgos Saslis committed 1 month ago
commit d36bf41f254f1097077dedc8a0c164ad61f93810
parent 9ff6756
4 files changed +40 -26
modified crates/radicle-node/src/runtime.rs
@@ -177,7 +177,7 @@ impl Runtime {
        log::info!(target: "node", "Opening node database..");
        let db = home
            .database_mut()?
-
            .journal_mode(node::db::JournalMode::default())?
+
            .journal_mode(node::db::sqlite_ext::JournalMode::WAL)?
            .init(
                &id,
                announcement.features,
modified crates/radicle/src/node/db.rs
@@ -20,6 +20,8 @@ use crate::node::{
};
use crate::sql::transaction;

+
pub mod sqlite_ext;
+

/// How long to wait for the database lock to be released before failing a read.
const DB_READ_TIMEOUT: time::Duration = time::Duration::from_secs(3);
/// How long to wait for the database lock to be released before failing a write.
@@ -49,19 +51,6 @@ pub enum Error {
    NoRows,
}

-
/// Database journal mode.
-
#[derive(Debug, Default, Copy, Clone, serde::Serialize, serde::Deserialize)]
-
#[serde(rename_all = "camelCase")]
-
pub enum JournalMode {
-
    /// "WAL" mode. Good for concurrent reads & writes, but keeps some extra files around.
-
    #[serde(rename = "wal")]
-
    #[default]
-
    WriteAheadLog,
-
    /// Default "rollback" mode. Certain writes may block reads.
-
    #[serde(alias = "rollback")]
-
    Rollback,
-
}
-

/// A file-backed database storing information about the network.
#[derive(Clone)]
pub struct Database {
@@ -107,7 +96,7 @@ impl Database {
    pub fn reader<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
        let mut db = sql::Connection::open_thread_safe_with_flags(
            path,
-
            sqlite::OpenFlags::new().with_read_only(),
+
            sql::OpenFlags::new().with_read_only(),
        )?;
        db.set_busy_timeout(DB_READ_TIMEOUT.as_millis() as usize)?;
        db.execute(Self::PRAGMA)?;
@@ -115,16 +104,9 @@ impl Database {
        Ok(Self { db: Arc::new(db) })
    }

-
    /// Set journal mode.
-
    pub fn journal_mode(self, mode: JournalMode) -> Result<Self, Error> {
-
        match mode {
-
            JournalMode::Rollback => {
-
                self.db.execute("PRAGMA journal_mode = DELETE;")?;
-
            }
-
            JournalMode::WriteAheadLog => {
-
                self.db.execute("PRAGMA journal_mode = WAL;")?;
-
            }
-
        }
+
    /// Set `journal_mode` pragma.
+
    pub fn journal_mode(self, mode: sqlite_ext::JournalMode) -> Result<Self, Error> {
+
        self.db.execute(format!("PRAGMA journal_mode = {mode};"))?;
        Ok(self)
    }

added crates/radicle/src/node/db/sqlite_ext.rs
@@ -0,0 +1,32 @@
+
//! This module contains definitions for use with the `sqlite` crate.
+

+
/// Value for a `journal_mode` pragma statement.
+
/// For a description of all variants please refer to
+
/// <https://sqlite.org/pragma.html#pragma_journal_mode>.
+
/// Note that when SQLite documentation talks about "the application",
+
/// the application linked against this crate, e.g. Radicle Node, Radicle CLI,
+
/// and others, is meant.
+
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
+
pub enum JournalMode {
+
    #[default]
+
    DELETE,
+
    TRUNCATE,
+
    PERSIST,
+
    MEMORY,
+
    WAL,
+
    OFF,
+
}
+

+
impl std::fmt::Display for JournalMode {
+
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+
        f.write_str(match self {
+
            Self::DELETE => "DELETE",
+
            Self::TRUNCATE => "TRUNCATE",
+
            Self::PERSIST => "PERSIST",
+
            Self::MEMORY => "MEMORY",
+
            Self::WAL => "WAL",
+
            Self::OFF => "OFF",
+
        })
+
    }
+
}
modified crates/radicle/src/profile.rs
@@ -253,7 +253,7 @@ impl Profile {
        home.policies_mut()?;
        home.notifications_mut()?;
        home.database_mut()?
-
            .journal_mode(node::db::JournalMode::default())?
+
            .journal_mode(node::db::sqlite_ext::JournalMode::WAL)?
            .init(
                &public_key,
                config.node.features(),