use std::cell::Cell;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::thread;
use std::time::{Duration, Instant};
use anyhow::{Context, Result, anyhow, ensure};
use rusqlite::{Connection, ErrorCode, OptionalExtension};
use zeroize::Zeroizing;
use crate::keys::StoreKey;
pub(super) const BUSY_TIMEOUT: Duration = Duration::from_secs(5);
pub(super) fn open_connection(path: &Path, key: Option<&StoreKey>) -> Result<Connection> {
let conn = Connection::open(path)
.with_context(|| format!("opening sqlite db at {}", path.display()))?;
conn.busy_timeout(BUSY_TIMEOUT)
.context("arming busy_timeout")?;
if let Some(key) = key {
apply_key(&conn, key, path)?;
}
set_wal_mode(&conn)?;
conn.pragma_update(None, "synchronous", "NORMAL")
.context("setting synchronous=NORMAL")?;
Ok(conn)
}
pub(super) fn apply_key(conn: &Connection, key: &StoreKey, path: &Path) -> Result<()> {
require_sqlcipher(conn)
.with_context(|| format!("opening the encrypted store at {}", path.display()))?;
let pragma = Zeroizing::new(format!("PRAGMA key = \"{}\";", key.sql_literal().as_str()));
conn.execute_batch(&pragma)
.context("setting the store key")?;
match conn.query_row("SELECT count(*) FROM sqlite_master", [], |r| {
r.get::<_, i64>(0)
}) {
Ok(_) => Ok(()),
Err(e) if e.sqlite_error_code() == Some(ErrorCode::NotADatabase) => Err(anyhow!(
"the store key does not open {}; it is plaintext or under another key",
path.display()
)),
Err(e) => Err(e).with_context(|| format!("reading {} under the store key", path.display())),
}
}
pub(super) fn require_sqlcipher(conn: &Connection) -> Result<()> {
let version: Option<String> = conn
.query_row("PRAGMA cipher_version", [], |r| r.get(0))
.optional()
.context("reading cipher_version")?;
ensure!(
version.is_some_and(|v| !v.is_empty()),
"this build has no SQLCipher; build second-brain-core with the `encryption` feature"
);
Ok(())
}
pub(crate) fn page_size(conn: &Connection) -> Result<i64> {
let value: rusqlite::types::Value = conn.query_row("PRAGMA page_size", [], |r| r.get(0))?;
match value {
rusqlite::types::Value::Integer(n) => Ok(n),
rusqlite::types::Value::Text(text) => text
.trim()
.parse()
.with_context(|| format!("PRAGMA page_size answered {text:?}")),
other => Err(anyhow!("PRAGMA page_size answered {other:?}")),
}
}
fn set_wal_mode(conn: &Connection) -> Result<()> {
let deadline = Instant::now() + BUSY_TIMEOUT;
loop {
match conn.pragma_update(None, "journal_mode", "WAL") {
Ok(()) => return Ok(()),
Err(err) if is_database_busy(&err) && Instant::now() < deadline => {
thread::sleep(Duration::from_millis(20));
}
Err(err) => return Err(err).context("setting journal_mode=WAL"),
}
}
}
fn is_database_busy(err: &rusqlite::Error) -> bool {
err.sqlite_error_code() == Some(ErrorCode::DatabaseBusy)
}
pub(super) fn default_pool_size() -> usize {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(4)
.clamp(2, 16)
}
thread_local! {
static READ_DEADLINE: Cell<Option<Instant>> = const { Cell::new(None) };
}
pub struct ReadDeadline {
previous: Option<Instant>,
not_send: PhantomData<*const ()>,
}
impl ReadDeadline {
pub fn set(deadline: Instant) -> Self {
ReadDeadline {
previous: READ_DEADLINE.replace(Some(deadline)),
not_send: PhantomData,
}
}
}
impl Drop for ReadDeadline {
fn drop(&mut self) {
READ_DEADLINE.set(self.previous);
}
}
pub(super) struct ReaderPool {
path: PathBuf,
size: usize,
inner: Mutex<PoolState>,
returned: Condvar,
}
struct PoolState {
free: Vec<Connection>,
key: Option<Arc<StoreKey>>,
spares: usize,
draining: bool,
}
impl ReaderPool {
pub(super) fn open(path: &Path, size: usize, key: Option<Arc<StoreKey>>) -> Result<Self> {
let free = (0..size)
.map(|_| open_connection(path, key.as_deref()))
.collect::<Result<Vec<_>>>()?;
Ok(Self {
path: path.to_path_buf(),
size,
inner: Mutex::new(PoolState {
free,
key,
spares: 0,
draining: false,
}),
returned: Condvar::new(),
})
}
pub(super) fn path(&self) -> &Path {
&self.path
}
pub(super) fn key(&self) -> Option<Arc<StoreKey>> {
self.lock().key.clone()
}
pub(super) fn checkout(&self) -> PooledConn<'_> {
let mut state = self.lock();
if let Some(deadline) = READ_DEADLINE.get() {
let wait = deadline.saturating_duration_since(Instant::now());
state = self
.returned
.wait_timeout_while(state, wait, |s| s.free.is_empty() || s.draining)
.unwrap_or_else(|e| e.into_inner())
.0;
if state.free.is_empty() && !state.draining {
state.spares += 1;
let key = state.key.clone();
drop(state);
match open_connection(&self.path, key.as_deref()) {
Ok(conn) => {
return PooledConn {
conn: Some(conn),
pool: self,
spare: true,
};
}
Err(e) => {
tracing::warn!("no read connection to spare past a deadline: {e:#}");
state = self.lock();
state.spares -= 1;
self.returned.notify_all();
}
}
}
}
let mut state = self
.returned
.wait_while(state, |s| s.free.is_empty() || s.draining)
.unwrap_or_else(|e| e.into_inner());
PooledConn {
conn: state.free.pop(),
pool: self,
spare: false,
}
}
fn lock(&self) -> MutexGuard<'_, PoolState> {
self.inner.lock().unwrap_or_else(|e| e.into_inner())
}
pub(super) fn exclusive(&self, wait: Duration) -> Option<Exclusive<'_>> {
let mut state = self.lock();
state.draining = true;
let (mut state, _) = self
.returned
.wait_timeout_while(state, wait, |s| s.free.len() < self.size || s.spares > 0)
.unwrap_or_else(|e| e.into_inner());
if state.free.len() < self.size || state.spares > 0 {
state.draining = false;
self.returned.notify_all();
return None;
}
Some(Exclusive { pool: self, state })
}
}
pub(super) struct Exclusive<'a> {
pool: &'a ReaderPool,
state: MutexGuard<'a, PoolState>,
}
impl Exclusive<'_> {
pub(super) fn close_all(&mut self) {
self.state.free.clear();
}
pub(super) fn refill(mut self, key: Option<Arc<StoreKey>>) -> Result<()> {
let opened = (0..self.pool.size)
.map(|_| open_connection(&self.pool.path, key.as_deref()))
.collect::<Result<Vec<_>>>();
match opened {
Ok(conns) => {
self.state.free = conns;
self.state.key = key;
Ok(())
}
Err(e) => self.fail_reads(e),
}
}
pub(super) fn fail_reads(mut self, cause: anyhow::Error) -> Result<()> {
let placeholders = (0..self.pool.size)
.map(|_| unusable_connection())
.collect::<Result<Vec<_>>>();
match placeholders {
Ok(conns) => {
self.state.free = conns;
Err(cause)
}
Err(p) => Err(cause.context(format!("{p:#}"))),
}
}
}
impl Drop for Exclusive<'_> {
fn drop(&mut self) {
self.state.draining = false;
self.pool.returned.notify_all();
}
}
pub(super) fn unusable_connection() -> Result<Connection> {
let conn = Connection::open_in_memory()?;
conn.pragma_update(None, "query_only", true)?;
Ok(conn)
}
pub(crate) struct PooledConn<'a> {
conn: Option<Connection>,
pool: &'a ReaderPool,
spare: bool,
}
impl std::ops::Deref for PooledConn<'_> {
type Target = Connection;
fn deref(&self) -> &Connection {
self.conn.as_ref().expect("taken only in Drop")
}
}
impl Drop for PooledConn<'_> {
fn drop(&mut self) {
let Some(conn) = self.conn.take() else {
return;
};
if self.spare {
drop(conn);
self.pool.lock().spares -= 1;
} else {
self.pool.lock().free.push(conn);
}
self.pool.returned.notify_all();
}
}
#[cfg(test)]
mod tests {
use std::sync::mpsc;
use std::time::Duration;
use super::*;
fn pool(size: usize) -> (tempfile::TempDir, ReaderPool) {
let dir = tempfile::tempdir().unwrap();
let pool = ReaderPool::open(&dir.path().join("brain.db"), size, None).unwrap();
(dir, pool)
}
#[test]
fn a_read_past_its_deadline_opens_a_spare_connection_instead_of_waiting() {
let (_dir, pool) = pool(1);
let held = pool.checkout();
let (done, answered) = mpsc::channel();
thread::scope(|scope| {
scope.spawn(|| {
let _bound = ReadDeadline::set(Instant::now() + Duration::from_millis(50));
let spare = pool.checkout();
let one: i64 = spare.query_row("SELECT 1", [], |r| r.get(0)).unwrap();
done.send(one).unwrap();
});
let answer = answered.recv_timeout(Duration::from_secs(10));
drop(held);
assert_eq!(
answer,
Ok(1),
"the bounded read waited for the held connection"
);
});
assert_eq!(
pool.lock().free.len(),
1,
"only the pooled connection came back"
);
assert_eq!(pool.lock().spares, 0);
}
#[test]
fn a_read_without_a_deadline_waits_for_a_pooled_connection() {
let (_dir, pool) = pool(1);
let held = pool.checkout();
let (done, answered) = mpsc::channel();
thread::scope(|scope| {
scope.spawn(|| {
let conn = pool.checkout();
done.send(conn.is_autocommit()).unwrap();
});
assert!(answered.recv_timeout(Duration::from_millis(100)).is_err());
drop(held);
assert!(answered.recv_timeout(Duration::from_secs(10)).is_ok());
});
}
#[test]
fn a_deadline_guard_restores_the_one_before_it() {
let outer = Instant::now() + Duration::from_secs(60);
let _outer = ReadDeadline::set(outer);
{
let _inner = ReadDeadline::set(Instant::now());
}
assert_eq!(READ_DEADLINE.get(), Some(outer));
}
}