-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBFileTest.cc
180 lines (154 loc) · 4.59 KB
/
DBFileTest.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <unistd.h>
#include <iostream>
#include "Comparison.h"
#include "ComparisonEngine.h"
#include "DBFile.h"
#include "Schema.h"
#include "gtest/gtest.h"
extern "C" {
int yyparse(void); // defined in y.tab.c
}
extern struct AndList *final;
class DBFileTest : public ::testing::Test {
protected:
FILE *tableFile;
DBFile *dbfile;
Record temp;
Schema *myschema = new Schema("catalog", "lineitem");
const char *tbl_path = "data/lineitem.tbl";
struct SortInfo *sortinfo;
void ParseCNF();
};
void DBFileTest::ParseCNF() {
Record literal;
cout << "\n enter sort order CNF (l_partkey) (when done press enter and ctrl-D):\n\t";
if (yyparse() != 0) {
cout << " Error: can't parse your CNF.\n";
exit(1);
}
CNF sort_pred;
sort_pred.GrowFromParseTree(final, myschema, literal); // constructs CNF predicate
OrderMaker dummy, sortorder;
sort_pred.GetSortOrders(sortorder, dummy);
sortinfo = new SortInfo;
sortinfo->myOrder = &sortorder;
cout << "Enter the runlength: " << endl;
cin >> sortinfo->runLength;
}
// load test for sorted dbfile
TEST_F(DBFileTest, DBFileLoad) {
dbfile = new DBFile;
cout << "DBFile name is: lineitem.bin " << endl;
ParseCNF();
dbfile->Create("lineitem.bin", sorted, (void *)sortinfo);
cout << "Created the file successfully!" << endl;
cout << " lineitem.tbl will be loaded " << endl;
dbfile->Load(*myschema, tbl_path);
EXPECT_EQ(1, dbfile->Close());
}
// open and sequencially scan the sorted file
TEST_F(DBFileTest, DBFileSeqScan) {
dbfile = new DBFile;
cout << "DBFile name is: lineitem.bin " << endl;
dbfile->Open("lineitem.bin");
dbfile->MoveFirst();
int counter = 0;
while (dbfile->GetNext(temp) == 1) {
counter += 1;
if (counter % 10000 == 0) {
cout << counter << "\n";
}
}
cout << " scanned " << counter << " recs \n";
dbfile->Close();
EXPECT_EQ(6005, counter);
}
// scan and filter sorted file
// pass the query CNF
TEST_F(DBFileTest, DBFileScanFilter) {
cout << "Enter your CNF: (enter : (l_partkey = 200) AND (l_orderkey > 5000) )" << endl;
if (yyparse() != 0) {
exit(1);
}
CNF cnf;
Record literal;
dbfile = new DBFile;
cnf.GrowFromParseTree(final, myschema, literal);
dbfile->Open("lineitem.bin");
dbfile->MoveFirst();
int counter = 0;
while (dbfile->GetNext(temp, cnf, literal) == 1) {
counter += 1;
if (counter % 10000 == 0) {
cout << counter << "\n";
}
}
cout << " scanned " << counter << " recs \n";
dbfile->Close();
EXPECT_EQ(5, counter);
delete dbfile;
}
// // create a new file, writes to it and then scans the records
// // repeats the same again to ensure complete testing of interleaving operations
TEST_F(DBFileTest, DBFileInterleave) {
dbfile = new DBFile;
tableFile = fopen(tbl_path, "r");
ParseCNF();
dbfile->Create("lineitemwrite2.bin", sorted, (void *)sortinfo);
int counter = 0;
Record tempRec;
fseek(tableFile, 0, SEEK_SET);
while (tempRec.SuckNextRecord(myschema, tableFile) == 1) {
dbfile->Add(&tempRec);
counter++;
}
cout << "Written " << counter << " records" << endl;
EXPECT_EQ(6005, counter);
dbfile->Close();
dbfile->Open("lineitemwrite2.bin");
dbfile->MoveFirst();
counter = 0;
while (dbfile->GetNext(temp) == 1) {
counter++;
}
cout << " scanned " << counter << " recs \n";
EXPECT_EQ(6005, counter);
fseek(tableFile, 0, SEEK_SET);
// add records again
counter = 0;
while (tempRec.SuckNextRecord(myschema, tableFile) == 1) {
dbfile->Add(&tempRec);
counter++;
}
cout << "Added " << counter << " records again" << endl;
EXPECT_EQ(6005, counter);
//scanning again
dbfile->MoveFirst();
counter = 0;
while (dbfile->GetNext(tempRec)) {
counter++;
}
cout << "Scanned " << counter << " records" << endl;
EXPECT_EQ(12010, counter);
dbfile->Close();
delete dbfile;
unlink("lineitemwrite2.bin");
}
// adding records to sorted file
TEST_F(DBFileTest, DBFileWrite) {
dbfile = new DBFile;
tableFile = fopen(tbl_path, "r");
ParseCNF();
dbfile->Create("lineitemwrite.bin", sorted, (void *)sortinfo);
int count = 0;
Record tempRec;
while (tempRec.SuckNextRecord(myschema, tableFile) == 1) {
dbfile->Add(&tempRec);
count++;
}
dbfile->Close();
cout << "Written " << count << " records" << endl;
delete dbfile;
unlink("lineitemwrite.bin");
EXPECT_EQ(6005, count);
}