Merge branch 'suxunbin'
merge suxunbin's test scripts for repeated insert/delete
This commit is contained in:
commit
13c21c8a56
119
Main/gbuild.cpp
119
Main/gbuild.cpp
|
@ -1,9 +1,9 @@
|
||||||
/*=============================================================================
|
/*=============================================================================
|
||||||
# Filename: gbuild.cpp
|
# Filename: gbuild.cpp
|
||||||
# Author: Bookug Lobert
|
# Author: Bookug Lobert suxunbin
|
||||||
# Mail: 1181955272@qq.com
|
# Mail: 1181955272@qq.com suxunbin@pku.edu.cn
|
||||||
# Last Modified: 2015-10-24 19:27
|
# Last Modified: 2018-10-19 20:30
|
||||||
# Description: firstly written by liyouhuan, modified by zengli
|
# Description: firstly written by liyouhuan, modified by zengli and suxunbin
|
||||||
TODO: add -h/--help for help message
|
TODO: add -h/--help for help message
|
||||||
=============================================================================*/
|
=============================================================================*/
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ TODO: add -h/--help for help message
|
||||||
#include "../Database/Database.h"
|
#include "../Database/Database.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
#define SYSTEM_PATH "data/system/system.nt"
|
||||||
|
|
||||||
//[0]./gbuild [1]data_folder_path [2]rdf_file_path
|
//[0]./gbuild [1]data_folder_path [2]rdf_file_path
|
||||||
int
|
int
|
||||||
|
@ -44,15 +45,39 @@ main(int argc, char * argv[])
|
||||||
return -1;
|
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
|
//if(_db_path[0] != '/' && _db_path[0] != '~') //using relative path
|
||||||
//{
|
//{
|
||||||
//_db_path = string("../") + _db_path;
|
//_db_path = string("../") + _db_path;
|
||||||
//}
|
//}
|
||||||
string _rdf = string(argv[2]);
|
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
|
//if(_rdf[0] != '/' && _rdf[0] != '~') //using relative path
|
||||||
//{
|
//{
|
||||||
//_rdf = string("../") + _rdf;
|
//_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);
|
Database _db(_db_path);
|
||||||
bool flag = _db.build(_rdf);
|
bool flag = _db.build(_rdf);
|
||||||
if (flag)
|
if (flag)
|
||||||
|
@ -62,12 +87,96 @@ main(int argc, char * argv[])
|
||||||
f.open("./"+ _db_path +".db/success.txt");
|
f.open("./"+ _db_path +".db/success.txt");
|
||||||
f.close();
|
f.close();
|
||||||
}
|
}
|
||||||
else
|
else //if fails, drop database and return
|
||||||
{
|
{
|
||||||
cout << "import RDF file to database failed." << endl;
|
cout << "import RDF file to database failed." << endl;
|
||||||
string cmd = "rm -r " + _db_path + ".db";
|
string cmd = "rm -r " + _db_path + ".db";
|
||||||
system(cmd.c_str());
|
system(cmd.c_str());
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
if (!boost::filesystem::exists("system.db"))
|
||||||
|
return 0;
|
||||||
//system("clock");
|
//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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
800
Main/ghttp.cpp
800
Main/ghttp.cpp
File diff suppressed because it is too large
Load Diff
|
@ -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 "../Util/Util.h"
|
||||||
#include "../Database/Database.h"
|
#include "../Database/Database.h"
|
||||||
|
|
||||||
|
@ -10,11 +18,13 @@ int main(int argc, char * argv[])
|
||||||
if(boost::filesystem::exists("system.db"))
|
if(boost::filesystem::exists("system.db"))
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//build system.db
|
||||||
Util util;
|
Util util;
|
||||||
string _db_path = "system";
|
string _db_path = "system";
|
||||||
string _rdf = "data/system/system.nt";
|
string _rdf = "data/system/system.nt";
|
||||||
Database _db(_db_path);
|
Database* _db = new Database(_db_path);
|
||||||
bool flag = _db.build(_rdf);
|
bool flag = _db->build(_rdf);
|
||||||
if (flag)
|
if (flag)
|
||||||
{
|
{
|
||||||
cout << "import RDF file to database done." << endl;
|
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.open("./"+ _db_path +".db/success.txt");
|
||||||
f.close();
|
f.close();
|
||||||
}
|
}
|
||||||
else
|
else //if fails, drop system.db and return
|
||||||
{
|
{
|
||||||
cout << "import RDF file to database failed." << endl;
|
cout << "import RDF file to database failed." << endl;
|
||||||
string cmd = "rm -r " + _db_path + ".db";
|
string cmd = "rm -r " + _db_path + ".db";
|
||||||
system(cmd.c_str());
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 "../api/http/cpp/client.h"
|
||||||
#include "../Util/Util.h"
|
#include "../Util/Util.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
#define ROOT_USERNAME "root"
|
||||||
|
#define ROOT_PASSWORD "123456"
|
||||||
|
|
||||||
bool isNum(char *str)
|
bool isNum(char *str)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < strlen(str); i++)
|
for(int i = 0; i < strlen(str); i++)
|
||||||
|
@ -49,6 +60,6 @@ int main(int argc, char *argv[])
|
||||||
CHttpClient hc;
|
CHttpClient hc;
|
||||||
string res;
|
string res;
|
||||||
int ret;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
<root> <has_password> "123456" .
|
<root> <has_password> "123456" .
|
||||||
|
<system> <built_by> <root> .
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
<s0> <p0> <o0>.
|
|
@ -127,10 +127,13 @@ cout << answer << std::endl;
|
||||||
gc.unload("lubm", "root", "123456");
|
gc.unload("lubm", "root", "123456");
|
||||||
|
|
||||||
// show all databases already built and if they are loaded
|
// show all databases already built and if they are loaded
|
||||||
gc.show();
|
gc.show("root", "123456");
|
||||||
|
|
||||||
// show statistical information of a loaded database
|
// 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)
|
//add a user(with username: Jack, password: 2)
|
||||||
answer = gc.user("add_user", "root", "123456", "Jack", "2");
|
answer = gc.user("add_user", "root", "123456", "Jack", "2");
|
||||||
|
|
20
makefile
20
makefile
|
@ -59,6 +59,8 @@ objdir = .objs/
|
||||||
|
|
||||||
exedir = bin/
|
exedir = bin/
|
||||||
|
|
||||||
|
testdir = scripts/
|
||||||
|
|
||||||
lib_antlr = lib/libantlr.a
|
lib_antlr = lib/libantlr.a
|
||||||
|
|
||||||
api_cpp = api/socket/cpp/lib/libgstoreconnector.a
|
api_cpp = api/socket/cpp/lib/libgstoreconnector.a
|
||||||
|
@ -111,7 +113,7 @@ inc = -I./tools/libantlr3c-3.4/ -I./tools/libantlr3c-3.4/include
|
||||||
|
|
||||||
#gtest
|
#gtest
|
||||||
|
|
||||||
TARGET = $(exedir)gbuild $(exedir)gserver $(exedir)gserver_backup_scheduler $(exedir)gclient $(exedir)gquery $(exedir)gconsole $(api_java) $(exedir)gadd $(exedir)gsub $(exedir)ghttp $(exedir)gmonitor $(exedir)gshow $(exedir)shutdown $(exedir)ginit
|
TARGET = $(exedir)gbuild $(exedir)gserver $(exedir)gserver_backup_scheduler $(exedir)gclient $(exedir)gquery $(exedir)gconsole $(api_java) $(exedir)gadd $(exedir)gsub $(exedir)ghttp $(exedir)gmonitor $(exedir)gshow $(exedir)shutdown $(exedir)ginit $(testdir)update_test
|
||||||
|
|
||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
bash scripts/test.sh
|
bash scripts/test.sh
|
||||||
|
@ -158,22 +160,23 @@ $(exedir)gconsole: $(lib_antlr) $(objdir)gconsole.o $(objfile) $(api_cpp)
|
||||||
$(exedir)ghttp: $(lib_antlr) $(objdir)ghttp.o ./Server/server_http.hpp ./Server/client_http.hpp $(objfile)
|
$(exedir)ghttp: $(lib_antlr) $(objdir)ghttp.o ./Server/server_http.hpp ./Server/client_http.hpp $(objfile)
|
||||||
$(CC) $(EXEFLAG) -o $(exedir)ghttp $(objdir)ghttp.o $(objfile) $(library) $(inc) -DUSE_BOOST_REGEX $(openmp)
|
$(CC) $(EXEFLAG) -o $(exedir)ghttp $(objdir)ghttp.o $(objfile) $(library) $(inc) -DUSE_BOOST_REGEX $(openmp)
|
||||||
|
|
||||||
|
$(testdir)update_test: $(lib_antlr) $(objdir)update_test.o $(objfile)
|
||||||
|
$(CC) $(EXEFLAG) -o $(testdir)update_test $(objdir)update_test.o $(objfile) $(library) $(openmp)
|
||||||
#executables end
|
#executables end
|
||||||
|
|
||||||
|
|
||||||
#objects in Main/ begin
|
#objects in Main/ begin
|
||||||
|
|
||||||
$(objdir)ginit.o: Main/ginit.cpp $(lib_antlr)
|
$(objdir)ginit.o: Main/ginit.cpp Database/Database.h Util/Util.h $(lib_antlr)
|
||||||
$(CC) $(CFLAGS) Main/ginit.cpp $(inc) -o $(objdir)ginit.o $(openmp)
|
$(CC) $(CFLAGS) Main/ginit.cpp $(inc) -o $(objdir)ginit.o $(openmp)
|
||||||
|
|
||||||
$(objdir)shutdown.o: Main/shutdown.cpp $(lib_antlr)
|
$(objdir)shutdown.o: Main/shutdown.cpp Database/Database.h Util/Util.h $(lib_antlr)
|
||||||
$(CC) $(CFLAGS) Main/shutdown.cpp $(inc) -o $(objdir)shutdown.o $(openmp)
|
$(CC) $(CFLAGS) Main/shutdown.cpp $(inc) -o $(objdir)shutdown.o $(openmp)
|
||||||
|
|
||||||
$(objdir)gmonitor.o: Main/gmonitor.cpp $(lib_antlr)
|
$(objdir)gmonitor.o: Main/gmonitor.cpp Database/Database.h Util/Util.h $(lib_antlr)
|
||||||
$(CC) $(CFLAGS) Main/gmonitor.cpp $(inc) -o $(objdir)gmonitor.o $(openmp)
|
$(CC) $(CFLAGS) Main/gmonitor.cpp $(inc) -o $(objdir)gmonitor.o $(openmp)
|
||||||
|
|
||||||
$(objdir)gshow.o: Main/gshow.cpp $(lib_antlr)
|
$(objdir)gshow.o: Main/gshow.cpp Database/Database.h Util/Util.h $(lib_antlr)
|
||||||
$(CC) $(CFLAGS) Main/gshow.cpp $(inc) -o $(objdir)gshow.o $(openmp)
|
$(CC) $(CFLAGS) Main/gshow.cpp $(inc) -o $(objdir)gshow.o $(openmp)
|
||||||
|
|
||||||
$(objdir)gbuild.o: Main/gbuild.cpp Database/Database.h Util/Util.h $(lib_antlr)
|
$(objdir)gbuild.o: Main/gbuild.cpp Database/Database.h Util/Util.h $(lib_antlr)
|
||||||
|
@ -200,6 +203,11 @@ $(objdir)ghttp.o: Main/ghttp.cpp Server/server_http.hpp Server/client_http.hpp D
|
||||||
|
|
||||||
#objects in Main/ end
|
#objects in Main/ end
|
||||||
|
|
||||||
|
#objects in scripts/ begin
|
||||||
|
$(objdir)update_test.o: scripts/update_test.cpp Database/Database.h Util/Util.h $(lib_antlr)
|
||||||
|
$(CC) $(CFLAGS) scripts/update_test.cpp $(inc) -o $(objdir)update_test.o $(openmp)
|
||||||
|
#objects in scripts/ end
|
||||||
|
|
||||||
|
|
||||||
#objects in kvstore/ begin
|
#objects in kvstore/ begin
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
#set -v
|
#set -v
|
||||||
|
|
||||||
|
#initialize system.db
|
||||||
|
"bin/ginit" "--make" >& /dev/null
|
||||||
|
|
||||||
|
#test
|
||||||
db=("bbug" "lubm" "num" "small")
|
db=("bbug" "lubm" "num" "small")
|
||||||
op=("bin/gbuild" "bin/gquery" "bin/gadd" "bin/gsub")
|
op=("bin/gbuild" "bin/gquery" "bin/gadd" "bin/gsub")
|
||||||
path="./data/"
|
path="./data/"
|
||||||
|
@ -141,6 +145,3 @@ then
|
||||||
else
|
else
|
||||||
echo "Test failed!"
|
echo "Test failed!"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
"bin/ginit" "--make" >& /dev/null
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,185 @@
|
||||||
|
/*=============================================================================
|
||||||
|
# Filename: update_test.cpp
|
||||||
|
# Author: suxunbin
|
||||||
|
# Mail: suxunbin@pku.edu.cn
|
||||||
|
# Last Modified: 2018-10-25 21:25
|
||||||
|
# Description: used to test the correctness of update triples
|
||||||
|
=============================================================================*/
|
||||||
|
|
||||||
|
#include "../Util/Util.h"
|
||||||
|
#include "../Database/Database.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//triple information
|
||||||
|
class triple{
|
||||||
|
public:
|
||||||
|
string subject;
|
||||||
|
string predicate;
|
||||||
|
string object;
|
||||||
|
|
||||||
|
triple(int s, int p, int o){
|
||||||
|
subject = "<s" + Util::int2string(s) + ">";
|
||||||
|
predicate = "<p" + Util::int2string(p) + ">";
|
||||||
|
object = "<o" + Util::int2string(o) + ">";
|
||||||
|
}
|
||||||
|
triple(string s, string p, string o){
|
||||||
|
subject = s;
|
||||||
|
predicate = p;
|
||||||
|
object = o;
|
||||||
|
}
|
||||||
|
inline bool operator<(const triple& t) const
|
||||||
|
{
|
||||||
|
if (this->subject < t.subject)
|
||||||
|
return true;
|
||||||
|
else if (this->subject > t.subject)
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (this->predicate < t.predicate)
|
||||||
|
return true;
|
||||||
|
else if (this->predicate > t.predicate)
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (this->object < t.object)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inline bool operator==(const triple& t) const
|
||||||
|
{
|
||||||
|
if (this->subject != t.subject)
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (this->predicate != t.predicate)
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (this->object != t.object)
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::set<triple> update_triples;
|
||||||
|
std::set<triple> db_triples;
|
||||||
|
Database* db;
|
||||||
|
|
||||||
|
int main(int argc, char * argv[])
|
||||||
|
{
|
||||||
|
//build update_test.db
|
||||||
|
Util util;
|
||||||
|
string db_name = "update_test";
|
||||||
|
string db_path = "data/update_test.nt";
|
||||||
|
db = new Database(db_name);
|
||||||
|
bool flag = db->build(db_path);
|
||||||
|
if (flag)
|
||||||
|
{
|
||||||
|
cout << "update_test.db is built done." << endl;
|
||||||
|
ofstream f;
|
||||||
|
f.open("./" + db_name + ".db/success.txt");
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
else //if fails, drop update_test.db and return
|
||||||
|
{
|
||||||
|
cout << "update_test.db is built failed." << endl;
|
||||||
|
string cmd = "rm -r " + db_name + ".db";
|
||||||
|
system(cmd.c_str());
|
||||||
|
delete db;
|
||||||
|
db = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//load update_test.db
|
||||||
|
delete db;
|
||||||
|
db = new Database(db_name);
|
||||||
|
db->load();
|
||||||
|
|
||||||
|
//update triples test
|
||||||
|
srand((unsigned)time(NULL));
|
||||||
|
update_triples.clear();
|
||||||
|
triple temp(0, 0, 0);
|
||||||
|
update_triples.insert(temp);
|
||||||
|
for (int i = 0; i < 100; i++)
|
||||||
|
{
|
||||||
|
int a = rand() % 5 + 1;
|
||||||
|
int b = rand() % 5 + 1;
|
||||||
|
for (int j = 0; j < a; j++)
|
||||||
|
{
|
||||||
|
int s = rand() % 10;
|
||||||
|
int p = rand() % 10;
|
||||||
|
int o = rand() % 10;
|
||||||
|
triple t(s, p, o);
|
||||||
|
update_triples.insert(t);
|
||||||
|
string query = "INSERT DATA{" + t.subject + " " + t.predicate + " " + t.object + ".}";
|
||||||
|
ResultSet _rs;
|
||||||
|
FILE* ofp = stdout;
|
||||||
|
int ret = db->query(query, _rs, ofp);
|
||||||
|
}
|
||||||
|
for (int j = 0; j < b; j++)
|
||||||
|
{
|
||||||
|
int s = rand() % 10;
|
||||||
|
int p = rand() % 10;
|
||||||
|
int o = rand() % 10;
|
||||||
|
triple t(s, p, o);
|
||||||
|
std::set<triple>::iterator it = update_triples.find(t);
|
||||||
|
if (it != update_triples.end())
|
||||||
|
update_triples.erase(it);
|
||||||
|
string query = "DELETE DATA{" + t.subject + " " + t.predicate + " " + t.object + ".}";
|
||||||
|
ResultSet _rs;
|
||||||
|
FILE* ofp = stdout;
|
||||||
|
int ret = db->query(query, _rs, ofp);
|
||||||
|
}
|
||||||
|
db_triples.clear();
|
||||||
|
string query = "select ?s ?p ?o where{?s ?p ?o.}";
|
||||||
|
ResultSet _rs;
|
||||||
|
FILE* ofp = NULL;
|
||||||
|
int ret = db->query(query, _rs, ofp);
|
||||||
|
for (int i = 0; i < _rs.ansNum; i++)
|
||||||
|
{
|
||||||
|
string s = _rs.answer[i][0];
|
||||||
|
string p = _rs.answer[i][1];
|
||||||
|
string o = _rs.answer[i][2];
|
||||||
|
triple t(s, p, o);
|
||||||
|
db_triples.insert(t);
|
||||||
|
}
|
||||||
|
if (update_triples.size() != db_triples.size())
|
||||||
|
{
|
||||||
|
cout << "Update triples exist errors." << endl;
|
||||||
|
delete db;
|
||||||
|
db = NULL;
|
||||||
|
string cmd = "rm -r " + db_name + ".db";
|
||||||
|
system(cmd.c_str());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
std::set<triple>::iterator it1;
|
||||||
|
std::set<triple>::iterator it2;
|
||||||
|
for (it1 = update_triples.begin(), it2 = db_triples.begin(); it1 != update_triples.end(); it1++, it2++)
|
||||||
|
{
|
||||||
|
if (*it1 == *it2)
|
||||||
|
continue;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << "Update triples exist errors." << endl;
|
||||||
|
delete db;
|
||||||
|
db = NULL;
|
||||||
|
string cmd = "rm -r " + db_name + ".db";
|
||||||
|
system(cmd.c_str());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete db;
|
||||||
|
db = NULL;
|
||||||
|
string cmd = "rm -r " + db_name + ".db";
|
||||||
|
system(cmd.c_str());
|
||||||
|
cout << "Test passed!" << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue