use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard, PoisonError};
use rusqlite::Connection;
use super::capture::{self, Position};
use super::checkpoint::{HARD, restart, restart_again_at};
use super::connection::{self, Role, execute, sql_error};
use super::group::{BEGIN, ROLLBACK};
use super::{Config, Durability};
use crate::{Error, ErrorKind, Result};
const WRITE_PAGE: &str = "insert into sqlite_dbpage (pgno, data) values (?1, ?2)";
const WRITE_FIRST: &str = "insert into sqlite_dbpage (pgno, data) values (1, ?1)";
const WRITE_FIRST_AND_CUT: &str = "insert into sqlite_dbpage (pgno, data) values (1, ?1), (?2, null)";
const PAGES: &str = "pragma page_count";
const CHECKPOINT: &str = super::given::own!("pragma wal_checkpoint(passive)");
const PAGE_SIZE: &str = "pragma page_size";
pub(crate) struct Applier {
path: PathBuf,
durability: Durability,
connection: Mutex<Option<Connection>>,
wal: Mutex<Wal>,
}
#[derive(Default)]
struct Wal {
at: Position,
checkpointed: u32,
restarted: bool,
restart_from: u32,
}
impl Applier {
pub(crate) fn new(path: impl Into<PathBuf>, durability: Durability) -> Applier {
Applier { path: path.into(), durability, connection: Mutex::new(None), wal: Mutex::default() }
}
pub(crate) fn apply(&self, page_size: u32, size: u32, pages: &[(u32, &[u8])]) -> Result<Position> {
let mut held = self.lock();
let connection = self.connection(&mut held, page_size)?;
capture::take_wal_end();
execute(connection, BEGIN).map_err(|error| sql_error("a copy's commit: its transaction", error))?;
let written = write(connection, page_size, size, pages);
let committed =
written.and_then(|()| connection::commit(connection).map_err(|error| sql_error("a copy's commit", error)));
if committed.is_err() && !connection.is_autocommit() {
let _ = execute(connection, ROLLBACK);
}
if pages.iter().any(|&(number, _)| number == 1) || committed.is_err() {
held.take();
}
committed.map_err(|error| error.within(self.path.display()))?;
self.ended(capture::take_wal_end()).map_err(|error| error.within(self.path.display()))
}
fn ended(&self, frame: Option<u32>) -> Result<Position> {
let mut wal = self.wal.lock().unwrap_or_else(PoisonError::into_inner);
let Some(frame) = frame else {
return Ok(wal.at);
};
let follows = !wal.restarted && wal.at != Position::default() && frame > wal.at.frame;
let at = if follows { Position { frame, ..wal.at } } else { capture::wal_at(&self.path, frame)? };
if !at.same_generation(&wal.at) {
(wal.checkpointed, wal.restart_from) = (0, 0);
}
(wal.at, wal.restarted) = (at, false);
Ok(at)
}
pub(crate) fn is_long(&self) -> bool {
let wal = self.wal.lock().unwrap_or_else(PoisonError::into_inner);
wal.at.frame >= wal.checkpointed + super::capture::CHECKPOINT_AT
}
pub(crate) fn checkpoint(&self) -> Result<()> {
let held = self.lock();
let Some(connection) = held.as_ref() else {
return Ok(());
};
let (frame, restart_from) = {
let wal = self.wal.lock().unwrap_or_else(PoisonError::into_inner);
(wal.at.frame, wal.restart_from)
};
let restarting = frame >= HARD && frame >= restart_from;
let checkpointed = if restarting {
restart(connection, Config::default().busy_timeout).map(|(busy, _, _)| busy == 0)
} else {
connection.query_row(CHECKPOINT, [], |_| Ok(false))
};
let started_over = checkpointed.map_err(|error| sql_error(self.path.display().to_string(), error))?;
let mut wal = self.wal.lock().unwrap_or_else(PoisonError::into_inner);
(wal.checkpointed, wal.restarted) = (wal.at.frame, true);
if restarting {
wal.restart_from = if started_over { 0 } else { restart_again_at(frame) };
}
Ok(())
}
pub(crate) fn close(&self) {
self.lock().take();
}
fn connection<'a>(&self, held: &'a mut Option<Connection>, page_size: u32) -> Result<&'a Connection> {
if held.is_none() {
let config = Config { page_size, durability: self.durability, keep_wal: true, ..Config::default() };
let connection = open(&self.path, &config)?;
capture::hook_wal(&connection);
*held = Some(connection);
}
Ok(held.as_ref().expect("opened just above"))
}
fn lock(&self) -> MutexGuard<'_, Option<Connection>> {
self.connection.lock().unwrap_or_else(PoisonError::into_inner)
}
}
pub(crate) fn checkpoint_away(path: &Path) -> Result<()> {
let wal = super::capture::wal_of(path);
if !wal.exists() {
return Ok(());
}
let config = Config { keep_wal: true, ..Config::default() };
let connection = connection::open(path, &config, Role::Writer)?;
let busy: i64 = connection
.query_row(super::file::TRUNCATE, [], |row| row.get(0))
.map_err(|error| sql_error(path.display().to_string(), error))?;
if busy != 0 {
return Err(Error::new(
ErrorKind::Unavailable,
format!("{}: a reader holds its WAL, which a copy that becomes a home empties first", path.display()),
));
}
Ok(())
}
fn open(path: &Path, config: &Config) -> Result<Connection> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|error| Error::io(path.display().to_string(), error))?;
}
let connection = connection::open(path, config, Role::Writer)?;
super::pages::refuse_without_dbpage(&connection, path)?;
Ok(connection)
}
fn write(connection: &Connection, page_size: u32, size: u32, pages: &[(u32, &[u8])]) -> Result<()> {
let own_size: u32 = connection.query_row(PAGE_SIZE, [], |row| row.get(0)).map_err(|e| sql_error("its pages", e))?;
if own_size != page_size {
return Err(Error::new(
ErrorKind::Corrupt,
format!("its pages are {own_size} bytes and its home's {page_size}: it is a copy of another file"),
));
}
let had: u32 = connection.query_row(PAGES, [], |row| row.get(0)).map_err(|e| sql_error("its pages", e))?;
let mut first = None;
let mut statement = connection.prepare_cached(WRITE_PAGE).map_err(|error| sql_error("a page", error))?;
for &(number, page) in pages {
if number == 1 {
first = Some(page);
continue;
}
statement
.execute(rusqlite::params![number, page])
.map_err(|error| sql_error(format!("page {number}"), error))?;
}
drop(statement);
let cut = had.max(1) > size;
match (first, cut) {
(Some(first), true) => connection.execute(WRITE_FIRST_AND_CUT, rusqlite::params![first, size + 1]),
(Some(first), false) => connection.execute(WRITE_FIRST, [first]),
(None, false) => Ok(0),
(None, true) => {
return Err(Error::new(ErrorKind::Corrupt, "a commit that makes the file smaller writes no page 1"));
}
}
.map(drop)
.map_err(|error| sql_error("page 1", error))
}