-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathb_plus_tree_leaf_page.h
73 lines (66 loc) · 2.87 KB
/
b_plus_tree_leaf_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
/**
* b_plus_tree_leaf_page.h
*
* Store indexed key and record id(record id = page id combined with slot id,
* see include/common/rid.h for detailed implementation) together within leaf
* page. Only support unique key.
* Leaf page format (keys are stored in order):
* ----------------------------------------------------------------------
* | HEADER | KEY(1) + RID(1) | KEY(2) + RID(2) | ... | KEY(n) + RID(n)
* ----------------------------------------------------------------------
*
* Header format (size in byte, 24 bytes in total):
* ---------------------------------------------------------------------
* | PageType (4) | CurrentSize (4) | MaxSize (4) | ParentPageId (4) |
* ---------------------------------------------------------------------
* ------------------------------
* | PageId (4) | NextPageId (4)
* ------------------------------
*/
#pragma once
#include <utility>
#include <vector>
#include "page/b_plus_tree_page.h"
namespace cmudb {
#define B_PLUS_TREE_LEAF_PAGE_TYPE \
BPlusTreeLeafPage<KeyType, ValueType, KeyComparator>
INDEX_TEMPLATE_ARGUMENTS
class BPlusTreeLeafPage : public BPlusTreePage {
public:
// After creating a new leaf page from buffer pool, must call initialize
// method to set default values
void Init(page_id_t page_id, page_id_t parent_id = INVALID_PAGE_ID);
// helper methods
page_id_t GetNextPageId() const;
void SetNextPageId(page_id_t next_page_id);
KeyType KeyAt(int index) const;
int KeyIndex(const KeyType &key, const KeyComparator &comparator) const;
const MappingType &GetItem(int index);
// insert and delete methods
int Insert(const KeyType &key, const ValueType &value,
const KeyComparator &comparator);
bool Lookup(const KeyType &key, ValueType &value,
const KeyComparator &comparator) const;
int RemoveAndDeleteRecord(const KeyType &key,
const KeyComparator &comparator);
// Split and Merge utility methods
void MoveHalfTo(BPlusTreeLeafPage *recipient,
BufferPoolManager *buffer_pool_manager /* Unused */);
void MoveAllTo(BPlusTreeLeafPage *recipient, int /* Unused */,
BufferPoolManager * /* Unused */);
void MoveFirstToEndOf(BPlusTreeLeafPage *recipient,
BufferPoolManager *buffer_pool_manager);
void MoveLastToFrontOf(BPlusTreeLeafPage *recipient, int parentIndex,
BufferPoolManager *buffer_pool_manager);
// Debug
std::string ToString(bool verbose = false) const;
private:
void CopyHalfFrom(MappingType *items, int size);
void CopyAllFrom(MappingType *items, int size);
void CopyLastFrom(const MappingType &item);
void CopyFirstFrom(const MappingType &item, int parentIndex,
BufferPoolManager *buffer_pool_manager);
page_id_t next_page_id_;
MappingType array[0];
};
} // namespace cmudb