-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathb_plus_tree_internal_page.h
112 lines (94 loc) · 3.21 KB
/
b_plus_tree_internal_page.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//===----------------------------------------------------------------------===//
//
// CMU-DB Project (15-445/645)
// ***DO NO SHARE PUBLICLY***
//
// Identification: src/include/page/b_plus_tree_internal_page.h
//
// Copyright (c) 2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#pragma once
#include <queue>
#include <string>
#include "storage/page/b_plus_tree_page.h"
namespace bustub {
#define B_PLUS_TREE_INTERNAL_PAGE_TYPE BPlusTreeInternalPage<KeyType, ValueType, KeyComparator>
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:
* K(i) <= K < K(i+1).
* NOTE: since the number of keys does not equal to number of child pointers,
* the first key always remains invalid. That is to say, any search/lookup
* should ignore the first key.
*
* Internal page format (keys are stored in increasing order):
* --------------------------------------------------------------------------
* | HEADER | KEY(1)+PAGE_ID(1) | KEY(2)+PAGE_ID(2) | ... | KEY(n)+PAGE_ID(n) |
* --------------------------------------------------------------------------
*/
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;
/**
* Writes the necessary header information to a newly created page, must be called after
* the creation of a new page to make a valid BPlusTreeInternalPage
* @param max_size Maximal size of the page
*/
void Init(int max_size = INTERNAL_PAGE_SIZE);
/**
* @param index The index of the key to get. Index must be non-zero.
* @return Key at index
*/
auto KeyAt(int index) const -> KeyType;
/**
*
* @param index The index of the key to set. Index must be non-zero.
* @param key The new value for key
*/
void SetKeyAt(int index, const KeyType &key);
/**
*
* @param value the value to search for
*/
auto ValueIndex(const ValueType &value) const -> int;
/**
*
* @param index the index
* @return the value at the index
*/
auto ValueAt(int index) const -> ValueType;
/**
* @brief For test only, return a string representing all keys in
* this internal page, formatted as "(key1,key2,key3,...)"
*
* @return std::string
*/
auto ToString() const -> std::string {
std::string kstr = "(";
bool first = true;
// first key of internal page is always invalid
for (int i = 1; i < GetSize(); i++) {
KeyType key = KeyAt(i);
if (first) {
first = false;
} else {
kstr.append(",");
}
kstr.append(std::to_string(key.ToString()));
}
kstr.append(")");
return kstr;
}
private:
// Flexible array member for page data.
MappingType array_[0];
};
} // namespace bustub