From 0d995dff198dc9be0cdf99a844a94b758ac68f6d Mon Sep 17 00:00:00 2001 From: Alex Chi Date: Wed, 24 Jan 2024 14:39:00 +0800 Subject: [PATCH] fix clippy warnings Signed-off-by: Alex Chi --- .../src/bin/compaction-simulator.rs | 10 +++++-- mini-lsm-starter/src/compact.rs | 23 ++++++++------- mini-lsm/src/compact.rs | 28 +++++++++---------- mini-lsm/src/compact/leveled.rs | 2 +- mini-lsm/src/iterators/concat_iterator.rs | 24 +++++++--------- mini-lsm/src/iterators/two_merge_iterator.rs | 6 ++-- mini-lsm/src/lsm_storage.rs | 2 +- mini-lsm/src/manifest.rs | 4 +-- mini-lsm/src/tests/week1_day7.rs | 2 +- 9 files changed, 50 insertions(+), 51 deletions(-) diff --git a/mini-lsm-starter/src/bin/compaction-simulator.rs b/mini-lsm-starter/src/bin/compaction-simulator.rs index f91058f..3e9d023 100644 --- a/mini-lsm-starter/src/bin/compaction-simulator.rs +++ b/mini-lsm-starter/src/bin/compaction-simulator.rs @@ -70,6 +70,12 @@ pub struct MockStorage { total_writes: usize, } +impl Default for MockStorage { + fn default() -> Self { + Self::new() + } +} + impl MockStorage { pub fn new() -> Self { let snapshot = LsmStorageState { @@ -206,8 +212,8 @@ fn generate_random_split( let ne = begin + len * (i + 1) / split - 1; let mut begin_bytes = BytesMut::new(); let mut end_bytes = BytesMut::new(); - begin_bytes.put_u64(nb as u64); - end_bytes.put_u64(ne as u64); + begin_bytes.put_u64(nb); + end_bytes.put_u64(ne); result.push((begin_bytes.into(), end_bytes.into())); } result diff --git a/mini-lsm-starter/src/compact.rs b/mini-lsm-starter/src/compact.rs index 270931e..b0debfb 100644 --- a/mini-lsm-starter/src/compact.rs +++ b/mini-lsm-starter/src/compact.rs @@ -51,13 +51,13 @@ impl CompactionController { pub fn generate_compaction_task(&self, snapshot: &LsmStorageState) -> Option { match self { CompactionController::Leveled(ctrl) => ctrl - .generate_compaction_task(&snapshot) + .generate_compaction_task(snapshot) .map(CompactionTask::Leveled), CompactionController::Simple(ctrl) => ctrl - .generate_compaction_task(&snapshot) + .generate_compaction_task(snapshot) .map(CompactionTask::Simple), CompactionController::Tiered(ctrl) => ctrl - .generate_compaction_task(&snapshot) + .generate_compaction_task(snapshot) .map(CompactionTask::Tiered), CompactionController::NoCompaction => unreachable!(), } @@ -71,13 +71,13 @@ impl CompactionController { ) -> (LsmStorageState, Vec) { match (self, task) { (CompactionController::Leveled(ctrl), CompactionTask::Leveled(task)) => { - ctrl.apply_compaction_result(&snapshot, task, output) + ctrl.apply_compaction_result(snapshot, task, output) } (CompactionController::Simple(ctrl), CompactionTask::Simple(task)) => { - ctrl.apply_compaction_result(&snapshot, task, output) + ctrl.apply_compaction_result(snapshot, task, output) } (CompactionController::Tiered(ctrl), CompactionTask::Tiered(task)) => { - ctrl.apply_compaction_result(&snapshot, task, output) + ctrl.apply_compaction_result(snapshot, task, output) } _ => unreachable!(), } @@ -86,11 +86,10 @@ impl CompactionController { impl CompactionController { pub fn flush_to_l0(&self) -> bool { - if let Self::Leveled(_) | Self::Simple(_) | Self::NoCompaction = self { - true - } else { - false - } + matches!( + self, + Self::Leveled(_) | Self::Simple(_) | Self::NoCompaction + ) } } @@ -164,6 +163,6 @@ impl LsmStorageInner { } } }); - return Ok(Some(handle)); + Ok(Some(handle)) } } diff --git a/mini-lsm/src/compact.rs b/mini-lsm/src/compact.rs index fca211c..19146dc 100644 --- a/mini-lsm/src/compact.rs +++ b/mini-lsm/src/compact.rs @@ -57,13 +57,13 @@ impl CompactionController { pub fn generate_compaction_task(&self, snapshot: &LsmStorageState) -> Option { match self { CompactionController::Leveled(ctrl) => ctrl - .generate_compaction_task(&snapshot) + .generate_compaction_task(snapshot) .map(CompactionTask::Leveled), CompactionController::Simple(ctrl) => ctrl - .generate_compaction_task(&snapshot) + .generate_compaction_task(snapshot) .map(CompactionTask::Simple), CompactionController::Tiered(ctrl) => ctrl - .generate_compaction_task(&snapshot) + .generate_compaction_task(snapshot) .map(CompactionTask::Tiered), CompactionController::NoCompaction => unreachable!(), } @@ -77,13 +77,13 @@ impl CompactionController { ) -> (LsmStorageState, Vec) { match (self, task) { (CompactionController::Leveled(ctrl), CompactionTask::Leveled(task)) => { - ctrl.apply_compaction_result(&snapshot, task, output) + ctrl.apply_compaction_result(snapshot, task, output) } (CompactionController::Simple(ctrl), CompactionTask::Simple(task)) => { - ctrl.apply_compaction_result(&snapshot, task, output) + ctrl.apply_compaction_result(snapshot, task, output) } (CompactionController::Tiered(ctrl), CompactionTask::Tiered(task)) => { - ctrl.apply_compaction_result(&snapshot, task, output) + ctrl.apply_compaction_result(snapshot, task, output) } _ => unreachable!(), } @@ -92,11 +92,10 @@ impl CompactionController { impl CompactionController { pub fn flush_to_l0(&self) -> bool { - if let Self::Leveled(_) | Self::Simple(_) | Self::NoCompaction = self { - true - } else { - false - } + matches!( + self, + Self::Leveled(_) | Self::Simple(_) | Self::NoCompaction + ) } } @@ -379,10 +378,11 @@ impl LsmStorageInner { } fn trigger_flush(&self) -> Result<()> { - if { + let res = { let state = self.state.read(); state.imm_memtables.len() >= self.options.num_memtable_limit - } { + }; + if res { self.force_flush_next_imm_memtable()?; } @@ -405,6 +405,6 @@ impl LsmStorageInner { } } }); - return Ok(Some(handle)); + Ok(Some(handle)) } } diff --git a/mini-lsm/src/compact/leveled.rs b/mini-lsm/src/compact/leveled.rs index a4fe1e5..a44b8b1 100644 --- a/mini-lsm/src/compact/leveled.rs +++ b/mini-lsm/src/compact/leveled.rs @@ -78,7 +78,7 @@ impl LeveledCompactionController { .sum::() as usize, ); } - let base_level_size_bytes = self.options.base_level_size_mb as usize * 1024 * 1024; + let base_level_size_bytes = self.options.base_level_size_mb * 1024 * 1024; // select base level and compute target level size target_level_size[self.options.max_levels - 1] = diff --git a/mini-lsm/src/iterators/concat_iterator.rs b/mini-lsm/src/iterators/concat_iterator.rs index 298d10b..63d7991 100644 --- a/mini-lsm/src/iterators/concat_iterator.rs +++ b/mini-lsm/src/iterators/concat_iterator.rs @@ -71,22 +71,18 @@ impl SstConcatIterator { } fn move_until_valid(&mut self) -> Result<()> { - loop { - if let Some(iter) = self.current.as_mut() { - if iter.is_valid() { - break; - } - if self.next_sst_idx >= self.sstables.len() { - self.current = None; - } else { - self.current = Some(SsTableIterator::create_and_seek_to_first( - self.sstables[self.next_sst_idx].clone(), - )?); - self.next_sst_idx += 1; - } - } else { + while let Some(iter) = self.current.as_mut() { + if iter.is_valid() { break; } + if self.next_sst_idx >= self.sstables.len() { + self.current = None; + } else { + self.current = Some(SsTableIterator::create_and_seek_to_first( + self.sstables[self.next_sst_idx].clone(), + )?); + self.next_sst_idx += 1; + } } Ok(()) } diff --git a/mini-lsm/src/iterators/two_merge_iterator.rs b/mini-lsm/src/iterators/two_merge_iterator.rs index b7b33df..4fb5941 100644 --- a/mini-lsm/src/iterators/two_merge_iterator.rs +++ b/mini-lsm/src/iterators/two_merge_iterator.rs @@ -22,10 +22,8 @@ impl TwoMergeIterator { } fn skip_b(&mut self) -> Result<()> { - if self.a.is_valid() { - if self.b.is_valid() && self.b.key() == self.a.key() { - self.b.next()?; - } + if self.a.is_valid() && self.b.is_valid() && self.b.key() == self.a.key() { + self.b.next()?; } Ok(()) } diff --git a/mini-lsm/src/lsm_storage.rs b/mini-lsm/src/lsm_storage.rs index 4be7319..cd6111f 100644 --- a/mini-lsm/src/lsm_storage.rs +++ b/mini-lsm/src/lsm_storage.rs @@ -336,7 +336,7 @@ impl LsmStorageInner { for table_id in state .l0_sstables .iter() - .chain(state.levels.iter().map(|(_, files)| files).flatten()) + .chain(state.levels.iter().flat_map(|(_, files)| files)) { let table_id = *table_id; let sst = SsTable::open( diff --git a/mini-lsm/src/manifest.rs b/mini-lsm/src/manifest.rs index 6e41a69..62d968c 100644 --- a/mini-lsm/src/manifest.rs +++ b/mini-lsm/src/manifest.rs @@ -43,9 +43,9 @@ impl Manifest { .context("failed to recover manifest")?; let mut buf = Vec::new(); file.read_to_end(&mut buf)?; - let mut stream = Deserializer::from_slice(&buf).into_iter::(); + let stream = Deserializer::from_slice(&buf).into_iter::(); let mut records = Vec::new(); - while let Some(x) = stream.next() { + for x in stream { records.push(x?); } Ok(( diff --git a/mini-lsm/src/tests/week1_day7.rs b/mini-lsm/src/tests/week1_day7.rs index 54694d0..9bd1e64 100644 --- a/mini-lsm/src/tests/week1_day7.rs +++ b/mini-lsm/src/tests/week1_day7.rs @@ -71,7 +71,7 @@ fn test_task3_block_key_compression() { } let dir = tempdir().unwrap(); let path = dir.path().join("1.sst"); - let sst = builder.build_for_test(&path).unwrap(); + let sst = builder.build_for_test(path).unwrap(); assert!( sst.block_meta.len() <= 25, "you have {} blocks, expect 25",