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..");
|
log::info!(target: "node", "Opening node database..");
|
||||||
let db = home
|
let db = home
|
||||||
.database_mut()?
|
.database_mut()?
|
||||||
.journal_mode(node::db::JournalMode::default())?
|
.journal_mode(node::db::sqlite_ext::JournalMode::WAL)?
|
||||||
.init(
|
.init(
|
||||||
&id,
|
&id,
|
||||||
announcement.features,
|
announcement.features,
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ use crate::node::{
|
||||||
};
|
};
|
||||||
use crate::sql::transaction;
|
use crate::sql::transaction;
|
||||||
|
|
||||||
|
pub mod sqlite_ext;
|
||||||
|
|
||||||
/// How long to wait for the database lock to be released before failing a read.
|
/// 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);
|
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.
|
/// How long to wait for the database lock to be released before failing a write.
|
||||||
|
|
@ -49,19 +51,6 @@ pub enum Error {
|
||||||
NoRows,
|
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.
|
/// A file-backed database storing information about the network.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
|
|
@ -107,7 +96,7 @@ impl Database {
|
||||||
pub fn reader<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
|
pub fn reader<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
|
||||||
let mut db = sql::Connection::open_thread_safe_with_flags(
|
let mut db = sql::Connection::open_thread_safe_with_flags(
|
||||||
path,
|
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.set_busy_timeout(DB_READ_TIMEOUT.as_millis() as usize)?;
|
||||||
db.execute(Self::PRAGMA)?;
|
db.execute(Self::PRAGMA)?;
|
||||||
|
|
@ -115,16 +104,9 @@ impl Database {
|
||||||
Ok(Self { db: Arc::new(db) })
|
Ok(Self { db: Arc::new(db) })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set journal mode.
|
/// Set `journal_mode` pragma.
|
||||||
pub fn journal_mode(self, mode: JournalMode) -> Result<Self, Error> {
|
pub fn journal_mode(self, mode: sqlite_ext::JournalMode) -> Result<Self, Error> {
|
||||||
match mode {
|
self.db.execute(format!("PRAGMA journal_mode = {mode};"))?;
|
||||||
JournalMode::Rollback => {
|
|
||||||
self.db.execute("PRAGMA journal_mode = DELETE;")?;
|
|
||||||
}
|
|
||||||
JournalMode::WriteAheadLog => {
|
|
||||||
self.db.execute("PRAGMA journal_mode = WAL;")?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(self)
|
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.policies_mut()?;
|
||||||
home.notifications_mut()?;
|
home.notifications_mut()?;
|
||||||
home.database_mut()?
|
home.database_mut()?
|
||||||
.journal_mode(node::db::JournalMode::default())?
|
.journal_mode(node::db::sqlite_ext::JournalMode::WAL)?
|
||||||
.init(
|
.init(
|
||||||
&public_key,
|
&public_key,
|
||||||
config.node.features(),
|
config.node.features(),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue