-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
418 lines (342 loc) · 11.3 KB
/
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <unordered_set>
#include "QueryPlan.h"
#define tables_dir "data-myTable/"
using namespace std;
extern "C" struct YY_BUFFER_STATE *yy_scan_string(const char *);
extern "C" void yy_delete_buffer(struct YY_BUFFER_STATE *);
extern "C" {
int yyparse(void); // defined in y.tab.c
}
// these data structures hold the result of the parsing
extern struct FuncOperator *finalFunction; // the aggregate function (NULL if no agg)
extern struct TableList *tables; // the list of tables and aliases in the query
extern struct AndList *boolean; // the predicate in the WHERE clause
extern struct NameList *groupingAtts; // grouping atts (NULL if no grouping)
extern struct NameList *attsToSelect; // the set of attributes in the SELECT (NULL if no such atts)
extern int distinctAtts; // 1 if there is a DISTINCT in a non-aggregate query
extern int distinctFunc; // 1 if there is a DISTINCT in an aggregate query
extern struct SchemaList *schemas; // the list of tables and aliases in the query
extern struct CreateTableType *createTableType; // type of table to create along with sorting attributes (if any)
extern char *bulkFileName; // bulk loading file name string
extern char *outputFileName; // output file name or STDOUT string
extern int commandFlag; // 1 if the command is a create table command.
extern int NumAtt;
// set to store all the relations been added so far
unordered_set<string> rels;
// name of file to store all the relations names
char *rel_file = "relations.txt";
// name of the catalog file
char *cat_file = "catalog";
// std out
std::streambuf *coutbuf;
bool flag_exec = true;
bool op_stdout = true;
std::streambuf *stream_buffer_cout = NULL;
fstream file;
void printQueryOutput(Query *q) {
cout << "\n Printing output of the query";
Record tempRec;
int count = 0;
while (q->root->outputPipe->Remove(&tempRec)) {
tempRec.Print(q->root->outschema);
count++;
if (count % 1000 == 0) cout << "\n"
<< count++;
}
cout << "\n " << count;
}
int main1() {
// std::ofstream out("query_plan_op/q13.txt");
// std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
// std::cout.rdbuf(out.rdbuf());
// char *query = "CREATE TABLE mytable (att1 INTEGER, att2 DOUBLE, att3 STRING) AS HEAP";
// yy_scan_string(query);
cout << "\n Enter Query to Run: \n";
yyparse();
char *statFileName = "Statistics.txt";
Statistics *stats = new Statistics;
stats->Read(statFileName);
Query q(stats);
q.QueryPlan();
q.execute();
printQueryOutput(&q);
// std::cout.rdbuf(coutbuf);
return 0;
}
// CREATE
Schema *createTable(DBFile *dbfile) {
string tableName = string(tables->tableName);
// check if the rel exists
int count = rels.count(tableName);
cout << "\n Outside: count " << count;
if (rels.count(tableName) != 0) {
cout << "\n Inside: count " << count;
return NULL;
}
cout << "\n TableName: " << tableName;
// Attribute* myAtts = new Attribute[NumAtt];
Attribute myAtts[NumAtt];
int i = 0;
while (schemas) {
myAtts[i].name = strdup(schemas->attName);
if (strcmp(schemas->type, "INTEGER") == 0)
myAtts[i].myType = Int;
else if (strcmp(schemas->type, "DOUBLE") == 0)
myAtts[i].myType = Double;
else if (strcmp(schemas->type, "STRING") == 0)
myAtts[i].myType = String;
schemas = schemas->next;
i++;
}
Schema *schema = new Schema((char *)tableName.c_str(), i, myAtts);
NumAtt = 0;
string dir = string(tables_dir);
string table_path = dir + tableName + ".bin";
if (strcmp(createTableType->heapOrSorted, "HEAP") == 0) {
dbfile->Create(table_path.c_str(), heap, NULL);
cout << "\n Created a DBFile";
}
rels.insert(tableName);
dbfile->Close();
return schema;
}
void loadRelations() {
ifstream infile;
infile.open(rel_file);
string buffer;
while (getline(infile, buffer)) {
rels.insert(buffer);
}
cout << "\n Inserted everything into the rels set";
infile.close();
}
void writeSchemaToDisk(Schema *schema) {
Attribute *atts = schema->GetAtts();
string rel_name = schema->getfileName();
// just write the relation names
cout << "\n Relation Name in file : " << rel_name;
ofstream infile;
infile.open(rel_file, fstream::app);
infile << rel_name << "\n";
infile.close();
ofstream catalog;
catalog.open(cat_file, fstream::app);
// write the initial crap BEGIN
catalog << "\nBEGIN";
catalog << "\n"
<< rel_name;
catalog << "\n"
<< rel_name << ".tbl";
for (int i = 0; i < schema->GetNumAtts(); i++) {
string att_type;
switch (atts[i].myType) {
case Int:
att_type = "Int";
break;
case Double:
att_type = "Double";
break;
case String:
att_type = "String";
break;
}
catalog << "\n"
<< string(atts[i].name) << " " << att_type;
}
catalog << "\nEND\n";
catalog.close();
}
void insert(DBFile *dbfile) {
// check if the relname exists
string table_name = string(tables->tableName);
if (rels.count(table_name) == 0) {
// rel_name does not exist
cout << "\n ERROR! Relation does not exist";
return;
}
Schema schema(cat_file, table_name.c_str());
string temp = tables_dir + table_name + ".bin";
dbfile->Open(temp.c_str());
temp.clear();
// temp = tables_dir + string(bulkFileName);
// load the dbfile
dbfile->Load(schema, bulkFileName);
// dbfile->Load(schema, temp.c_str());
dbfile->Close();
cout << "\n Hey I loaded the file!";
}
void printQueryOutputToFile(Query *q) {
cout << "\n Printing output of the query";
FILE *file = fopen(outputFileName, "w");
WriteOut wo;
wo.Run(*(q->root->outputPipe), file, *(q->root->outschema));
wo.WaitUntilDone();
fclose(file);
// cout << "\n " << count;
}
void dropTable() {
string relName = string(tables->tableName); // outputFileName->name
if (rels.count(relName) == 0) {
cout << "The table doesn't exist. Cannot drop." << endl;
return;
}
string file_path = tables_dir + relName + ".bin";
string meta_path = file_path + ".data";
if (unlink(file_path.c_str()) != 0)
perror("ERROR: Cannot delete .bin");
if (unlink(meta_path.c_str()) != 0)
perror("ERROR: Cannot delete .bin.meta");
rels.erase(relName);
cout << "\n Table Dropped";
ifstream infile;
infile.open(rel_file);
string buffer;
ofstream tempFile("temp");
while (getline(infile, buffer)) { // while the file has more lines.
cout << "\n Buffer: " << buffer;
if (buffer.compare(relName) != 0)
tempFile << buffer << endl;
}
infile.close();
tempFile.close();
unlink(rel_file);
rename("temp", rel_file);
}
void setOutput() {
// std::cout.flush();
// std::ofstream out(outputFileName);
// coutbuf = std::cout.rdbuf(); //save old buf
// std::cout.rdbuf(out.rdbuf());
file.open(outputFileName, ios::out);
stream_buffer_cout = std::cout.rdbuf();
streambuf *stream_buffer_file = file.rdbuf();
std::cout.rdbuf(stream_buffer_file);
}
void resetOutput() {
// std::cout.rdbuf(coutbuf);
if (stream_buffer_cout != NULL) {
std::cout.flush();
std::cout.rdbuf(stream_buffer_cout);
file.close();
stream_buffer_cout = NULL;
}
}
void chooseOutput() {
// string temp = "out.txt";
// outputFileName = (char *) temp.c_str();
if (strcmp(outputFileName, "STDOUT") == 0) {
// reset
// resetOutput();
flag_exec = true;
op_stdout = true;
} else if (strcmp(outputFileName, "NONE") == 0) {
flag_exec = false;
} else {
cout << "\n To reset to STDOUT type \'SET OUTPUT STDOUT\' \n";
flag_exec = true;
op_stdout = false;
// setOutput();
}
}
bool checkrels() {
TableList *head = tables;
while(head != NULL) {
if(rels.count(string(head->tableName)) == 0) return false;
head = head->next;
}
return true;
}
int main() {
// set it to stdout
// std::ofstream temp("qkwuhegfvhaegr.txt");
// coutbuf = std::cout.rdbuf(); //save old buf
// std::cout.rdbuf(temp.rdbuf());
// std::cout.rdbuf(coutbuf);
// run infinite loop to check for things
DBFile *dbfile = NULL;
Schema *schema = NULL;
char *statFileName = "Statistics.txt";
Statistics *stats = NULL;
Query *q = NULL;
cout << "\n Query Constructor.......";
loadRelations();
cout << "\n Stage 1";
while (1) {
string cmd;
cout << "\n Enter Query: ";
std::getline(cin, cmd);
YY_BUFFER_STATE *buf = yy_scan_string(cmd.c_str());
yyparse();
cout << "\n Command Flag: " << commandFlag;
switch (commandFlag) {
case 1:
dbfile = new DBFile;
cout << "\n Create the TABLE.";
schema = createTable(dbfile);
if (schema == NULL) {
cout << "\n Relation already exists!";
break;
} else {
// new relation thats why write to the disk
writeSchemaToDisk(schema);
cout << "\n Printing output schema......";
printOutputSchema(schema);
}
delete dbfile;
dbfile = NULL;
break;
case 2:
dbfile = new DBFile;
insert(dbfile);
delete dbfile;
dbfile = NULL;
break;
case 3:
dropTable();
break;
case 4:
chooseOutput();
break;
case 5:
if(checkrels() == false) {
cout << "\n Table doesn't exist";
break;
}
stats = new Statistics;
stats->Read(statFileName);
q = new Query(stats);
q->QueryPlan();
if (flag_exec == true) {
q->execute();
cout << "\n I'm executing your SQL Query";
if (op_stdout == true)
printQueryOutput(q);
else
printQueryOutputToFile(q);
}
q->cleanup();
delete q;
delete stats;
break;
case 6:
cout << "\n BYE, exiting";
if (dbfile != NULL)
dbfile->Close();
// resetOutput();
cout << endl;
exit(0);
break;
default:
cout << "\n In default!";
cout << "\n Command Flag: " << commandFlag;
}
yy_delete_buffer(buf);
commandFlag = -1;
cout << endl;
}
}