Skip to content

Commit

Permalink
Merge pull request #36 from dermesser/do-more-compactions
Browse files Browse the repository at this point in the history
Do compactions after writing memtables (#34)
  • Loading branch information
dermesser committed Jul 15, 2023
2 parents 0f94f6f + b543432 commit 9bbf70b
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/db_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,10 @@ impl DB {

/// flush makes sure that all pending changes (e.g. from put()) are stored on disk.
pub fn flush(&mut self) -> Result<()> {
assert!(self.log.is_some());
self.log.as_mut().unwrap().flush()
if let Some(ref mut log) = self.log.as_mut() {
log.flush()?;
}
Ok(())
}
}

Expand Down Expand Up @@ -565,9 +567,7 @@ impl DB {
/// make_room_for_write checks if the memtable has become too large, and triggers a compaction
/// if it's the case.
fn make_room_for_write(&mut self, force: bool) -> Result<()> {
if !force && self.mem.approx_mem_usage() < self.opt.write_buffer_size {
Ok(())
} else if self.mem.len() == 0 {
if !force && self.mem.approx_mem_usage() < self.opt.write_buffer_size || self.mem.len() == 0 {
Ok(())
} else {
// Create new memtable.
Expand All @@ -594,8 +594,9 @@ impl DB {
/// maybe_do_compaction starts a blocking compaction if it makes sense.
fn maybe_do_compaction(&mut self) -> Result<()> {
if self.imm.is_some() {
self.compact_memtable()
} else if self.vset.borrow().needs_compaction() {
self.compact_memtable()?;
}
if self.vset.borrow().needs_compaction() {
let c = self.vset.borrow_mut().pick_compaction();
if let Some(c) = c {
self.start_compaction(c)
Expand Down Expand Up @@ -949,6 +950,7 @@ impl DB {

impl Drop for DB {
fn drop(&mut self) {
self.flush().ok();
let _ = self.release_lock();
}
}
Expand Down

0 comments on commit 9bbf70b

Please sign in to comment.