Skip to content

Commit

Permalink
Improve index page code
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 7, 2024
1 parent 47e8d7d commit 1a57b1b
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 120 deletions.
2 changes: 1 addition & 1 deletion bustubx/src/storage/codec/index_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl BPlusTreePageTypeCodec {
match flag {
1 => Ok((BPlusTreePageType::LeafPage, offset)),
2 => Ok((BPlusTreePageType::InternalPage, offset)),
_ => Err(BustubxError::Storage("Invalid page type".to_string())),
_ => Err(BustubxError::Storage(format!("Invalid page type {}", flag))),
}
}
}
41 changes: 27 additions & 14 deletions bustubx/src/storage/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ impl BPlusTreeIndex {
let (old_internal_key, new_internal_key) = match left_sibling_tree_page {
BPlusTreePage::Internal(ref mut left_sibling_internal_page) => {
let kv = left_sibling_internal_page
.split_off(left_sibling_internal_page.current_size as usize - 1)
.split_off(
left_sibling_internal_page.header.current_size as usize - 1,
)
.remove(0);
if let BPlusTreePage::Internal(ref mut curr_internal_page) =
curr_page
Expand All @@ -217,7 +219,8 @@ impl BPlusTreeIndex {
);
let max_leaf_kv =
self.find_max_leafkv(left_sibling_internal_page.value_at(
left_sibling_internal_page.current_size as usize - 1,
left_sibling_internal_page.header.current_size as usize
- 1,
));
(kv.0, max_leaf_kv.0)
} else {
Expand All @@ -226,7 +229,9 @@ impl BPlusTreeIndex {
}
BPlusTreePage::Leaf(ref mut left_sibling_leaf_page) => {
let kv = left_sibling_leaf_page
.split_off(left_sibling_leaf_page.current_size as usize - 1)
.split_off(
left_sibling_leaf_page.header.current_size as usize - 1,
)
.remove(0);
if let BPlusTreePage::Leaf(ref mut curr_leaf_page) = curr_page {
curr_leaf_page.insert(
Expand All @@ -238,7 +243,8 @@ impl BPlusTreeIndex {
kv.0,
left_sibling_leaf_page
.key_at(
left_sibling_leaf_page.current_size as usize - 1,
left_sibling_leaf_page.header.current_size as usize
- 1,
)
.clone(),
)
Expand Down Expand Up @@ -381,7 +387,8 @@ impl BPlusTreeIndex {
&self.index_metadata.key_schema,
);
// 更新next page id
left_sibling_leaf_page.next_page_id = curr_leaf_page.next_page_id;
left_sibling_leaf_page.header.next_page_id =
curr_leaf_page.header.next_page_id;
} else {
panic!("Internal page can not merge from leaf page");
}
Expand Down Expand Up @@ -410,7 +417,8 @@ impl BPlusTreeIndex {
);
parent_internal_page.delete_page_id(deleted_page_id);
// 根节点只有一个子节点(叶子)时,则叶子节点成为新的根节点
if parent_page_id == self.root_page_id && parent_internal_page.current_size == 0
if parent_page_id == self.root_page_id
&& parent_internal_page.header.current_size == 0
{
self.root_page_id = curr_page_id;
// 删除旧的根节点
Expand Down Expand Up @@ -457,7 +465,8 @@ impl BPlusTreeIndex {
&self.index_metadata.key_schema,
);
// 更新next page id
curr_leaf_page.next_page_id = right_sibling_leaf_page.next_page_id;
curr_leaf_page.header.next_page_id =
right_sibling_leaf_page.header.next_page_id;
} else {
panic!("Internal page can not merge from leaf page");
}
Expand All @@ -482,7 +491,8 @@ impl BPlusTreeIndex {
);
parent_internal_page.delete_page_id(deleted_page_id);
// 根节点只有一个子节点(叶子)时,则叶子节点成为新的根节点
if parent_page_id == self.root_page_id && parent_internal_page.current_size == 0
if parent_page_id == self.root_page_id
&& parent_internal_page.header.current_size == 0
{
self.root_page_id = curr_page_id;
// 删除旧的根节点
Expand Down Expand Up @@ -607,13 +617,13 @@ impl BPlusTreeIndex {
self.leaf_max_size,
);
new_leaf_page.batch_insert(
leaf_page.split_off(leaf_page.current_size as usize / 2),
leaf_page.split_off(leaf_page.header.current_size as usize / 2),
&self.index_metadata.key_schema,
);

// 更新next page id
new_leaf_page.next_page_id = leaf_page.next_page_id;
leaf_page.next_page_id = new_page.page_id;
new_leaf_page.header.next_page_id = leaf_page.header.next_page_id;
leaf_page.header.next_page_id = new_page.page_id;

new_page.data = new_leaf_page.to_bytes();
self.buffer_pool_manager.unpin_page(new_page_id, true);
Expand All @@ -633,7 +643,7 @@ impl BPlusTreeIndex {
self.internal_max_size as u32,
);
new_internal_page.batch_insert(
internal_page.split_off(internal_page.current_size as usize / 2),
internal_page.split_off(internal_page.header.current_size as usize / 2),
&self.index_metadata.key_schema,
);

Expand Down Expand Up @@ -713,7 +723,8 @@ impl BPlusTreeIndex {
loop {
match curr_page {
BPlusTreePage::Internal(internal_page) => {
let page_id = internal_page.value_at(internal_page.current_size as usize - 1);
let page_id =
internal_page.value_at(internal_page.header.current_size as usize - 1);
let page = self
.buffer_pool_manager
.fetch_page(page_id)
Expand All @@ -725,7 +736,9 @@ impl BPlusTreeIndex {
self.buffer_pool_manager.unpin_page(page_id, false);
}
BPlusTreePage::Leaf(leaf_page) => {
return leaf_page.kv_at(leaf_page.current_size as usize - 1).clone();
return leaf_page
.kv_at(leaf_page.header.current_size as usize - 1)
.clone();
}
}
}
Expand Down
Loading

0 comments on commit 1a57b1b

Please sign in to comment.