Skip to content

Commit

Permalink
traverse index properly
Browse files Browse the repository at this point in the history
interior index cells have values that are not in the leaves, e.g.

     (interior: 3)
    /            \
(leaf: 2)     (leaf: 4)

so their values need to be emitted after the left subtree is emitted.
  • Loading branch information
jussisaurio committed Oct 7, 2024
1 parent e5cf052 commit 6283963
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions core/storage/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct BTreeCursor {
record: RefCell<Option<OwnedRecord>>,
null_flag: bool,
database_header: Rc<RefCell<DatabaseHeader>>,
going_upwards: bool,
}

impl BTreeCursor {
Expand All @@ -73,6 +74,7 @@ impl BTreeCursor {
record: RefCell::new(None),
null_flag: false,
database_header,
going_upwards: false,
}
}

Expand Down Expand Up @@ -110,6 +112,7 @@ impl BTreeCursor {
}
None => match parent {
Some(ref parent) => {
self.going_upwards = true;
self.page.replace(Some(parent.clone()));
continue;
}
Expand Down Expand Up @@ -151,19 +154,31 @@ impl BTreeCursor {
left_child_page,
..
}) => {
mem_page.advance();
let mem_page =
MemPage::new(Some(mem_page.clone()), *left_child_page as usize, 0);
self.page.replace(Some(Rc::new(mem_page)));
continue;
if self.going_upwards {
self.going_upwards = false;
mem_page.advance();
let record = crate::storage::sqlite3_ondisk::read_record(payload)?;
let rowid = match record.values.last() {
Some(OwnedValue::Integer(rowid)) => *rowid as u64,
_ => unreachable!("index cells should have an integer rowid"),
};
return Ok(CursorResult::Ok((Some(rowid), Some(record))));
} else {
let mem_page =
MemPage::new(Some(mem_page.clone()), *left_child_page as usize, 0);
self.page.replace(Some(Rc::new(mem_page)));
continue;
}
}
BTreeCell::IndexLeafCell(IndexLeafCell { payload, .. }) => {
self.going_upwards = false;
mem_page.advance();
let record = crate::storage::sqlite3_ondisk::read_record(payload)?;
let rowid = match record.values.last() {
Some(OwnedValue::Integer(rowid)) => *rowid as u64,
_ => unreachable!("index cells should have an integer rowid"),
};

return Ok(CursorResult::Ok((Some(rowid), Some(record))));
}
}
Expand Down Expand Up @@ -375,7 +390,6 @@ impl BTreeCursor {
let SeekKey::IndexKey(index_key) = key else {
unreachable!("index seek key should be a record");
};
mem_page.advance();
let record = crate::storage::sqlite3_ondisk::read_record(payload)?;
let comparison = match cmp {
SeekOp::GT => index_key < &record,
Expand All @@ -388,6 +402,8 @@ impl BTreeCursor {
self.page.replace(Some(Rc::new(mem_page)));
found_cell = true;
break;
} else {
mem_page.advance();
}
}
BTreeCell::IndexLeafCell(_) => {
Expand Down

0 comments on commit 6283963

Please sign in to comment.