From cda9e29e86d35a93f595104d1271da9b8313d257 Mon Sep 17 00:00:00 2001 From: Caesar11 Date: Thu, 28 May 2015 02:25:06 -0400 Subject: [PATCH] 1. fix "exit(0)" bug when open RDF file failed. 2. update api example code. author: hanshuo --- Database/Database.cpp | 44 ++++++++++++++++------------ README.md | 7 +++++ api/cpp/example/CppAPIExample.cpp | 16 +++++----- api/java/example/JavaAPIExample.java | 16 +++++----- main/gload.cpp | 10 ++++++- 5 files changed, 58 insertions(+), 35 deletions(-) diff --git a/Database/Database.cpp b/Database/Database.cpp index ad30399..a61fff5 100644 --- a/Database/Database.cpp +++ b/Database/Database.cpp @@ -221,6 +221,7 @@ bool Database::remove(const string& _rdf_file) bool Database::build(const string& _rdf_file) { long tv_build_begin = util::get_cur_time(); + bool flag = true; std::string store_path = this->name; util::create_dir(store_path); @@ -233,8 +234,14 @@ bool Database::build(const string& _rdf_file) cout << "begin encode RDF from : " << _rdf_file << " ..." << endl; // to be switched to new encodeRDF method. -// this->encodeRDF(_rdf_file); - this->encodeRDF_new(_rdf_file); +// flag = this->encodeRDF(_rdf_file); + flag = this->encodeRDF_new(_rdf_file); + if (!flag) + { + std::cout << "encode RDF failed."<< std::endl; + return false; + } + cout << "finish encode." << endl; std::string _entry_file = this->getSignatureBFile(); (this->kvstore)->open(); @@ -474,27 +481,27 @@ bool Database::encodeRDF(const string _rdf_file) bool Database::encodeRDF_new(const string _rdf_file) { Database::log("In encodeRDF_new"); + bool flag = true; + int ** _p_id_tuples = NULL; int _id_tuples_max = 0; /* map sub2id, pre2id, entity/literal in obj2id, store in kvstore, encode RDF data into signature */ - this->sub2id_pre2id_obj2id_RDFintoSignature(_rdf_file, _p_id_tuples, _id_tuples_max); - + flag = this->sub2id_pre2id_obj2id_RDFintoSignature(_rdf_file, _p_id_tuples, _id_tuples_max); + if (!flag) return false; /* map subid 2 objid_list & * subIDpreID 2 objid_list & * subID 2 _list */ - this->s2o_sp2o_s2po(_p_id_tuples, _id_tuples_max); - + flag = this->s2o_sp2o_s2po(_p_id_tuples, _id_tuples_max); + if (!flag) return false; /* map objid 2 subid_list & * objIDpreID 2 subid_list & * objID 2 _list */ - this->o2s_op2s_o2ps(_p_id_tuples, _id_tuples_max); + flag = this->o2s_op2s_o2ps(_p_id_tuples, _id_tuples_max); + if (!flag) return false; - bool flag = this->saveDBInfoFile(); - if (!flag) - { - return false; - } + flag = this->saveDBInfoFile(); + if (!flag) return false; Database::log("finish encodeRDF_new"); @@ -527,14 +534,14 @@ bool Database::sub2id_pre2id_obj2id_RDFintoSignature(const string _rdf_file, int ifstream _fin(_rdf_file.c_str()); if(!_fin){ cerr << "sub2id&pre2id&obj2id: Fail to open : " << _rdf_file << endl; - exit(0); + return false; } std::string _six_tuples_file = this->getSixTuplesFile(); std::ofstream _six_tuples_fout(_six_tuples_file.c_str()); if(! _six_tuples_fout){ cerr << "sub2id&pre2id&obj2id: Fail to open: " << _six_tuples_file << endl; - exit(0); + return false; } TripleWithObjType* triple_array = new TripleWithObjType[RDFParser::TRIPLE_NUM_PER_GROUP]; @@ -665,17 +672,18 @@ bool Database::sub2id_pre2id_obj2id_RDFintoSignature(const string _rdf_file, int //_entity_bitset is used up, double the space if (this->entity_num >= entitybitset_max) { - entitybitset_max *= 2; - EntityBitSet** _new_entity_bitset = new EntityBitSet* [entitybitset_max]; - memcpy(_new_entity_bitset, _entity_bitset, sizeof(EntityBitSet*) * this->entity_num); + EntityBitSet** _new_entity_bitset = new EntityBitSet* [entitybitset_max * 2]; + memcpy(_new_entity_bitset, _entity_bitset, sizeof(EntityBitSet*) * entitybitset_max); delete[] _entity_bitset; _entity_bitset = _new_entity_bitset; - for(int i = this->entity_num; i < entitybitset_max; i ++) + for(int i = entitybitset_max; i < entitybitset_max * 2; i ++) { _entity_bitset[i] = new EntityBitSet(); _entity_bitset[i] -> reset(); } + + entitybitset_max *= 2; } { diff --git a/README.md b/README.md index 5fa64b1..9aa4e32 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,13 @@ gclient is designed as a client to send commands and receive feedbacks. ip=127.0.0.1 port=3305 -> +You can also assign gserver's ip and port. + + [root@centos74 Gstore]# ./gclient 172.31.19.15 3307 + ip=172.31.19.15 port=3307 + -> + + We can use these following commands now: `->import db_name rdf_triple_file_name;` diff --git a/api/cpp/example/CppAPIExample.cpp b/api/cpp/example/CppAPIExample.cpp index fcad8cb..0153e0e 100644 --- a/api/cpp/example/CppAPIExample.cpp +++ b/api/cpp/example/CppAPIExample.cpp @@ -17,18 +17,18 @@ int main(int argc, char * argv[]) // build a new database by a RDF file. // note that the relative path is related to gserver. - gc.build("db_LUBM10", "example/rdf_triple/LUBM_10_GStore.n3"); + gc.build("db_LUBM10", "example/LUBM_10.n3"); // then you can execute SPARQL query on this database. std::string sparql = "select ?x where \ { \ - ?x rdf:type . \ - ?y ub:name . \ - ?x ub:takesCourse ?y. \ - ?z ub:teacherOf ?y. \ - ?z ub:name . \ - ?z ub:worksFor ?w. \ - ?w ub:name . \ + ?x . \ + ?y . \ + ?x ?y. \ + ?z ?y. \ + ?z . \ + ?z ?w. \ + ?w . \ }"; std::string answer = gc.query(sparql); std::cout << answer << std::endl; diff --git a/api/java/example/JavaAPIExample.java b/api/java/example/JavaAPIExample.java index 7d57988..1c7b1f9 100644 --- a/api/java/example/JavaAPIExample.java +++ b/api/java/example/JavaAPIExample.java @@ -17,18 +17,18 @@ public class JavaAPIExample // build a new database by a RDF file. // note that the relative path is related to gserver. - gc.build("db_LUBM10", "example/rdf_triple/LUBM_10_GStore.n3"); + gc.build("db_LUBM10", "example/LUBM_10.n3"); // then you can execute SPARQL query on this database. String sparql = "select ?x where " + "{" - + "?x rdf:type . " - + "?y ub:name . " - + "?x ub:takesCourse ?y. " - + "?z ub:teacherOf ?y. " - + "?z ub:name . " - + "?z ub:worksFor ?w. " - + "?w ub:name . " + + "?x . " + + "?y . " + + "?x ?y. " + + "?z ?y. " + + "?z . " + + "?z ?w. " + + "?w . " + "}"; String answer = gc.query(sparql); System.out.println(answer); diff --git a/main/gload.cpp b/main/gload.cpp index f5af4e5..89b10b1 100644 --- a/main/gload.cpp +++ b/main/gload.cpp @@ -25,7 +25,15 @@ int main(int argc, char * argv[]) string _db_path = string(argv[1]); string _rdf = string(argv[2]); Database _db(_db_path); - _db.build(_rdf); + bool flag = _db.build(_rdf); + if (flag) + { + cout << "import RDF file to database done." << endl; + } + else + { + cout << "import RDF file to database failed." << endl; + } system("clock"); return 0; }