-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapDBFile.cc
60 lines (48 loc) · 1.45 KB
/
HeapDBFile.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
#include "HeapDBFile.h"
#include <iostream>
#include <fstream>
HeapDBFile::HeapDBFile() = default;
HeapDBFile::~HeapDBFile() = default;
int HeapDBFile::Create(const char *fpath, fType file_type, void *startup) {
this->writePage = 0;
return GenericDBFile::Create(fpath, file_type, startup);
}
void HeapDBFile::Add(Record &addme) {
if (page->Append(&addme) == 0) {
FlushPage();
page->Append(&addme);
}
needFlush = true;
}
int HeapDBFile::GetNext(Record &fetchme, CNF &cnf, Record &literal) {
this->FlushPageIfNeeded();
int recordAvailable = 0;
while ((recordAvailable = page->GetFirst(&fetchme)) == 1 || readCursor < writePage) {
if (recordAvailable == 1) {
if (comp.Compare(&fetchme, &literal, &cnf)) return 1;
} else {
file->GetPage(page, readCursor++);
}
}
return 0;
}
void HeapDBFile::WriteMetadata(const char *fpath, fType file_type, void *startup) {
ofstream metadata(fpath + std::string(".metadata"));
if (metadata.is_open()) {
metadata << file_type << '\n';
metadata << '\n';
metadata.close();
} else {
cout << "Unable to open file for write";
exit(1);
}
}
void HeapDBFile::ReadMetadata(const char *fpath) {
writePage = file->GetLength() - 1;
if (writePage < 0) writePage = 0;
}
void HeapDBFile::FlushPage() {
file->AddPage(page, writePage++);
page->EmptyItOut();
needFlush = false;
}