The implementation of B+ tree printing

BACKGROUND


The B+ tree is a kind of Balanced tree which is used to manipulate indexes saving in the relational database system. By using B+ tree, it is faster to retrieve targetted record in the heap file.
In this essay, let me introduce the recent implementation of printing B+ tree in one of my current project.

As usual, B+ has a low level and multiple fanout number, and its non-leaf has a different structure from leaf node. In database lower layer, non-leaf contains multiple keys to search for different leaves. And leaf node contains real key value and RID of record (can be used to get record from heap file).

Analyse


In normal situation, B+ tree has a printing structure as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"keys":["P"],
"children":[
{"keys":["C","G","M"],
"children": [
{"keys": ["A:[(1,1),(1,2)]","B:[(2,1),(2,2)]"]},
{"keys": ["D:[(3,1),(3,2)]","E:[(4,1)]","F:[(5,1)]"]},
{"keys": ["J:[(5,1),(5,2)]","K:[(6,1),(6,2)]","L:[(7,1)]"]},
{"keys": ["N:[(8,1)]","O:[(9,1)]"]}
]},
{"keys":["T","X"],
"children": [
{"keys": ["Q:[(10,1)]","R:[(11,1)]","S:[(12,1)]"]},
{"keys": ["U:[(13,1)]","V:[(14,1)]"]},
{"keys": ["Y:[(15,1)]","Z:[(16,1)]"]}
]}
]
}

Its tree-formatted structure is like below

We could see from what we mentioned, the non-leaf of B+ tree has “keys” and “children”, but the leaf only has “keys”.
So the printing of leaf and non-leaf is different, we could use different situation in our BFS Algorithm.
Further, The end of each printing component doesn’t have ‘,’. About this problem, we print the first one in the beginning, then print “,” + left one iteratively.

In a corner case, only one leaf node and each entry has same key value, i. e. if leaf entries contain duplicate values, there’s no duplicate key printing anymore, and the RID of it should be in the same quotation marks of former one’s. I designed a way to avoid this problem : remember the former keys when handling leaves, and forget them in the suitable time.

Implementation


The code is below, I use a DFS recursive way to print the entire tree. For the short height of B+ tree, my recursive way cannot lead to the overflow of heap. My code is in the following.

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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

void IndexManager::printBtree(IXFileHandle &ixfileHandle, const Attribute &attribute) const {
int rootPage;
ixfileHandle.fileHandle.readRoot(rootPage);
void *memory = malloc(PAGE_SIZE);
ixfileHandle.readPage(rootPage, memory);
unsigned page = (unsigned) rootPage;
string ans = serializeTree(ixfileHandle, attribute.type, page, memory);
cout << ans << endl;
memset(memory, 0, PAGE_SIZE);
free(memory);
return;
}

