use std::path::{Path, PathBuf};
use anyhow::{bail, Context, Result};
use super::{checkpoint_into_container, CheckpointStats, Store};
use crate::container::{Container, HOT_SUFFIX};
use crate::fold::FoldCfg;
pub struct ContainerStore {
store: Option<Store>,
container: PathBuf,
hot: PathBuf,
}
impl ContainerStore {
pub fn open(path: &Path, cfg: FoldCfg) -> Result<ContainerStore> {
let hot = prepare(path)?;
let store = Store::open(&hot, cfg)
.with_context(|| format!("open the working directory for {}", path.display()))?;
Ok(ContainerStore { store: Some(store), container: path.to_path_buf(), hot })
}
pub fn store(&mut self) -> &mut Store {
self.store.as_mut().expect("a ContainerStore holds its store until close")
}
pub fn hot_directory(&self) -> &Path {
&self.hot
}
pub fn checkpoint(&mut self) -> Result<CheckpointStats> {
let store = self.store.as_mut().expect("open");
store.sync()?;
store.flush()?;
checkpoint_into_container(&self.hot, &self.container)
}
pub fn close(mut self) -> Result<CheckpointStats> {
let store = self.store.as_mut().expect("open");
store.sync()?;
store.flush()?;
self.store = None;
settle(&self.container)
}
}
pub fn prepare(container: &Path) -> Result<PathBuf> {
let hot = hot_path(container);
let exists = container.exists();
if hot.exists() {
if exists {
Container::open(container).with_context(|| {
format!("{} has a hot directory but is not a container", container.display())
})?;
}
} else if exists {
let open = Container::open(container)?;
materialize(&open, &hot)?;
} else {
crate::vfs::mkdir_all(&hot)?;
}
Ok(hot)
}
pub fn settle(container: &Path) -> Result<CheckpointStats> {
let hot = hot_path(container);
let stats = checkpoint_into_container(&hot, container)?;
crate::vfs::remove_tree(&hot)
.with_context(|| format!("remove the working directory {}", hot.display()))?;
Ok(stats)
}
pub fn hot_path(container: &Path) -> PathBuf {
let mut name = container.as_os_str().to_os_string();
name.push(HOT_SUFFIX);
PathBuf::from(name)
}
fn materialize(container: &Container, hot: &Path) -> Result<()> {
let staging = hot.with_extension("materializing");
let _ = crate::vfs::remove_tree(&staging);
crate::vfs::mkdir_all(&staging)?;
let mut dirs: std::collections::BTreeSet<PathBuf> = std::collections::BTreeSet::new();
dirs.insert(staging.clone());
let mut buf = vec![0u8; 1 << 20];
for name in container.names().map(String::from).collect::<Vec<_>>() {
let source = container
.extent(&name)
.ok_or_else(|| anyhow::anyhow!("container lost member {name}"))?;
let len = crate::readat::ReadAt::len(&source)?;
let dst = staging.join(&name);
if let Some(parent) = dst.parent() {
crate::vfs::mkdir_all(parent)?;
let mut ancestor = Some(parent);
while let Some(d) = ancestor {
if !d.starts_with(&staging) {
break;
}
dirs.insert(d.to_path_buf());
if d == staging {
break;
}
ancestor = d.parent();
}
}
let f = crate::vfs::create(&dst)?;
let mut at = 0u64;
while at < len {
let take = buf.len().min((len - at) as usize);
crate::readat::ReadAt::read_exact_at(&source, &mut buf[..take], at)?;
crate::vfs::write_all_at(&f, &dst, &buf[..take], at)?;
at += take as u64;
}
crate::vfs::sync_file(&f, &dst)?;
}
if !staging.join("MANIFEST").exists() {
bail!("container holds no MANIFEST, so it names no store to open");
}
let mut ordered: Vec<&PathBuf> = dirs.iter().collect();
ordered.sort_by_key(|d| std::cmp::Reverse(d.components().count()));
for d in ordered {
crate::vfs::sync_dir(d)?;
}
crate::vfs::rename_noreplace(&staging, hot)
.with_context(|| format!("publish the working directory {}", hot.display()))?;
if let Some(parent) = hot.parent() {
let _ = crate::vfs::sync_dir(parent);
}
Ok(())
}