-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
341 lines (261 loc) · 13.7 KB
/
main.cpp
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
#include <QtCore/QCoreApplication>
#include <QCoreApplication>
#include <queue>
#include "aggHelper.h"
#include "TcpSocketServer.h"
#include "tableManager.h"
#include "dbProcess.h"
#include "view.h"
#include "jsCall.cpp"
string table::default_path;
string tableManager::curOperatUser;
queue<processObject> dbProcess::processQueue;
queue<processObject> dbProcess::correspondQueue;
tableManager* tableManager::tablemanager;
view* view::viewmanager;
table* dbProcess::countTable;
table* tableManager::jurisdictionTable;
aggHelper* aggHelper::agghelper;
typeHelper* typeHelper::typehelper;
int IO::singleFileLen;
#define RegisterJSType(TypeName,JSName) qRegisterMetaType<TypeName>(JSName)
using namespace std;
void outputVec(const vector<int> &vec)
{
for(int i : vec)
cout<<i<<" ";
cout<<endl;
}
class dbRun :public QThread{
virtual void run(){
while(true){
dbProcess::processRequst();
}
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
RegisterJSType(table*,"table*");
RegisterJSType(col*,"col*");
RegisterJSType(Basic*,"Basic*");
RegisterJSType(jsCollection*,"jsCollection*");
RegisterJSType(processObject,"processObject");
IO::singleFileLen=1000;
table::default_path="C:\\database_test";
manageContain::contain->init();
//创建所需表
col* USERNAME=new col(STR,"USERNAME");
col* PASSWORD=new col (STR,"PASSWORD");
table* Count=new table ("Count",{USERNAME,PASSWORD});
Count->add({typeHelper::typehelper->strToBasic("'ZT'"),typeHelper::typehelper->strToBasic(("'zt'"))});
Count->saveFile();
col* USER=new col(STR,"USERNAME");
col* TABLE=new col (STR,"TABLE");
table* Jurisdiction=new table ("Jurisdiction",{USER,TABLE});
Jurisdiction->add({typeHelper::typehelper->strToBasic("'ZT'"),typeHelper::typehelper->strToBasic(("'student'"))});
Jurisdiction->add({typeHelper::typehelper->strToBasic("'ZT'"),typeHelper::typehelper->strToBasic(("'teacher'"))});
Jurisdiction->saveFile();
col* viewName=new col(STR,"viewName");
col* viewCode=new col (STR,"viewCode");
table* views=new table ("views",{viewName,viewCode});
views->saveFile();
//静态工具初始化
typeHelper::typehelper=typeHelper::getTypeHelper();
manageContain::contain->init();
tableManager::tablemanager=tableManager::getTableManager(10);
aggHelper::agghelper=aggHelper::getHelper();
view::viewmanager=view::getViewManager();
//读取账户表 权限表
dbProcess::setCount(table::loadFile("Count"));
tableManager::setJurisdiction(table::loadFile("Jurisdiction"));
col* NAME=new col(STR,"NAME");
col* SCORE=new col (INT,"SCORE");
col* TEACHER=new col (STR,"TEACHER");
table* student=new table ("student",{NAME,SCORE,TEACHER});
student->add({typeHelper::typehelper->strToBasic("'zt'"),typeHelper::typehelper->strToBasic(("57")),typeHelper::typehelper->strToBasic("'jp'")});
student->add({typeHelper::typehelper->strToBasic("'gj'"),typeHelper::typehelper->strToBasic(("71")),typeHelper::typehelper->strToBasic("'gb'")});
student->add({typeHelper::typehelper->strToBasic("'yf'"),typeHelper::typehelper->strToBasic(("78")),typeHelper::typehelper->strToBasic("'gjb'")});
student->add({typeHelper::typehelper->strToBasic("'xr'"),typeHelper::typehelper->strToBasic(("89")),typeHelper::typehelper->strToBasic("'gjb'")});
student->add({typeHelper::typehelper->strToBasic("'wx'"),typeHelper::typehelper->strToBasic(("63")),typeHelper::typehelper->strToBasic("'jp'")});
student->add({typeHelper::typehelper->strToBasic("'ww'"),typeHelper::typehelper->strToBasic(("78")),typeHelper::typehelper->strToBasic("'gb'")});
long long startTime=getCurrentTime();
for(int i=0;i<10000;++i){
student->add({typeHelper::typehelper->strToBasic("'xr'"),typeHelper::typehelper->strToBasic((QString::fromStdString(to_string(i)))),typeHelper::typehelper->strToBasic("'jp'")});
}
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
student->saveFile();
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
student=tableManager::tablemanager->loadTable("student");
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
vector<int> resVec=student->find({"","(x==34)",""});
cout<<getCurrentTime()-startTime<<endl;
student->del(resVec);
//student->del({34,45});
startTime=getCurrentTime();
student->updateFile();
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
for(int i=0;i<100;++i){
student->add({typeHelper::typehelper->strToBasic("'xr'"),typeHelper::typehelper->strToBasic((QString::fromStdString(to_string(i)))),typeHelper::typehelper->strToBasic("'jp'")});
}
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
student->updateFile();
cout<<getCurrentTime()-startTime<<endl;
/*
TcpSocketServer *m_pTcpServer=new TcpSocketServer();
if (!m_pTcpServer->listen(QHostAddress::Any, 8888))
{
qDebug() << "m_pTcpServer->listen() error";
}
dbRun* dbrun=new dbRun();
dbrun->start();
ServerResponseThread RT;
QObject::connect(&RT, SIGNAL(db_response_signal(processObject)), m_pTcpServer, SLOT(response_handle(processObject)));
RT.start();*/
/*读写速度测试
col* NAME=new col(STR,"NAME");
col* SCORE=new col (INT,"SCORE");
col* TEACHER=new col (STR,"TEACHER");
table* student=new table ("student",{NAME,SCORE,TEACHER});
student->add({typeHelper::typehelper->strToBasic("'zt'"),typeHelper::typehelper->strToBasic(("57")),typeHelper::typehelper->strToBasic("'jp'")});
student->add({typeHelper::typehelper->strToBasic("'gj'"),typeHelper::typehelper->strToBasic(("71")),typeHelper::typehelper->strToBasic("'gb'")});
student->add({typeHelper::typehelper->strToBasic("'yf'"),typeHelper::typehelper->strToBasic(("78")),typeHelper::typehelper->strToBasic("'gjb'")});
student->add({typeHelper::typehelper->strToBasic("'xr'"),typeHelper::typehelper->strToBasic(("89")),typeHelper::typehelper->strToBasic("'gjb'")});
student->add({typeHelper::typehelper->strToBasic("'wx'"),typeHelper::typehelper->strToBasic(("63")),typeHelper::typehelper->strToBasic("'jp'")});
student->add({typeHelper::typehelper->strToBasic("'ww'"),typeHelper::typehelper->strToBasic(("78")),typeHelper::typehelper->strToBasic("'gb'")});
long long startTime=getCurrentTime();
for(int i=0;i<10000;++i){
student->add({typeHelper::typehelper->strToBasic("'xr'"),typeHelper::typehelper->strToBasic((QString::fromStdString(to_string(i)))),typeHelper::typehelper->strToBasic("'jp'")});
}
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
student->saveFile();
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
student=tableManager::tablemanager->loadTable("student");
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
vector<int> resVec=student->find({"","(x==34)",""});
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
student->del(resVec);
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
student->updateFile();
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
for(int i=0;i<100;++i){
student->add({typeHelper::typehelper->strToBasic("'xr'"),typeHelper::typehelper->strToBasic((QString::fromStdString(to_string(i)))),typeHelper::typehelper->strToBasic("'jp'")});
}
cout<<getCurrentTime()-startTime<<endl;
startTime=getCurrentTime();
student->updateFile();
cout<<getCurrentTime()-startTime<<endl;*/
/*读写压力测试
* startTime=getCurrentTime();
for(int i=0;i<1;i++){
student=table::loadFile("student");
student->saveFile();
delete student;
cout<<i<<endl;
}
cout<<getCurrentTime()-startTime<<endl;*/
/*
多表连接
table* student=tableManager::tablemanager->loadTable("student");
student->add({typeHelper::typehelper->strToBasic("'wuwei'"),typeHelper::typehelper->strToBasic(("63")),typeHelper::typehelper->strToBasic("'sbjp'")});
student->add({typeHelper::typehelper->strToBasic("'wuweibb'"),typeHelper::typehelper->strToBasic(("63")),typeHelper::typehelper->strToBasic("'sbjp'")});
table* teacher=tableManager::tablemanager->loadTable("teacher");
teacher->add({typeHelper::typehelper->strToBasic("'jpsb'"),typeHelper::typehelper->strToBasic("'YANGZHOU'")});
vector<string> stuVec={"NAME","SCORE","TEACHER"};
vector<string> teaVec={"TEACHER","LOC"};
cout<<tableManager::tablemanager->tableJoin("tsleft","student",stuVec,"teacher",teaVec,"TEACHER","TEACHER","leftjoin")->toStr().toStdString();
cout<<tableManager::tablemanager->tableJoin("tsouter","teacher",teaVec,"student",stuVec,"TEACHER","TEACHER","outerjoin")->toStr().toStdString();*/
/*
col* USERNAME=new col(STR,"USERNAME");
col* PASSWORD=new col (STR,"PASSWORD");
table* Count=new table ("Count",{USERNAME,PASSWORD});
Count->add({typeHelper::typehelper->strToBasic("'ZT'"),typeHelper::typehelper->strToBasic(("'zt'"))});
Count->saveFile();
col* USER=new col(STR,"USERNAME");
col* TABLE=new col (STR,"TABLE");
table* Jurisdiction=new table ("Jurisdiction",{USER,TABLE});
Jurisdiction->add({typeHelper::typehelper->strToBasic("'ZT'"),typeHelper::typehelper->strToBasic(("'student'"))});
Jurisdiction->add({typeHelper::typehelper->strToBasic("'ZT'"),typeHelper::typehelper->strToBasic(("'teacher'"))});
Jurisdiction->saveFile();
col* viewName=new col(STR,"viewName");
col* viewCode=new col (STR,"viewCode");
table* views=new table ("views",{viewName,viewCode});
views->saveFile();*/
/*
//创建表 增加数据
col* NAME=new col(STR,"NAME");
col* SCORE=new col (INT,"SCORE");
col* TEACHER=new col (STR,"TEACHER");
table* student=new table ("student",{NAME,SCORE,TEACHER});
student->add({typeHelper::typehelper->strToBasic("'zt'"),typeHelper::typehelper->strToBasic(("57")),typeHelper::typehelper->strToBasic("'jp'")});
student->add({typeHelper::typehelper->strToBasic("'gj'"),typeHelper::typehelper->strToBasic(("71")),typeHelper::typehelper->strToBasic("'gb'")});
student->add({typeHelper::typehelper->strToBasic("'yf'"),typeHelper::typehelper->strToBasic(("78")),typeHelper::typehelper->strToBasic("'gjb'")});
student->add({typeHelper::typehelper->strToBasic("'xr'"),typeHelper::typehelper->strToBasic(("89")),typeHelper::typehelper->strToBasic("'gjb'")});
student->add({typeHelper::typehelper->strToBasic("'wx'"),typeHelper::typehelper->strToBasic(("63")),typeHelper::typehelper->strToBasic("'jp'")});
student->add({typeHelper::typehelper->strToBasic("'ww'"),typeHelper::typehelper->strToBasic(("78")),typeHelper::typehelper->strToBasic("'gb'")});
student->saveFile();
col* tName=new col(STR,"TEACHER");
col* loc=new col (STR,"LOC");
table* teacher=new table ("teacher",{tName,loc});
teacher->add({typeHelper::typehelper->strToBasic("'jp'"),typeHelper::typehelper->strToBasic(("'HangZhou'"))});
teacher->add({typeHelper::typehelper->strToBasic("'gb'"),typeHelper::typehelper->strToBasic(("'BeiJing'"))});
teacher->add({typeHelper::typehelper->strToBasic("'gjb'"),typeHelper::typehelper->strToBasic(("'ShangHai'"))});
teacher->saveFile();
//对表查询
//select max(student.SCORE)from student where student.NAME!="zt"
table* stu_1=tableManager::tablemanager->loadTable("student");
vector<int> resIndex=stu_1->find({"(x!='zt')","",""});
vector<Basic*> resBasic=stu_1->getCol("SCORE")->getData(resIndex);
cout<<aggHelper::helper->max(resBasic)->toStr()<<endl;
//select teacher.loc from teacher where teacher.tName==(select max(student.SCORE) from student )
table* stu_2=tableManager::tablemanager->loadTable("student");
table* tch=tableManager::tablemanager->loadTable("teacher");
string maxScore=aggHelper::helper->max(stu_2->getCol("SCORE")->getAllData())->toStr();
vector<int> getData=stu_2->find({"","(x=="+maxScore+")",""});
vector<Basic*> tchs=stu_2->getCol("TEACHER")->getData(stu_2->find({"","(x=="+maxScore+")",""}));
cout<<tch->getCol("LOC")->genNewCol(tch->find_in("TEACHER",tchs))->toStr()<<endl;
//定点修改
stu_1->del(stu_1->find({"(((x=='xr')||(x=='wx'))||(x=='ww'))","",""}));
stu_1->updateFile();
stu_1=table::loadFile("student");
stu_1->saveFile();
//更换索引
index* ni=new binarySearchIndex(SCORE);
stu_1->changeIndex(1,ni);
cout<<stu_1->genNewTable({"NAME","SCORE","TEACHER"},stu_1->find({"","(x<71)",""}))->toStr()<<endl;
//事务功能
cout<<stu_1->toStr()<<endl;
stu_1->updateFile();
stu_1->add({typeHelper::typehelper->strToBasic("'lz'"),typeHelper::typehelper->strToBasic(("54")),typeHelper::typehelper->strToBasic("'cnnndsb'")});
cout<<stu_1->toStr()<<endl;
stu_1->rollback();
cout<<stu_1->toStr()<<endl;
col* USERNAME=new col(STR,"USERNAME");
col* PASSWORD=new col (STR,"PASSWORD");
table* Count=new table ("Count",{USERNAME,PASSWORD});
Count->add({typeHelper::typehelper->strToBasic("'ZT'"),typeHelper::typehelper->strToBasic(("'zt'"))});
Count->saveFile();
col* USER=new col(STR,"USERNAME");
col* TABLE=new col (STR,"TABLE");
table* Jurisdiction=new table ("Jurisdiction",{USER,TABLE});
Jurisdiction->add({typeHelper::typehelper->strToBasic("'ZT'"),typeHelper::typehelper->strToBasic(("'student'"))});
Jurisdiction->add({typeHelper::typehelper->strToBasic("'ZT'"),typeHelper::typehelper->strToBasic(("'teacher'"))});
Jurisdiction->saveFile();
col* viewName=new col(STR,"viewName");
col* viewCode=new col (STR,"viewCode");
table* views=new table ("views",{viewName,viewCode});
views->saveFile();
*/
return a.exec();
};