From 320d15412b0326b96e69e00421e465f6bb9b49b8 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 19 Dec 2023 20:57:54 +0900 Subject: [PATCH] index: let caller of segment-level save-in() squash segments explicitly 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. --- lib/src/default_index/mutable.rs | 15 +++++++-------- lib/src/default_index/store.rs | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/src/default_index/mutable.rs b/lib/src/default_index/mutable.rs index 55ae68c87b..7260225ebd 100644 --- a/lib/src/default_index/mutable.rs +++ b/lib/src/default_index/mutable.rs @@ -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()); @@ -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")) } @@ -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> { - self.0.save_in(dir) + pub(super) fn squash_and_save_in(self, dir: &Path) -> io::Result> { + self.0.maybe_squash_with_ancestors().save_in(dir) } } diff --git a/lib/src/default_index/store.rs b/lib/src/default_index/store.rs index af60248ff8..0fb02fb640 100644 --- a/lib/src/default_index/store.rs +++ b/lib/src/default_index/store.rs @@ -197,7 +197,7 @@ impl DefaultIndexStore { op_id: &OperationId, ) -> Result, 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 {