radicle/node/db: Directly represent SQLite pragmas
Instead of introducing our own names and aliases, directly model SQLite pragmas with the defaults that they also take in SQLite, to avoid confusion. Co-authored-by: Lorenz Leutgeb <lorenz.leutgeb@radicle.xyz>
This commit is contained in:
parent
9ff67562cb
commit
d36bf41f25
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -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(),
|
||||
|
|
|
|||
Loading…
Reference in New Issue