-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabase.cc
More file actions
183 lines (142 loc) · 4.63 KB
/
Database.cc
File metadata and controls
183 lines (142 loc) · 4.63 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
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
#include "Database.h"
Database::Database() {
}
Database::~Database() {}
void Database::TurnOn() {
}
void Database::ExecuteQuery() {
// Parse the query.
yyparse();
switch (queryType) {
case CREATE:
ExecuteCreateQuery();
cerr << "\nTable created successfully.\n";
break;
case INSERT:
ExecuteInsertQuery();
cerr << "\nTable loaded successfully.\n";
break;
case DROP:
ExecuteDropQuery();
cerr << "\nTable dropped successfully.\n";
break;
case SELECT:
ExecuteSelectQuery();
cerr << "\nQuery ran successfully.\n";
break;
case SET:
this->queryOutputType = outputType;
this->outputFileName = fileName;
cerr << "\nOutput mode changed successfully.\n";
break;
}
}
void Database::ShutDown() {
}
OutputType Database::GetOutputType() {
return this->queryOutputType;
}
void Database::ExecuteCreateQuery() {
PathConfig *pathConfig = PathConfig::GetInstance();
// Create new Schema.
Schema *newSchema = CreateNewSchema(tables->tableName, createAtts);
// Save schema in the file.
newSchema->Write(pathConfig->GetSchemaPath(tables->tableName), tables->tableName);
// Get file path from path config
char *filePath = pathConfig->GetDBFilePath(tables->tableName);
DBFile dbFile;
switch (fileType) {
case HEAP:
dbFile.Create(filePath, heap, NULL);
break;
case SORTED: {
SortInfo *sortInfo = new SortInfo();
sortInfo->runLength = DEFAULT_RUN_LENGTH;
sortInfo->myOrder = new OrderMaker(newSchema, sortAtts);
dbFile.Create(filePath, sorted, sortInfo);
break;
}
}
dbFile.Close();
}
void Database::ExecuteInsertQuery() {
DBFile dbFile;
// Get file path from path config
PathConfig *pathConfig = PathConfig::GetInstance();
char *filePath = pathConfig->GetDBFilePath(tables->tableName);
dbFile.Open(filePath);
Schema schema(pathConfig->GetSchemaPath(tables->tableName), tables->tableName);
dbFile.Load(schema, fileName);
dbFile.Close();
}
void Database::ExecuteDropQuery() {
PathConfig *pathConfig = PathConfig::GetInstance();
char *filePath = pathConfig->GetDBFilePath(tables->tableName);
// remove the binary file.
remove(filePath);
// remove the metadata file.
remove(pathConfig->GetMetadataFilePath(filePath));
// remove the schema.
remove(pathConfig->GetSchemaPath(tables->tableName));
}
void Database::ExecuteSelectQuery() {
TableList *tableList = tables;
// Get file path from path config
PathConfig *pathConfig = PathConfig::GetInstance();
// Open All db files.
unordered_map<char *, DBFile *> dbFileMap;
while (tableList) {
DBFile *dbFile = new DBFile();
dbFile->Open(pathConfig->GetDBFilePath(tableList->tableName));
dbFile->MoveFirst();
dbFileMap[tableList->tableName] = dbFile;
tableList = tableList->next;
}
// Read Statistic to generate query plan.
Statistics statistics;
statistics.Read(pathConfig->GetStatisticsFilePath());
// Build Query.
Query query = {finalFunction, tables, boolean, groupingAtts, attsToSelect, distinctAtts, distinctFunc};
// Generate query plan.
QueryPlan queryPlan(&dbFileMap, &statistics, &query);
if (queryOutputType == NO_OUT) {
queryPlan.Print();
} else {
// Get Query Plan.
RelOpPlanNode *planTree = queryPlan.GetQueryPlan();
// Run Query Plan.
QueryRunner queryRunner(planTree, queryOutputType, outputFileName);
queryRunner.Run();
}
// Close all tables.
for (auto const &dbFileMapItem : dbFileMap) {
dbFileMapItem.second->Close();
}
}
Schema *Database::CreateNewSchema(char *tableName, NameList *attsNameList) {
NameList *nameList = attsNameList;
int numAtts = 0;
while (nameList) {
numAtts++;
nameList = nameList->next;
}
Attribute *atts = new Attribute[numAtts];
nameList = attsNameList;
for (int i = 0; i < numAtts; i++) {
atts[i].name = nameList->name;
switch (nameList->type) {
case INT:
atts[i].myType = Int;
break;
case DOUBLE:
atts[i].myType = Double;
break;
case STRING:
atts[i].myType = String;
break;
}
nameList = nameList->next;
}
Schema *newSchema = new Schema(tableName, numAtts, atts);
return newSchema;
}