string IndexManager::serializeTree(IXFileHandle &ixfileHandle, const AttrType type, PageNum root, void *memory) const {
int numberOfNodes = *(short int *)((char *)memory + 1);
char leafIndicator = *(char *)memory;
int index;
string s = "";
if (numberOfNodes == 0) {
return "";
}
if (leafIndicator == '0') {
s += "{\"keys\":[";
int offset = 5;
// int branch : length = 4, point = 2
if (type == TypeInt) {
int tempKey = *(int *)((char *)memory + offset + 4);
s += "\"" + to_string(tempKey) + "\"";
for (index = 1; index < numberOfNodes; index++) {
tempKey = *(int *)((char *)memory + offset + 4 + 8 * index);
s += ",\"" + to_string(tempKey) + "\"";
}
s += "],\"children\":[";
void *tempPage = malloc(PAGE_SIZE);
PageNum tempPageNum = *(unsigned *)((char *)memory + offset);
ixfileHandle.readPage(tempPageNum, tempPage);
s += serializeTree(ixfileHandle, type, tempPageNum, tempPage);

for (index = 0; index < numberOfNodes; index++) {
tempPageNum = *(unsigned *)((char *)memory + offset + 8 * (1 + index));
ixfileHandle.readPage(tempPageNum, tempPage);
s += ",";
s += serializeTree(ixfileHandle, type, tempPageNum, tempPage);
}
s += "]}";
free(tempPage);
}
// float branch : length = 4, point = 2
else if (type == TypeReal) {
int tempKey = *(float *)((char *)memory + offset + 4);
s += "\"" + to_string(tempKey) + "\"";
for (index = 1; index < numberOfNodes; index++) {
tempKey = *(float *)((char *)memory + offset + 4 + 8 * index);
s += ",\"" + to_string(tempKey) + "\"";
}
s += "],\"children\":[";
void *tempPage = malloc(PAGE_SIZE);
PageNum tempPageNum = *(unsigned *)((char *)memory + offset);
ixfileHandle.readPage(tempPageNum, tempPage);
s += serializeTree(ixfileHandle, type, tempPageNum, tempPage);
for (index = 0; index < numberOfNodes; index++) {
tempPageNum = *(unsigned *)((char *)memory + offset + 8 * (1 + index));////
ixfileHandle.readPage(tempPageNum, tempPage);
s += ",";
s += serializeTree(ixfileHandle, type, tempPageNum, tempPage);
}
s += "]}";
free(tempPage);
}
// varchar branch
else {
//use varchar directory to calculate the length of each element
string tempKey = "";
short int tailOffset = *(short int *)((char *)memory + PAGE_SIZE - 2);
int varcharLength = tailOffset - offset - 4;
for (int i = 0; i < varcharLength; i++) {
char ch = *((char *)memory + offset + 4 + i);
tempKey += ch;
}
s += "\"" + tempKey + "\"";
for (index = 1; index < numberOfNodes; index++) {
tempKey = "";
tailOffset = *(short int *)((char *)memory + PAGE_SIZE - 2 * (index + 1));
varcharLength = tailOffset - *(short int *)((char *)memory + PAGE_SIZE - 2 * index) - 4;
for (int i = 0; i < varcharLength; i++) {
char ch = *((char *)memory + *(short int *)((char *)memory + PAGE_SIZE - 2 * index) + 4 + i);
tempKey += ch;
}
s += ",\"" + tempKey + "\"";
}
s += "],\"children\":[";
void *tempPage = malloc(PAGE_SIZE);
PageNum tempPageNum = *(unsigned *)((char *)memory + offset);
ixfileHandle.readPage(tempPageNum, tempPage);
s += serializeTree(ixfileHandle, type, tempPageNum, tempPage);

for (index = 0; index < numberOfNodes; index++) {
tailOffset = *(short int *)((char *)memory + PAGE_SIZE - 2 * (index + 1));
tempPageNum = *(unsigned *)((char *)memory + tailOffset);
ixfileHandle.readPage(tempPageNum, tempPage);
s += ",";
s += serializeTree(ixfileHandle, type, tempPageNum, tempPage);
}
s += "]}";
free(tempPage);
}
}
//for the Leaf node
else {
s += "{\"keys\":[";
int offset = 5;
index = 0;
//Int branch : Key length = 4 bytes, RID = 4 bytes
if (type == TypeInt) {
//handle the first one, no ","
int tempKey = *(int *)((char *)memory + offset);
RID tempRID;
tempRID.pageNum = *(unsigned *)((char *)memory + offset + 4);
tempRID.slotNum = *(unsigned *)((char *)memory + offset + 8);
index = 1;
s += "\"" + to_string(tempKey) + ":[(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
int before = tempKey;
for (; index < numberOfNodes;) {
tempKey = *(int *)((char *)memory + offset + 12 * index);
if (tempKey == before) {
tempRID.pageNum = *(unsigned *)((char *)memory + offset + 12 * index + 4);
tempRID.slotNum = *(unsigned *)((char *)memory + offset + 12 * index + 8);
index++;
s += ",(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
} else {
break;
}
}
//handle the rest elements which have a "," in the beginning part
while (index < numberOfNodes) {
int tempKey = *(int *)((char *)memory + offset + 12 * index);
tempRID.pageNum = *(unsigned *)((char *)memory + offset + 12 * index + 4);
tempRID.slotNum = *(unsigned *)((char *)memory + offset + 12 * index + 8);
if (tempKey != before) {
s += "]\",\"" + to_string(tempKey) + ":[(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
before = tempKey;
}
else {
s += ",(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
}
index++;
}
s += "]\"]}";
}
else if (type == TypeReal) {
//handle the first one, no ","
int tempKey = *(float *)((char *)memory + offset);
RID tempRID;
tempRID.pageNum = *(unsigned *)((char *)memory + offset + 4);
tempRID.slotNum = *(unsigned *)((char *)memory + offset + 8);
index = 1;
s += "\"" + to_string(tempKey) + ":[(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
int before = tempKey;
for (; index < numberOfNodes;) {
tempKey = *(float *)((char *)memory + offset + 12 * index);
if (tempKey == before) {
tempRID.pageNum = *(unsigned *)((char *)memory + offset + 12 * index + 4);
tempRID.slotNum = *(unsigned *)((char *)memory + offset + 12 * index + 8);
index++;
s += ",(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
} else {
break;
}
}
//handle the rest elements which have a "," in the beginning part
while (index < numberOfNodes) {
int tempKey = *(float *)((char *)memory + offset + 12 * index);
tempRID.pageNum = *(unsigned *)((char *)memory + offset + 8 * index + 4);
tempRID.slotNum = *(unsigned *)((char *)memory + offset + 8 * index + 8);
if (tempKey != before) {
s += "]\",\"" + to_string(tempKey) + ":[(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
before = tempKey;
}
else {
s += ",(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
}
index++;
}
s += "]\"]}";
}
//varchar situation
else {
//handle the first one, no ","
string tempKey = "";
short int tailOffset = *(short int *)((char *)memory + PAGE_SIZE - 6);
int varcharLength = tailOffset - offset - 8;
for (int i = 0; i < varcharLength; i++) {
char ch = *((char *)memory + offset + i);
tempKey += ch;
}
RID tempRID;
tempRID.pageNum = *(unsigned *)((char *)memory + tailOffset - 8);
tempRID.slotNum = *(unsigned *)((char *)memory + tailOffset - 4);
index = 1;
s += "\"" + tempKey + ":[(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
string before = tempKey;
for (; index < numberOfNodes;) {
tailOffset = *(short int *)((char *)memory + PAGE_SIZE - 4 - 2 * (index + 1));
varcharLength = tailOffset - *(short int *)((char *)memory + PAGE_SIZE - 4 - 2 * index) - 8;
for (int i = 0; i < varcharLength; i++) {
char ch = *((char *)memory + tailOffset - varcharLength - 8 + i);
tempKey += ch;
}
//tempKey = *(int *)((char *)memory + offset + 8 * index);
if (tempKey == before) {
tempRID.pageNum = *(unsigned *)((char *)memory + tailOffset - 8);
tempRID.slotNum = *(unsigned *)((char *)memory + tailOffset - 4);
index++;
s += ",(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
} else {
break;
}
}

//handle the rest elements which have a "," in the beginning part
while (index < numberOfNodes) {
tempKey = "";
tailOffset = *(short int *)((char *)memory + PAGE_SIZE - 4 - 2 * (index + 1));
varcharLength = tailOffset - *(short int *)((char *)memory + PAGE_SIZE - 4 - 2 * index) - 8;
for (int i = 0; i < varcharLength; i++) {
char ch = *((char *)memory + tailOffset - varcharLength - 8 + i);
tempKey += ch;
}
tempRID.pageNum = *(unsigned *)((char *)memory + tailOffset - 8);
tempRID.slotNum = *(unsigned *)((char *)memory + tailOffset - 4);
if (tempKey != before) {
s += "]\",\"" + tempKey + ":[(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
before = tempKey;
}
else {
s += ",(" + to_string(tempRID.pageNum) + "," + to_string(tempRID.slotNum) + ")";
}
index++;
}
s += "]\"]}";
}
}
return s;
}

