Skip to content

Commit

Permalink
minor changes. added week 3 day 2 tests. all tests except full compac…
Browse files Browse the repository at this point in the history
…tion test in week 2 still passing
  • Loading branch information
redixhumayun committed Jun 10, 2024
1 parent 31ee0c9 commit 53bfd9d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
4 changes: 2 additions & 2 deletions mini-lsm-starter/src/mem_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ impl MemTable {
}

/// Get an iterator over a range of keys.
pub fn scan(&self, _lower: Bound<&[u8]>, _upper: Bound<&[u8]>) -> MemTableIterator {
pub fn scan(&self, lower: Bound<&[u8]>, upper: Bound<&[u8]>) -> MemTableIterator {
let mut iterator = MemTableIteratorBuilder {
map: self.map.clone(),
iter_builder: |map| map.range((map_bound(_lower), map_bound(_upper))),
iter_builder: |map| map.range((map_bound(lower), map_bound(upper))),
item: (Bytes::new(), Bytes::new()),
}
.build();
Expand Down
1 change: 1 addition & 0 deletions mini-lsm-starter/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ mod week2_day4;
mod week2_day5;
mod week2_day6;
mod week3_day1;
mod week3_day2;
1 change: 0 additions & 1 deletion mini-lsm-starter/src/tests/harness.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::fmt;
use std::{
collections::BTreeMap, ops::Bound, os::unix::fs::MetadataExt, path::Path, sync::Arc,
time::Duration,
Expand Down
61 changes: 61 additions & 0 deletions mini-lsm-starter/src/tests/week3_day2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::time::Duration;

use tempfile::tempdir;

use crate::{
compact::CompactionOptions,
lsm_storage::{LsmStorageOptions, MiniLsm},
tests::harness::dump_files_in_dir,
};

#[test]
fn test_task3_compaction_integration() {
let dir = tempdir().unwrap();
let mut options = LsmStorageOptions::default_for_week2_test(CompactionOptions::NoCompaction);
options.enable_wal = true;
let storage = MiniLsm::open(&dir, options.clone()).unwrap();
let _txn = storage.new_txn().unwrap();
for i in 0..=20000 {
storage
.put(b"0", format!("{:02000}", i).as_bytes())
.unwrap();
}
std::thread::sleep(Duration::from_secs(1)); // wait until all memtables flush
while {
let snapshot = storage.inner.state.read();
!snapshot.imm_memtables.is_empty()
} {
storage.inner.force_flush_next_imm_memtable().unwrap();
}
assert!(storage.inner.state.read().l0_sstables.len() > 1);
storage.force_full_compaction().unwrap();
storage.dump_structure();
dump_files_in_dir(&dir);
assert!(storage.inner.state.read().l0_sstables.is_empty());
assert_eq!(storage.inner.state.read().levels.len(), 1);
// same key in the same SST
assert_eq!(storage.inner.state.read().levels[0].1.len(), 1);
for i in 0..=100 {
storage
.put(b"1", format!("{:02000}", i).as_bytes())
.unwrap();
}
storage
.inner
.force_freeze_memtable(&storage.inner.state_lock.lock())
.unwrap();
std::thread::sleep(Duration::from_secs(1)); // wait until all memtables flush
while {
let snapshot = storage.inner.state.read();
!snapshot.imm_memtables.is_empty()
} {
storage.inner.force_flush_next_imm_memtable().unwrap();
}
storage.force_full_compaction().unwrap();
storage.dump_structure();
dump_files_in_dir(&dir);
assert!(storage.inner.state.read().l0_sstables.is_empty());
assert_eq!(storage.inner.state.read().levels.len(), 1);
// same key in the same SST, now we should split two
assert_eq!(storage.inner.state.read().levels[0].1.len(), 2);
}

0 comments on commit 53bfd9d

Please sign in to comment.