-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGenericDBFile.cc
137 lines (95 loc) · 3.37 KB
/
GenericDBFile.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
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
#include "GenericDBFile.h"
GenericDBFile::GenericDBFile() {
isDBFileOpen = false;
isInWriteMode = false;
currentlyBeingReadPageNumber = 0;
}
GenericDBFile::~GenericDBFile() {
}
/* **************************************** ALL PUBLIC METHODS *************************************************** */
int GenericDBFile::Create(const char *fpath) {
strcpy(dbFileName, fpath);
return isDBFileOpen = dbFile.Open(0, const_cast<char *>(fpath));
}
int GenericDBFile::Open(const char *fpath) {
strcpy(dbFileName, fpath);
isDBFileOpen = dbFile.Open(1, const_cast<char *>(fpath));
// Return 0 if it is not able to open the file.
if (!isDBFileOpen) return isDBFileOpen;
// Move read pointer to the first page.
MoveFirst();
return isDBFileOpen;
}
void GenericDBFile::Add(Record &addme) {
// Throw error if file is not open.
DoFileOpenCheck();
SwitchToWriteMode();
AddToDBFile(addme);
}
void GenericDBFile::Load(Schema &myschema, const char *loadpath) {
// Throw error if file is not open.
DoFileOpenCheck();
SwitchToWriteMode();
// Load record from table file and add record to the file recursively,
// while all the records are loaded from table file.
FILE *tableFile = fopen(loadpath, "r");
Record temp;
while (temp.SuckNextRecord(&myschema, tableFile)) {
AddToDBFile(temp);
}
}
void GenericDBFile::MoveFirst() {
// Throw error if file is not open.
DoFileOpenCheck();
SwitchToReadMode();
// Load read page buffer with the first i.e 0th page.
currentlyBeingReadPageNumber = 0;
GetPageFromDataFile(readBufferPage, currentlyBeingReadPageNumber);
}
int GenericDBFile::GetNext(Record &fetchme) {
// Return 0 if file is not open.
if (!isDBFileOpen) return 0;
SwitchToReadMode();
return GetNextFromDBFile(fetchme);
}
int GenericDBFile::GetNext(Record &fetchme, CNF &cnf, Record &literal) {
// Return 0 if file is not open.
if (!isDBFileOpen) return 0;
SwitchToReadMode();
return GetNextFromDBFile(fetchme, cnf, literal);
}
int GenericDBFile::Close() {
// Return 0 if file is not open.
if (!isDBFileOpen) return 0;
SwitchToReadMode();
// Close the file.
dbFile.Close();
return !(isDBFileOpen = false);
}
/* **************************************** ALL PROTECTED METHODS ************************************************** */
off_t GenericDBFile::GetLengthInPages() {
return dbFile.GetLength() - 1;
}
bool GenericDBFile::GetPageFromDataFile(Page &page, off_t pageNumber) {
page.EmptyItOut();
// if file contains that many pages, load the page from the file.
if (pageNumber < 0 || GetLengthInPages() - 1 < pageNumber) return false;
dbFile.GetPage(&page, pageNumber);
return true;
}
void GenericDBFile::AddPageToDataFile(Page &page, off_t pageNumber) {
dbFile.AddPage(&page, pageNumber);
page.EmptyItOut();
}
int GenericDBFile::GetRecordFromReadBufferPage(Record &rec) {
if (readBufferPage.GetFirst(&rec)) return 1;
if (!GetPageFromDataFile(readBufferPage, ++currentlyBeingReadPageNumber)) return 0;
return GetRecordFromReadBufferPage(rec);
}
/* **************************************** ALL PRIVATE METHODS ************************************************** */
void GenericDBFile::DoFileOpenCheck() {
if (!isDBFileOpen) {
std::cerr << "BAD: File is not open!\n";
exit(1);
}
}