Use system.db to optimize ghttp

This commit is contained in:
suxunbin 2018-10-19 20:32:52 +08:00
parent 67ea4578cb
commit 22db1a45d6
7 changed files with 769 additions and 225 deletions

View File

@ -1,9 +1,9 @@
/*=============================================================================
# Filename: gbuild.cpp
# Author: Bookug Lobert
# Mail: 1181955272@qq.com
# Last Modified: 2015-10-24 19:27
# Description: firstly written by liyouhuan, modified by zengli
# Author: Bookug Lobert suxunbin
# Mail: 1181955272@qq.com suxunbin@pku.edu.cn
# Last Modified: 2018-10-19 20:30
# Description: firstly written by liyouhuan, modified by zengli and suxunbin
TODO: add -h/--help for help message
=============================================================================*/
@ -11,6 +11,7 @@ TODO: add -h/--help for help message
#include "../Database/Database.h"
using namespace std;
#define SYSTEM_PATH "data/system/system.nt"
//[0]./gbuild [1]data_folder_path [2]rdf_file_path
int
@ -44,15 +45,39 @@ main(int argc, char * argv[])
return -1;
}
//check if the db_name is system
if (_db_path == "system")
{
cout<< "Your database's name can not be system."<<endl;
return -1;
}
//if(_db_path[0] != '/' && _db_path[0] != '~') //using relative path
//{
//_db_path = string("../") + _db_path;
//}
string _rdf = string(argv[2]);
//check if the db_path is the path of system.nt
if (_rdf == SYSTEM_PATH)
{
cout<< "You have no rights to access system files"<<endl;
return -1;
}
//if(_rdf[0] != '/' && _rdf[0] != '~') //using relative path
//{
//_rdf = string("../") + _rdf;
//}
//check if the database is already built
int isbuilt;
if (boost::filesystem::exists(_db_path + ".db"))
isbuilt = 1;
else
isbuilt = 0;
//build database
Database _db(_db_path);
bool flag = _db.build(_rdf);
if (flag)
@ -62,12 +87,96 @@ main(int argc, char * argv[])
f.open("./"+ _db_path +".db/success.txt");
f.close();
}
else
else //if fails, drop database and return
{
cout << "import RDF file to database failed." << endl;
string cmd = "rm -r " + _db_path + ".db";
system(cmd.c_str());
return 0;
}
if (!boost::filesystem::exists("system.db"))
return 0;
//system("clock");
Database system_db("system");
system_db.load();
//if isbuilt is false, add database information to system.db
if (isbuilt == 0)
{
string time = Util::get_date_time();
string sparql = "INSERT DATA {<" + _db_path + "> <database_status> \"already_built\"." + "<" + _db_path + "> <built_by> <root>."
+ "<" + _db_path + "> <built_time> \"" + time + "\".}";
ResultSet _rs;
FILE* ofp = stdout;
string msg;
int ret = system_db.query(sparql, _rs, ofp);
if (ret <= -100) // select query
{
if (ret == -100)
msg = _rs.to_str();
else //query error
msg = "query failed";
}
else //update query
{
if (ret >= 0)
msg = "update num : " + Util::int2string(ret);
else //update error
msg = "update failed.";
if (ret != -100)
cout << msg << endl;
}
return 0;
}
else //if isbuilt is true, update built_time of the database
{
string sparql = "DELETE {<" + _db_path + "> <built_time> ?t .}"
+ "WHERE{<" + _db_path + "> <built_time> ?t .}";
ResultSet _rs;
FILE* ofp = stdout;
string msg;
int ret = system_db.query(sparql, _rs, ofp);
if (ret <= -100) // select query
{
if (ret == -100)
msg = _rs.to_str();
else //query error
msg = "query failed";
}
else //update query
{
if (ret >= 0)
msg = "update num : " + Util::int2string(ret);
else //update error
msg = "update failed.";
if (ret != -100)
cout << msg << endl;
}
cout << "delete successfully" << endl;
}
string time = Util::get_date_time();
string sparql = "INSERT DATA {<" + _db_path + "> <built_time> \"" + time + "\".}";
ResultSet _rs;
FILE* ofp = stdout;
string msg;
int ret = system_db.query(sparql, _rs, ofp);
if (ret <= -100) // select query
{
if (ret == -100)
msg = _rs.to_str();
else //query error
msg = "query failed";
}
else //update query
{
if (ret >= 0)
msg = "update num : " + Util::int2string(ret);
else //update error
msg = "update failed.";
if (ret != -100)
cout << msg << endl;
}
cout << "insert successfully" << endl;
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,11 @@
/*=============================================================================
# Filename: ginit.cpp
# Author: suxunbin
# Mail: suxunbin@pku.edu.cn
# Last Modified: 2018-10-17 14:59
# Description: used to initialize the system.db
=============================================================================*/
#include "../Util/Util.h"
#include "../Database/Database.h"
@ -10,11 +18,13 @@ int main(int argc, char * argv[])
if(boost::filesystem::exists("system.db"))
return 0;
}
//build system.db
Util util;
string _db_path = "system";
string _rdf = "data/system/system.nt";
Database _db(_db_path);
bool flag = _db.build(_rdf);
Database* _db = new Database(_db_path);
bool flag = _db->build(_rdf);
if (flag)
{
cout << "import RDF file to database done." << endl;
@ -22,11 +32,44 @@ int main(int argc, char * argv[])
f.open("./"+ _db_path +".db/success.txt");
f.close();
}
else
else //if fails, drop system.db and return
{
cout << "import RDF file to database failed." << endl;
string cmd = "rm -r " + _db_path + ".db";
system(cmd.c_str());
delete _db;
_db = NULL;
return 0;
}
//insert built_time of system.db
delete _db;
_db = new Database(_db_path);
_db->load();
string time = Util::get_date_time();
string sparql = "INSERT DATA {<system> <built_time> \"" + time + "\".}";
ResultSet _rs;
FILE* ofp = stdout;
string msg;
int ret = _db->query(sparql, _rs, ofp);
if (ret <= -100) // select query
{
if (ret == -100)
msg = _rs.to_str();
else //query error
msg = "query failed";
}
else //update query
{
if (ret >= 0)
msg = "update num : " + Util::int2string(ret);
else //update error
msg = "update failed.";
if (ret != -100)
cout << msg << endl;
}
delete _db;
_db = NULL;
cout << "system.db is built successfully!" << endl;
return 0;
}

