Skip to content

Commit

Permalink
index: let caller of segment-level save-in() squash segments explicitly
Browse files Browse the repository at this point in the history
There are many unit tests that call mutable_segment.save_in(), but I don't
think these callers expect that the segment file could be squashed depending
on the size. Let's make it caller's responsibility.

maybe_squash_with_ancestors() should be cheap if segment_num_commits() == 0,
so it's okay to call it before checking the emptiness.
  • Loading branch information
yuja committed Dec 23, 2023
1 parent 1d80bbb commit 320d154
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
15 changes: 7 additions & 8 deletions lib/src/default_index/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,10 @@ impl MutableIndexSegment {
return Ok(self.parent_file.unwrap());
}

let squashed = self.maybe_squash_with_ancestors();
let mut buf = Vec::new();
squashed.serialize_parent_filename(&mut buf);
self.serialize_parent_filename(&mut buf);
let local_entries_offset = buf.len();
squashed.serialize_local_entries(&mut buf);
self.serialize_local_entries(&mut buf);
let mut hasher = Blake2b512::new();
hasher.update(&buf);
let index_file_id_hex = hex::encode(hasher.finalize());
Expand All @@ -291,9 +290,9 @@ impl MutableIndexSegment {
Ok(ReadonlyIndexSegment::load_with_parent_file(
&mut &buf[local_entries_offset..],
index_file_id_hex,
squashed.parent_file,
squashed.commit_id_length,
squashed.change_id_length,
self.parent_file,
self.commit_id_length,
self.change_id_length,
)
.expect("in-memory index data should be valid and readable"))
}
Expand Down Expand Up @@ -405,8 +404,8 @@ impl DefaultMutableIndex {
self.0.add_commit_data(commit_id, change_id, parent_ids);
}

pub(super) fn save_in(self, dir: &Path) -> io::Result<Arc<ReadonlyIndexSegment>> {
self.0.save_in(dir)
pub(super) fn squash_and_save_in(self, dir: &Path) -> io::Result<Arc<ReadonlyIndexSegment>> {
self.0.maybe_squash_with_ancestors().save_in(dir)
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/default_index/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl DefaultIndexStore {
op_id: &OperationId,
) -> Result<Arc<ReadonlyIndexSegment>, DefaultIndexStoreError> {
let index_segment = mutable_index
.save_in(&self.dir)
.squash_and_save_in(&self.dir)
.map_err(DefaultIndexStoreError::SaveIndex)?;
self.associate_file_with_operation(&index_segment, op_id)
.map_err(|source| DefaultIndexStoreError::AssociateIndex {
Expand Down

0 comments on commit 320d154

Please sign in to comment.