Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/origin-src/src/frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ impl Frames {
unwanted.then_some(frames)
}

/// The intact frames at the end of a file, decoded by `decode`, oldest
/// first: read backwards from the end, up to a broken one or the
/// header. They are those after damage that [`Frames::parse`] stops at.
pub(crate) fn parse_tail<T>(
self,
bytes: &[u8],
decode: impl FnMut(&[u8]) -> Result<T, DecodeError>,
) -> Vec<T> {
self.back(bytes, decode, |_| true).0
}

/// Reads frames backwards from the end of `bytes`, oldest first, while
/// `wanted` is true of them, up to a broken one or the header. Returns
/// them, and whether it stopped at one `wanted` is false of.
Expand Down
29 changes: 27 additions & 2 deletions apps/origin-src/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,17 @@ impl Index {

/// Adds the published records of the op log entries after the last one
/// indexed, and appends them to the index, as the module explains.
/// Returns `false` if the op log no longer has them: then only a
/// rebuild can catch up.
/// Returns `false` if the op log no longer has them, or damage lost
/// some: then only a rebuild can catch up.
fn catch_up<P: Platform>(&mut self, store: &Store<P>) -> Result<bool, StoreError> {
let oplog = store.oplog();
let read = match oplog.after(self.applied) {
Err(StoreError::Expired { .. }) => return Ok(false),
read => read?,
};
if read.damage.is_some() {
return Ok(false);
}
let Some(last) = read.entries.last() else {
return Ok(true);
};
Expand Down Expand Up @@ -913,6 +916,7 @@ mod tests {
use origin_shared::{Base, BlobId, Inputs, Kind, Record, Tree, Verdict};

use super::*;
use crate::oplog::FramePart;

fn store() -> (tempfile::TempDir, Store) {
let dir = tempfile::tempdir().unwrap();
Expand Down Expand Up @@ -1230,6 +1234,27 @@ mod tests {
assert_eq!(reopened.heads(change(1)), [revision_id(&written)]);
}

#[test]
fn op_log_damage_rebuilds_the_index() {
let (_dir, store) = store();
Index::open(&store).unwrap();
let written: Vec<_> = (1..=3)
.map(|seed| revision(change(seed), &[], "lost"))
.collect();
for revision in &written {
store.write_all(slice::from_ref(revision)).unwrap();
}
// The entry of the second revision: the store has it, the op log
// no longer says so.
store
.oplog()
.damage(store.oplog().last_seq().unwrap() - 1, FramePart::Check);
let index = Index::open(&store).unwrap();
for (seed, revision) in (1..).zip(&written) {
assert_eq!(index.heads(change(seed)), [revision_id(revision)]);
}
}

#[test]
fn rebuilds_once_the_op_log_dropped_what_it_missed() {
let (_dir, store) = store();
Expand Down
Loading