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
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ review bots read and write at the same time, written in Rust.
server. `OriginService` (`service.rs`) is the one API over a repository:
the HTTP routes (`http.rs`) and the command line's commands that the API
offers only translate into its calls, so neither duplicates the other.
`sync.rs` shares records with a remote (`remote.rs`), which answers the
same calls over the SDK or, for a local path, in-process.
- `tools/xtask`: development tasks, run with `cargo xtask`, such as timing
origin against git (`just bench-git`).

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 @@ -20,6 +20,7 @@ pub mod remote;
pub mod server;
pub mod service;
pub mod store;
pub mod sync;
pub mod targets;
pub mod tree;
pub mod workspace;
Expand Down
2 changes: 1 addition & 1 deletion apps/origin-src/src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! A remote is a server over HTTP, `http://host[:port]/<repository>`, or a
//! repository on a local path: a server's repository, or a working copy's.
//! A local one is served by an in-process [`OriginService`] run by this
//! node, so both answer the same calls, and syncing never
//! node, so both answer the same calls, and [`sync`](crate::sync) never
//! tells them apart. Calls block: an HTTP remote runs the SDK on a runtime
//! of its own.

Expand Down
83 changes: 57 additions & 26 deletions apps/origin-src/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ impl OriginService {
}
}

/// The service, answering fetches in parts of at most `limit` bytes.
#[cfg(test)]
pub(crate) fn with_pack_limit(self, limit: usize) -> Self {
Self {
pack_limit: limit,
..self
}
}

