mod places;
pub(in crate::store) mod receipt;
mod stage;
pub use self::places::Listing;
#[cfg(test)]
pub use self::places::Places;
use std::fs;
use std::io::{self, BufRead, BufReader};
use std::path::Path;
use super::RECORD_FILES;
use super::sweep::Selection;
use crate::conversation::ids::parse_conversation_id;
use crate::conversation::rows::TURNS;
use crate::conversation::turn_records::migration::Folded;
use crate::conversation::{Error, Refused};
use crate::limits;
use crate::store::FileStore;
use crate::store::error::MemoryError;
use crate::store::fileio::{atomic_write, private_mkdir, remove_file, remove_tree};
const PREPARE: &str = ".fold-preparing";
const PENDING: &str = ".fold-pending";
const RETIRED: &str = ".fold-retired";
const FILES: &str = "files";
const RECEIPTS: &str = "receipts";
const DISCARD: &str = "discard";
#[cfg(test)]
thread_local! {
pub(super) static STOP: std::cell::Cell<u8> = const { std::cell::Cell::new(0) };
}
fn step(_done: u8) -> Result<(), Error> {
#[cfg(test)]
if STOP.with(std::cell::Cell::get) == _done {
return Err(Error::Value(format!("stopped after fold step {_done}")));
}
Ok(())
}
fn unreadable(id: &str) -> Error {
Refused::format_unsupported(id, "unreadable fold staging").into()
}
impl FileStore {
pub fn fold_records(
&self,
conversation_id: &str,
listing: &mut Listing,
discard_pending: bool,
limits: &limits::Records,
) -> Result<Folded, Error> {
let _held = self.lock()?;
parse_conversation_id(conversation_id)?;
if !self.records_enabled()? {
let why = "records are not enabled on this store; run `neosian records enable`";
return Err(MemoryError::configuration(why).into());
}
let places = self.places_now(conversation_id, listing)?;
if places.staged {
self.settle_sweep(conversation_id)?;
}
if places.stored {
self.settle_stored(conversation_id)?;
}
let mut found = Folded {
conversation_id: conversation_id.to_string(),
..Folded::default()
};
let pending = self.pending_work(conversation_id, places.stored, places.staged)?;
let (drafts, restores) = (pending.drafts.len(), pending.restores.len());
if drafts + restores > 0 && !discard_pending {
return Err(Error::Value(format!(
"conversation {conversation_id} holds pending work ({drafts} open drafts, \
{restores} staged restores); let it finish or pass --discard-pending to \
remove it; nothing in this conversation was folded"
)));
}
(found.drafts, found.restores) = (drafts as u64, restores as u64);
if !places.stored {
self.discard_pending(conversation_id, &pending)?;
return Ok(found);
}
let log = self.conversation_file(conversation_id, TURNS)?;
let preparing = self.conversation_file(conversation_id, PREPARE)?;
private_mkdir(&preparing.join(RECEIPTS))?;
let result = (|| {
if log.is_file() {
let bound = limits.max_turn_bytes;
stage::stage(self, conversation_id, &log, &preparing, bound, &mut found)?;
} else {
atomic_write(&preparing.join(FILES), "", true)?;
}
if found.folded == 0 && drafts + restores == 0 {
return Ok(());
}
let discard = serde_json::to_string(&pending).map_err(io::Error::other)?;
atomic_write(&preparing.join(DISCARD), &discard, true)?;
self.sync(&preparing.join(RECEIPTS).join("entry"))?;
step(1)?;
let pending = self.conversation_file(conversation_id, PENDING)?;
fs::rename(&preparing, &pending)?;
self.sync(&pending)?;
step(2)?;
self.finish_fold(conversation_id, &pending)
})();
remove_tree(&preparing)?;
result.map(|()| found)
}
pub(super) fn settle_fold(&self, id: &str) -> Result<(), Error> {
let directory = self.conversation_dir(id)?;
for name in [PREPARE, RETIRED] {
if directory.join(name).try_exists()? {
remove_tree(&self.conversation_file(id, name)?)?;
}
}
if !directory.join(PENDING).try_exists()? {
return Ok(());
}
let pending = self.conversation_file(id, PENDING)?;
let names = self.stored_names()?;
let owner = names
.iter()
.find(|name| name.as_str() == id)
.or_else(|| names.iter().find(|name| name.eq_ignore_ascii_case(id)))
.ok_or_else(|| unreadable(id))?;
self.finish_fold(owner, &pending)
}
fn finish_fold(&self, id: &str, pending: &Path) -> Result<(), Error> {
let log = self.conversation_file(id, &format!("{PENDING}/{TURNS}"))?;
if log.try_exists()? {
let live = self.conversation_file(id, TURNS)?;
fs::rename(&log, &live)?;
self.sync(&live)?;
}
step(3)?;
let staged = self.conversation_file(id, &format!("{PENDING}/{RECEIPTS}"))?;
for entry in fs::read_dir(&staged).map_err(|_| unreadable(id))? {
let name = entry?.file_name();
let turn = name.to_str().and_then(|name| name.parse::<u64>().ok());
let target = self.folded_file(id, turn.ok_or_else(|| unreadable(id))?)?;
if !target.try_exists()? {
private_mkdir(&self.folded_dir(id)?)?;
fs::rename(staged.join(&name), &target)?;
}
}
self.sync_existing(&self.folded_dir(id)?)?;
step(4)?;
let files = self.conversation_file(id, &format!("{PENDING}/{FILES}"))?;
let list = fs::File::open(&files).map_err(|_| unreadable(id))?;
for line in BufReader::new(list).lines() {
let turn = line?.parse::<u64>().map_err(|_| unreadable(id))?;
remove_file(&self.record_file(id, turn)?)?;
step(5)?;
}
let records = self.conversation_file(id, RECORD_FILES)?;
let _ = fs::remove_dir(&records);
self.sync_existing(&records)?;
let discard = self.conversation_file(id, &format!("{PENDING}/{DISCARD}"))?;
let discard = fs::read(&discard).map_err(|_| unreadable(id))?;
let discard: Selection = serde_json::from_slice(&discard).map_err(|_| unreadable(id))?;
let staged = !discard.restores.is_empty() && self.staged_exactly(id)?;
self.discard_named(id, &discard, staged)?;
step(6)?;
let retired = self.conversation_file(id, RETIRED)?;
fs::rename(pending, &retired)?;
self.sync(&retired)?;
step(7)?;
remove_tree(&retired)?;
self.sync(&retired)?;
Ok(())
}
}