Skip to content

Commit 10d5bcb

Browse files
authored
refactor(workspace): hand the snapshot's index to the command (#100)
Every command that reads the working copy opened the record index again after its snapshot, and saving a revision opened it once more, through the annotations, to check whether the change diverged. The snapshot now reads the index once and hands it to the command in `WorkingCopy`. Saving adds the records it just wrote with `Index::add`, so the index knows the new revision as one read afterwards would, without reading it again. Divergence is checked against that index, and the annotations are only opened when the change did diverge. This is groundwork for workspaces: later in the stack, the snapshot also uses this index to tell whether the working copy's change moved on without it. > [!NOTE] > Written by Claude Opus 5.5 in Claude Code (via T3 Code)
1 parent 255ba78 commit 10d5bcb

4 files changed

Lines changed: 98 additions & 67 deletions

File tree

‎apps/origin-src/src/index.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,28 @@ impl Index {
227227
Ok((index, tail != Tail::Damaged))
228228
}
229229

230+
/// Adds the records among `objects`, which this process just wrote, so this
231+
/// index knows them without being read again.
232+
///
233+
/// # Errors
234+
///
235+
/// Fails if a record doesn't decode.
236+
pub(crate) fn add<'o>(
237+
&mut self,
238+
objects: impl IntoIterator<Item = &'o EncodedObject>,
239+
) -> Result<(), StoreError> {
240+
for object in objects {
241+
let entry = Entry::of(object).map_err(|source| StoreError::Decode {
242+
id: object.id(),
243+
source,
244+
})?;
245+
if let Some(entry) = entry {
246+
self.insert(entry);
247+
}
248+
}
249+
Ok(())
250+
}
251+
230252
/// Adds a record, unless it's already indexed.
231253
fn insert(&mut self, entry: Entry) {
232254
if !self.known.insert(entry.record()) {

‎apps/origin-src/src/workspace/annotate.rs‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ impl WorkingCopy {
2424
/// unique prefix of its id, a change for its head, or a revision. A
2525
/// prefix naming a change and another revision than its head is
2626
/// ambiguous.
27-
fn find_revision(&self, index: &Index, rev: &str) -> Result<RevisionId, WorkspaceError> {
27+
fn find_revision(&self, rev: &str) -> Result<RevisionId, WorkspaceError> {
2828
if rev == "@" {
2929
return Ok(self.id);
3030
}
31+
let index = &self.index;
3132
match (index.find_change(rev), index.find_revision(rev)) {
3233
(Err(FindError::NotFound(_)), revision) => Ok(revision?),
3334
(Ok(change), Err(FindError::NotFound(_))) => Workspace::single_head(index, change),
@@ -44,14 +45,10 @@ impl WorkingCopy {
4445

4546
/// The record `annotated` names, as [`WorkingCopy::find_revision`]
4647
/// finds a revision, or a statement by a unique prefix of its id.
47-
fn find_record(
48-
&self,
49-
index: &Index,
50-
annotated: Annotated<'_>,
51-
) -> Result<RecordId, WorkspaceError> {
48+
fn find_record(&self, annotated: Annotated<'_>) -> Result<RecordId, WorkspaceError> {
5249
Ok(match annotated {
53-
Annotated::Revision(rev) => RecordId::Revision(self.find_revision(index, rev)?),
54-
Annotated::Statement(prefix) => RecordId::Statement(index.find_statement(prefix)?),
50+
Annotated::Revision(rev) => RecordId::Revision(self.find_revision(rev)?),
51+
Annotated::Statement(prefix) => RecordId::Statement(self.index.find_statement(prefix)?),
5552
})
5653
}
5754
}
@@ -99,16 +96,21 @@ impl Workspace {
9996
Principal::Node(self.config.node().clone())
10097
}
10198

102-
/// Issues `core.divergent` if `revision`, just saved, made its change
103-
/// diverge. The statement is the same for the same heads, so checking
104-
/// again writes nothing.
105-
pub(super) fn flag_divergence(&self, revision: RevisionId) -> Result<(), WorkspaceError> {
99+
/// Issues a `core.divergent` statement if saving `revision` made its change
100+
/// diverge, as `index`, which has `revision`, shows. The statement is the same
101+
/// for the same heads, so calling this again writes nothing.
102+
pub(super) fn flag_divergence(
103+
&self,
104+
index: &Index,
105+
revision: RevisionId,
106+
) -> Result<(), WorkspaceError> {
106107
let revision: Revision = self.store.get(revision)?;
107108
// A new change has a single revision.
108-
if revision.predecessors.is_empty() {
109+
if revision.predecessors.is_empty() || !index.is_divergent(revision.change) {
109110
return Ok(());
110111
}
111-
Annotations::open(&self.store)?.flag_divergence(revision.change, self.node_principal())?;
112+
Annotations::new(&self.store, index.clone())
113+
.flag_divergence(revision.change, self.node_principal())?;
112114
Ok(())
113115
}
114116

@@ -135,8 +137,8 @@ impl Workspace {
135137
return Err(WorkspaceError::ReservedKind(kind));
136138
}
137139
let working_copy = self.refresh()?;
138-
let mut annotations = Annotations::open(&self.store)?;
139-
let subject = working_copy.find_record(annotations.index(), annotated)?;
140+
let subject = working_copy.find_record(annotated)?;
141+
let mut annotations = Annotations::new(&self.store, working_copy.index);
140142
let payload = payload.map(Payload::blob).transpose()?;
141143
let statement = Statement {
142144
subject,
@@ -232,8 +234,8 @@ impl Workspace {
232234
/// Fails if the revision cannot be found or a record cannot be read.
233235
pub fn statements(&mut self, rev: &str) -> Result<(), WorkspaceError> {
234236
let working_copy = self.snapshot()?;
235-
let annotations = Annotations::open(&self.store)?;
236-
let revision = working_copy.find_revision(annotations.index(), rev)?;
237+
let revision = working_copy.find_revision(rev)?;
238+
let annotations = Annotations::new(&self.store, working_copy.index);
237239
let subject = RecordId::Revision(revision);
238240
if annotations.index().statements(subject).is_empty() {
239241
println!("No statements about revision {}", revision.short());
@@ -326,8 +328,8 @@ impl Workspace {
326328
/// written.
327329
pub fn promote(&mut self, rev: &str) -> Result<(), WorkspaceError> {
328330
let working_copy = self.refresh()?;
329-
let mut annotations = Annotations::open(&self.store)?;
330-
let revision = working_copy.find_revision(annotations.index(), rev)?;
331+
let revision = working_copy.find_revision(rev)?;
332+
let mut annotations = Annotations::new(&self.store, working_copy.index);
331333
let promoted = annotations.promote(RecordId::Revision(revision), &self.node_principal())?;
332334
if promoted.is_empty() {
333335
println!("Nothing to promote to revision {}", revision.short());

‎apps/origin-src/src/workspace/land.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use origin_shared::{
1414
};
1515

1616
use super::{WorkingCopy, Workspace, WorkspaceError};
17-
use crate::index::Index;
1817
use crate::policy::Policy;
1918
use crate::queue::{Blocked, Landing, Pending, Proposal, Queue};
2019

@@ -33,11 +32,10 @@ impl Workspace {
3332
onto: Option<TargetName>,
3433
) -> Result<(), WorkspaceError> {
3534
let working_copy = self.refresh()?;
36-
let index = Index::open(&self.store)?;
37-
let change = working_copy.find_change(&index, change.unwrap_or("@"))?;
35+
let change = working_copy.find_change(change.unwrap_or("@"))?;
3836
let onto = match onto {
3937
Some(onto) => onto,
40-
None => self.target_of(Self::single_head(&index, change)?)?,
38+
None => self.target_of(Self::single_head(&working_copy.index, change)?)?,
4139
};
4240
let key = self.config.node_key()?;
4341
let landings =

0 commit comments

Comments
 (0)