From 69d4f6f7d3fc972c05d0d6af6fb0cee308d13f80 Mon Sep 17 00:00:00 2001 From: clundro Date: Fri, 24 Mar 2023 17:03:48 +0800 Subject: [PATCH 1/5] chore(p2): remove #define type in BTree Signed-off-by: clundro --- src/include/storage/index/b_plus_tree.h | 9 ++-- src/include/storage/index/b_plus_tree_index.h | 2 - src/include/storage/index/index_iterator.h | 3 ++ .../storage/page/b_plus_tree_internal_page.h | 10 +++-- .../storage/page/b_plus_tree_leaf_page.h | 8 ++-- src/include/storage/page/b_plus_tree_page.h | 3 +- src/storage/index/b_plus_tree.cpp | 43 +++++++++++-------- src/storage/index/b_plus_tree_index.cpp | 24 ++++++++--- .../page/b_plus_tree_internal_page.cpp | 8 ++-- src/storage/page/b_plus_tree_leaf_page.cpp | 10 +++-- 10 files changed, 74 insertions(+), 46 deletions(-) diff --git a/src/include/storage/index/b_plus_tree.h b/src/include/storage/index/b_plus_tree.h index bc1c31b27..d27b657f0 100644 --- a/src/include/storage/index/b_plus_tree.h +++ b/src/include/storage/index/b_plus_tree.h @@ -56,8 +56,6 @@ class Context { auto IsRootPage(page_id_t page_id) -> bool { return page_id == root_page_id_; } }; -#define BPLUSTREE_TYPE BPlusTree - // Main class providing the API for the Interactive B+ Tree. INDEX_TEMPLATE_ARGUMENTS class BPlusTree { @@ -66,8 +64,8 @@ class BPlusTree { public: explicit BPlusTree(std::string name, page_id_t header_page_id, BufferPoolManager *buffer_pool_manager, - const KeyComparator &comparator, int leaf_max_size = LEAF_PAGE_SIZE, - int internal_max_size = INTERNAL_PAGE_SIZE); + const KeyComparator &comparator, int leaf_max_size = LeafPage::LEAF_PAGE_SIZE, + int internal_max_size = InternalPage::INTERNAL_PAGE_SIZE); // Returns true if this B+ tree has no keys and values. auto IsEmpty() const -> bool; @@ -140,6 +138,9 @@ class BPlusTree { page_id_t header_page_id_; }; +template +using BPLUSTREE_TYPE = BPlusTree; + /** * @brief for test only. PrintableBPlusTree is a printalbe B+ tree. * We first convert B+ tree into a printable B+ tree and the print it. diff --git a/src/include/storage/index/b_plus_tree_index.h b/src/include/storage/index/b_plus_tree_index.h index 3b2c50e0f..42879ed36 100644 --- a/src/include/storage/index/b_plus_tree_index.h +++ b/src/include/storage/index/b_plus_tree_index.h @@ -22,8 +22,6 @@ namespace bustub { -#define BPLUSTREE_INDEX_TYPE BPlusTreeIndex - INDEX_TEMPLATE_ARGUMENTS class BPlusTreeIndex : public Index { public: diff --git a/src/include/storage/index/index_iterator.h b/src/include/storage/index/index_iterator.h index 7618823e1..cba7478d4 100644 --- a/src/include/storage/index/index_iterator.h +++ b/src/include/storage/index/index_iterator.h @@ -21,6 +21,9 @@ namespace bustub { INDEX_TEMPLATE_ARGUMENTS class IndexIterator { + using LeafPage = BPlusTreeLeafPage; + using MappingType = typename LeafPage::MappingType; + public: // you may define your own constructor based on your member variables IndexIterator(); diff --git a/src/include/storage/page/b_plus_tree_internal_page.h b/src/include/storage/page/b_plus_tree_internal_page.h index c26a5a86a..11bd1d1b0 100644 --- a/src/include/storage/page/b_plus_tree_internal_page.h +++ b/src/include/storage/page/b_plus_tree_internal_page.h @@ -17,9 +17,8 @@ namespace bustub { -#define B_PLUS_TREE_INTERNAL_PAGE_TYPE BPlusTreeInternalPage -#define INTERNAL_PAGE_HEADER_SIZE 12 -#define INTERNAL_PAGE_SIZE ((BUSTUB_PAGE_SIZE - INTERNAL_PAGE_HEADER_SIZE) / (sizeof(MappingType))) +constexpr const auto INTERNAL_PAGE_HEADER_SIZE = 12; + /** * Store n indexed keys and n+1 child pointers (page_id) within internal page. * Pointer PAGE_ID(i) points to a subtree in which all keys K satisfy: @@ -36,6 +35,11 @@ namespace bustub { INDEX_TEMPLATE_ARGUMENTS class BPlusTreeInternalPage : public BPlusTreePage { public: + using MappingType = MappingType_; + + constexpr static const auto INTERNAL_PAGE_SIZE = + (BUSTUB_PAGE_SIZE - INTERNAL_PAGE_HEADER_SIZE) / (sizeof(MappingType)); + // Deleted to disallow initialization BPlusTreeInternalPage() = delete; BPlusTreeInternalPage(const BPlusTreeInternalPage &other) = delete; diff --git a/src/include/storage/page/b_plus_tree_leaf_page.h b/src/include/storage/page/b_plus_tree_leaf_page.h index cf7a294b7..b70b62a6c 100644 --- a/src/include/storage/page/b_plus_tree_leaf_page.h +++ b/src/include/storage/page/b_plus_tree_leaf_page.h @@ -18,9 +18,7 @@ namespace bustub { -#define B_PLUS_TREE_LEAF_PAGE_TYPE BPlusTreeLeafPage -#define LEAF_PAGE_HEADER_SIZE 16 -#define LEAF_PAGE_SIZE ((BUSTUB_PAGE_SIZE - LEAF_PAGE_HEADER_SIZE) / sizeof(MappingType)) +constexpr const auto LEAF_PAGE_HEADER_SIZE = 16; /** * Store indexed key and record id(record id = page id combined with slot id, @@ -43,6 +41,10 @@ namespace bustub { INDEX_TEMPLATE_ARGUMENTS class BPlusTreeLeafPage : public BPlusTreePage { public: + using MappingType = MappingType_; + + constexpr static const auto LEAF_PAGE_SIZE = ((BUSTUB_PAGE_SIZE - LEAF_PAGE_HEADER_SIZE) / sizeof(MappingType)); + // Delete all constructor / destructor to ensure memory safety BPlusTreeLeafPage() = delete; BPlusTreeLeafPage(const BPlusTreeLeafPage &other) = delete; diff --git a/src/include/storage/page/b_plus_tree_page.h b/src/include/storage/page/b_plus_tree_page.h index 48914421a..fc2b1490b 100644 --- a/src/include/storage/page/b_plus_tree_page.h +++ b/src/include/storage/page/b_plus_tree_page.h @@ -20,7 +20,8 @@ namespace bustub { -#define MappingType std::pair +template +using MappingType_ = std::pair; #define INDEX_TEMPLATE_ARGUMENTS template diff --git a/src/storage/index/b_plus_tree.cpp b/src/storage/index/b_plus_tree.cpp index a906af0b9..6f6739b10 100644 --- a/src/storage/index/b_plus_tree.cpp +++ b/src/storage/index/b_plus_tree.cpp @@ -9,8 +9,10 @@ namespace bustub { INDEX_TEMPLATE_ARGUMENTS -BPLUSTREE_TYPE::BPlusTree(std::string name, page_id_t header_page_id, BufferPoolManager *buffer_pool_manager, - const KeyComparator &comparator, int leaf_max_size, int internal_max_size) +BPlusTree::BPlusTree(std::string name, page_id_t header_page_id, + BufferPoolManager *buffer_pool_manager, + const KeyComparator &comparator, int leaf_max_size, + int internal_max_size) : index_name_(std::move(name)), bpm_(buffer_pool_manager), comparator_(std::move(comparator)), @@ -26,7 +28,7 @@ BPLUSTREE_TYPE::BPlusTree(std::string name, page_id_t header_page_id, BufferPool * Helper function to decide whether current b+tree is empty */ INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_TYPE::IsEmpty() const -> bool { return true; } +auto BPlusTree::IsEmpty() const -> bool { return true; } /***************************************************************************** * SEARCH *****************************************************************************/ @@ -36,7 +38,8 @@ auto BPLUSTREE_TYPE::IsEmpty() const -> bool { return true; } * @return : true means key exists */ INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_TYPE::GetValue(const KeyType &key, std::vector *result, Transaction *txn) -> bool { +auto BPlusTree::GetValue(const KeyType &key, std::vector *result, + Transaction *txn) -> bool { // Declaration of context instance. Context ctx; (void)ctx; @@ -54,7 +57,8 @@ auto BPLUSTREE_TYPE::GetValue(const KeyType &key, std::vector *result * keys return false, otherwise return true. */ INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_TYPE::Insert(const KeyType &key, const ValueType &value, Transaction *txn) -> bool { +auto BPlusTree::Insert(const KeyType &key, const ValueType &value, Transaction *txn) + -> bool { // Declaration of context instance. Context ctx; (void)ctx; @@ -72,7 +76,7 @@ auto BPLUSTREE_TYPE::Insert(const KeyType &key, const ValueType &value, Transact * necessary. */ INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_TYPE::Remove(const KeyType &key, Transaction *txn) { +void BPlusTree::Remove(const KeyType &key, Transaction *txn) { // Declaration of context instance. Context ctx; (void)ctx; @@ -87,7 +91,7 @@ void BPLUSTREE_TYPE::Remove(const KeyType &key, Transaction *txn) { * @return : index iterator */ INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_TYPE::Begin() -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE(); } +auto BPlusTree::Begin() -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE(); } /* * Input parameter is low key, find the leaf page that contains the input key @@ -95,7 +99,9 @@ auto BPLUSTREE_TYPE::Begin() -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE() * @return : index iterator */ INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_TYPE::Begin(const KeyType &key) -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE(); } +auto BPlusTree::Begin(const KeyType &key) -> INDEXITERATOR_TYPE { + return INDEXITERATOR_TYPE(); +} /* * Input parameter is void, construct an index iterator representing the end @@ -103,13 +109,13 @@ auto BPLUSTREE_TYPE::Begin(const KeyType &key) -> INDEXITERATOR_TYPE { return IN * @return : index iterator */ INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_TYPE::End() -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE(); } +auto BPlusTree::End() -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE(); } /** * @return Page id of the root of this tree */ INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_TYPE::GetRootPageId() -> page_id_t { return 0; } +auto BPlusTree::GetRootPageId() -> page_id_t { return 0; } /***************************************************************************** * UTILITIES AND DEBUG @@ -120,7 +126,7 @@ auto BPLUSTREE_TYPE::GetRootPageId() -> page_id_t { return 0; } * Read data from file and insert one by one */ INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_TYPE::InsertFromFile(const std::string &file_name, Transaction *txn) { +void BPlusTree::InsertFromFile(const std::string &file_name, Transaction *txn) { int64_t key; std::ifstream input(file_name); while (input) { @@ -137,7 +143,7 @@ void BPLUSTREE_TYPE::InsertFromFile(const std::string &file_name, Transaction *t * Read data from file and remove one by one */ INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_TYPE::RemoveFromFile(const std::string &file_name, Transaction *txn) { +void BPlusTree::RemoveFromFile(const std::string &file_name, Transaction *txn) { int64_t key; std::ifstream input(file_name); while (input) { @@ -149,14 +155,14 @@ void BPLUSTREE_TYPE::RemoveFromFile(const std::string &file_name, Transaction *t } INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_TYPE::Print(BufferPoolManager *bpm) { +void BPlusTree::Print(BufferPoolManager *bpm) { auto root_page_id = GetRootPageId(); auto guard = bpm->FetchPageBasic(root_page_id); PrintTree(guard.PageId(), guard.template As()); } INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_TYPE::PrintTree(page_id_t page_id, const BPlusTreePage *page) { +void BPlusTree::PrintTree(page_id_t page_id, const BPlusTreePage *page) { if (page->IsLeafPage()) { auto *leaf = reinterpret_cast(page); std::cout << "Leaf Page: " << page_id << "\tNext: " << leaf->GetNextPageId() << std::endl; @@ -197,7 +203,7 @@ void BPLUSTREE_TYPE::PrintTree(page_id_t page_id, const BPlusTreePage *page) { * This method is used for debug only, You don't need to modify */ INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_TYPE::Draw(BufferPoolManager *bpm, const std::string &outf) { +void BPlusTree::Draw(BufferPoolManager *bpm, const std::string &outf) { if (IsEmpty()) { LOG_WARN("Drawing an empty tree"); return; @@ -216,7 +222,8 @@ void BPLUSTREE_TYPE::Draw(BufferPoolManager *bpm, const std::string &outf) { * This method is used for debug only, You don't need to modify */ INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_TYPE::ToGraph(page_id_t page_id, const BPlusTreePage *page, std::ofstream &out) { +void BPlusTree::ToGraph(page_id_t page_id, const BPlusTreePage *page, + std::ofstream &out) { std::string leaf_prefix("LEAF_"); std::string internal_prefix("INT_"); if (page->IsLeafPage()) { @@ -294,7 +301,7 @@ void BPLUSTREE_TYPE::ToGraph(page_id_t page_id, const BPlusTreePage *page, std:: } INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_TYPE::DrawBPlusTree() -> std::string { +auto BPlusTree::DrawBPlusTree() -> std::string { if (IsEmpty()) { return "()"; } @@ -307,7 +314,7 @@ auto BPLUSTREE_TYPE::DrawBPlusTree() -> std::string { } INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_TYPE::ToPrintableBPlusTree(page_id_t root_id) -> PrintableBPlusTree { +auto BPlusTree::ToPrintableBPlusTree(page_id_t root_id) -> PrintableBPlusTree { auto root_page_guard = bpm_->FetchPageBasic(root_id); auto root_page = root_page_guard.template As(); PrintableBPlusTree proot; diff --git a/src/storage/index/b_plus_tree_index.cpp b/src/storage/index/b_plus_tree_index.cpp index b4cefb9fb..8013e0747 100644 --- a/src/storage/index/b_plus_tree_index.cpp +++ b/src/storage/index/b_plus_tree_index.cpp @@ -16,7 +16,8 @@ namespace bustub { * Constructor */ INDEX_TEMPLATE_ARGUMENTS -BPLUSTREE_INDEX_TYPE::BPlusTreeIndex(std::unique_ptr &&metadata, BufferPoolManager *buffer_pool_manager) +BPlusTreeIndex::BPlusTreeIndex(std::unique_ptr &&metadata, + BufferPoolManager *buffer_pool_manager) : Index(std::move(metadata)), comparator_(GetMetadata()->GetKeySchema()) { page_id_t header_page_id; buffer_pool_manager->NewPage(&header_page_id); @@ -25,7 +26,8 @@ BPLUSTREE_INDEX_TYPE::BPlusTreeIndex(std::unique_ptr &&metadata, } INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_INDEX_TYPE::InsertEntry(const Tuple &key, RID rid, Transaction *transaction) -> bool { +auto BPlusTreeIndex::InsertEntry(const Tuple &key, RID rid, Transaction *transaction) + -> bool { // construct insert index key KeyType index_key; index_key.SetFromKey(key); @@ -34,7 +36,8 @@ auto BPLUSTREE_INDEX_TYPE::InsertEntry(const Tuple &key, RID rid, Transaction *t } INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_INDEX_TYPE::DeleteEntry(const Tuple &key, RID rid, Transaction *transaction) { +void BPlusTreeIndex::DeleteEntry(const Tuple &key, RID rid, + Transaction *transaction) { // construct delete index key KeyType index_key; index_key.SetFromKey(key); @@ -43,7 +46,8 @@ void BPLUSTREE_INDEX_TYPE::DeleteEntry(const Tuple &key, RID rid, Transaction *t } INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_INDEX_TYPE::ScanKey(const Tuple &key, std::vector *result, Transaction *transaction) { +void BPlusTreeIndex::ScanKey(const Tuple &key, std::vector *result, + Transaction *transaction) { // construct scan index key KeyType index_key; index_key.SetFromKey(key); @@ -52,13 +56,19 @@ void BPLUSTREE_INDEX_TYPE::ScanKey(const Tuple &key, std::vector *result, T } INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_INDEX_TYPE::GetBeginIterator() -> INDEXITERATOR_TYPE { return container_->Begin(); } +auto BPlusTreeIndex::GetBeginIterator() -> INDEXITERATOR_TYPE { + return container_->Begin(); +} INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_INDEX_TYPE::GetBeginIterator(const KeyType &key) -> INDEXITERATOR_TYPE { return container_->Begin(key); } +auto BPlusTreeIndex::GetBeginIterator(const KeyType &key) -> INDEXITERATOR_TYPE { + return container_->Begin(key); +} INDEX_TEMPLATE_ARGUMENTS -auto BPLUSTREE_INDEX_TYPE::GetEndIterator() -> INDEXITERATOR_TYPE { return container_->End(); } +auto BPlusTreeIndex::GetEndIterator() -> INDEXITERATOR_TYPE { + return container_->End(); +} template class BPlusTreeIndex, RID, GenericComparator<4>>; template class BPlusTreeIndex, RID, GenericComparator<8>>; diff --git a/src/storage/page/b_plus_tree_internal_page.cpp b/src/storage/page/b_plus_tree_internal_page.cpp index e0ab2b33c..45f898e52 100644 --- a/src/storage/page/b_plus_tree_internal_page.cpp +++ b/src/storage/page/b_plus_tree_internal_page.cpp @@ -24,27 +24,27 @@ namespace bustub { * Including set page type, set current size, and set max page size */ INDEX_TEMPLATE_ARGUMENTS -void B_PLUS_TREE_INTERNAL_PAGE_TYPE::Init(int max_size) {} +void BPlusTreeInternalPage::Init(int max_size) {} /* * Helper method to get/set the key associated with input "index"(a.k.a * array offset) */ INDEX_TEMPLATE_ARGUMENTS -auto B_PLUS_TREE_INTERNAL_PAGE_TYPE::KeyAt(int index) const -> KeyType { +auto BPlusTreeInternalPage::KeyAt(int index) const -> KeyType { // replace with your own code KeyType key{}; return key; } INDEX_TEMPLATE_ARGUMENTS -void B_PLUS_TREE_INTERNAL_PAGE_TYPE::SetKeyAt(int index, const KeyType &key) {} +void BPlusTreeInternalPage::SetKeyAt(int index, const KeyType &key) {} /* * Helper method to get the value associated with input "index"(a.k.a array * offset) */ INDEX_TEMPLATE_ARGUMENTS -auto B_PLUS_TREE_INTERNAL_PAGE_TYPE::ValueAt(int index) const -> ValueType { return 0; } +auto BPlusTreeInternalPage::ValueAt(int index) const -> ValueType { return 0; } // valuetype for internalNode should be page id_t template class BPlusTreeInternalPage, page_id_t, GenericComparator<4>>; diff --git a/src/storage/page/b_plus_tree_leaf_page.cpp b/src/storage/page/b_plus_tree_leaf_page.cpp index 3b325d721..37d11207f 100644 --- a/src/storage/page/b_plus_tree_leaf_page.cpp +++ b/src/storage/page/b_plus_tree_leaf_page.cpp @@ -26,23 +26,25 @@ namespace bustub { * Including set page type, set current size to zero, set next page id and set max size */ INDEX_TEMPLATE_ARGUMENTS -void B_PLUS_TREE_LEAF_PAGE_TYPE::Init(int max_size) {} +void BPlusTreeLeafPage::Init(int max_size) {} /** * Helper methods to set/get next page id */ INDEX_TEMPLATE_ARGUMENTS -auto B_PLUS_TREE_LEAF_PAGE_TYPE::GetNextPageId() const -> page_id_t { return INVALID_PAGE_ID; } +auto BPlusTreeLeafPage::GetNextPageId() const -> page_id_t { + return INVALID_PAGE_ID; +} INDEX_TEMPLATE_ARGUMENTS -void B_PLUS_TREE_LEAF_PAGE_TYPE::SetNextPageId(page_id_t next_page_id) {} +void BPlusTreeLeafPage::SetNextPageId(page_id_t next_page_id) {} /* * Helper method to find and return the key associated with input "index"(a.k.a * array offset) */ INDEX_TEMPLATE_ARGUMENTS -auto B_PLUS_TREE_LEAF_PAGE_TYPE::KeyAt(int index) const -> KeyType { +auto BPlusTreeLeafPage::KeyAt(int index) const -> KeyType { // replace with your own code KeyType key{}; return key; From aa7c4bf74f87ba9eae8ff37995f71075224a34e8 Mon Sep 17 00:00:00 2001 From: clundro Date: Fri, 24 Mar 2023 17:09:41 +0800 Subject: [PATCH 2/5] [trash] fix code format Signed-off-by: clundro --- src/include/storage/page/b_plus_tree_page.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/include/storage/page/b_plus_tree_page.h b/src/include/storage/page/b_plus_tree_page.h index fc2b1490b..d0a8dce54 100644 --- a/src/include/storage/page/b_plus_tree_page.h +++ b/src/include/storage/page/b_plus_tree_page.h @@ -14,6 +14,7 @@ #include #include #include +#include #include "buffer/buffer_pool_manager.h" #include "storage/index/generic_key.h" From 663582a3cf032c6db7adbe20c9734e5c107d4bf4 Mon Sep 17 00:00:00 2001 From: clundro Date: Sat, 25 Mar 2023 01:30:54 +0800 Subject: [PATCH 3/5] [spam] delete uncessary changes. Signed-off-by: clundro --- src/include/storage/index/b_plus_tree.h | 5 ++-- src/include/storage/index/b_plus_tree_index.h | 2 ++ .../storage/page/b_plus_tree_internal_page.h | 2 ++ .../storage/page/b_plus_tree_leaf_page.h | 2 ++ src/storage/index/b_plus_tree_index.cpp | 24 ++++++------------- .../page/b_plus_tree_internal_page.cpp | 8 +++---- src/storage/page/b_plus_tree_leaf_page.cpp | 14 ++++++----- 7 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/include/storage/index/b_plus_tree.h b/src/include/storage/index/b_plus_tree.h index d27b657f0..ad3036d05 100644 --- a/src/include/storage/index/b_plus_tree.h +++ b/src/include/storage/index/b_plus_tree.h @@ -56,6 +56,8 @@ class Context { auto IsRootPage(page_id_t page_id) -> bool { return page_id == root_page_id_; } }; +#define BPLUSTREE_TYPE BPlusTree + // Main class providing the API for the Interactive B+ Tree. INDEX_TEMPLATE_ARGUMENTS class BPlusTree { @@ -138,9 +140,6 @@ class BPlusTree { page_id_t header_page_id_; }; -template -using BPLUSTREE_TYPE = BPlusTree; - /** * @brief for test only. PrintableBPlusTree is a printalbe B+ tree. * We first convert B+ tree into a printable B+ tree and the print it. diff --git a/src/include/storage/index/b_plus_tree_index.h b/src/include/storage/index/b_plus_tree_index.h index 42879ed36..3b2c50e0f 100644 --- a/src/include/storage/index/b_plus_tree_index.h +++ b/src/include/storage/index/b_plus_tree_index.h @@ -22,6 +22,8 @@ namespace bustub { +#define BPLUSTREE_INDEX_TYPE BPlusTreeIndex + INDEX_TEMPLATE_ARGUMENTS class BPlusTreeIndex : public Index { public: diff --git a/src/include/storage/page/b_plus_tree_internal_page.h b/src/include/storage/page/b_plus_tree_internal_page.h index 11bd1d1b0..517f23a61 100644 --- a/src/include/storage/page/b_plus_tree_internal_page.h +++ b/src/include/storage/page/b_plus_tree_internal_page.h @@ -17,6 +17,8 @@ namespace bustub { +#define B_PLUS_TREE_INTERNAL_PAGE_TYPE BPlusTreeInternalPage + constexpr const auto INTERNAL_PAGE_HEADER_SIZE = 12; /** diff --git a/src/include/storage/page/b_plus_tree_leaf_page.h b/src/include/storage/page/b_plus_tree_leaf_page.h index b70b62a6c..48d514790 100644 --- a/src/include/storage/page/b_plus_tree_leaf_page.h +++ b/src/include/storage/page/b_plus_tree_leaf_page.h @@ -18,6 +18,8 @@ namespace bustub { +#define B_PLUS_TREE_LEAF_PAGE_TYPE BPlusTreeLeafPage + constexpr const auto LEAF_PAGE_HEADER_SIZE = 16; /** diff --git a/src/storage/index/b_plus_tree_index.cpp b/src/storage/index/b_plus_tree_index.cpp index 8013e0747..b4cefb9fb 100644 --- a/src/storage/index/b_plus_tree_index.cpp +++ b/src/storage/index/b_plus_tree_index.cpp @@ -16,8 +16,7 @@ namespace bustub { * Constructor */ INDEX_TEMPLATE_ARGUMENTS -BPlusTreeIndex::BPlusTreeIndex(std::unique_ptr &&metadata, - BufferPoolManager *buffer_pool_manager) +BPLUSTREE_INDEX_TYPE::BPlusTreeIndex(std::unique_ptr &&metadata, BufferPoolManager *buffer_pool_manager) : Index(std::move(metadata)), comparator_(GetMetadata()->GetKeySchema()) { page_id_t header_page_id; buffer_pool_manager->NewPage(&header_page_id); @@ -26,8 +25,7 @@ BPlusTreeIndex::BPlusTreeIndex(std::unique_pt } INDEX_TEMPLATE_ARGUMENTS -auto BPlusTreeIndex::InsertEntry(const Tuple &key, RID rid, Transaction *transaction) - -> bool { +auto BPLUSTREE_INDEX_TYPE::InsertEntry(const Tuple &key, RID rid, Transaction *transaction) -> bool { // construct insert index key KeyType index_key; index_key.SetFromKey(key); @@ -36,8 +34,7 @@ auto BPlusTreeIndex::InsertEntry(const Tuple } INDEX_TEMPLATE_ARGUMENTS -void BPlusTreeIndex::DeleteEntry(const Tuple &key, RID rid, - Transaction *transaction) { +void BPLUSTREE_INDEX_TYPE::DeleteEntry(const Tuple &key, RID rid, Transaction *transaction) { // construct delete index key KeyType index_key; index_key.SetFromKey(key); @@ -46,8 +43,7 @@ void BPlusTreeIndex::DeleteEntry(const Tuple } INDEX_TEMPLATE_ARGUMENTS -void BPlusTreeIndex::ScanKey(const Tuple &key, std::vector *result, - Transaction *transaction) { +void BPLUSTREE_INDEX_TYPE::ScanKey(const Tuple &key, std::vector *result, Transaction *transaction) { // construct scan index key KeyType index_key; index_key.SetFromKey(key); @@ -56,19 +52,13 @@ void BPlusTreeIndex::ScanKey(const Tuple &key } INDEX_TEMPLATE_ARGUMENTS -auto BPlusTreeIndex::GetBeginIterator() -> INDEXITERATOR_TYPE { - return container_->Begin(); -} +auto BPLUSTREE_INDEX_TYPE::GetBeginIterator() -> INDEXITERATOR_TYPE { return container_->Begin(); } INDEX_TEMPLATE_ARGUMENTS -auto BPlusTreeIndex::GetBeginIterator(const KeyType &key) -> INDEXITERATOR_TYPE { - return container_->Begin(key); -} +auto BPLUSTREE_INDEX_TYPE::GetBeginIterator(const KeyType &key) -> INDEXITERATOR_TYPE { return container_->Begin(key); } INDEX_TEMPLATE_ARGUMENTS -auto BPlusTreeIndex::GetEndIterator() -> INDEXITERATOR_TYPE { - return container_->End(); -} +auto BPLUSTREE_INDEX_TYPE::GetEndIterator() -> INDEXITERATOR_TYPE { return container_->End(); } template class BPlusTreeIndex, RID, GenericComparator<4>>; template class BPlusTreeIndex, RID, GenericComparator<8>>; diff --git a/src/storage/page/b_plus_tree_internal_page.cpp b/src/storage/page/b_plus_tree_internal_page.cpp index 45f898e52..e0ab2b33c 100644 --- a/src/storage/page/b_plus_tree_internal_page.cpp +++ b/src/storage/page/b_plus_tree_internal_page.cpp @@ -24,27 +24,27 @@ namespace bustub { * Including set page type, set current size, and set max page size */ INDEX_TEMPLATE_ARGUMENTS -void BPlusTreeInternalPage::Init(int max_size) {} +void B_PLUS_TREE_INTERNAL_PAGE_TYPE::Init(int max_size) {} /* * Helper method to get/set the key associated with input "index"(a.k.a * array offset) */ INDEX_TEMPLATE_ARGUMENTS -auto BPlusTreeInternalPage::KeyAt(int index) const -> KeyType { +auto B_PLUS_TREE_INTERNAL_PAGE_TYPE::KeyAt(int index) const -> KeyType { // replace with your own code KeyType key{}; return key; } INDEX_TEMPLATE_ARGUMENTS -void BPlusTreeInternalPage::SetKeyAt(int index, const KeyType &key) {} +void B_PLUS_TREE_INTERNAL_PAGE_TYPE::SetKeyAt(int index, const KeyType &key) {} /* * Helper method to get the value associated with input "index"(a.k.a array * offset) */ INDEX_TEMPLATE_ARGUMENTS -auto BPlusTreeInternalPage::ValueAt(int index) const -> ValueType { return 0; } +auto B_PLUS_TREE_INTERNAL_PAGE_TYPE::ValueAt(int index) const -> ValueType { return 0; } // valuetype for internalNode should be page id_t template class BPlusTreeInternalPage, page_id_t, GenericComparator<4>>; diff --git a/src/storage/page/b_plus_tree_leaf_page.cpp b/src/storage/page/b_plus_tree_leaf_page.cpp index 37d11207f..1e4047ebc 100644 --- a/src/storage/page/b_plus_tree_leaf_page.cpp +++ b/src/storage/page/b_plus_tree_leaf_page.cpp @@ -1,3 +1,6 @@ +/* + + */ //===----------------------------------------------------------------------===// // // CMU-DB Project (15-445/645) @@ -14,6 +17,7 @@ #include "common/exception.h" #include "common/rid.h" #include "storage/page/b_plus_tree_leaf_page.h" +#include "storage/page/b_plus_tree_page.h" namespace bustub { @@ -26,25 +30,23 @@ namespace bustub { * Including set page type, set current size to zero, set next page id and set max size */ INDEX_TEMPLATE_ARGUMENTS -void BPlusTreeLeafPage::Init(int max_size) {} +void B_PLUS_TREE_LEAF_PAGE_TYPE::Init(int max_size) {} /** * Helper methods to set/get next page id */ INDEX_TEMPLATE_ARGUMENTS -auto BPlusTreeLeafPage::GetNextPageId() const -> page_id_t { - return INVALID_PAGE_ID; -} +auto B_PLUS_TREE_LEAF_PAGE_TYPE::GetNextPageId() const -> page_id_t { return INVALID_PAGE_ID; } INDEX_TEMPLATE_ARGUMENTS -void BPlusTreeLeafPage::SetNextPageId(page_id_t next_page_id) {} +void B_PLUS_TREE_LEAF_PAGE_TYPE::SetNextPageId(page_id_t next_page_id) {} /* * Helper method to find and return the key associated with input "index"(a.k.a * array offset) */ INDEX_TEMPLATE_ARGUMENTS -auto BPlusTreeLeafPage::KeyAt(int index) const -> KeyType { +auto B_PLUS_TREE_LEAF_PAGE_TYPE::KeyAt(int index) const -> KeyType { // replace with your own code KeyType key{}; return key; From c0e2341c8401b695acfdb3d24bdcaef2dc618b7a Mon Sep 17 00:00:00 2001 From: clundro Date: Sat, 25 Mar 2023 01:33:57 +0800 Subject: [PATCH 4/5] [spam] change to old b_plus_tree Signed-off-by: clundro --- src/storage/index/b_plus_tree.cpp | 43 +++++++++++++------------------ 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/src/storage/index/b_plus_tree.cpp b/src/storage/index/b_plus_tree.cpp index 6f6739b10..a906af0b9 100644 --- a/src/storage/index/b_plus_tree.cpp +++ b/src/storage/index/b_plus_tree.cpp @@ -9,10 +9,8 @@ namespace bustub { INDEX_TEMPLATE_ARGUMENTS -BPlusTree::BPlusTree(std::string name, page_id_t header_page_id, - BufferPoolManager *buffer_pool_manager, - const KeyComparator &comparator, int leaf_max_size, - int internal_max_size) +BPLUSTREE_TYPE::BPlusTree(std::string name, page_id_t header_page_id, BufferPoolManager *buffer_pool_manager, + const KeyComparator &comparator, int leaf_max_size, int internal_max_size) : index_name_(std::move(name)), bpm_(buffer_pool_manager), comparator_(std::move(comparator)), @@ -28,7 +26,7 @@ BPlusTree::BPlusTree(std::string name, page_i * Helper function to decide whether current b+tree is empty */ INDEX_TEMPLATE_ARGUMENTS -auto BPlusTree::IsEmpty() const -> bool { return true; } +auto BPLUSTREE_TYPE::IsEmpty() const -> bool { return true; } /***************************************************************************** * SEARCH *****************************************************************************/ @@ -38,8 +36,7 @@ auto BPlusTree::IsEmpty() const -> bool { ret * @return : true means key exists */ INDEX_TEMPLATE_ARGUMENTS -auto BPlusTree::GetValue(const KeyType &key, std::vector *result, - Transaction *txn) -> bool { +auto BPLUSTREE_TYPE::GetValue(const KeyType &key, std::vector *result, Transaction *txn) -> bool { // Declaration of context instance. Context ctx; (void)ctx; @@ -57,8 +54,7 @@ auto BPlusTree::GetValue(const KeyType &key, * keys return false, otherwise return true. */ INDEX_TEMPLATE_ARGUMENTS -auto BPlusTree::Insert(const KeyType &key, const ValueType &value, Transaction *txn) - -> bool { +auto BPLUSTREE_TYPE::Insert(const KeyType &key, const ValueType &value, Transaction *txn) -> bool { // Declaration of context instance. Context ctx; (void)ctx; @@ -76,7 +72,7 @@ auto BPlusTree::Insert(const KeyType &key, co * necessary. */ INDEX_TEMPLATE_ARGUMENTS -void BPlusTree::Remove(const KeyType &key, Transaction *txn) { +void BPLUSTREE_TYPE::Remove(const KeyType &key, Transaction *txn) { // Declaration of context instance. Context ctx; (void)ctx; @@ -91,7 +87,7 @@ void BPlusTree::Remove(const KeyType &key, Tr * @return : index iterator */ INDEX_TEMPLATE_ARGUMENTS -auto BPlusTree::Begin() -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE(); } +auto BPLUSTREE_TYPE::Begin() -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE(); } /* * Input parameter is low key, find the leaf page that contains the input key @@ -99,9 +95,7 @@ auto BPlusTree::Begin() -> INDEXITERATOR_TYPE * @return : index iterator */ INDEX_TEMPLATE_ARGUMENTS -auto BPlusTree::Begin(const KeyType &key) -> INDEXITERATOR_TYPE { - return INDEXITERATOR_TYPE(); -} +auto BPLUSTREE_TYPE::Begin(const KeyType &key) -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE(); } /* * Input parameter is void, construct an index iterator representing the end @@ -109,13 +103,13 @@ auto BPlusTree::Begin(const KeyType &key) -> * @return : index iterator */ INDEX_TEMPLATE_ARGUMENTS -auto BPlusTree::End() -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE(); } +auto BPLUSTREE_TYPE::End() -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE(); } /** * @return Page id of the root of this tree */ INDEX_TEMPLATE_ARGUMENTS -auto BPlusTree::GetRootPageId() -> page_id_t { return 0; } +auto BPLUSTREE_TYPE::GetRootPageId() -> page_id_t { return 0; } /***************************************************************************** * UTILITIES AND DEBUG @@ -126,7 +120,7 @@ auto BPlusTree::GetRootPageId() -> page_id_t * Read data from file and insert one by one */ INDEX_TEMPLATE_ARGUMENTS -void BPlusTree::InsertFromFile(const std::string &file_name, Transaction *txn) { +void BPLUSTREE_TYPE::InsertFromFile(const std::string &file_name, Transaction *txn) { int64_t key; std::ifstream input(file_name); while (input) { @@ -143,7 +137,7 @@ void BPlusTree::InsertFromFile(const std::str * Read data from file and remove one by one */ INDEX_TEMPLATE_ARGUMENTS -void BPlusTree::RemoveFromFile(const std::string &file_name, Transaction *txn) { +void BPLUSTREE_TYPE::RemoveFromFile(const std::string &file_name, Transaction *txn) { int64_t key; std::ifstream input(file_name); while (input) { @@ -155,14 +149,14 @@ void BPlusTree::RemoveFromFile(const std::str } INDEX_TEMPLATE_ARGUMENTS -void BPlusTree::Print(BufferPoolManager *bpm) { +void BPLUSTREE_TYPE::Print(BufferPoolManager *bpm) { auto root_page_id = GetRootPageId(); auto guard = bpm->FetchPageBasic(root_page_id); PrintTree(guard.PageId(), guard.template As()); } INDEX_TEMPLATE_ARGUMENTS -void BPlusTree::PrintTree(page_id_t page_id, const BPlusTreePage *page) { +void BPLUSTREE_TYPE::PrintTree(page_id_t page_id, const BPlusTreePage *page) { if (page->IsLeafPage()) { auto *leaf = reinterpret_cast(page); std::cout << "Leaf Page: " << page_id << "\tNext: " << leaf->GetNextPageId() << std::endl; @@ -203,7 +197,7 @@ void BPlusTree::PrintTree(page_id_t page_id, * This method is used for debug only, You don't need to modify */ INDEX_TEMPLATE_ARGUMENTS -void BPlusTree::Draw(BufferPoolManager *bpm, const std::string &outf) { +void BPLUSTREE_TYPE::Draw(BufferPoolManager *bpm, const std::string &outf) { if (IsEmpty()) { LOG_WARN("Drawing an empty tree"); return; @@ -222,8 +216,7 @@ void BPlusTree::Draw(BufferPoolManager *bpm, * This method is used for debug only, You don't need to modify */ INDEX_TEMPLATE_ARGUMENTS -void BPlusTree::ToGraph(page_id_t page_id, const BPlusTreePage *page, - std::ofstream &out) { +void BPLUSTREE_TYPE::ToGraph(page_id_t page_id, const BPlusTreePage *page, std::ofstream &out) { std::string leaf_prefix("LEAF_"); std::string internal_prefix("INT_"); if (page->IsLeafPage()) { @@ -301,7 +294,7 @@ void BPlusTree::ToGraph(page_id_t page_id, co } INDEX_TEMPLATE_ARGUMENTS -auto BPlusTree::DrawBPlusTree() -> std::string { +auto BPLUSTREE_TYPE::DrawBPlusTree() -> std::string { if (IsEmpty()) { return "()"; } @@ -314,7 +307,7 @@ auto BPlusTree::DrawBPlusTree() -> std::strin } INDEX_TEMPLATE_ARGUMENTS -auto BPlusTree::ToPrintableBPlusTree(page_id_t root_id) -> PrintableBPlusTree { +auto BPLUSTREE_TYPE::ToPrintableBPlusTree(page_id_t root_id) -> PrintableBPlusTree { auto root_page_guard = bpm_->FetchPageBasic(root_id); auto root_page = root_page_guard.template As(); PrintableBPlusTree proot; From 08bb1fd0dd3651191d964d1d569fb00ba9e5f4f6 Mon Sep 17 00:00:00 2001 From: clundro Date: Sat, 25 Mar 2023 11:30:37 +0800 Subject: [PATCH 5/5] [spam] wipe off Signed-off-by: clundro --- src/storage/page/b_plus_tree_leaf_page.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/storage/page/b_plus_tree_leaf_page.cpp b/src/storage/page/b_plus_tree_leaf_page.cpp index 1e4047ebc..219978b7b 100644 --- a/src/storage/page/b_plus_tree_leaf_page.cpp +++ b/src/storage/page/b_plus_tree_leaf_page.cpp @@ -1,6 +1,3 @@ -/* - - */ //===----------------------------------------------------------------------===// // // CMU-DB Project (15-445/645)