From f87324bd5b9975debeeafe152878be58577ca695 Mon Sep 17 00:00:00 2001 From: Shubham Mishra Date: Sat, 28 Feb 2026 14:02:10 +0530 Subject: [PATCH 1/3] fix index truncate edge cases --- crates/commitlog/src/index/indexfile.rs | 34 ++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/commitlog/src/index/indexfile.rs b/crates/commitlog/src/index/indexfile.rs index e527ac91186..b2e715f7e10 100644 --- a/crates/commitlog/src/index/indexfile.rs +++ b/crates/commitlog/src/index/indexfile.rs @@ -198,7 +198,14 @@ impl + From> IndexFileMut { let key = key.into(); let (found_key, index) = self .find_index(Key::from(key)) - .map(|(found, index)| (found.into(), index))?; + .map(|(found, index)| (found.into(), index)) + .or_else(|e| { + match e { + // If key is smaller than first entry, truncate all entries + IndexError::KeyNotFound => Ok((key, 0)), + _ => Err(e), + } + })?; // If returned key is smaller than asked key, truncate from next entry self.num_entries = if found_key == key { @@ -505,6 +512,31 @@ mod tests { Ok(()) } + + #[test] + fn test_truncate_eddge_cases() -> Result<(), IndexError> { + // index file with no entries + let mut index = create_and_fill_index(10, 0)?; + + // Truncate from key smaller than first entry should truncate all entries + index.truncate(1)?; + assert_eq!(index.num_entries, 0); + + + // first entry will be with key 2 + let mut index = create_and_fill_index(10, 5)?; + assert_eq!(index.num_entries, 4); + index.truncate(1)?; + assert_eq!(index.num_entries, 0); + + + index.truncate(0)?; + assert_eq!(index.num_entries, 0); + + + Ok(()) + } + #[test] fn test_close_open_index() -> Result<(), IndexError> { // Create a temporary directory for testing From 83f0f86960a097c7cc9c477e41ca694b681dccac Mon Sep 17 00:00:00 2001 From: Shubham Mishra Date: Sat, 28 Feb 2026 21:27:14 +0530 Subject: [PATCH 2/3] lint: white space --- crates/commitlog/src/index/indexfile.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/commitlog/src/index/indexfile.rs b/crates/commitlog/src/index/indexfile.rs index b2e715f7e10..4f8b0979e7a 100644 --- a/crates/commitlog/src/index/indexfile.rs +++ b/crates/commitlog/src/index/indexfile.rs @@ -522,18 +522,15 @@ mod tests { index.truncate(1)?; assert_eq!(index.num_entries, 0); - // first entry will be with key 2 let mut index = create_and_fill_index(10, 5)?; assert_eq!(index.num_entries, 4); index.truncate(1)?; assert_eq!(index.num_entries, 0); - index.truncate(0)?; assert_eq!(index.num_entries, 0); - Ok(()) } From 307ff233df41cce302c50983ca7d81d77e96c324 Mon Sep 17 00:00:00 2001 From: Shubham Mishra Date: Mon, 2 Mar 2026 10:34:58 +0530 Subject: [PATCH 3/3] white space --- crates/commitlog/src/index/indexfile.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/commitlog/src/index/indexfile.rs b/crates/commitlog/src/index/indexfile.rs index 4f8b0979e7a..080fac97472 100644 --- a/crates/commitlog/src/index/indexfile.rs +++ b/crates/commitlog/src/index/indexfile.rs @@ -512,7 +512,6 @@ mod tests { Ok(()) } - #[test] fn test_truncate_eddge_cases() -> Result<(), IndexError> { // index file with no entries