-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBEngine.cc
executable file
·174 lines (150 loc) · 4.08 KB
/
DBEngine.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
/*
* DBEngine.cc
*
* Created on: May 3, 2018
* Author: adhikari
*/
#include <fstream>
#include <string>
#include "DBEngine.h"
using namespace std;
extern char* catalog_path;
extern char* dbfile_dir;
extern char* tpch_dir;
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 AttrList *attrs;
extern struct NameList *sort;
extern char *table, *file, *oldtable, *output;
bool DBEngine::Create() {
//check whether table being create is already in the catalog file
if(CheckCatalog(table))
return false;
//create a new metafile and update the DBFile type
const char* nameMeta = (string(table)+".txt").c_str();
ofstream meta(nameMeta);
fType type = (sort? sorted : heap);
meta<<type<<endl;
OrderMaker order;
//create a new relation in the catalog file
//using table name and attribute information
const char* types[3]={"Int", "Double", "String"};
Type atypes[3]={Int, Double, String};
//cout<<"\nhere1.5";
ofstream catalog(catalog_path, std::ios_base::app);
catalog << "BEGIN\n" <<table<< '\n' <<table<< ".tbl" << endl;
int num=0;
//cout<<"\nhere 1";
AttrList* att = attrs;
for (; ; ++num) {
if(att==NULL)
break;
//cout<<"\nhere1.5";
catalog<<att->name<<' '<<types[att->type]<< endl;
att=att->next;
}
//cout<<"\nhere 2";
catalog<<"END" << endl;
Attribute* tempattrs = new Attribute[num];
num=0;
for (AttrList* att = attrs; att; att = att->next, ++num) {
tempattrs[num].name = strdup(att->name);
tempattrs[num].myType = atypes[att->type];
}
//create new schema using attribute information
Schema sch("", num, tempattrs);
if(type==sorted) {
order.growFromParseTree(sort, &sch);
}
//encapsulate ordermaker and runlength into a structure for creation of DBFile
struct SortInfo {
OrderMaker * order;
int length;
}info;
info.order=ℴ
info.length=500;
//create DBFile
DBFile dbfile;
dbfile.Create((char*)(dbfile_dir+string(table)+".bin").c_str(), type, (void*)&info);
dbfile.Close();
delete[] tempattrs;
catalog.close();
return true;
}
bool DBEngine::CheckCatalog(const char* relName) {
ifstream in (catalog_path);
string str;
while (getline(in, str)) {
if (str == relName) {
in.close();
return true;
}
}
in.close();
return false;
}
bool DBEngine::Insert() {
DBFile dbfile;
char* path = new char[strlen(dbfile_dir)+strlen(oldtable)+4];
strcpy(path,dbfile_dir);
strcat(path, oldtable);
strcat(path,".bin");
//char* finalpath = (char*)((string(dbfile_dir)+string(path)).c_str());
Schema sch(catalog_path, oldtable);
//cout<<"\n"<<dbfile_dir;
if(dbfile.Open(path)==0) {
delete[] path;
return false;
}
else {
char* tblpath = new char[strlen(tpch_dir)+strlen(file)];
strcpy(tblpath, tpch_dir);
strcat(tblpath, file);
//(char*)(string(tpch_dir)+string(file)).c_str();
dbfile.Load(sch, tblpath);
dbfile.Close();
//delete[] path;
return true;
}
}
bool DBEngine::Drop() {
ifstream in(catalog_path);
ofstream out("cat.tmp");
string line, temp="";
string relation = oldtable;
//cout<<"\n"<<catalog_path<<","<<oldtable;
bool present=false, found=false;
while(getline(in, line)) {
//cout<<"\nhere!";
if(line.empty())
continue;
//cout<<"\nline - "<<line;
if(line==oldtable) {
//cout<<"\nline - "<<line;
present=true;
found=true;
}
temp+=line+"\n";
if(line=="END") {
if(!found)
out<<temp<<endl;
found=false;
temp.clear();
}
}
rename("cat.tmp",catalog_path);
in.close();
out.close();
if(present) {
//cout<<"\ndeleting files from "<<dbfile_dir;
remove((dbfile_dir+relation+".bin").c_str());
remove((dbfile_dir+relation+".txt").c_str());
return true;
}
return false;
}