diff --git a/src/include/storage/index/b_plus_tree.h b/src/include/storage/index/b_plus_tree.h index bc1c31b27..ad3036d05 100644 --- a/src/include/storage/index/b_plus_tree.h +++ b/src/include/storage/index/b_plus_tree.h @@ -66,8 +66,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; 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..517f23a61 100644 --- a/src/include/storage/page/b_plus_tree_internal_page.h +++ b/src/include/storage/page/b_plus_tree_internal_page.h @@ -18,8 +18,9 @@ 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 +37,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..48d514790 100644 --- a/src/include/storage/page/b_plus_tree_leaf_page.h +++ b/src/include/storage/page/b_plus_tree_leaf_page.h @@ -19,8 +19,8 @@ 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 +43,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..d0a8dce54 100644 --- a/src/include/storage/page/b_plus_tree_page.h +++ b/src/include/storage/page/b_plus_tree_page.h @@ -14,13 +14,15 @@ #include #include #include +#include #include "buffer/buffer_pool_manager.h" #include "storage/index/generic_key.h" namespace bustub { -#define MappingType std::pair +template +using MappingType_ = std::pair; #define INDEX_TEMPLATE_ARGUMENTS template diff --git a/src/storage/page/b_plus_tree_leaf_page.cpp b/src/storage/page/b_plus_tree_leaf_page.cpp index 3b325d721..219978b7b 100644 --- a/src/storage/page/b_plus_tree_leaf_page.cpp +++ b/src/storage/page/b_plus_tree_leaf_page.cpp @@ -14,6 +14,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 {