-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbgen.cc
More file actions
179 lines (157 loc) · 5.06 KB
/
Copy pathdbgen.cc
File metadata and controls
179 lines (157 loc) · 5.06 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
#include <iostream>
#include <cstring>
#include "Config.h"
#include "DBFile.h"
#include "Catalog.h"
#include "Schema.h"
using namespace std;
// create a DBFile from corresponding TPC-H data
bool createDBFile(Schema _schema, string _heapPath, string _textPath) {
char* heapPath = new char[_heapPath.length()+1];
char* textPath = new char[_textPath.length()+1];
strcpy(heapPath, _heapPath.c_str());
strcpy(textPath, _textPath.c_str());
// create DBFile
DBFile dbFile;
if(dbFile.Create(heapPath, Heap) == -1) {
return false;
}
// load text data into heap file
dbFile.Load(_schema, textPath);
// close DBFile
if(dbFile.Close() == -1) {
return false;
}
return true;
}
bool testDBFile(Schema _schema, string _heapPath) {
// open DBFile
char* heapPath = new char[_heapPath.length()+1];
strcpy(heapPath, _heapPath.c_str());
DBFile dbFile;
if(dbFile.Open(heapPath) == -1) {
cerr << "ERROR: DBFile::Open" << endl << endl;
return false;
}
// retrieve data from DBFile
dbFile.MoveFirst();
while(true) {
Record record;
if(dbFile.GetNext(record) == 0) {
record.print(cout, _schema); cout << endl;
} else {
break;
}
}
// close DBFile
if(dbFile.Close() == -1) {
cerr << "ERROR: DBFile::Close" << endl << endl;
return false;
}
return true;
}
int main(int argc, char* argv[]) {
if(argc == 2 || argc == 4) {
// load catalog
string database = "catalog.sqlite";
Catalog catalog(database);
if(argc == 2) {
if(argv[1] == string("-all")) {
string userHome = getenv("HOME");
string defaultPath = userHome + "/.sqlite-jarvis/";
string heapPath = "heap/"; string textPath = "text/";
string yn;
cout << "Jarvis: DBFiles for the 8 tables will be automatically generated." << endl;
cout << "Make sure that you have following directories before proceeding." << endl << endl;
cout << userHome << endl;
cout << " └ .sqlite-jarvis" << endl;
cout << " ├ heap (where your DBFiles will be stored.)" << endl;
cout << " └ text (where your .tbl files should be stored.)" << endl << endl;
cout << "You should put .tbl files under " << defaultPath << textPath << " first!" << endl;
cout << "Existing files will be overwritten. Continue? (Y/N) " << flush;
cin >> yn;
if(yn == "Y" || yn == "y") {
cout << "Generating DBFiles" << flush;
// preparing
int size = 8;
vector<string> tableNames = {"customer", "lineitem", "nation", "orders",
"part", "partsupp", "region", "supplier"};
vector<string> heapPaths, textPaths;
for(size_t i = 0; i < size; i++) {
heapPaths.push_back(defaultPath + heapPath + tableNames[i] + ".dat");
textPaths.push_back(defaultPath + textPath + tableNames[i] + ".tbl");
}
// batch
for(size_t i = 0; i < size; i++) {
cout << "." << flush;
Schema schema;
if(!catalog.GetSchema(tableNames[i], schema)) {
cerr << endl << "ERROR: Table '" << tableNames[i] << "' does not exist." << endl << endl;
return -1;
}
if(createDBFile(schema, heapPaths[i], textPaths[i])) {
// update new DBFile path in catalog
catalog.SetDataFile(tableNames[i], heapPaths[i]);
} else {
cerr << endl << "FAILED! (createDBFile of " << tableNames[i] << ")" << endl << endl;
return -1;
}
}
// save catalog to update new DBFile path
if(catalog.Save()) {
cout << " OK!" << endl;
return 0;
} else {
cerr << " FAILED! (Catalog::Save)" << endl << endl;
return -1;
}
} else {
return 1;
}
} else { // print
string tableName = argv[1];
Schema schema;
if(!catalog.GetSchema(tableName, schema)) {
cerr << endl << "ERROR: Table '" << tableName << "' does not exist." << endl << endl;
return -1;
}
string heapPath; catalog.GetDataFile(tableName, heapPath);
if(!testDBFile(schema, heapPath)) {
return -1;
}
}
} else if(argc == 4) {
string tableName, heapPath, textPath;
tableName = argv[1]; heapPath = argv[2]; textPath = argv[3];
cout << "Generating a DBFile for table '" << tableName << "'... " << flush;
Schema schema;
if(!catalog.GetSchema(tableName, schema)) {
cerr << "ERROR: Table '" << tableName << "' does not exist." << endl << endl;
exit(-1);
}
if(createDBFile(schema, heapPath, textPath)) {
// update new DBFile path in catalog
catalog.SetDataFile(tableName, heapPath);
// save catalog to update new DBFile path
if(catalog.Save()) {
cout << "OK!" << endl;
return 0;
} else {
cerr << "FAILED! (Catalog::Save)" << endl << endl;
return -1;
}
} else {
cerr << "FAILED! (createDBFile of " << tableName << ")" << endl << endl;
return -1;
}
}
} else {
cout << endl;
cout << "Jarvis: This simple program generates a DBFile from TPC-H data." << endl << endl;
cout << "Usage: " << endl;
cout << "\tCreate:\t"<< argv[0] << " [TABLE_NAME] [HEAP_FILE_PATH] [TEXT_FILE_PATH]" << endl;
cout << "\tPrint:\t"<< argv[0] << " [TABLE_NAME]" << endl;
cout << "\tBatch:\t"<< argv[0] << " -all" << endl << endl;
}
return -1;
}