View File

@ -1,8 +1,19 @@
/*=============================================================================
# Filename: shutdown.cpp
# Author: suxunbin
# Mail: suxunbin@pku.edu.cn
# Last Modified: 2018-10-16 16:15
# Description: used to stop the ghttp server
=============================================================================*/
#include "../api/http/cpp/client.h"
#include "../Util/Util.h"
using namespace std;
#define ROOT_USERNAME "root"
#define ROOT_PASSWORD "123456"
bool isNum(char *str)
{
for(int i = 0; i < strlen(str); i++)
@ -49,6 +60,6 @@ int main(int argc, char *argv[])
CHttpClient hc;
string res;
int ret;
ret = hc.Get("http://127.0.0.1:"+port+"/?operation=stop", res);
ret = hc.Get("http://127.0.0.1:" + port + "/?operation=stop&username=" + ROOT_USERNAME + "&password=" + ROOT_PASSWORD, res);
return 0;
}

View File

@ -1 +1,2 @@
<root> <has_password> "123456" .
<system> <built_by> <root> .

View File

@ -127,10 +127,13 @@ cout << answer << std::endl;
gc.unload("lubm", "root", "123456");
// show all databases already built and if they are loaded
gc.show();
gc.show("root", "123456");
// show statistical information of a loaded database
gc.monitor("lubm");
gc.monitor("lubm", "root", "123456");
// save updates of a loaded database
gc.checkpoint("lubm", "root", "123456");
//add a user(with username: Jack, password: 2)
answer = gc.user("add_user", "root", "123456", "Jack", "2");

View File

@ -2,6 +2,10 @@
#set -v
#initialize system.db
"bin/ginit" "--make"
#test
db=("bbug" "lubm" "num" "small")
op=("bin/gbuild" "bin/gquery" "bin/gadd" "bin/gsub")
path="./data/"
@ -142,5 +146,4 @@ else
echo "Test failed!"
fi
"bin/ginit" "--make"