checkin part 2 solution

Signed-off-by: Alex Chi <iskyzh@gmail.com>
This commit is contained in:
Alex Chi
2024-01-24 14:32:13 +08:00
parent 9c4057c166
commit 9473c89330
25 changed files with 945 additions and 253 deletions

View File

@@ -343,6 +343,7 @@ fn main() {
} else {
storage.dump_original_id(false, false);
}
println!("--- Compaction Task ---");
let mut num_compactions = 0;
while let Some(task) = {
println!("--- Compaction Task ---");

View File

@@ -107,6 +107,19 @@ fn main() -> Result<()> {
} else {
println!("{} not exist", key);
}
} else if line == "scan" {
let mut iter = lsm.scan(std::ops::Bound::Unbounded, std::ops::Bound::Unbounded)?;
let mut cnt = 0;
while iter.is_valid() {
println!(
"{:?}={:?}",
Bytes::copy_from_slice(iter.key()),
Bytes::copy_from_slice(iter.value()),
);
iter.next()?;
cnt += 1;
}
println!("{} keys scanned", cnt);
} else if line.starts_with("scan ") {
let Some((_, rest)) = line.split_once(' ') else {
println!("invalid command");
@@ -137,7 +150,7 @@ fn main() -> Result<()> {
lsm.force_flush()?;
} else if line == "full_compaction" {
lsm.force_full_compaction()?;
} else if line == "quit" {
} else if line == "quit" || line == "close" {
lsm.close()?;
break;
} else {

View File

@@ -166,10 +166,16 @@ impl MiniLsm {
self.inner.scan(lower, upper)
}
/// Only call this in test cases due to race conditions
pub fn force_flush(&self) -> Result<()> {
self.inner
.force_freeze_memtable(&self.inner.state_lock.lock())?;
self.inner.force_flush_next_imm_memtable()
if !self.inner.state.read().memtable.is_empty() {
self.inner
.force_freeze_memtable(&self.inner.state_lock.lock())?;
}
if !self.inner.state.read().imm_memtables.is_empty() {
self.inner.force_flush_next_imm_memtable()?;
}
Ok(())
}
pub fn force_full_compaction(&self) -> Result<()> {
@@ -247,7 +253,7 @@ impl LsmStorageInner {
Self::path_of_wal_static(&self.path, id)
}
fn sync_dir(&self) -> Result<()> {
pub(super) fn sync_dir(&self) -> Result<()> {
unimplemented!()
}

View File

@@ -88,6 +88,11 @@ impl MemTable {
self.approximate_size
.load(std::sync::atomic::Ordering::Relaxed)
}
/// Only use this function when closing the database
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
}
type SkipMapRangeIter<'a> =