-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTreeNode.h
227 lines (196 loc) · 7.2 KB
/
BTreeNode.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (C) 2008 by The Regents of the University of California
* Redistribution of this file is permitted under the terms of the GNU
* Public License (GPL).
*
* @author Junghoo "John" Cho <cho AT cs.ucla.edu>
* @date 5/28/2008
*/
#ifndef BTREENODE_H
#define BTREENODE_H
#include "RecordFile.h"
#include "PageFile.h"
#define ENTRY_OFFSET sizeof(PageId)
const PageId INVALID_PID = -1;
const unsigned ROOT_DEPTH = 1;
/**
* BTLeafNode: The class representing a B+tree leaf node.
*/
class BTLeafNode {
public:
/**Constructor**/
BTLeafNode();
//Check if the node is full
bool isFull();
/**Getter**/
int getMaxKeyCount();
void showEntries();
/**
* Insert the (key, rid) pair to the node.
* Remember that all keys inside a B+tree node should be kept sorted.
* @param key[IN] the key to insert
* @param rid[IN] the RecordId to insert
* @return 0 if successful. Return an error code if the node is full.
*/
RC insert(int key, const RecordId& rid);
/**
* Insert the (key, rid) pair to the node
* and split the node half and half with sibling.
* The first key of the sibling node is returned in siblingKey.
* Remember that all keys inside a B+tree node should be kept sorted.
* @param key[IN] the key to insert.
* @param rid[IN] the RecordId to insert.
* @param sibling[IN] the sibling node to split with. This node MUST be EMPTY when this function is called.
* @param siblingKey[OUT] the first key in the sibling node after split.
* @return 0 if successful. Return an error code if there is an error.
*/
RC insertAndSplit(int key, const RecordId& rid, BTLeafNode& sibling, int& siblingKey);
/**
* If searchKey exists in the node, set eid to the index entry
* with searchKey and return 0. If not, set eid to the index entry
* immediately after the largest index key that is smaller than searchKey,
* and return the error code RC_NO_SUCH_RECORD.
* Remember that keys inside a B+tree node are always kept sorted.
* @param searchKey[IN] the key to search for.
* @param eid[OUT] the index entry number with searchKey or immediately
behind the largest key smaller than searchKey.
* @return 0 if searchKey is found. If not, RC_NO_SEARCH_RECORD.
*/
RC locate(int searchKey, int& eid);
/**
* Read the (key, rid) pair from the eid entry.
* @param eid[IN] the entry number to read the (key, rid) pair from
* @param key[OUT] the key from the slot
* @param rid[OUT] the RecordId from the slot
* @return 0 if successful. Return an error code if there is an error.
*/
RC readEntry(int eid, int& key, RecordId& rid);
/**
* Return the pid of the next slibling node.
* @return the PageId of the next sibling node
*/
PageId getNextNodePtr();
/**
* Set the next slibling node PageId.
* @param pid[IN] the PageId of the next sibling node
* @return 0 if successful. Return an error code if there is an error.
*/
RC setNextNodePtr(PageId pid);
/**
* Return the number of keys stored in the node.
* @return the number of keys in the node
*/
int getKeyCount();
/**
* Read the content of the node from the page pid in the PageFile pf.
* @param pid[IN] the PageId to read
* @param pf[IN] PageFile to read from
* @return 0 if successful. Return an error code if there is an error.
*/
RC read(PageId pid, const PageFile& pf);
/**
* Write the content of the node to the page pid in the PageFile pf.
* @param pid[IN] the PageId to write to
* @param pf[IN] PageFile to write to
* @return 0 if successful. Return an error code if there is an error.
*/
RC write(PageId pid, PageFile& pf);
private:
/**
* The main memory buffer for loading the content of the disk page
* that contains the node.
*/
char buffer[PageFile::PAGE_SIZE];
typedef struct {
RecordId rid;
int key;
} LeafEntry;
int keyCount;
int maxKeyCount;
};
/**
* BTNonLeafNode: The class representing a B+tree nonleaf node.
*/
class BTNonLeafNode {
public:
/**Constructor**/
BTNonLeafNode();
/***Getter***/
int getMaxKeyCount();
bool isFull();
/**Test purpose **/
void showEntries();
void showEntriesWithFirstPageId();
PageId getFirstPid();
PageId getNextPid(int eid);
/**
* Insert a (key, pid) pair to the node.
* Remember that all keys inside a B+tree node should be kept sorted.
* @param key[IN] the key to insert
* @param pid[IN] the PageId to insert
* @return 0 if successful. Return an error code if the node is full.
*/
RC insert(int key, PageId pid);
/**
* Insert the (key, pid) pair to the node
* and split the node half and half with sibling.
* The sibling node MUST be empty when this function is called.
* The middle key after the split is returned in midKey.
* Remember that all keys inside a B+tree node should be kept sorted.
* @param key[IN] the key to insert
* @param pid[IN] the PageId to insert
* @param sibling[IN] the sibling node to split with. This node MUST be empty when this function is called.
* @param midKey[OUT] the key in the middle after the split. This key should be inserted to the parent node.
* @return 0 if successful. Return an error code if there is an error.
*/
RC insertAndSplit(int key, PageId pid, BTNonLeafNode& sibling, int& midKey);
/**
* Given the searchKey, find the child-node pointer to follow and
* output it in pid.
* Remember that the keys inside a B+tree node are sorted.
* @param searchKey[IN] the searchKey that is being looked up.
* @param pid[OUT] the pointer to the child node to follow.
* @return 0 if successful. Return an error code if there is an error.
*/
RC locateChildPtr(int searchKey, PageId& pid);
/**
* Initialize the root node with (pid1, key, pid2).
* @param pid1[IN] the first PageId to insert
* @param key[IN] the key that should be inserted between the two PageIds
* @param pid2[IN] the PageId to insert behind the key
* @return 0 if successful. Return an error code if there is an error.
*/
RC initializeRoot(PageId pid1, int key, PageId pid2);
/**
* Return the number of keys stored in the node.
* @return the number of keys in the node
*/
int getKeyCount();
/**
* Read the content of the node from the page pid in the PageFile pf.
* @param pid[IN] the PageId to read
* @param pf[IN] PageFile to read from
* @return 0 if successful. Return an error code if there is an error.
*/
RC read(PageId pid, const PageFile& pf);
/**
* Write the content of the node to the page pid in the PageFile pf.
* @param pid[IN] the PageId to write to
* @param pf[IN] PageFile to write to
* @return 0 if successful. Return an error code if there is an error.
*/
RC write(PageId pid, PageFile& pf);
private:
/**
* The main memory buffer for loading the content of the disk page
* that contains the node.
*/
char buffer[PageFile::PAGE_SIZE];
typedef struct{
PageId pid;
int key;
} NonLeafEntry;
int keyCount;
int maxKeyCount;
};
#endif /* BTREENODE_H */