The Thought line of the above code is drew in the following picture:

As for the third situation, it has multiple adjacent leaf node, picture like below :

1
{"keys":["00000:[(87,0),(84,0),(81,0),(78,0),(75,0),(72,0),(69,0),(66,0),(63,0),(60,0),(57,0),(54,0),(51,0),(48,0),(45,0),(42,0),(39,0),(36,0),(33,0),(30,0),(27,0),(24,0),(21,0),(18,0),(15,0),(12,0),(9,0),(6,0),(3,0),(0,0)]","00001:[(88,1),(85,1),(82,1),(79,1),(76,1),(73,1),(70,1),(67,1),(64,1),(61,1),(58,1),(55,1),(52,1),(49,1),(46,1),(43,1),(40,1),(37,1),(34,1),(31,1),(28,1),(25,1),(22,1),(19,1),(16,1),(13,1),(10,1),(7,1),(4,1),(1,1)]","00002:[(89,2),(86,2),(83,2),(80,2),(77,2),(74,2),(71,2),(68,2),(65,2),(62,2),(59,2),(56,2),(53,2),(50,2),(47,2),(44,2),(41,2),(38,2),(35,2),(32,2),(29,2),(26,2),(23,2),(20,2),(17,2),(14,2),(11,2),(8,2),(5,2),(2,2)]"]}

Reference :


Database Management System, third edition, Ramakrishnan Gehrke, 2003.

AllenInWood wechat
Allen's wechat public media 疏理