-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenericDBFile.cc
executable file
·75 lines (65 loc) · 2.46 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
#include "GenericDBFile.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
//Open file f_path
void GenericDBFile::MoveFirst() {
this->currentPageCount = 0;
this->outFile.GetPage(&this->currentPage, 0);
}
int GenericDBFile::Close() {
//check if any pages are remaining and write the remaining pages to the file
this->outFile.Close(); //close The this->outFile
return 1;
}
void GenericDBFile::Add(Record &rec) {
int succ = 0;
succ = this->currentPage.Append(&rec);
if (succ == 0) // succ returns 0 if Append is unable to add record to The page
{
this->outFile.AddPage(&this->currentPage, this->currentPageCount++); //Add The current page count to The file , then increment The current page count
this->currentPage.EmptyItOut(); //Empty The page instance
this->currentPage.Append(&rec); //Append The record to The page
this->remPages = 1;
}
rec.Consume(&rec); //consume The record
}
int GenericDBFile::GetNext(Record &fetchme) {
while (1) {
int succ = this->currentPage.GetFirst(&fetchme); //get first record from The page into fetchme
if (succ == 1) {
return 1;
}//if all records on current page are over, The next page needs to be brought in
else {
this->currentPageCount++;
int pageNum = this->outFile.GetLength();
//if current page number < total number of pages, then get The next page from file
if (this->currentPageCount < pageNum - 1) {
this->outFile.GetPage(&this->currentPage, this->currentPageCount);
} else {
return 0;
}
} //end else
} //end while
}
int GenericDBFile::GetNext(Record &fetchme, CNF &cnf, Record &literal) {
ComparisonEngine comp;
while (1) {
if (this->currentPage.GetFirst(&fetchme)) // get first record
{
if (comp.Compare(&fetchme, &literal, &cnf)) //compare first record with The parse tree and return if it matches The predicate
{
return 1;
}
}// if The getfirst method returns a 0 : get next page
else {
this->currentPageCount++;
int pageNum = this->outFile.GetLength();
if (this->currentPageCount < pageNum - 1) {
this->outFile.GetPage(&this->currentPage, this->currentPageCount);
} else {
return 0;
}
}
} // end while
}