feat: add monitor for Http server

not finished, just a start

by zengli, no changes to others
This commit is contained in:
bookug 2017-06-15 21:22:03 +08:00
parent 38575b16e6
commit 670093e42a
9 changed files with 517 additions and 711 deletions

View File

@ -731,54 +731,54 @@ Database::load_vstree(unsigned _vstree_size)
void
Database::check()
{
string tstr;
unsigned pid = this->kvstore->getIDByPredicate("<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>");
cout<<"check: pre "<<pid<<endl;
this->stringindex->randomAccess(pid, &tstr, false);
cout<<"string index: "<<tstr<<endl;
//cout<<"kvstore: "<<this->kvstore->getPredicateByID(pid)<<endl;
//string tstr;
//unsigned pid = this->kvstore->getIDByPredicate("<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>");
//cout<<"check: pre "<<pid<<endl;
//this->stringindex->randomAccess(pid, &tstr, false);
//cout<<"string index: "<<tstr<<endl;
////cout<<"kvstore: "<<this->kvstore->getPredicateByID(pid)<<endl;
unsigned sid = this->kvstore->getIDByEntity("<http://www.Department4.University1821.edu/Course50>");
cout<<"check: sub "<<sid<<endl;
this->stringindex->randomAccess(sid, &tstr, true);
cout<<"string index: "<<tstr<<endl;
cout<<"kvstore: "<<this->kvstore->getEntityByID(sid)<<endl;
//unsigned sid = this->kvstore->getIDByEntity("<http://www.Department4.University1821.edu/Course50>");
//cout<<"check: sub "<<sid<<endl;
//this->stringindex->randomAccess(sid, &tstr, true);
//cout<<"string index: "<<tstr<<endl;
//cout<<"kvstore: "<<this->kvstore->getEntityByID(sid)<<endl;
unsigned oid = this->kvstore->getIDByString("<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#Course>");
cout<<"check: obj "<<oid<<endl;
this->stringindex->randomAccess(oid, &tstr, true);
cout<<"string index: "<<tstr<<endl;
cout<<"kvstore: "<<this->kvstore->getEntityByID(oid)<<endl;
//unsigned oid = this->kvstore->getIDByString("<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#Course>");
//cout<<"check: obj "<<oid<<endl;
//this->stringindex->randomAccess(oid, &tstr, true);
//cout<<"string index: "<<tstr<<endl;
//cout<<"kvstore: "<<this->kvstore->getEntityByID(oid)<<endl;
unsigned* list = NULL; unsigned len = 0;
this->kvstore->getsubIDlistByobjIDpreID(oid, pid, list, len);
FILE* fp = fopen("kv.txt", "w");
for(unsigned i = 0; i < len; ++i)
{
fprintf(fp, "%u\n", list[i]);
string ts;
if(Util::is_literal_ele(list[i]))
ts = this->kvstore->getLiteralByID(list[i]);
else
ts = this->kvstore->getEntityByID(list[i]);
if(ts == "")
{
fprintf(fp, "Error in id2string\n");
}
else
{
fprintf(fp, "%s\n", ts.c_str());
}
this->stringindex->randomAccess(list[i], &tstr, true);
fprintf(fp, "string index: %s\n", ts.c_str());
}
this->stringindex->randomAccess(86006539, &tstr, true);
cout<<"check: 86006539 "<<tstr<<endl;
cout<<this->kvstore->getStringByID(86006539)<<endl;
this->stringindex->randomAccess(82855205, &tstr, true);
cout<<this->kvstore->getStringByID(82855205)<<endl;
cout<<"check: 82855205 "<<tstr<<endl;
fclose(fp);
//unsigned* list = NULL; unsigned len = 0;
//this->kvstore->getsubIDlistByobjIDpreID(oid, pid, list, len);
//FILE* fp = fopen("kv.txt", "w");
//for(unsigned i = 0; i < len; ++i)
//{
//fprintf(fp, "%u\n", list[i]);
//string ts;
//if(Util::is_literal_ele(list[i]))
//ts = this->kvstore->getLiteralByID(list[i]);
//else
//ts = this->kvstore->getEntityByID(list[i]);
//if(ts == "")
//{
//fprintf(fp, "Error in id2string\n");
//}
//else
//{
//fprintf(fp, "%s\n", ts.c_str());
//}
//this->stringindex->randomAccess(list[i], &tstr, true);
//fprintf(fp, "string index: %s\n", ts.c_str());
//}
//this->stringindex->randomAccess(86006539, &tstr, true);
//cout<<"check: 86006539 "<<tstr<<endl;
//cout<<this->kvstore->getStringByID(86006539)<<endl;
//this->stringindex->randomAccess(82855205, &tstr, true);
//cout<<this->kvstore->getStringByID(82855205)<<endl;
//cout<<"check: 82855205 "<<tstr<<endl;
//fclose(fp);
}
//NOTICE: we ensure that if the unload() exists normally, then all updates have already been written to disk
@ -841,6 +841,36 @@ Database::getName()
return this->name;
}
TYPE_TRIPLE_NUM
Database::getTripleNum()
{
return this->triples_num;
}
TYPE_ENTITY_LITERAL_ID
Database::getEntityNum()
{
return this->entity_num;
}
TYPE_ENTITY_LITERAL_ID
Database::getLiteralNum()
{
return this->literal_num;
}
TYPE_ENTITY_LITERAL_ID
Database::getSubNum()
{
return this->sub_num;
}
TYPE_PREDICATE_ID
Database::getPreNum()
{
return this->pre_num;
}
int
Database::query(const string _query, ResultSet& _result_set, FILE* _fp)
{
@ -1831,6 +1861,7 @@ Database::sub2id_pre2id_obj2id_RDFintoSignature(const string _rdf_file)
{
//_sub_id = this->entity_num;
_sub_id = this->allocEntityID();
this->sub_num++;
//this->entity_num++;
(this->kvstore)->setIDByEntity(_sub, _sub_id);
(this->kvstore)->setEntityByID(_sub_id, _sub);

View File

@ -66,6 +66,12 @@ public:
//name of this DB
string getName();
//get infos
TYPE_TRIPLE_NUM getTripleNum();
TYPE_ENTITY_LITERAL_ID getEntityNum();
TYPE_ENTITY_LITERAL_ID getLiteralNum();
TYPE_ENTITY_LITERAL_ID getSubNum();
TYPE_PREDICATE_ID getPreNum();
//root Path of this DB + sixTuplesFile
string getSixTuplesFile();
@ -86,6 +92,7 @@ private:
TYPE_TRIPLE_NUM triples_num;
TYPE_ENTITY_LITERAL_ID entity_num;
TYPE_ENTITY_LITERAL_ID sub_num;
//BETTER: add object num
TYPE_PREDICATE_ID pre_num;
TYPE_ENTITY_LITERAL_ID literal_num;

71
Main/HttpConnector.cpp → Main/ghttp.cpp Executable file → Normal file
View File

@ -1,19 +1,13 @@
/*=============================================================================
# Filename: ghttp.cpp
# Author: Bookug Lobert
# Mail: zengli-bookug@pku.edu.cn
# Last Modified: 2017-06-15 15:09
# Description: created by lvxin, improved by lijing
=============================================================================*/
#include "../Server/server_http.hpp"
#include "../Server/client_http.hpp"
//Added for the json-example
#define BOOST_SPIRIT_THREADSAFE
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
//Added for the default_resource example
#include <fstream>
#include <boost/filesystem.hpp>
//#include <boost/regex.hpp>
#include <vector>
#include <algorithm>
#include <memory>
//db
#include "../Database/Database.h"
#include "../Util/Util.h"
@ -30,6 +24,16 @@ void default_resource_send(const HttpServer &server, const shared_ptr<HttpServer
const shared_ptr<ifstream> &ifs);
Database *current_database = NULL;
int connection_num = 0;
//TODO+BETTER: port should be optional
//1. admin.html: build/load/query/unload
//2. index.html: only query (maybe load/unload if using multiple databases)
//3. ghttp: can add or not add a db as parameter
//BETTER: How about change HttpConnector into a console?
//TODO: we need a route
//JSON parser: http://www.tuicool.com/articles/yUJb6f
//(or use boost spirit to generate parser when compiling)
int main() {
Util util;
@ -222,6 +226,40 @@ int main() {
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << success.length() << "\r\n\r\n" << success;
return 0;
};
server.resource["^/monitor$"]["GET"]=[&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
if(current_database == NULL)
{
string error = "No database used now.";
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << error.length() << "\r\n\r\n" << error;
return 0;
}
//BETTER: use JSON format to send/receive messages
//C++ can not deal with JSON directly, JSON2string string2JSON
string success;
string name = current_database->getName();
success = success + "database: " + name + "\n";
TYPE_TRIPLE_NUM triple_num = current_database->getTripleNum();
success = success + "triple num: " + Util::int2string(triple_num) + "\n";
TYPE_ENTITY_LITERAL_ID entity_num = current_database->getEntityNum();
success = success + "entity num: " + Util::int2string(entity_num) + "\n";
TYPE_ENTITY_LITERAL_ID literal_num = current_database->getLiteralNum();
success = success + "literal num: " + Util::int2string(literal_num) + "\n";
TYPE_ENTITY_LITERAL_ID sub_num = current_database->getSubNum();
success = success + "subject num: " + Util::int2string(sub_num) + "\n";
TYPE_PREDICATE_ID pre_num = current_database->getPreNum();
success = success + "predicate num: " + Util::int2string(pre_num) + "\n";
//BETTER: how to compute the connection num in Boost::asio?
int conn_num = connection_num / 2;
//int conn_num = 3; //currectly connected sessions
success = success + "connection num: " + Util::int2string(conn_num) + "\n";
//TODO: add the info of memory and thread, operation num and IO frequency
//success = "<p>" + success + "</p>";
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << success.length() << "\r\n\r\n" << success;
return 0;
};
// server.resource["^/json$"]["POST"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
// try {
@ -245,6 +283,11 @@ int main() {
//Default file: index.html
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
server.default_resource["GET"]=[&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
//BETTER: use lock to ensure thread safe
connection_num++;
//NOTICE: it seems a visit will output twice times
//And different pages in a browser is viewed as two connections here
//cout<<"new connection"<<endl;
try {
auto web_root_path=boost::filesystem::canonical("./Server/web");
auto path=boost::filesystem::canonical(web_root_path/request->path);

332
Main/gmonitor.cpp Normal file
View File

@ -0,0 +1,332 @@
/*=============================================================================
# Filename: gmonitor.cpp
# Author: Bookug Lobert
# Mail: zengli-bookug@pku.edu.cn
# Last Modified: 2017-06-14 00:37
# Description:
=============================================================================*/
#include "../Database/Database.h"
#include "../Util/Util.h"
using namespace std;
//TODO: monitor can be implemented in any console
//NOTICE: no need to use this program, just use browser to visit
void
help()
{
printf("\
/*=============================================================================\n\
# Filename: gquery.cpp\n\
# Author: Bookug Lobert\n\
# Mail: 1181955272@qq.com\n\
# Last Modified: 2015-10-20 12:23\n\
# Description: query a database, there are several ways to use this program:\n\
1. ./gquery print the help message\n\
2. ./gquery --help simplified as -h, equal to 1\n\
3. ./gquery db_folder query_path load query from given path fro given database\n\
4. ./gquery db_folder load the given database and open console\n\
=============================================================================*/\n");
}
int
main(int argc, char * argv[])
{
//chdir(dirname(argv[0]));
//#ifdef DEBUG
Util util;
//#endif
//TODO: default ip and port
if (argc == 1 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)
{
help();
return 0;
}
cout << "gquery..." << endl;
if (argc < 2)
{
cout << "error: lack of DB_store to be queried" << endl;
return 0;
}
{
cout << "argc: " << argc << "\t";
cout << "DB_store:" << argv[1] << "\t";
cout << endl;
}
string db_folder = string(argv[1]);
int len = db_folder.length();
if(db_folder.substr(len-3, 3) == ".db")
{
cout<<"your database can not end with .db"<<endl;
return -1;
}
//if(db_folder[0] != '/' && db_folder[0] != '~') //using relative path
//{
//db_folder = string("../") + db_folder;
//}
Database _db(db_folder);
_db.load();
cout << "finish loading" << endl;
// read query from file.
if (argc >= 3)
{
// ifstream fin(argv[2]);
// if(!fin)
// {
// cout << "can not open: " << buf << endl;
// return 0;
// }
//
// memset(buf, 0, sizeof(buf));
// stringstream _ss;
// while(!fin.eof()){
// fin.getline(buf, 9999);
// _ss << buf << "\n";
// }
// fin.close();
//
// string query = _ss.str();
string query = string(argv[2]);
//if(query[0] != '/' && query[0] != '~') //using relative path
//{
//query = string("../") + query;
//}
query = Util::getQueryFromFile(query.c_str());
if (query.empty())
{
return 0;
}
printf("query is:\n%s\n\n", query.c_str());
ResultSet _rs;
FILE* ofp = stdout;
if (argc >= 4)
{
ofp = fopen(argv[3], "w");
}
string msg;
int ret = _db.query(query, _rs, ofp);
if(argc >= 4)
{
fclose(ofp);
ofp = NULL;
}
//cout<<"gquery ret: "<<ret<<endl;
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;
}
// read query file path from terminal.
// BETTER: sighandler ctrl+C/D/Z
string query;
//char resolved_path[PATH_MAX+1];
#ifdef READLINE_ON
char *buf, prompt[] = "gsql>";
//const int commands_num = 3;
//char commands[][20] = {"help", "quit", "sparql"};
printf("Type `help` for information of all commands\n");
printf("Type `help command_t` for detail of command_t\n");
rl_bind_key('\t', rl_complete);
while (true)
{
buf = readline(prompt);
if (buf == NULL)
continue;
else
add_history(buf);
if (strncmp(buf, "help", 4) == 0)
{
if (strcmp(buf, "help") == 0)
{
//print commands message
printf("help - print commands message\n");
printf("quit - quit the console normally\n");
printf("sparql - load query from the second argument\n");
}
else
{
//TODO: help for a given command
}
continue;
}
else if (strcmp(buf, "quit") == 0)
break;
else if (strncmp(buf, "sparql", 6) != 0)
{
printf("unknown commands\n");
continue;
}
//TODO: sparql + string, not only path
string query_file;
//BETTER:build a parser for this console
bool ifredirect = false;
char* rp = buf;
int pos = strlen(buf) - 1;
while (pos > -1)
{
if (*(rp + pos) == '>')
{
ifredirect = true;
break;
}
pos--;
}
rp += pos;
char* p = buf + strlen(buf) - 1;
FILE* fp = stdout; ///default to output on screen
if (ifredirect)
{
char* tp = p;
while (*tp == ' ' || *tp == '\t')
tp--;
*(tp + 1) = '\0';
tp = rp + 1;
while (*tp == ' ' || *tp == '\t')
tp++;
fp = fopen(tp, "w"); //NOTICE:not judge here!
p = rp - 1; //NOTICE: all separated with ' ' or '\t'
}
while (*p == ' ' || *p == '\t') //set the end of path
p--;
*(p + 1) = '\0';
p = buf + 6;
while (*p == ' ' || *p == '\t') //acquire the start of path
p++;
//TODO: support the soft links(or hard links)
//there are also readlink and getcwd functions for help
//http://linux.die.net/man/2/readlink
//NOTICE:getcwd and realpath cannot acquire the real path of file
//in the same directory and the program is executing when the
//system starts running
//NOTICE: use realpath(p, NULL) is ok, but need to free the memory
char* q = realpath(p, NULL); //QUERY:still not work for soft links
#ifdef DEBUG_PRECISE
printf("%s\n", p);
#endif
if (q == NULL)
{
printf("invalid path!\n");
free(q);
free(buf);
continue;
}
else
printf("%s\n", q);
//query = getQueryFromFile(p);
query = Util::getQueryFromFile(q);
if (query.empty())
{
free(q);
//free(resolved_path);
free(buf);
if (ifredirect)
fclose(fp);
continue;
}
printf("query is:\n");
printf("%s\n\n", query.c_str());
ResultSet _rs;
int ret = _db.query(query, _rs, fp);
//int ret = _db.query(query, _rs, NULL);
string msg;
//cout<<"gquery ret: "<<ret<<endl;
if (ret <= -100) //select query
{
if(ret == -100)
{
msg = "";
}
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;
}
//test...
//string answer_file = query_file+".out";
//Util::save_to_file(answer_file.c_str(), _rs.to_str());
free(q);
//free(resolved_path);
free(buf);
if (ifredirect)
fclose(fp);
#ifdef DEBUG_PRECISE
printf("after buf freed!\n");
#endif
}
//#else //DEBUG:this not work!
// while(true)
// {
// cout << "please input query file path:" << endl;
// string query_file;
// cin >> query_file;
// //char* q = realpath(query_file.c_str(), NULL);
// string query = getQueryFromFile(query_file.c_str());
// if(query.empty())
// {
// //free(resolved_path);
// continue;
// }
// cout << "query is:" << endl;
// cout << query << endl << endl;
// ResultSet _rs;
// _db.query(query, _rs, stdout);
// //free(resolved_path);
// }
#endif // READLINE_ON
return 0;
}

View File

@ -1,626 +0,0 @@
/*=============================================================================
# Filename: Server.cpp
# Author: Bookug Lobert
# Mail: 1181955272@qq.com
# Last Modified: 2015-10-25 13:47
# Description:
=============================================================================*/
#include "Server.h"
using namespace std;
Server::Server(unsigned short _port)
{
this->connectionPort = _port;
this->connectionMaxNum = Socket::MAX_CONNECTIONS;
this->databaseMaxNum = 1; // will be updated when supporting multiple databases.
this->database = NULL;
this->next_backup = 0;
this->scheduler_pid = 0;
}
Server::~Server()
{
delete this->database;
}
bool
Server::createConnection()
{
bool flag;
flag = this->socket.create();
if (!flag)
{
cerr << Util::getTimeString() << "cannot create socket. @Server::createConnection" << endl;
return false;
}
flag = this->socket.bind(this->connectionPort);
if (!flag)
{
cerr << Util::getTimeString() << "cannot bind to port " << this->connectionPort << ". @Server::createConnection" << endl;
return false;
}
flag = this->socket.listen();
if (!flag)
{
cerr << Util::getTimeString() << "cannot listen to port" << this->connectionPort << ". @Server::createConnection" << endl;
return false;
}
return true;
}
bool
Server::deleteConnection()
{
bool flag = this->socket.close();
return flag;
}
bool
Server::response(Socket _socket, std::string& _msg)
{
bool flag = _socket.send(_msg);
return flag;
}
void
Server::listen()
{
while (true)
{
Socket new_server_socket;
cout << Util::getTimeString() << "Wait for input..." << endl;
this->socket.accept(new_server_socket);
cout << Util::getTimeString() << "accept new socket." << endl;
string recv_cmd;
bool recv_return = new_server_socket.recv(recv_cmd);
if (!recv_return)
{
cerr << Util::getTimeString() << "receive command from client error. @Server::listen" << endl;
continue;
}
cout << Util::getTimeString() << "received msg: " << recv_cmd << endl;
Operation operation;
bool parser_return = this->parser(recv_cmd, operation);
cout << Util::getTimeString() << "parser_return=" << parser_return << endl; //debug
if (!parser_return)
{
cout << Util::getTimeString() << "parser command error. @Server::listen" << endl;
string ret_msg = "invalid command.";
this->response(new_server_socket, ret_msg);
new_server_socket.close();
continue;
}
string ret_msg;
bool _stop = false;
CommandType cmd_type = operation.getCommand();
switch (cmd_type)
{
case CMD_TEST:
{
ret_msg = "OK";
break;
}
case CMD_LOAD:
{
string db_name = operation.getParameter(0);
this->loadDatabase(db_name, "", ret_msg);
break;
}
case CMD_UNLOAD:
{
string db_name = operation.getParameter(0);
this->unloadDatabase(db_name, "", ret_msg);
break;
}
case CMD_IMPORT:
{
string db_name = operation.getParameter(0);
string rdf_path = operation.getParameter(1);
this->importRDF(db_name, "", rdf_path, ret_msg);
break;
}
case CMD_DROP:
{
string db_name = operation.getParameter(0);
this->dropDatabase(db_name, "", ret_msg);
break;
}
case CMD_QUERY:
{
string query = operation.getParameter(0);
pthread_t timer = Server::start_timer();
if (timer == 0) {
cerr << Util::getTimeString() << "Failed to start timer." << endl;
}
this->query(query, ret_msg);
if (timer != 0 && !Server::stop_timer(timer)) {
cerr << Util::getTimeString() << "Failed to stop timer." << endl;
}
break;
}
case CMD_SHOW:
{
string para = operation.getParameter(0);
if (para == "databases" || para == "all")
{
this->showDatabases(para, "", ret_msg);
}
else
{
ret_msg = "invalid command.";
}
break;
}
//case CMD_INSERT:
//{
// string db_name = operation.getParameter(0);
// string rdf_path = operation.getParameter(1);
// this->insertTriple(db_name, "", rdf_path, ret_msg);
// break;
//}
case CMD_STOP:
{
this->stopServer(ret_msg);
_stop = true;
break;
}
case CMD_BACKUP: {
string para = operation.getParameter(0);
stringstream ss(para);
long time_backup;
ss >> time_backup;
if (this->next_backup == 0) {
break;
}
while (this->next_backup < time_backup) {
this->next_backup += Util::gserver_backup_interval;
}
if (this->next_backup == time_backup) {
this->backup(ret_msg);
}
else {
ret_msg = "done";
}
break;
}
default:
cerr << Util::getTimeString() << "this command is not supported by now. @Server::listen" << endl;
}
this->response(new_server_socket, ret_msg);
new_server_socket.close();
if (_stop) {
this->deleteConnection();
cout << Util::getTimeString() << "server stopped." << endl;
break;
}
if (this->next_backup > 0) {
time_t cur_time = time(NULL);
if (cur_time >= this->next_backup) {
string str;
this->backup(str);
}
}
}
}
bool
Server::parser(std::string _raw_cmd, Operation& _ret_oprt)
{
int cmd_start_pos = 0;
int raw_len = (int)_raw_cmd.size();
for (int i = 0; i < raw_len; i++) {
if (_raw_cmd[i] == '\n') {
_raw_cmd[i] = ' ';
}
}
while (cmd_start_pos < raw_len && _raw_cmd[cmd_start_pos] == ' ') {
cmd_start_pos++;
}
if (cmd_start_pos == raw_len)
{
return false;
}
int idx1 = raw_len;
for (int i = cmd_start_pos; i < raw_len; i++) {
if (_raw_cmd[i] == ' ')
{
idx1 = i;
break;
}
}
string cmd = _raw_cmd.substr(cmd_start_pos, idx1 - cmd_start_pos);
int para_start_pos = idx1;
while (para_start_pos < raw_len && _raw_cmd[para_start_pos] == ' ')
para_start_pos++;
int para_cnt;
if (cmd == "test") {
_ret_oprt.setCommand(CMD_TEST);
para_cnt = 0;
}
else if (cmd == "load") {
_ret_oprt.setCommand(CMD_LOAD);
para_cnt = 1;
}
else if (cmd == "unload") {
_ret_oprt.setCommand(CMD_UNLOAD);
para_cnt = 1;
}
else if (cmd == "import")
{
_ret_oprt.setCommand(CMD_IMPORT);
para_cnt = 2;
}
else if (cmd == "query")
{
_ret_oprt.setCommand(CMD_QUERY);
para_cnt = 1;
}
else if (cmd == "show")
{
_ret_oprt.setCommand(CMD_SHOW);
para_cnt = 1;
}
else if (cmd == "insert")
{
_ret_oprt.setCommand(CMD_INSERT);
para_cnt = 2;
}
else if (cmd == "drop") {
_ret_oprt.setCommand(CMD_DROP);
para_cnt = 1;
}
else if (cmd == "stop") {
_ret_oprt.setCommand(CMD_STOP);
para_cnt = 0;
}
else if (cmd == "backup") {
_ret_oprt.setCommand(CMD_BACKUP);
para_cnt = 1;
}
else
{
return false;
}
vector<string> paras;
int cur_idx = para_start_pos;
for (int i = 1; i <= para_cnt; i++)
{
if (cur_idx >= raw_len)
{
return false;
}
int next_idx = raw_len;
if (i < para_cnt)
{
for (int j = cur_idx; j<raw_len; j++)
if (_raw_cmd[j] == ' ')
{
next_idx = j;
break;
}
}
else
{
for (int j = raw_len - 1; j>cur_idx; j--)
if (_raw_cmd[j] != ' ')
{
next_idx = j + 1;
break;
}
}
paras.push_back(_raw_cmd.substr(cur_idx, next_idx - cur_idx));
cur_idx = next_idx;
while (cur_idx < raw_len && _raw_cmd[cur_idx] == ' ')
cur_idx++;
}
if (cur_idx != raw_len)
{
return false;
}
_ret_oprt.setParameter(paras);
return true;
}
bool
Server::createDatabase(std::string _db_name, std::string _ac_name, std::string& _ret_msg)
{
// to be implemented...
return false;
}
bool
Server::dropDatabase(std::string _db_name, std::string _ac_name, std::string& _ret_msg)
{
if (this->database != NULL) {
_ret_msg = "please do not use this command when you are using a database.";
return false;
}
size_t length = _db_name.length();
if (length < 3 || _db_name.substr(length - 3, 3) != ".db") {
_ret_msg = "you can only drop databases whose names end with \".db\"";
return false;
}
std::string cmd = std::string("rm -rf ") + _db_name;
int ret = system(cmd.c_str());
if (ret == 0) {
_ret_msg = "drop database done.";
return true;
}
else {
_ret_msg = "drop database failed.";
return false;
}
}
bool
Server::loadDatabase(std::string _db_name, std::string _ac_name, std::string& _ret_msg)
{
if (this->database == NULL)
{
this->database = new Database(_db_name);
}
else
{
_ret_msg = "please unload the current db first: " + this->database->getName();
return false;
}
if (!this->database->load()) {
_ret_msg = "load database failed.";
delete this->database;
this->database = NULL;
return false;
}
pid_t fpid = vfork();
// child, scheduler
if (fpid == 0) {
time_t cur_time = time(NULL);
long time_backup = Util::read_backup_time();
long first_backup = cur_time - (cur_time - time_backup) % Util::gserver_backup_interval
+ Util::gserver_backup_interval;
this->next_backup = first_backup;
string s_port = Util::int2string(this->connectionPort);
string s_next_backup = Util::int2string(first_backup);
execl("bin/gserver_backup_scheduler", "gserver_backup_scheduler", s_port.c_str(), s_next_backup.c_str(), NULL);
exit(0);
return true;
}
// parent
if (fpid > 0) {
this->scheduler_pid = fpid;
}
// fork failure
else if (fpid < 0) {
cerr << Util::getTimeString() << "Database will not be backed-up automatically." << endl;
}
_ret_msg = "load database done.";
return true;
}
bool
Server::unloadDatabase(std::string _db_name, std::string _ac_name, std::string& _ret_msg)
{
if (this->database == NULL || this->database->getName() != _db_name)
{
_ret_msg = "database:" + _db_name + " is not loaded.";
return false;
}
delete this->database;
this->database = NULL;
_ret_msg = "unload database done.";
this->next_backup = 0;
kill(this->scheduler_pid, SIGTERM);
waitpid(this->scheduler_pid, NULL, 0);
this->scheduler_pid = 0;
return true;
}
bool
Server::importRDF(std::string _db_name, std::string _ac_name, std::string _rdf_path, std::string& _ret_msg)
{
//if (this->database != NULL && this->database->getName() != _db_name)
if (this->database != NULL)
{
//delete this->database;
//NOTICE:if there is a db loaded, we should not build directly, tell user to unload it first
_ret_msg = "please unload the current db first: " + this->database->getName();
return false;
}
this->database = new Database(_db_name);
bool flag = this->database->build(_rdf_path);
delete this->database;
this->database = NULL;
if (flag)
{
_ret_msg = "import RDF file to database done.";
}
else
{
_ret_msg = "import RDF file to database failed.";
}
return flag;
}
//bool
//Server::insertTriple(std::string _db_name, std::string _ac_name, std::string _rdf_path, std::string& _ret_msg)
//{
// if (this->database != NULL)
// {
// this->database->unload();
// delete this->database;
// }
// this->database = new Database(_db_name);
// bool flag = this->database->insert(_rdf_path);
// if (flag)
// {
// _ret_msg = "insert triple file to database done.";
// }
// else
// {
// _ret_msg = "import triple file to database failed.";
// }
// return flag;
//}
bool
Server::query(const string _query, string& _ret_msg)
{
cout << Util::getTimeString() << "Server query(): " << _query << endl;
if (this->database == NULL)
{
_ret_msg = "database has not been loaded.";
return false;
}
FILE* output = NULL;
#ifdef OUTPUT_QUERY_RESULT
string path = "logs/gserver_query.log";
output = fopen(path.c_str(), "w");
#endif
ResultSet res_set;
int query_ret = this->database->query(_query, res_set, output);
if (output != NULL)
{
fclose(output);
}
bool flag = true;
//cout<<"Server query ret: "<<query_ret<<endl;
if (query_ret <= -100) //select query
{
//_ret_msg = "results are too large!";
//BETTER: divide and transfer if too large to be placed in memory, using Stream
if (query_ret == -100)
{
_ret_msg = res_set.to_str();
}
else //query error
{
flag = false;
_ret_msg = "query failed.";
//BETTER: {type:select} {success:false}
}
}
else //update query
{
if (query_ret >= 0)
{
_ret_msg = "update num: " + Util::int2string(query_ret);
}
else //update error
{
flag = false;
_ret_msg = "update failed.";
}
}
return flag;
}
bool
Server::showDatabases(string _para, string _ac_name, string& _ret_msg)
{
if (_para == "all")
{
_ret_msg = Util::getItemsFromDir(Util::db_home);
return true;
}
if (this->database != NULL)
{
_ret_msg = "\n" + this->database->getName() + "\n";
}
else
{
_ret_msg = "\n[empty]\n";
}
return true;
}
bool Server::stopServer(string& _ret_msg) {
if (this->database != NULL) {
delete this->database;
this->database = NULL;
}
_ret_msg = "server stopped.";
return true;
}
bool Server::backup(string& _ret_msg) {
this->next_backup += Util::gserver_backup_interval;
if (this->database == NULL) {
_ret_msg = "No database in use.";
return false;
}
if (!this->database->backup()) {
_ret_msg = "Backup failed.";
return false;
}
_ret_msg = "done";
return true;
}
pthread_t Server::start_timer() {
pthread_t timer_thread;
if (pthread_create(&timer_thread, NULL, Server::timer, NULL) == 0) {
return timer_thread;
}
return 0;
}
bool Server::stop_timer(pthread_t _timer) {
return pthread_kill(_timer, SIGTERM) == 0;
}
void* Server::timer(void* _args) {
signal(SIGTERM, Server::timer_sigterm_handler);
sleep(Util::gserver_query_timeout);
cerr << Util::getTimeString() << "Query out of time." << endl;
abort();
}
void Server::timer_sigterm_handler(int _signal_num) {
pthread_exit(0);
}

View File

@ -1,16 +1,7 @@
#ifndef CLIENT_HTTP_HPP
#define CLIENT_HTTP_HPP
#include <boost/asio.hpp>
#include <boost/utility/string_ref.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/functional/hash.hpp>
#include <unordered_map>
#include <map>
#include <random>
#include <mutex>
#include <type_traits>
#include "../Util/Util.h"
#ifndef CASE_INSENSITIVE_EQUALS_AND_HASH
#define CASE_INSENSITIVE_EQUALS_AND_HASH

View File

@ -1,17 +1,7 @@
#ifndef SERVER_HTTP_HPP
#define SERVER_HTTP_HPP
#include <boost/asio.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/functional/hash.hpp>
#include <map>
#include <unordered_map>
#include <thread>
#include <functional>
#include <iostream>
#include <sstream>
#include <memory>
#include "../Util/Util.h"
#ifndef CASE_INSENSITIVE_EQUALS_AND_HASH
#define CASE_INSENSITIVE_EQUALS_AND_HASH

View File

@ -77,6 +77,37 @@ in the sparql query can point to the same node in data graph)
#include <readline/readline.h>
#include <readline/history.h>
//Below are for boost
//Added for the json-example
#define BOOST_SPIRIT_THREADSAFE
//#include <boost/spirit.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
//Added for the default_resource example
#include <boost/filesystem.hpp>
//#include <boost/regex.hpp>
//#include <boost/thread/thread.hpp>
//#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/utility/string_ref.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/functional/hash.hpp>
#include <unordered_map>
#include <random>
#include <type_traits>
//NOTICE: hpp is different from static library(*.a) or dynamic library(*.so)
//It places the implementations totally in header file, hpp = *.h + *.cpp
//NOTICE: use below to forbid the warnings in third-part library
//#pragma warning(push)
//#pragma warning(disable:4009)
//#include <***>
//#pragma warning(pop)
//===================================================================================================================
//if used as only-read application(like sparql endpoint)
#define ONLY_READ 1
//if use pthread and lock
@ -148,6 +179,8 @@ in the sparql query can point to the same node in data graph)
#define xfree(x) free(x); x = NULL;
//===================================================================================================================
//NOTICE:include Util.h and below in each main function
//(the beginning position)
//#ifdef DEBUG
@ -164,6 +197,7 @@ typedef unsigned(*HashFunction)(const char*);
//type for the triple num
//NOTICE: this should use unsigned (triple num may > 2500000000)
typedef unsigned TYPE_TRIPLE_NUM;
//TODO: use long if need to run 5B dataset
//type for entity/literal ID
typedef unsigned TYPE_ENTITY_LITERAL_ID;
@ -201,6 +235,8 @@ typedef struct TYPE_ID_TUPLE
TYPE_ENTITY_LITERAL_ID objid;
}ID_TUPLE;
//===================================================================================================================
/******** all static&universal constants and fucntions ********/
class Util
{
@ -361,6 +397,8 @@ private:
static bool isValidIPV6(std::string);
};
//===================================================================================================================
class BlockInfo
{
public:

View File

@ -109,7 +109,7 @@ inc = -I./tools/libantlr3c-3.4/ -I./tools/libantlr3c-3.4/include
#gtest
TARGET = $(exedir)gbuild $(exedir)gserver $(exedir)gserver_backup_scheduler $(exedir)gclient $(exedir)gquery $(exedir)gconsole $(api_java) $(exedir)gadd $(exedir)gsub $(exedir)HttpConnector
TARGET = $(exedir)gbuild $(exedir)gserver $(exedir)gserver_backup_scheduler $(exedir)gclient $(exedir)gquery $(exedir)gconsole $(api_java) $(exedir)gadd $(exedir)gsub $(exedir)ghttp
all: $(TARGET)
@ -140,8 +140,8 @@ $(exedir)gclient: $(lib_antlr) $(objdir)gclient.o $(objfile)
$(exedir)gconsole: $(lib_antlr) $(objdir)gconsole.o $(objfile) $(api_cpp)
$(CC) $(EXEFLAG) -o $(exedir)gconsole $(objdir)gconsole.o $(objfile) $(library) -L./api/cpp/lib -lgstoreconnector
$(exedir)HttpConnector: $(lib_antlr) $(objdir)HttpConnector.o ./Server/server_http.hpp ./Server/client_http.hpp $(objfile)
$(CC) $(EXEFLAG) -o $(exedir)HttpConnector $(objdir)HttpConnector.o $(objfile) $(library) $(inc) -DUSE_BOOST_REGEX
$(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
#executables end
@ -168,8 +168,8 @@ $(objdir)gclient.o: Main/gclient.cpp Server/Client.h Util/Util.h $(lib_antlr)
$(objdir)gconsole.o: Main/gconsole.cpp Database/Database.h Util/Util.h api/cpp/src/GstoreConnector.h $(lib_antlr)
$(CC) $(CFLAGS) Main/gconsole.cpp $(inc) -o $(objdir)gconsole.o -I./api/cpp/src/ #-DREADLINE_ON
$(objdir)HttpConnector.o: Main/HttpConnector.cpp Server/server_http.hpp Server/client_http.hpp Database/Database.h Util/Util.h $(lib_antlr)
$(CC) $(CFLAGS) Main/HttpConnector.cpp $(inc) -o $(objdir)HttpConnector.o -DUSE_BOOST_REGEX
$(objdir)ghttp.o: Main/ghttp.cpp Server/server_http.hpp Server/client_http.hpp Database/Database.h Util/Util.h $(lib_antlr)
$(CC) $(CFLAGS) Main/ghttp.cpp $(inc) -o $(objdir)ghttp.o -DUSE_BOOST_REGEX
#objects in Main/ end