/// The service, with `index`, just read, as its index.
#[must_use]
pub fn with_index(self, index: Index) -> Self {
Expand Down Expand Up @@ -382,7 +391,7 @@ impl OriginService {
let decoded = object
.decode()
.map_err(|source| ServiceError::Malformed { id, source })?;
for (needed, kind) in Dependencies::of(&decoded) {
for (needed, kind) in Dependencies::of(&decoded).all() {
match held.get(&needed) {
Some(&held) if held != Some(kind) => {
return Err(ServiceError::WrongKind { id, needed, kind });
Expand Down Expand Up @@ -719,54 +728,76 @@ impl OriginService {
}

/// What an object needs the repository to have for it to be usable, as
/// [`OriginService::put_records`] lists, and the kind each must be. A
/// [`OriginService::put_records`] lists, and the kind each must be: the
/// records it refers to, and the content, snapshots and blobs. A
/// revision's predecessors and a statement's payload are history and
/// details: the repository may lack them, and retention may have dropped a
/// payload.
struct Dependencies;
/// payload. An acceptance needs nothing here: its chain brings what it
/// refers to.
#[derive(Debug, Default)]
pub(crate) struct Dependencies {
/// Revisions, statements and acceptances.
pub records: Vec<(ObjectId, ObjectKind)>,
/// Trees and blobs.
pub content: Vec<(ObjectId, ObjectKind)>,
}

impl Dependencies {
fn of(object: &Object) -> Vec<(ObjectId, ObjectKind)> {
pub(crate) fn of(object: &Object) -> Self {
match object {
Object::Blob(_) | Object::Acceptance(_) => Vec::new(),
Object::Tree(tree) => tree
.iter()
.map(|(_, entry)| (entry.id, entry.kind.object_kind()))
.collect(),
Object::Blob(_) | Object::Acceptance(_) => Self::default(),
Object::Tree(tree) => Self {
records: Vec::new(),
content: tree
.iter()
.map(|(_, entry)| (entry.id, entry.kind.object_kind()))
.collect(),
},
Object::Revision(revision) => {
let base = match revision.base {
Base::Acceptance(id) => (id.into(), ObjectKind::Acceptance),
Base::Revision(id) => (id.into(), ObjectKind::Revision),
};
let mut needed = vec![
(revision.snapshot.into(), ObjectKind::Tree),
(revision.description.into(), ObjectKind::Blob),
base,
];
needed.extend(
let mut records = vec![base];
records.extend(
revision
.deps
.iter()
.map(|&id| (id.into(), ObjectKind::Revision)),
);
needed
Self {
records,
content: vec![
(revision.snapshot.into(), ObjectKind::Tree),
(revision.description.into(), ObjectKind::Blob),
],
}
}
Object::Statement(statement) => Self::of_statement(statement),
}
}

/// What a statement needs: its subject, and the statements it
/// supersedes or promotes.
fn of_statement(statement: &Statement) -> Vec<(ObjectId, ObjectKind)> {
pub(crate) fn of_statement(statement: &Statement) -> Self {
let mut dependencies = Self::default();
let subject = (statement.subject.into(), statement.subject.kind());
[Some(subject)]
.into_iter()
.chain(
[statement.supersedes, statement.promoted_from]
.map(|id| id.map(|id| (id.into(), ObjectKind::Statement))),
)
.flatten()
.collect()
match statement.subject {
RecordId::Snapshot(_) => dependencies.content.push(subject),
_ => dependencies.records.push(subject),
}
dependencies.records.extend(
[statement.supersedes, statement.promoted_from]
.into_iter()
.flatten()
.map(|id| (id.into(), ObjectKind::Statement)),
);
dependencies
}

/// Every object needed, and the kind it must be.
pub(crate) fn all(&self) -> impl Iterator<Item = (ObjectId, ObjectKind)> {
self.records.iter().chain(&self.content).copied()
}
}

Expand Down
45 changes: 38 additions & 7 deletions apps/origin-src/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,7 @@ impl<P: Platform> Store<P> {
/// failure may not be durable.
pub fn write_all(&self, objects: &[EncodedObject]) -> Result<usize, StoreError> {
let batch = self.batch();
let written = Self::on_every_core(objects, |chunk| batch.write_all(chunk))
.into_iter()
.sum::<Result<usize, _>>()?;
let written = batch.stage_all(objects)?;
batch.commit()?;
Ok(written)
}
Expand Down Expand Up @@ -327,6 +325,24 @@ impl<P: Platform> Store<P> {
Ok(self.path(id).exists() || self.find_packed(id)?.is_some())
}

/// Those of `ids` the store has, in order.
///
/// # Errors
///
/// Fails like [`Store::contains`].
pub fn present<I: Into<ObjectId> + Copy>(
&self,
ids: impl IntoIterator<Item = I>,
) -> Result<Vec<I>, StoreError> {
let mut present = Vec::new();
for id in ids {
if self.contains(id.into())? {
present.push(id);
}
}
Ok(present)
}

/// When this node stored the object: when its loose file was written,
/// which packing it keeps.
///
Expand Down Expand Up @@ -675,11 +691,26 @@ impl<P: Platform> Batch<'_, P> {
Ok(self.store.packs.find(id).is_some())
}

/// Stages each of `objects` in turn, returning how many were new.
fn write_all(&self, objects: &[EncodedObject]) -> Result<usize, StoreError> {
objects.iter().try_fold(0, |written, object| {
Ok(written + usize::from(self.write(object)?))
/// Makes `path`, a file or a directory written in place, durable before
/// anything the batch stages is published.
pub(crate) fn sync_first(&self, path: impl Into<PathBuf>) {
self.write.sync_with(path);
}

/// Stages `objects` from every core, returning how many were new, as
/// [`Store::write_all`] does.
///
/// # Errors
///
/// Fails like [`Batch::write`].
pub fn stage_all(&self, objects: &[EncodedObject]) -> Result<usize, StoreError> {
Store::<P>::on_every_core(objects, |chunk| {
chunk.iter().try_fold(0, |written, object| {
Ok(written + usize::from(self.write(object)?))
})
})
.into_iter()
.sum()
}

/// Publishes every object staged, durably, and logs the new records in
Expand Down
Loading