-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericDBFile.cc
More file actions
72 lines (55 loc) · 1.53 KB
/
Copy pathGenericDBFile.cc
File metadata and controls
72 lines (55 loc) · 1.53 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
#include <iostream>
#include "GenericDBFile.h"
GenericDBFile::GenericDBFile() {
file = new File();
page = new Page();
}
GenericDBFile::~GenericDBFile() {
delete page;
delete file;
}
int GenericDBFile::Create(const char *fpath, fType file_type, void *startup) {
this->WriteMetadata(fpath, file_type, startup);
this->file->Open(0, (char *) fpath);
return 1;
}
int GenericDBFile::Open(const char *f_path) {
file->Open(1, (char *) f_path);
ReadMetadata(f_path);
return 1;
}
void GenericDBFile::Load(Schema &f_schema, const char *loadpath) {
FILE *tableFile = fopen(loadpath, "r");
Record temp;
int counter = 0;
while (temp.SuckNextRecord(&f_schema, tableFile) == 1) {
counter++;
Add(temp);
// if (counter % 10000 == 0) std::cerr << counter << "\n";
}
// Flush the page with rest of the records
FlushPage();
MoveFirst();
}
void GenericDBFile::MoveFirst() {
this->FlushPageIfNeeded();
this->readCursor = 0;
page->EmptyItOut();
}
int GenericDBFile::Close() {
this->FlushPageIfNeeded();
page->EmptyItOut();
return file->Close();
}
int GenericDBFile::GetNext(Record &fetchme) {
this->FlushPageIfNeeded();
if (page->GetFirst(&fetchme) == 1) return 1;
if (readCursor < file->GetLength() - 1) file->GetPage(page, readCursor++);
return page->GetFirst(&fetchme);
}
void GenericDBFile::FlushPageIfNeeded() {
if (!needFlush) return;
cout << "Flushing while switching from writes to read\n";
FlushPage();
MoveFirst();
}