|
8 | 8 | // Copyright (c) 2018, Carnegie Mellon University Database Group
|
9 | 9 | //
|
10 | 10 | //===----------------------------------------------------------------------===//
|
| 11 | +/** |
| 12 | + * b_plus_tree.h |
| 13 | + * |
| 14 | + * Implementation of simple b+ tree data structure where internal pages direct |
| 15 | + * the search and leaf pages contain actual data. |
| 16 | + * (1) We only support unique key |
| 17 | + * (2) support insert & remove |
| 18 | + * (3) The structure should shrink and grow dynamically |
| 19 | + * (4) Implement index iterator for range scan |
| 20 | + */ |
11 | 21 | #pragma once
|
12 | 22 |
|
| 23 | +#include <algorithm> |
| 24 | +#include <deque> |
| 25 | +#include <optional> |
13 | 26 | #include <queue>
|
14 |
| -#include <string> |
| 27 | +#include <shared_mutex> |
15 | 28 | #include <vector>
|
16 | 29 |
|
| 30 | +#include "common/config.h" |
| 31 | +#include "common/macros.h" |
17 | 32 | #include "concurrency/transaction.h"
|
18 | 33 | #include "storage/index/index_iterator.h"
|
19 | 34 | #include "storage/page/b_plus_tree_internal_page.h"
|
20 | 35 | #include "storage/page/b_plus_tree_leaf_page.h"
|
| 36 | +#include "storage/page/b_plus_tree_root_page.h" |
| 37 | +#include "storage/page/page_guard.h" |
21 | 38 |
|
22 | 39 | namespace bustub {
|
23 | 40 |
|
24 |
| -#define BPLUSTREE_TYPE BPlusTree<KeyType, ValueType, KeyComparator> |
25 |
| - |
26 | 41 | /**
|
27 |
| - * Main class providing the API for the Interactive B+ Tree. |
| 42 | + * @brief Definition of the Context class. |
28 | 43 | *
|
29 |
| - * Implementation of simple b+ tree data structure where internal pages direct |
30 |
| - * the search and leaf pages contain actual data. |
31 |
| - * (1) We only support unique key |
32 |
| - * (2) support insert & remove |
33 |
| - * (3) The structure should shrink and grow dynamically |
34 |
| - * (4) Implement index iterator for range scan |
| 44 | + * Hint: This class is designed to help you keep track of the pages |
| 45 | + * that you're modifying or accessing. |
35 | 46 | */
|
| 47 | +class Context { |
| 48 | + public: |
| 49 | + std::optional<WritePageGuard> header_page_{std::nullopt}; |
| 50 | + page_id_t root_page_id_{INVALID_PAGE_ID}; |
| 51 | + std::deque<WritePageGuard> write_set_; |
| 52 | + std::deque<ReadPageGuard> read_set_; |
| 53 | + |
| 54 | + auto IsRootPage(page_id_t page_id) -> bool { return page_id == root_page_id_; } |
| 55 | +}; |
| 56 | + |
| 57 | +#define BPLUSTREE_TYPE BPlusTree<KeyType, ValueType, KeyComparator> |
| 58 | +// Main class providing the API for the Interactive B+ Tree. |
36 | 59 | INDEX_TEMPLATE_ARGUMENTS
|
37 | 60 | class BPlusTree {
|
38 | 61 | using InternalPage = BPlusTreeInternalPage<KeyType, page_id_t, KeyComparator>;
|
39 | 62 | using LeafPage = BPlusTreeLeafPage<KeyType, ValueType, KeyComparator>;
|
40 | 63 |
|
41 | 64 | public:
|
42 |
| - explicit BPlusTree(std::string name, BufferPoolManager *buffer_pool_manager, const KeyComparator &comparator, |
43 |
| - int leaf_max_size = LEAF_PAGE_SIZE, int internal_max_size = INTERNAL_PAGE_SIZE); |
| 65 | + explicit BPlusTree(std::string name, page_id_t header_page_id, BufferPoolManager *buffer_pool_manager, |
| 66 | + const KeyComparator &comparator, int leaf_max_size = LEAF_PAGE_SIZE, |
| 67 | + int internal_max_size = INTERNAL_PAGE_SIZE); |
44 | 68 |
|
45 | 69 | // Returns true if this B+ tree has no keys and values.
|
46 | 70 | auto IsEmpty() const -> bool;
|
47 | 71 |
|
48 | 72 | // Insert a key-value pair into this B+ tree.
|
49 |
| - auto Insert(const KeyType &key, const ValueType &value, Transaction *transaction = nullptr) -> bool; |
| 73 | + auto Insert(const KeyType &key, const ValueType &value, Transaction *txn = nullptr) -> bool; |
50 | 74 |
|
51 | 75 | // Remove a key and its value from this B+ tree.
|
52 |
| - void Remove(const KeyType &key, Transaction *transaction = nullptr); |
| 76 | + void Remove(const KeyType &key, Transaction *txn); |
53 | 77 |
|
54 |
| - // return the value associated with a given key |
55 |
| - auto GetValue(const KeyType &key, std::vector<ValueType> *result, Transaction *transaction = nullptr) -> bool; |
| 78 | + // Return the value associated with a given key |
| 79 | + auto GetValue(const KeyType &key, std::vector<ValueType> *result, Transaction *txn = nullptr) -> bool; |
56 | 80 |
|
57 |
| - // return the page id of the root node |
| 81 | + // Return the page id of the root node |
58 | 82 | auto GetRootPageId() -> page_id_t;
|
59 | 83 |
|
60 |
| - // index iterator |
| 84 | + // Index iterator |
61 | 85 | auto Begin() -> INDEXITERATOR_TYPE;
|
62 |
| - auto Begin(const KeyType &key) -> INDEXITERATOR_TYPE; |
| 86 | + |
63 | 87 | auto End() -> INDEXITERATOR_TYPE;
|
64 | 88 |
|
65 |
| - // print the B+ tree |
| 89 | + auto Begin(const KeyType &key) -> INDEXITERATOR_TYPE; |
| 90 | + |
| 91 | + // Print the B+ tree |
66 | 92 | void Print(BufferPoolManager *bpm);
|
67 | 93 |
|
68 |
| - // draw the B+ tree |
| 94 | + // Draw the B+ tree |
69 | 95 | void Draw(BufferPoolManager *bpm, const std::string &outf);
|
70 | 96 |
|
71 | 97 | // read data from file and insert one by one
|
72 |
| - void InsertFromFile(const std::string &file_name, Transaction *transaction = nullptr); |
| 98 | + void InsertFromFile(const std::string &file_name, Transaction *txn = nullptr); |
73 | 99 |
|
74 | 100 | // read data from file and remove one by one
|
75 |
| - void RemoveFromFile(const std::string &file_name, Transaction *transaction = nullptr); |
| 101 | + void RemoveFromFile(const std::string &file_name, Transaction *txn = nullptr); |
76 | 102 |
|
77 | 103 | private:
|
78 |
| - void UpdateRootPageId(int insert_record = 0); |
79 |
| - |
80 | 104 | /* Debug Routines for FREE!! */
|
81 |
| - void ToGraph(BPlusTreePage *page, BufferPoolManager *bpm, std::ofstream &out) const; |
| 105 | + void ToGraph(page_id_t page_id, const BPlusTreePage *page, std::ofstream &out); |
| 106 | + |
| 107 | + void PrintTree(page_id_t page_id, const BPlusTreePage *page); |
82 | 108 |
|
83 |
| - void ToString(BPlusTreePage *page, BufferPoolManager *bpm) const; |
| 109 | + void Dump() { |
| 110 | + for (auto &f : log) { |
| 111 | + std::cout << f << std::endl; |
| 112 | + } |
| 113 | + } |
84 | 114 |
|
85 | 115 | // member variable
|
86 | 116 | std::string index_name_;
|
87 |
| - page_id_t root_page_id_; |
88 |
| - BufferPoolManager *buffer_pool_manager_; |
| 117 | + BufferPoolManager *bpm_; |
89 | 118 | KeyComparator comparator_;
|
| 119 | + std::vector<std::string> log; // NOLINT |
90 | 120 | int leaf_max_size_;
|
91 | 121 | int internal_max_size_;
|
| 122 | + page_id_t header_page_id_; |
92 | 123 | };
|
93 | 124 |
|
94 | 125 | } // namespace bustub
|
0 commit comments