-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap.cc
101 lines (86 loc) · 2.35 KB
/
Heap.cc
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
#include "TwoWayList.h"
#include "Record.h"
#include "Schema.h"
#include "File.h"
#include "Comparison.h"
#include "ComparisonEngine.h"
#include "DBFile.h"
#include "Defs.h"
#include "Heap.h"
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
using namespace std;
Heap::Heap(){
currentPage = new(std::nothrow) Page();
}
int Heap::Create(const char *f_path, void *startup) {
file = new File();
file->Open(0, f_path);
const char *m_path = *f_path + ".data";
FILE *metadata = fopen(m_path, "w");
fprintf(metadata, "%s", "heap");
fclose(metadata);
currPageNum = 0;
return 1;
}
void Heap::Load(Schema &f_schema, const char *loadpath) {
FILE *tableFile = fopen(loadpath, "r");
if (!tableFile) {
cout << "ERROR : Cannot open file. EXIT !!!\n";
exit(1);
}
Record temp;
while (temp.SuckNextRecord(&f_schema, tableFile) == 1) {
// check for page overflow
if (!currentPage->Append(&temp)) {
WriteCurrentPageToDisk();
// empty the page out
delete currentPage;
currentPage = new(std::nothrow) Page();
// append record to empty page
currentPage->Append(&temp);
}
}
WriteCurrentPageToDisk();
// MoveFirst();
}
int Heap::Open(const char *f_path) {
file = new File();
file->Open(1, f_path);
return 1;
}
void Heap::Add(Record *rec) {
// Get last page in the file
if (file->GetLength() != 0) {
if (!currPageNum + 1 == file->GetLength()) {
file->GetPage(currentPage, file->GetLength() - 2);
currPageNum = file->GetLength() - 1;
}
}
if (!currentPage->Append(rec)) {
WriteCurrentPageToDisk();
currPageNum++;
// empty the page out
delete currentPage;
currentPage = new(std::nothrow) Page();
// append record to empty page
currentPage->Append(rec);
}
// set to false as we're always appending a record to a page
currentPage->pageToDisk = false;
}
int Heap::GetNext(Record &fetchme, CNF &cnf, Record &literal) {
Record temp;
ComparisonEngine comp;
while (1) {
if (GenericDBFile::GetNext(temp)) {
if (comp.Compare(&temp, &literal, &cnf)) {
fetchme.Copy(&temp);
return 1;
}
} else {
return 0;
}
}
}