Skip to content

Commit

Permalink
[week1] 1.5 task1
Browse files Browse the repository at this point in the history
  • Loading branch information
HeartLinked committed Oct 3, 2024
1 parent 5a013ac commit 200c148
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
50 changes: 40 additions & 10 deletions mini-lsm-starter/src/iterators/two_merge_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#![allow(unused_variables)] // TODO(you): remove this lint after implementing this mod
#![allow(dead_code)] // TODO(you): remove this lint after implementing this mod

use anyhow::Result;

use super::StorageIterator;
Expand All @@ -19,7 +16,7 @@ impl<
> TwoMergeIterator<A, B>
{
pub fn create(a: A, b: B) -> Result<Self> {
unimplemented!()
Ok(TwoMergeIterator { a, b })
}
}

Expand All @@ -30,19 +27,52 @@ impl<
{
type KeyType<'a> = A::KeyType<'a>;

fn key(&self) -> Self::KeyType<'_> {
unimplemented!()
fn value(&self) -> &[u8] {
match (self.a.is_valid(), self.b.is_valid()) {
(true, true) => {
if self.a.key() <= self.b.key() {
self.a.value()
} else {
self.b.value()
}
}
(true, false) => self.a.value(),
_ => self.b.value(),
}
}

fn value(&self) -> &[u8] {
unimplemented!()
fn key(&self) -> Self::KeyType<'_> {
match (self.a.is_valid(), self.b.is_valid()) {
(true, true) => {
if self.a.key() <= self.b.key() {
self.a.key()
} else {
self.b.key()
}
}
(true, false) => self.a.key(),
_ => self.b.key(),
}
}

fn is_valid(&self) -> bool {
unimplemented!()
self.a.is_valid() || self.b.is_valid()
}

fn next(&mut self) -> Result<()> {
unimplemented!()
match (self.a.is_valid(), self.b.is_valid()) {
(true, true) => {
if self.a.key() < self.b.key() {
self.a.next()
} else if self.a.key() == self.b.key() {
self.a.next()?;
self.b.next() // 如果 key 相等,则两个迭代器都要增加
} else {
self.b.next()
}
}
(true, false) => self.a.next(),
_ => self.b.next(),
}
}
}
1 change: 1 addition & 0 deletions mini-lsm-starter/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ mod week1_day1;
mod week1_day2;
mod week1_day3;
mod week1_day4;
mod week1_day5;

0 comments on commit 200c148

Please sign in to comment.