Skip to content

Commit fa97112

Browse files
authored
chore(docs): revise docs related to B Plus Tree (#594)
1 parent df2976d commit fa97112

File tree

5 files changed

+38
-35
lines changed

5 files changed

+38
-35
lines changed

src/include/storage/page/b_plus_tree_header_page.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
namespace bustub {
66

7+
/**
8+
* The header page is just used to retrieve the root page,
9+
* preventing potential race condition under concurrent environment.
10+
*/
711
class BPlusTreeHeaderPage {
812
public:
913
// Delete all constructor / destructor to ensure memory safety

src/include/storage/page/b_plus_tree_internal_page.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,30 @@ namespace bustub {
2020
#define B_PLUS_TREE_INTERNAL_PAGE_TYPE BPlusTreeInternalPage<KeyType, ValueType, KeyComparator>
2121
#define INTERNAL_PAGE_HEADER_SIZE 12
2222
#define INTERNAL_PAGE_SIZE ((BUSTUB_PAGE_SIZE - INTERNAL_PAGE_HEADER_SIZE) / (sizeof(MappingType)))
23+
2324
/**
24-
* Store n indexed keys and n+1 child pointers (page_id) within internal page.
25+
* Store `n` indexed keys and `n + 1` child pointers (page_id) within internal page.
2526
* Pointer PAGE_ID(i) points to a subtree in which all keys K satisfy:
2627
* K(i) <= K < K(i+1).
27-
* NOTE: since the number of keys does not equal to number of child pointers,
28-
* the first key always remains invalid. That is to say, any search/lookup
28+
* NOTE: Since the number of keys does not equal to number of child pointers,
29+
* the first key always remains invalid. That is to say, any search / lookup
2930
* should ignore the first key.
3031
*
3132
* Internal page format (keys are stored in increasing order):
32-
* --------------------------------------------------------------------------
33-
* | HEADER | KEY(1)+PAGE_ID(1) | KEY(2)+PAGE_ID(2) | ... | KEY(n)+PAGE_ID(n) |
34-
* --------------------------------------------------------------------------
33+
* ----------------------------------------------------------------------------------
34+
* | HEADER | KEY(1) + PAGE_ID(1) | KEY(2) + PAGE_ID(2) | ... | KEY(n) + PAGE_ID(n) |
35+
* ----------------------------------------------------------------------------------
3536
*/
3637
INDEX_TEMPLATE_ARGUMENTS
3738
class BPlusTreeInternalPage : public BPlusTreePage {
3839
public:
39-
// Deleted to disallow initialization
40+
// Delete all constructor / destructor to ensure memory safety
4041
BPlusTreeInternalPage() = delete;
4142
BPlusTreeInternalPage(const BPlusTreeInternalPage &other) = delete;
4243

4344
/**
4445
* Writes the necessary header information to a newly created page, must be called after
45-
* the creation of a new page to make a valid BPlusTreeInternalPage
46+
* the creation of a new page to make a valid `BPlusTreeInternalPage`
4647
* @param max_size Maximal size of the page
4748
*/
4849
void Init(int max_size = INTERNAL_PAGE_SIZE);
@@ -54,36 +55,34 @@ class BPlusTreeInternalPage : public BPlusTreePage {
5455
auto KeyAt(int index) const -> KeyType;
5556

5657
/**
57-
*
5858
* @param index The index of the key to set. Index must be non-zero.
5959
* @param key The new value for key
6060
*/
6161
void SetKeyAt(int index, const KeyType &key);
6262

6363
/**
64-
*
65-
* @param value the value to search for
64+
* @param value The value to search for
65+
* @return The index that corresponds to the specified value
6666
*/
6767
auto ValueIndex(const ValueType &value) const -> int;
6868

6969
/**
70-
*
71-
* @param index the index
72-
* @return the value at the index
70+
* @param index The index to search for
71+
* @return The value at the index
7372
*/
7473
auto ValueAt(int index) const -> ValueType;
7574

7675
/**
7776
* @brief For test only, return a string representing all keys in
7877
* this internal page, formatted as "(key1,key2,key3,...)"
7978
*
80-
* @return std::string
79+
* @return The string representation of all keys in the current internal page
8180
*/
8281
auto ToString() const -> std::string {
8382
std::string kstr = "(";
8483
bool first = true;
8584

86-
// first key of internal page is always invalid
85+
// First key of internal page is always invalid
8786
for (int i = 1; i < GetSize(); i++) {
8887
KeyType key = KeyAt(i);
8988
if (first) {
@@ -103,4 +102,5 @@ class BPlusTreeInternalPage : public BPlusTreePage {
103102
// Flexible array member for page data.
104103
MappingType array_[0];
105104
};
105+
106106
} // namespace bustub

src/include/storage/page/b_plus_tree_leaf_page.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,19 @@ namespace bustub {
2323
#define LEAF_PAGE_SIZE ((BUSTUB_PAGE_SIZE - LEAF_PAGE_HEADER_SIZE) / sizeof(MappingType))
2424

2525
/**
26-
* Store indexed key and record id(record id = page id combined with slot id,
27-
* see include/common/rid.h for detailed implementation) together within leaf
26+
* Store indexed key and record id (record id = page id combined with slot id,
27+
* see `include/common/rid.h` for detailed implementation) together within leaf
2828
* page. Only support unique key.
2929
*
3030
* Leaf page format (keys are stored in order):
31-
* ----------------------------------------------------------------------
32-
* | HEADER | KEY(1) + RID(1) | KEY(2) + RID(2) | ... | KEY(n) + RID(n)
33-
* ----------------------------------------------------------------------
31+
* -----------------------------------------------------------------------
32+
* | HEADER | KEY(1) + RID(1) | KEY(2) + RID(2) | ... | KEY(n) + RID(n) |
33+
* -----------------------------------------------------------------------
3434
*
35-
* Header format (size in byte, 16 bytes in total):
36-
* ---------------------------------------------------------------------
37-
* | PageType (4) | CurrentSize (4) | MaxSize (4) |
38-
* ---------------------------------------------------------------------
39-
* -----------------------------------------------
40-
* | NextPageId (4)
41-
* -----------------------------------------------
35+
* Header format (size in byte, 16 bytes in total):
36+
* -----------------------------------------------------------------------
37+
* | PageType (4) | CurrentSize (4) | MaxSize (4) | NextPageId (4) | ... |
38+
* -----------------------------------------------------------------------
4239
*/
4340
INDEX_TEMPLATE_ARGUMENTS
4441
class BPlusTreeLeafPage : public BPlusTreePage {
@@ -54,16 +51,16 @@ class BPlusTreeLeafPage : public BPlusTreePage {
5451
*/
5552
void Init(int max_size = LEAF_PAGE_SIZE);
5653

57-
// helper methods
54+
// Helper methods
5855
auto GetNextPageId() const -> page_id_t;
5956
void SetNextPageId(page_id_t next_page_id);
6057
auto KeyAt(int index) const -> KeyType;
6158

6259
/**
63-
* @brief for test only return a string representing all keys in
60+
* @brief For test only return a string representing all keys in
6461
* this leaf page formatted as "(key1,key2,key3,...)"
6562
*
66-
* @return std::string
63+
* @return The string representation of all keys in the current internal page
6764
*/
6865
auto ToString() const -> std::string {
6966
std::string kstr = "(";
@@ -89,4 +86,5 @@ class BPlusTreeLeafPage : public BPlusTreePage {
8986
// Flexible array member for page data.
9087
MappingType array_[0];
9188
};
89+
9290
} // namespace bustub

src/include/storage/page/b_plus_tree_page.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ enum class IndexPageType { INVALID_INDEX_PAGE = 0, LEAF_PAGE, INTERNAL_PAGE };
3434
* contains information shared by both leaf page and internal page.
3535
*
3636
* Header format (size in byte, 12 bytes in total):
37-
* ----------------------------------------------------------------------------
38-
* | PageType (4) | CurrentSize (4) | MaxSize (4) |
39-
* ----------------------------------------------------------------------------
37+
* ---------------------------------------------------------
38+
* | PageType (4) | CurrentSize (4) | MaxSize (4) | ... |
39+
* ---------------------------------------------------------
4040
*/
4141
class BPlusTreePage {
4242
public:
@@ -57,7 +57,7 @@ class BPlusTreePage {
5757
auto GetMinSize() const -> int;
5858

5959
private:
60-
// member variable, attributes that both internal and leaf page share
60+
// Member variables, attributes that both internal and leaf page share
6161
IndexPageType page_type_ __attribute__((__unused__));
6262
int size_ __attribute__((__unused__));
6363
int max_size_ __attribute__((__unused__));

src/storage/index/b_plus_tree.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ BPLUSTREE_TYPE::BPlusTree(std::string name, page_id_t header_page_id, BufferPool
2727
*/
2828
INDEX_TEMPLATE_ARGUMENTS
2929
auto BPLUSTREE_TYPE::IsEmpty() const -> bool { return true; }
30+
3031
/*****************************************************************************
3132
* SEARCH
3233
*****************************************************************************/

0 commit comments

Comments
 (0)