Skip to content

Commit b20b32a

Browse files
authored
chore: rename root page -> header page (#508)
* chore: rename root page -> header page Signed-off-by: Alex Chi <[email protected]> * fix build Signed-off-by: Alex Chi <[email protected]> * newline Signed-off-by: Alex Chi <[email protected]> --------- Signed-off-by: Alex Chi <[email protected]>
1 parent 49deaa1 commit b20b32a

File tree

9 files changed

+72
-31
lines changed

9 files changed

+72
-31
lines changed

src/include/storage/index/b_plus_tree.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
#include "common/macros.h"
3232
#include "concurrency/transaction.h"
3333
#include "storage/index/index_iterator.h"
34+
#include "storage/page/b_plus_tree_header_page.h"
3435
#include "storage/page/b_plus_tree_internal_page.h"
3536
#include "storage/page/b_plus_tree_leaf_page.h"
36-
#include "storage/page/b_plus_tree_root_page.h"
3737
#include "storage/page/page_guard.h"
3838

3939
namespace bustub {
@@ -46,9 +46,17 @@ namespace bustub {
4646
*/
4747
class Context {
4848
public:
49+
// When you insert into / remove from the B+ tree, store the write guard of header page here.
50+
// Remember to drop the header page guard and set it to nullopt when you want to unlock all.
4951
std::optional<WritePageGuard> header_page_{std::nullopt};
52+
53+
// Save the root page id here so that it's easier to know if the current page is the root page.
5054
page_id_t root_page_id_{INVALID_PAGE_ID};
55+
56+
// Store the write guards of the pages that you're modifying here.
5157
std::deque<WritePageGuard> write_set_;
58+
59+
// You may want to use this when getting value, but not necessary.
5260
std::deque<ReadPageGuard> read_set_;
5361

5462
auto IsRootPage(page_id_t page_id) -> bool { return page_id == root_page_id_; }

src/include/storage/page/b_plus_tree_root_page.h renamed to src/include/storage/page/b_plus_tree_header_page.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#pragma once
22

33
#include "common/config.h"
4+
45
namespace bustub {
56

6-
class BPlusTreeRootPage {
7+
class BPlusTreeHeaderPage {
78
public:
89
// Delete all constructor / destructor to ensure memory safety
9-
BPlusTreeRootPage() = delete;
10-
BPlusTreeRootPage(const BPlusTreeRootPage &other) = delete;
10+
BPlusTreeHeaderPage() = delete;
11+
BPlusTreeHeaderPage(const BPlusTreeHeaderPage &other) = delete;
1112

1213
page_id_t root_page_id_;
1314
};

src/include/storage/page/b_plus_tree_internal_page.h

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,45 @@ namespace bustub {
3535
INDEX_TEMPLATE_ARGUMENTS
3636
class BPlusTreeInternalPage : public BPlusTreePage {
3737
public:
38-
// must call initialize method after "create" a new node
39-
void Init(page_id_t page_id, page_id_t parent_id = INVALID_PAGE_ID, int max_size = INTERNAL_PAGE_SIZE);
38+
// Deleted to disallow initialization
39+
BPlusTreeInternalPage() = delete;
40+
BPlusTreeInternalPage(const BPlusTreeInternalPage &other) = delete;
4041

42+
/**
43+
* Writes the necessary header information to a newly created page, must be called after
44+
* the creation of a new page to make a valid BPlusTreeInternalPage
45+
* @param max_size Maximal size of the page
46+
*/
47+
void Init(int max_size = INTERNAL_PAGE_SIZE);
48+
49+
/**
50+
* @param index The index of the key to get. Index must be non-zero.
51+
* @return Key at index
52+
*/
4153
auto KeyAt(int index) const -> KeyType;
54+
55+
/**
56+
*
57+
* @param index The index of the key to set. Index must be non-zero.
58+
* @param key The new value for key
59+
*/
4260
void SetKeyAt(int index, const KeyType &key);
61+
62+
/**
63+
*
64+
* @param value the value to search for
65+
*/
66+
auto ValueIndex(const ValueType &value) const -> int;
67+
68+
/**
69+
*
70+
* @param index the index
71+
* @return the value at the index
72+
*/
4373
auto ValueAt(int index) const -> ValueType;
4474

4575
private:
4676
// Flexible array member for page data.
47-
MappingType array_[1];
77+
MappingType array_[0];
4878
};
4979
} // namespace bustub

src/include/storage/page/b_plus_tree_leaf_page.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ namespace bustub {
4242
INDEX_TEMPLATE_ARGUMENTS
4343
class BPlusTreeLeafPage : public BPlusTreePage {
4444
public:
45-
// After creating a new leaf page from buffer pool, must call initialize
46-
// method to set default values
47-
void Init(page_id_t page_id, page_id_t parent_id = INVALID_PAGE_ID, int max_size = LEAF_PAGE_SIZE);
45+
// Delete all constructor / destructor to ensure memory safety
46+
BPlusTreeLeafPage() = delete;
47+
BPlusTreeLeafPage(const BPlusTreeLeafPage &other) = delete;
48+
49+
/**
50+
* After creating a new leaf page from buffer pool, must call initialize
51+
* method to set default values
52+
* @param max_size Max size of the leaf node
53+
*/
54+
void Init(int max_size = LEAF_PAGE_SIZE);
55+
4856
// helper methods
4957
auto GetNextPageId() const -> page_id_t;
5058
void SetNextPageId(page_id_t next_page_id);
@@ -53,6 +61,6 @@ class BPlusTreeLeafPage : public BPlusTreePage {
5361
private:
5462
page_id_t next_page_id_;
5563
// Flexible array member for page data.
56-
MappingType array_[1];
64+
MappingType array_[0];
5765
};
5866
} // namespace bustub

src/include/storage/page/b_plus_tree_page.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,19 @@ enum class IndexPageType { INVALID_INDEX_PAGE = 0, LEAF_PAGE, INTERNAL_PAGE };
3333
* It actually serves as a header part for each B+ tree page and
3434
* contains information shared by both leaf page and internal page.
3535
*
36-
* Header format (size in byte, 24 bytes in total):
36+
* Header format (size in byte, 12 bytes in total):
3737
* ----------------------------------------------------------------------------
38-
* | PageType (4) | LSN (4) | CurrentSize (4) | MaxSize (4) |
39-
* ----------------------------------------------------------------------------
40-
* | ParentPageId (4) | PageId(4) |
38+
* | PageType (4) | CurrentSize (4) | MaxSize (4) |
4139
* ----------------------------------------------------------------------------
4240
*/
4341
class BPlusTreePage {
4442
public:
43+
// Delete all constructor / destructor to ensure memory safety
44+
BPlusTreePage() = delete;
45+
BPlusTreePage(const BPlusTreePage &other) = delete;
46+
~BPlusTreePage() = delete;
47+
4548
auto IsLeafPage() const -> bool;
46-
auto IsRootPage() const -> bool;
4749
void SetPageType(IndexPageType page_type);
4850

4951
auto GetSize() const -> int;
@@ -60,16 +62,11 @@ class BPlusTreePage {
6062
auto GetPageId() const -> page_id_t;
6163
void SetPageId(page_id_t page_id);
6264

63-
void SetLSN(lsn_t lsn = INVALID_LSN);
64-
6565
private:
6666
// member variable, attributes that both internal and leaf page share
6767
IndexPageType page_type_ __attribute__((__unused__));
68-
lsn_t lsn_ __attribute__((__unused__));
6968
int size_ __attribute__((__unused__));
7069
int max_size_ __attribute__((__unused__));
71-
page_id_t parent_page_id_ __attribute__((__unused__));
72-
page_id_t page_id_ __attribute__((__unused__));
7370
};
7471

7572
} // namespace bustub

src/storage/index/b_plus_tree.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "common/logger.h"
55
#include "common/rid.h"
66
#include "storage/index/b_plus_tree.h"
7-
#include "storage/page/header_page.h"
87

98
namespace bustub {
109

@@ -16,7 +15,11 @@ BPLUSTREE_TYPE::BPlusTree(std::string name, page_id_t header_page_id, BufferPool
1615
comparator_(std::move(comparator)),
1716
leaf_max_size_(leaf_max_size),
1817
internal_max_size_(internal_max_size),
19-
header_page_id_(header_page_id) {}
18+
header_page_id_(header_page_id) {
19+
WritePageGuard guard = bpm_->FetchPageWrite(header_page_id_);
20+
auto root_page = guard.AsMut<BPlusTreeHeaderPage>();
21+
root_page->root_page_id_ = INVALID_PAGE_ID;
22+
}
2023

2124
/*
2225
* Helper function to decide whether current b+tree is empty

src/storage/page/b_plus_tree_internal_page.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace bustub {
2525
* max page size
2626
*/
2727
INDEX_TEMPLATE_ARGUMENTS
28-
void B_PLUS_TREE_INTERNAL_PAGE_TYPE::Init(page_id_t page_id, page_id_t parent_id, int max_size) {}
28+
void B_PLUS_TREE_INTERNAL_PAGE_TYPE::Init(int max_size) {}
2929
/*
3030
* Helper method to get/set the key associated with input "index"(a.k.a
3131
* array offset)

src/storage/page/b_plus_tree_leaf_page.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace bustub {
2727
* next page id and set max size
2828
*/
2929
INDEX_TEMPLATE_ARGUMENTS
30-
void B_PLUS_TREE_LEAF_PAGE_TYPE::Init(page_id_t page_id, page_id_t parent_id, int max_size) {}
30+
void B_PLUS_TREE_LEAF_PAGE_TYPE::Init(int max_size) {}
3131

3232
/**
3333
* Helper methods to set/get next page id

src/storage/page/b_plus_tree_page.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace bustub {
1818
* Page type enum class is defined in b_plus_tree_page.h
1919
*/
2020
auto BPlusTreePage::IsLeafPage() const -> bool { return false; }
21-
auto BPlusTreePage::IsRootPage() const -> bool { return false; }
2221
void BPlusTreePage::SetPageType(IndexPageType page_type) {}
2322

2423
/*
@@ -53,9 +52,4 @@ void BPlusTreePage::SetParentPageId(page_id_t parent_page_id) {}
5352
auto BPlusTreePage::GetPageId() const -> page_id_t { return 0; }
5453
void BPlusTreePage::SetPageId(page_id_t page_id) {}
5554

56-
/*
57-
* Helper methods to set lsn
58-
*/
59-
void BPlusTreePage::SetLSN(lsn_t lsn) { lsn_ = lsn; }
60-
6155
} // namespace bustub

0 commit comments

Comments
 (0)