Skip to content

Commit

Permalink
finish w1/d2/task4
Browse files Browse the repository at this point in the history
  • Loading branch information
AbnerZheng committed May 14, 2024
1 parent d78c2f1 commit 86a17f5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions mini-lsm-starter/src/iterators/merge_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl<I: 'static + for<'a> StorageIterator<KeyType<'a> = KeySlice<'a>>> StorageIt

fn next(&mut self) -> Result<()> {
let current = self.current.as_mut().unwrap();
println!("current: {}/{:?}:{:?}", current.0, current.1.key(), current.1.value());
while let Some(mut iter) = self.iters.pop() {
assert!(current.1.key() <= iter.1.key(), "heap invariant");
if iter.1.key() == current.1.key() {
Expand Down
27 changes: 21 additions & 6 deletions mini-lsm-starter/src/lsm_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub struct LsmIterator {

impl LsmIterator {
pub(crate) fn new(iter: LsmIteratorInner) -> Result<Self> {
let mut iter = iter;
while iter.is_valid() && iter.value().is_empty() {
iter.next()?
}
Ok(Self { inner: iter })
}
}
Expand All @@ -37,13 +41,24 @@ impl StorageIterator for LsmIterator {
}

fn next(&mut self) -> Result<()> {
loop {
self.inner.next()?;
if !self.value().is_empty() {
break;
}
// loop {
//
// self.inner.next()?;
//
// if !self.is_valid() {
// break;
// }
//
// if !self.value().is_empty() {
// break;
// }
// }
self.inner.next()?;
if self.is_valid() && self.value().is_empty() {
self.inner.next()
} else {
Ok(())
}
Ok(())
}
}

Expand Down
13 changes: 12 additions & 1 deletion mini-lsm-starter/src/lsm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::compact::{
CompactionController, CompactionOptions, LeveledCompactionController, LeveledCompactionOptions,
SimpleLeveledCompactionController, SimpleLeveledCompactionOptions, TieredCompactionController,
};
use crate::iterators::merge_iterator::MergeIterator;
use crate::lsm_iterator::{FusedIterator, LsmIterator};
use crate::manifest::Manifest;
use crate::mem_table::MemTable;
Expand Down Expand Up @@ -375,6 +376,16 @@ impl LsmStorageInner {
_lower: Bound<&[u8]>,
_upper: Bound<&[u8]>,
) -> Result<FusedIterator<LsmIterator>> {
unimplemented!()
let guard = self.state.read();
let iterator = Box::new(guard.memtable.scan(_lower, _upper));
let mut vec = guard
.imm_memtables
.iter()
.map(|m| Box::new(m.scan(_lower, _upper)))
.collect::<Vec<_>>();
vec.insert(0, iterator);
Ok(FusedIterator::new(LsmIterator::new(
MergeIterator::create(vec),
)?))
}
}
7 changes: 7 additions & 0 deletions mini-lsm-starter/src/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ where
I: for<'a> StorageIterator<KeyType<'a> = &'a [u8]>,
{
for (k, v) in expected {
println!(
"expected: {:?}/{:?}, actual: {:?}/{:?}",
k,
v,
as_bytes(iter.key()),
as_bytes(iter.value()),
);
assert!(iter.is_valid());
assert_eq!(
k,
Expand Down

0 comments on commit 86a17f5

Please sign in to comment.