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
64 changes: 63 additions & 1 deletion apps/origin-src/src/frames.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Append-only files of checked frames, which the [record index](crate::index)
//! is made of.
//! and the [op log](crate::oplog) are made of.
//!
//! A file is a header naming its format and version, then frames. A frame is
//! its payload's `u32` length, the payload, the first 8 bytes of the
Expand Down Expand Up @@ -119,6 +119,62 @@ impl Frames {
(frames, Tail::Clean)
}

/// The frames at the end of a file, decoded by `decode`, oldest first:
/// read backwards from the end, through the last frame `wanted` is true
/// of. Returns `None` if a frame on the way is broken, or if the file
/// has no such frame: then only [`Frames::parse`] can read it.
pub(crate) fn parse_back<T>(
self,
bytes: &[u8],
decode: impl FnMut(&[u8]) -> Result<T, DecodeError>,
wanted: impl FnMut(&T) -> bool,
) -> Option<Vec<T>> {
let (frames, unwanted) = self.back(bytes, decode, wanted);
unwanted.then_some(frames)
}

/// 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.
fn back<T>(
self,
bytes: &[u8],
mut decode: impl FnMut(&[u8]) -> Result<T, DecodeError>,
mut wanted: impl FnMut(&T) -> bool,
) -> (Vec<T>, bool) {
let mut end = bytes.len();
let mut frames = Vec::new();
let unwanted = loop {
let Some((start, frame)) = self.frame_before(bytes, end, &mut decode) else {
break false;
};
if !wanted(&frame) {
break true;
}
frames.push(frame);
end = start;
};
frames.reverse();
(frames, unwanted)
}

/// The frame of `bytes` that ends at `end`, decoded by `decode`, and
/// where it starts, if it's intact and after the header.
fn frame_before<T>(
self,
bytes: &[u8],
end: usize,
decode: &mut impl FnMut(&[u8]) -> Result<T, DecodeError>,
) -> Option<(usize, T)> {
let (_, len) = bytes[..end].split_last_chunk::<4>()?;
let start = end.checked_sub(u32::from_le_bytes(*len) as usize + Self::FRAMING)?;
if start < self.header.len() {
return None;
}
let (payload, _) = Self::payload(&bytes[start..end]).ok()?;
Some((start, decode(payload).ok()?))
}

/// Whether `bytes` end with a frame written in full that starts at or
/// after `offset`, whose trailing length and check are intact. An
/// append cut short never leaves one after it, so a frame at `offset`
Expand Down Expand Up @@ -269,6 +325,12 @@ impl Writer {
self.bytes(&u32::try_from(len).expect("fewer than 2^32").to_le_bytes());
}

/// Bytes preceded by their length.
pub(crate) fn prefixed(&mut self, bytes: &[u8]) {
self.len(bytes.len());
self.bytes(bytes);
}

/// A 0 byte for `None`, or a 1 byte and the id.
pub(crate) fn option(&mut self, id: Option<&[u8; 32]>) {
match id {
Expand Down
21 changes: 16 additions & 5 deletions apps/origin-src/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,21 +469,30 @@ pub enum FindError<I: Prefixed> {
}

/// What the index keeps of one record: its id, and the fields it's looked
/// up by.
/// up by. The op log records batches of them.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Entry {
pub enum Entry {
/// A revision.
Revision {
/// Its id.
id: RevisionId,
/// Its change.
change: ChangeId,
/// The revisions it evolves from.
predecessors: Vec<RevisionId>,
},
/// A statement.
Statement {
/// Its id.
id: StatementId,
/// What it's about.
subject: RecordId,
/// The statement it claims to supersede.
supersedes: Option<StatementId>,
/// Its promotion group, if it's promotable.
group: Option<Digest>,
},
/// An acceptance.
Acceptance(AcceptanceId),
}

Expand Down Expand Up @@ -523,7 +532,9 @@ impl Entry {
})
}

fn record(&self) -> RecordId {
/// The record.
#[must_use]
pub fn record(&self) -> RecordId {
match *self {
Self::Revision { id, .. } => RecordId::Revision(id),
Self::Statement { id, .. } => RecordId::Statement(id),
Expand All @@ -539,7 +550,7 @@ impl Entry {
/// change and a `u32` count of predecessors and their ids, and for a
/// statement its subject's kind and id, and the statement it supersedes
/// and its promotion group, each a 0 byte, or a 1 byte and the id.
fn write(&self, out: &mut Writer) {
pub(crate) fn write(&self, out: &mut Writer) {
Log::write_record(self.record(), out);
match self {
Self::Revision {
Expand Down Expand Up @@ -567,7 +578,7 @@ impl Entry {
}
}

fn read(reader: &mut Reader<'_>) -> Result<Self, DecodeError> {
pub(crate) fn read(reader: &mut Reader<'_>) -> Result<Self, DecodeError> {
Ok(match Log::read_record(reader)? {
RecordId::Revision(id) => {
let change = ChangeId::from_bytes(reader.array()?);
Expand Down
1 change: 1 addition & 0 deletions apps/origin-src/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod conflict;
pub mod fixture;
mod frames;
pub mod index;
pub mod oplog;
pub mod output;
pub mod policy;
pub mod queue;
Expand Down
Loading