pub mod datadir;
pub mod lock;
pub mod snapshot;
pub mod wal;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use memfork_core::{Db, Journal, JournalError, Record};
use parking_lot::Mutex;
pub use datadir::{DataDir, Source};
pub use lock::{DirLock, Endpoint, LockError};
pub use snapshot::{Snapshot, DEFAULT_RETENTION};
pub use wal::{FsyncPolicy, WalError};
pub const WAL_FILE: &str = "memfork.wal";
#[derive(Debug, Clone)]
pub struct Options {
pub fsync: FsyncPolicy,
pub retention: u64,
}
impl Default for Options {
fn default() -> Self {
Options {
fsync: FsyncPolicy::default(),
retention: DEFAULT_RETENTION,
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum OpenError {
#[error(transparent)]
Lock(#[from] LockError),
#[error(transparent)]
Wal(#[from] WalError),
#[error(transparent)]
Snapshot(#[from] snapshot::SnapshotError),
#[error("the recorded history could not be replayed: {0}")]
Replay(#[from] memfork_core::Error),
#[error(transparent)]
NoDataDir(#[from] datadir::NoDataDir),
}
#[derive(Debug, Clone, Default)]
pub struct Recovery {
pub from_snapshot: usize,
pub from_wal: usize,
pub torn_tail: Option<String>,
}
impl Recovery {
pub fn is_empty(&self) -> bool {
self.from_snapshot == 0 && self.from_wal == 0
}
}
#[derive(Debug)]
pub struct Store {
dir: PathBuf,
lock: DirLock,
wal: Mutex<wal::Wal>,
options: Options,
}
impl Store {
pub fn open(dir: &Path, options: Options) -> Result<(Db, Arc<Store>, Recovery), OpenError> {
let lock = DirLock::acquire(dir)?;
lock.publish(&Endpoint::for_this_process())?;
let db = Db::new();
let mut recovery = Recovery::default();
if let Some(snap) = snapshot::read(dir)? {
recovery.from_snapshot = snap.records.len();
snapshot::apply(&db, &snap)?;
}
let wal_path = dir.join(WAL_FILE);
let replay = wal::read(&wal_path)?;
recovery.from_wal = replay.records.len();
recovery.torn_tail = replay.torn_tail.clone();
for record in &replay.records {
db.apply_record(record)?;
}
if replay.torn_tail.is_some() {
wal::truncate(&wal_path, replay.valid_len)?;
}
let log = wal::Wal::open(&wal_path, options.fsync)?;
let store = Arc::new(Store {
dir: dir.to_path_buf(),
lock,
wal: Mutex::new(log),
options,
});
db.set_journal(Some(store.clone() as Arc<dyn Journal>));
Ok((db, store, recovery))
}
pub fn dir(&self) -> &Path {
&self.dir
}
pub fn options(&self) -> &Options {
&self.options
}
pub fn publish(&self, endpoint: &Endpoint) -> Result<(), LockError> {
self.lock.publish(endpoint)
}
pub fn flush(&self) -> Result<(), WalError> {
self.wal.lock().flush()
}
pub fn appended(&self) -> u64 {
self.wal.lock().appended()
}
pub fn compact(&self, db: &Db) -> Result<bool, OpenError> {
let retention = self.options.retention;
let branches = db.branches();
let needed = branches.iter().any(|b| b.seq > retention);
if !needed {
return Ok(false);
}
let mut horizon = std::collections::BTreeMap::new();
for b in &branches {
horizon.insert(b.name.clone(), b.seq.saturating_sub(retention));
}
let existing = snapshot::read(&self.dir)?.unwrap_or_default();
let replay = wal::read(&self.dir.join(WAL_FILE))?;
let all: Vec<Record> = existing.records.into_iter().chain(replay.records).collect();
let (kept, carried) = split_at_horizon(all, &horizon);
snapshot::write(
&self.dir,
&Snapshot {
records: carried,
horizon,
},
)?;
let log = self.wal.lock();
let path = log.path().to_path_buf();
drop(log);
wal::reset(&path)?;
let mut fresh = wal::Wal::open(&path, self.options.fsync)?;
for record in &kept {
fresh.append(record)?;
}
fresh.flush()?;
*self.wal.lock() = fresh;
Ok(true)
}
}
fn split_at_horizon(
records: Vec<Record>,
horizon: &std::collections::BTreeMap<String, u64>,
) -> (Vec<Record>, Vec<Record>) {
let mut after = Vec::new();
let mut at_or_before = Vec::new();
for record in records {
let keep_after = match &record {
Record::Commit { branch, seq, .. } => horizon.get(branch).is_some_and(|h| seq > h),
_ => false,
};
if keep_after {
after.push(record);
} else {
at_or_before.push(record);
}
}
(after, at_or_before)
}
impl Journal for Store {
fn append(&self, record: &Record) -> Result<(), JournalError> {
self.wal
.lock()
.append(record)
.map_err(|e| JournalError::new(e.to_string()))
}
}