Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(p2): remove #define type in BTree #547

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/include/storage/index/b_plus_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/include/storage/index/index_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace bustub {

INDEX_TEMPLATE_ARGUMENTS
class IndexIterator {
using LeafPage = BPlusTreeLeafPage<KeyType, ValueType, KeyComparator>;
using MappingType = typename LeafPage::MappingType;

public:
// you may define your own constructor based on your member variables
IndexIterator();
Expand Down
10 changes: 8 additions & 2 deletions src/include/storage/page/b_plus_tree_internal_page.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
namespace bustub {

#define B_PLUS_TREE_INTERNAL_PAGE_TYPE BPlusTreeInternalPage<KeyType, ValueType, KeyComparator>
#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:
Expand All @@ -36,6 +37,11 @@ namespace bustub {
INDEX_TEMPLATE_ARGUMENTS
class BPlusTreeInternalPage : public BPlusTreePage {
public:
using MappingType = MappingType_<KeyType, ValueType>;

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;
Expand Down
8 changes: 6 additions & 2 deletions src/include/storage/page/b_plus_tree_leaf_page.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
namespace bustub {

#define B_PLUS_TREE_LEAF_PAGE_TYPE BPlusTreeLeafPage<KeyType, ValueType, KeyComparator>
#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,
Expand All @@ -43,6 +43,10 @@ namespace bustub {
INDEX_TEMPLATE_ARGUMENTS
class BPlusTreeLeafPage : public BPlusTreePage {
public:
using MappingType = MappingType_<KeyType, ValueType>;

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;
Expand Down
4 changes: 3 additions & 1 deletion src/include/storage/page/b_plus_tree_page.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
#include <climits>
#include <cstdlib>
#include <string>
#include <utility>

#include "buffer/buffer_pool_manager.h"
#include "storage/index/generic_key.h"

namespace bustub {

#define MappingType std::pair<KeyType, ValueType>
template <typename KeyType, typename ValueType>
using MappingType_ = std::pair<KeyType, ValueType>;

#define INDEX_TEMPLATE_ARGUMENTS template <typename KeyType, typename ValueType, typename KeyComparator>

Expand Down
1 change: 1 addition & 0 deletions src/storage/page/b_plus_tree_leaf_page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down