Dissolve workspace

This commit is contained in:
Joscha 2022-06-23 12:20:20 +02:00
parent 1cc7dd8920
commit e601476d02
29 changed files with 24 additions and 29 deletions

75
src/vault.rs Normal file
View file

@ -0,0 +1,75 @@
mod euph;
mod migrate;
use std::path::Path;
use std::{fs, thread};
use rusqlite::Connection;
use tokio::sync::{mpsc, oneshot};
use self::euph::{EuphRequest, EuphVault};
enum Request {
Close(oneshot::Sender<()>),
Euph(EuphRequest),
}
pub struct Vault {
tx: mpsc::Sender<Request>,
}
impl Vault {
pub async fn close(&self) {
let (tx, rx) = oneshot::channel();
let _ = self.tx.send(Request::Close(tx)).await;
let _ = rx.await;
}
pub fn euph(&self, room: String) -> EuphVault {
EuphVault {
tx: self.tx.clone(),
room,
}
}
}
fn run(conn: Connection, mut rx: mpsc::Receiver<Request>) {
while let Some(request) = rx.blocking_recv() {
match request {
Request::Close(tx) => {
println!("Optimizing vault");
let _ = conn.execute_batch("PRAGMA optimize");
// Ensure `Vault::close` exits only after the sqlite connection
// has been closed properly.
drop(conn);
drop(tx);
break;
}
Request::Euph(r) => r.perform(&conn),
}
}
}
pub fn launch(path: &Path) -> rusqlite::Result<Vault> {
// If this fails, rusqlite will complain about not being able to open the db
// file, which saves me from adding a separate vault error type.
let _ = fs::create_dir_all(path.parent().expect("path to file"));
let mut conn = Connection::open(path)?;
// Setting locking mode before journal mode so no shared memory files
// (*-shm) need to be created by sqlite. Apparently, setting the journal
// mode is also enough to immediately acquire the exclusive lock even if the
// database was already using WAL.
// https://sqlite.org/pragma.html#pragma_locking_mode
conn.pragma_update(None, "locking_mode", "exclusive")?;
conn.pragma_update(None, "journal_mode", "wal")?;
conn.pragma_update(None, "foreign_keys", true)?;
conn.pragma_update(None, "trusted_schema", false)?;
migrate::migrate(&mut conn)?;
let (tx, rx) = mpsc::channel(8);
thread::spawn(move || run(conn, rx));
Ok(Vault { tx })
}