Skip to content

Commit

Permalink
support force full compaction
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Chi <[email protected]>
  • Loading branch information
skyzh committed Jan 18, 2024
1 parent 3aad027 commit 41d860e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 0 additions & 2 deletions mini-lsm/src/bin/minilsm_cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::time::Duration;

use anyhow::Result;
use mini_lsm::compact::{CompactionOptions, SimpleLeveledCompactionOptions};
use mini_lsm::lsm_storage::{LsmStorageOptions, MiniLsm};
Expand Down
34 changes: 34 additions & 0 deletions mini-lsm/src/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,40 @@ impl LsmStorageInner {
Ok(new_sst)
}

pub fn force_full_compaction(&self) -> Result<()> {
let CompactionOptions::NoCompaction = self.options.compaction_options else {
panic!("full compaction can only be called with compaction is not enabled")
};
let snapshot = {
let state = self.state.read();
state.clone()
};
let original_sstables = snapshot.l0_sstables.clone();
let sstables = self.compact(&CompactionTask::ForceFullCompaction(
original_sstables.clone(),
))?;
{
let _state_lock = self.state_lock.lock();
let mut state = self.state.read().as_ref().clone();
for sst in original_sstables.iter() {
let result = state.sstables.remove(sst);
assert!(result.is_some());
}
let mut ids = Vec::with_capacity(sstables.len());
for new_sst in sstables {
ids.push(new_sst.sst_id());
let result = state.sstables.insert(new_sst.sst_id(), new_sst);
assert!(result.is_none());
}
state.l0_sstables = ids;
*self.state.write() = Arc::new(state);
}
for sst in original_sstables {
std::fs::remove_file(self.path_of_sst(sst))?;
}
Ok(())
}

fn trigger_compaction(&self) -> Result<()> {
let snapshot = {
let state = self.state.read();
Expand Down

0 comments on commit 41d860e

Please sign in to comment.