Validate live checkpoint reuse across atomic project file changes

This commit is contained in:
gloamxun committed 2026-11-20 08:26:44 +08:00
1 parent e77b15d8bd
commit 287d4a7cf0
5 files changed
+377 -39

No files matched your search

@@ -109,10 +109,7 @@ impl ProjectSnapshot {
revision: u64,
edits: BTreeMap<PathBuf, Arc<[u8]>>,
) -> Result<Arc<Self>, String> {
self.validate()?;
let mut next = self.clone();
next.version.revision = revision;
next.validated = OnceLock::new();
let mut next = self.next_revision(revision)?;
for (path, data) in edits {
let previous = next.files.get_mut(&path).ok_or_else(|| {
format!("atomic edit requires an immutable file: {}", path.display())
@@ -124,6 +121,44 @@ impl ProjectSnapshot {
next.seal()
}
/// Set or remove immutable files in a single revision. New files require
/// a declared parent directory. Replacing a file preserves its captured
/// modification time, and removing a file discards it.
pub fn with_file_changes(
&self,
revision: u64,
changes: BTreeMap<PathBuf, Option<Arc<[u8]>>>,
) -> Result<Arc<Self>, String> {
let mut next = self.next_revision(revision)?;
for (path, data) in changes {
if let Some(data) = data {
if !next
.file(&path)
.is_some_and(|previous| Arc::ptr_eq(&previous.data, &data))
{
next.files.insert(path.clone(), Self::capture(path, data));
}
} else {
if next.files.remove(&path).is_none() {
return Err(format!(
"atomic removal requires an immutable file: {}",
path.display()
));
}
next.file_modifications.remove(&path);
}
}
next.seal()
}
fn next_revision(&self, revision: u64) -> Result<Self, String> {
self.validate()?;
let mut next = self.clone();
next.version.revision = revision;
next.validated = OnceLock::new();
Ok(next)
}
fn capture(path: PathBuf, data: Arc<[u8]>) -> SnapshotFile {
SnapshotFile {
version: ResourceVersion {
@@ -161,6 +196,12 @@ impl ProjectSnapshot {
/// Whether both snapshots give lookups the same configuration, names,
/// aliases and roles. The bytes and captured times are separate inputs.
pub fn same_lookup_namespace(&self, next: &Self) -> Result<bool, String> {
Ok(self.same_lookup_configuration(next)? && self.files.keys().eq(next.files.keys()))
}
/// Whether search settings, directories, aliases and generated roles agree.
/// File creation and removal can still change lookup results.
pub fn same_lookup_configuration(&self, next: &Self) -> Result<bool, String> {
self.validate()?;
next.validate()?;
Ok(self.configuration == next.configuration
@@ -168,8 +209,7 @@ impl ProjectSnapshot {
&& self.directories == next.directories
&& self.aliases == next.aliases
&& self.generated_roots == next.generated_roots
&& self.generated == next.generated
&& self.files.keys().eq(next.files.keys()))
&& self.generated == next.generated)
}
pub fn generated(&self) -> impl Iterator<Item = &Path> {
self.generated.iter().map(PathBuf::as_path)