-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBFile.cc
More file actions
431 lines (378 loc) · 10.8 KB
/
Copy pathDBFile.cc
File metadata and controls
431 lines (378 loc) · 10.8 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <sys/stat.h>
#include <cstring>
#include <vector>
#include <sstream>
#include "DBFile.h"
using namespace std;
DBFile::DBFile () :
fileName(""),
isMovedFirst(false),
isTreeTraversed(false) {
}
DBFile::~DBFile () {
}
DBFile::DBFile(const DBFile& _copyMe) :
file(_copyMe.file),
fileName(_copyMe.fileName),
isMovedFirst(false),
isTreeTraversed(false) {
}
DBFile& DBFile::operator=(const DBFile& _copyMe) {
// handle self-assignment first
if (this == &_copyMe) return *this;
file = _copyMe.file;
fileName = _copyMe.fileName;
return *this;
}
int DBFile::Create (char* f_path, FileType f_type) {
fileName = f_path;
fileType = f_type;
// return 0 on success, -1 otherwise
return file.Open(0, f_path); // mode = O_TRUNC | O_RDWR | O_CREAT;
}
int DBFile::Open (char* f_path) {
fileName = f_path;
// check whether file exists or not
struct stat fileStat;
if(stat(f_path, &fileStat) != 0) {
return Create(f_path, Heap);
} else {
// return 0 on success, -1 otherwise
return file.Open(fileStat.st_size, f_path); // mode = O_RDWR;
}
}
void DBFile::Load (Schema& schema, char* textFile) {
MoveFirst();
FILE* textData = fopen(textFile, "r");
while(true) {
Record record;
if(record.ExtractNextRecord(schema, *textData)) { // success on extract
AppendRecord(record);
} else { // no data left or error
break;
}
}
WriteToFile(); // add the last page to the file
fclose(textData);
}
int DBFile::Close () {
int ret = file.Close();
if(ret == -1)
cerr << "ERROR: Failed to close DBFile." << endl << endl;
return ret;
}
void DBFile::MoveFirst () {
iPage = 0; // reset page index to 0
isMovedFirst = true;
pageNow.EmptyItOut(); // the first page has no data
}
void DBFile::AppendRecord (Record& rec) {
if(!pageNow.Append(rec)) { // no space in the current page, pageNow
WriteToFile(); // add pageNow to the file
pageNow.Append(rec); // add rec to the pageNow
}
}
void DBFile::WriteToFile() {
file.AddPage(pageNow, iPage++);
pageNow.EmptyItOut(); // clear pageNow
}
int DBFile::GetNext (Record& rec) {
if(!isMovedFirst) {
MoveFirst();
}
off_t numPage = file.GetLength();
while(true) {
if(!pageNow.GetFirst(rec)) { // no record in the current page
isNewPage = true;
// check whether this is the last page
if(iPage == numPage) { // EOF
break;
} else { // move on to the next page
file.GetPage(pageNow, iPage++);
treeNodePtr = iPage;
}
} else { // record exists
return 0;
}
}
return -1;
}
int DBFile::GetRecord(Record& putItHere, off_t whichPage, off_t whichRecord) {
return file.GetRecord(putItHere, whichPage, whichRecord);
}
string DBFile::GetTableName() {
vector<string> path;
stringstream ss(fileName); string tok;
while(getline(ss, tok, '/')) {
path.push_back(tok);
}
string tempFileName = path[path.size()-1];
stringstream ss2(tempFileName); string tableName = "";
getline(ss2, tableName, '.');
return tableName;
}
const char* DBFile::GetFileName() {
char* fileNameC = new char[fileName.length()+1];
strcpy(fileNameC, fileName.c_str());
return fileNameC;
}
int DBFile::GetCurrentPageNum() {
return iPage;
}
void DBFile::LoadBPlusTree(BPlusTree& _tree) {
// read b+ tree and write to the DBFile
InitBPlusTreeNodeSchema();
MoveFirst();
Node* node; int nodeNum = 1; // root starts from page #1
Record rec; // record to be written in DBFile
int* attrs = new int[3]; // container for attributes
while(_tree.GetNext(node)) { // BFS
int numKeys = node->keys.size();
if(!node->isLeaf) { // InternalNode
InternalNode* iNode = (InternalNode*)node;
// first, create header record
attrs[0] = 0; // is_leaf = false
attrs[1] = ++nodeNum; // left_most_ptr
attrs[2] = numKeys; // num_recs
rec.extractBPlusTreeNode(attrs);
// rec.show(schInterHeader);
AppendRecord(rec);
// and then other records for body
for(int i = 0; i < numKeys; i++) {
attrs[0] = iNode->keys[i]; // key
attrs[1] = ++nodeNum; // ptr
attrs[2] = iNode->isDuplicate[i] ? 1 : 0; // is_duplicate
rec.extractBPlusTreeNode(attrs);
// if(i == 0 || i == numKeys-1) {
// rec.show(schInter);
// }
AppendRecord(rec);
}
// in the end, finalize the page (=a node)
WriteToFile(); // add pageNow to the file
iNode = NULL;
} else { // LeafNode
LeafNode* leaf = (LeafNode*)node;
// first, create header record
attrs[0] = 1; // is_leaf = true
attrs[1] = iPage+2; // right_most_ptr
attrs[2] = numKeys; // num_recs
rec.extractBPlusTreeNode(attrs);
// rec.show(schLeafHeader);
AppendRecord(rec);
// and then other records for body
for(int i = 0; i < numKeys; i++) {
attrs[0] = leaf->keys[i]; // key
attrs[1] = leaf->idxpage[i]; // page_num
attrs[2] = leaf->idxrec[i]; // rec_num
rec.extractBPlusTreeNode(attrs);
// if(i == 0 || i == numKeys-1) {
// rec.show(schLeaf);
// }
AppendRecord(rec);
}
// in the end, finalize the page (=a node)
WriteToFile(); // add pageNow to the file
leaf = NULL;
}
}
// free the memory
delete [] attrs;
// TestIndex();
}
void DBFile::InitBPlusTreeNodeSchema() {
// create Schema for each type of record manually
vector<string> attrs, types; vector<unsigned int> distincts;
// schInterHeader
attrs.push_back("is_leaf");
attrs.push_back("left_most_ptr");
attrs.push_back("num_recs");
types.push_back("INTEGER"); types.push_back("INTEGER"); types.push_back("INTEGER");
distincts.push_back(1); distincts.push_back(1); distincts.push_back(1);
Schema sih(attrs, types, distincts);
schInterHeader = sih;
attrs.clear(); types.clear(); distincts.clear();
// schInter
attrs.push_back("key");
attrs.push_back("ptr");
attrs.push_back("is_duplicate");
types.push_back("INTEGER"); types.push_back("INTEGER"); types.push_back("INTEGER");
distincts.push_back(1); distincts.push_back(1); distincts.push_back(1);
Schema si(attrs, types, distincts);
schInter = si;
attrs.clear(); types.clear(); distincts.clear();
// schLeafHeader
attrs.push_back("is_leaf");
attrs.push_back("right_most_ptr");
attrs.push_back("num_recs");
types.push_back("INTEGER"); types.push_back("INTEGER"); types.push_back("INTEGER");
distincts.push_back(1); distincts.push_back(1); distincts.push_back(1);
Schema slh(attrs, types, distincts);
schLeafHeader = slh;
attrs.clear(); types.clear(); distincts.clear();
// schLeaf
attrs.push_back("key");
attrs.push_back("page_num");
attrs.push_back("rec_num");
types.push_back("INTEGER"); types.push_back("INTEGER"); types.push_back("INTEGER");
distincts.push_back(1); distincts.push_back(1); distincts.push_back(1);
Schema sl(attrs, types, distincts);
schLeaf = sl;
attrs.clear(); types.clear(); distincts.clear();
}
int DBFile::GetNext(Record& _fetchMe, int _lower, int _upper, DBFile& _heap) {
int bitPos, key, pageNum, recNum;
if(!isTreeTraversed) {
MoveFirst(); isNewPage = true;
int isLeaf, edgePtr, numRecs = 0;
int leftPtr, rightPtr, isDuplicate;
int i = 0;
Record rec;
while(GetNext(rec) == 0) {
char* bits = rec.GetBits();
if(isNewPage) {
bitPos = ((int *) bits)[1];
isLeaf = *(int *)&(bits[bitPos]);
bitPos = ((int *) bits)[2];
edgePtr = *(int *)&(bits[bitPos]);
bitPos = ((int *) bits)[3];
numRecs = *(int *)&(bits[bitPos]);
// if(isLeaf == 0)
// rec.show(schInterHeader);
// else
// rec.show(schLeafHeader);
isNewPage = false;
i = 0;
} else {
if(isLeaf == 0) { // iNode
// rec.show(schInter);
bitPos = ((int *) bits)[1];
key = *(int *)&(bits[bitPos]);
if(i == 0) { leftPtr = edgePtr; }
else { leftPtr = rightPtr; }
bitPos = ((int *) bits)[2];
rightPtr = *(int *)&(bits[bitPos]);
bitPos = ((int *) bits)[3];
isDuplicate = *(int *)&(bits[bitPos]);
if(_lower < key || (_lower == key && isDuplicate == 1)) {
GetPage(leftPtr); isNewPage = true;
} else if(i == numRecs-1) { // last key in this node
GetPage(rightPtr); isNewPage = true;
} else { // continue to next key
i++;
}
} else { // leaf
// rec.show(schLeaf);
bitPos = ((int *) bits)[1];
key = *(int *)&(bits[bitPos]);
bitPos = ((int *) bits)[2];
pageNum = *(int *)&(bits[bitPos]);
bitPos = ((int *) bits)[3];
recNum = *(int *)&(bits[bitPos]);
if((_lower < key && key < _upper) ||
(key == _lower && key == _upper)) {
if(_heap.GetRecord(_fetchMe, pageNum, recNum) == 0) {
treeNodePtr = iPage;
isTreeTraversed = true;
return 0;
} else {
cerr << "ERROR: Something wrong while GetRecord()" << endl << endl;
return -1;
}
} else if(key <= _lower && key < _upper) { // this is because of duplicate
continue; // until key becomes greater than _lower
} else {
return -1;
}
}
}
}
} else { // at this point, we are in a leaf node
Record rec;
while(GetNext(rec) == 0) {
if(isNewPage) { // leafHeader
// rec.show(schLeafHeader);
isNewPage = false;
continue;
} else { // others
// rec.show(schLeaf);
}
char* bits = rec.GetBits();
bitPos = ((int *) bits)[1];
key = *(int *)&(bits[bitPos]);
bitPos = ((int *) bits)[2];
pageNum = *(int *)&(bits[bitPos]);
bitPos = ((int *) bits)[3];
recNum = *(int *)&(bits[bitPos]);
if((_lower < key && key < _upper) || (key == _lower && key == _upper)) {
if(_heap.GetRecord(_fetchMe, pageNum, recNum) == 0) {
treeNodePtr = iPage;
return 0;
} else {
cerr << "ERROR: Something wrong while GetRecord()" << endl << endl;
return -1;
}
} else {
return -1;
}
}
return -1;
}
}
int DBFile::GetPage(off_t _whichPage) {
if(!isMovedFirst) {
MoveFirst();
}
off_t numPage = file.GetLength();
if(_whichPage == numPage) {
cerr << "ERROR: Index out of bound." << endl << endl;
return -1;
} else {
iPage = _whichPage;
// cout << "--- " << iPage << " ---" << endl;
return file.GetPage(pageNow, _whichPage-1);
}
}
void DBFile::TestIndex() {
MoveFirst(); isNewPage = true;
int isLeaf, edgePtr, numRecs = 0;
int leftPtr, rightPtr, isDuplicate;
int bitPos, key, pageNum, recNum;
int i = 0;
Record rec;
while(GetNext(rec) == 0) {
char* bits = rec.GetBits();
if(isNewPage) {
cout << "--- " << iPage << " ---" << endl;
bitPos = ((int *) bits)[1];
isLeaf = *(int *)&(bits[bitPos]);
bitPos = ((int *) bits)[2];
edgePtr = *(int *)&(bits[bitPos]);
bitPos = ((int *) bits)[3];
numRecs = *(int *)&(bits[bitPos]);
if(isLeaf == 0) {
rec.show(schInterHeader);
} else {
rec.show(schLeafHeader);
}
isNewPage = false;
i = 0;
} else {
if(isLeaf == 0) { // iNode
rec.show(schInter);
if(i == numRecs-1) { // last key in this node
isNewPage = true;
} else { // continue to next key
i++;
}
} else { // leaf
rec.show(schLeaf);
if(i == numRecs-1) { // last key in this node
isNewPage = true;
} else { // continue to next key
i++;
}
}
}
}
}