feat: move old api to api/socket and add new api in api/http;
old api is mjust for socket(bin/gserver), new api support HTTP(bin/ghttp) only java is well dealed in new api, other languages waiting by zengli
This commit is contained in:
parent
b6296edb9c
commit
61a28de125
|
@ -46,6 +46,34 @@ int connection_num = 0;
|
|||
//TODO: control the authority, check it if requesting for build/load/unload
|
||||
//for sparql endpoint, just load database when starting, and comment out all functions except for query()
|
||||
|
||||
//REFERENCE: C++ URL encoder and decoder
|
||||
//http://blog.csdn.net/nanjunxiao/article/details/9974593
|
||||
string UrlDecode(string& SRC)
|
||||
{
|
||||
string ret;
|
||||
char ch;
|
||||
int ii;
|
||||
for(size_t i = 0; i < SRC.length(); ++i)
|
||||
{
|
||||
if(int(SRC[i]) == 37)
|
||||
{
|
||||
sscanf(SRC.substr(i+1,2).c_str(), "%x", &ii);
|
||||
ch = static_cast<char>(ii);
|
||||
ret += ch;
|
||||
i = i + 2;
|
||||
}
|
||||
else if(SRC[i] == '+')
|
||||
{
|
||||
ret += ' ';
|
||||
}
|
||||
else
|
||||
{
|
||||
ret += SRC[i];
|
||||
}
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int main() {
|
||||
Util util;
|
||||
//HTTP-server at port 9000 using 1 thread
|
||||
|
@ -167,8 +195,12 @@ int main() {
|
|||
//GET-example for the path /query/[query_file_path], responds with the matched string in path
|
||||
//For instance a request GET /query/db123 will receive: db123
|
||||
server.resource["^/query/(.*)$"]["GET"] = [&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
|
||||
//TODO: add a format parameter, better to change tp "?format=json"
|
||||
string format = "json";
|
||||
cout<<"HTTP: this is query"<<endl;
|
||||
string db_query=request->path_match[1];
|
||||
db_query = UrlDecode(db_query);
|
||||
cout<<"check: "<<db_query<<endl;
|
||||
string str = db_query;
|
||||
|
||||
if(current_database == NULL)
|
||||
|
@ -183,6 +215,7 @@ int main() {
|
|||
if(db_query[0]=='\"')
|
||||
{
|
||||
sparql = db_query.substr(1, db_query.length()-2);
|
||||
cout<<"check: this is string "<<sparql<<endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -195,6 +228,7 @@ int main() {
|
|||
return 0;
|
||||
}
|
||||
sparql = Util::getQueryFromFile(path);
|
||||
cout<<"check: this is path "<<sparql<<endl;
|
||||
}
|
||||
|
||||
if (sparql.empty()) {
|
||||
|
@ -211,7 +245,15 @@ int main() {
|
|||
{
|
||||
//TODO: if the result is too large? or if the result is placed in Stream?
|
||||
//Should use file donload
|
||||
string success = rs.to_str();
|
||||
string success = "";
|
||||
if(format == "json")
|
||||
{
|
||||
success = rs.to_JSON();
|
||||
}
|
||||
else
|
||||
{
|
||||
success = rs.to_str();
|
||||
}
|
||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << success.length() << "\r\n\r\n" << success;
|
||||
return 0;
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,50 @@
|
|||
/*=============================================================================
|
||||
# Filename: GstoreConnector.h
|
||||
# Author: Bookug Lobert
|
||||
# Mail: 1181955272@qq.com
|
||||
# Last Modified: 2016-02-21 21:22
|
||||
# Description: originally written by hanshuo, modified by zengli
|
||||
=============================================================================*/
|
||||
|
||||
#ifndef _GSTORECONNECTOR_H
|
||||
#define _GSTORECONNECTOR_H
|
||||
|
||||
#include "../../../../Server/Socket.h"
|
||||
#include <cstring>
|
||||
|
||||
//TODO: C++ URL encoder
|
||||
//http://blog.csdn.net/nanjunxiao/article/details/9974593
|
||||
//TODO: for a string, we should add \"\" in the head and tail, while for a path not do so
|
||||
|
||||
class GstoreConnector
|
||||
{
|
||||
public:
|
||||
GstoreConnector();
|
||||
GstoreConnector(std::string _ip);
|
||||
GstoreConnector(unsigned short _port);
|
||||
GstoreConnector(std::string _ip, unsigned short _port);
|
||||
~GstoreConnector();
|
||||
|
||||
bool test();
|
||||
bool load(std::string _db_name);
|
||||
bool unload(std::string _db_name);
|
||||
bool build(std::string _db_name, std::string _rdf_file_path);
|
||||
bool drop(std::string _db_name);
|
||||
std::string query(std::string _sparql, std::string _output = "/");
|
||||
std::string show(bool _type = false); //show current or all databases
|
||||
|
||||
static const std::string defaultServerIP;
|
||||
static const unsigned short defaultServerPort;
|
||||
|
||||
private:
|
||||
std::string serverIP;
|
||||
unsigned short serverPort;
|
||||
// std::string username;
|
||||
// std::string password;
|
||||
Socket socket;
|
||||
|
||||
bool connect();
|
||||
bool disconnect();
|
||||
};
|
||||
|
||||
#endif // _GSTORECONNECTOR_H
|
|
@ -2,7 +2,7 @@
|
|||
CC=ccache g++ -std=c++11
|
||||
|
||||
lib_dir=../lib/
|
||||
socket_obj_dir=../../../.objs/
|
||||
socket_obj_dir=../../../../.objs/
|
||||
|
||||
all: $(lib_dir)libgstoreconnector.a
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* JavaAPIExample.java
|
||||
*
|
||||
* Created on: 2014-11-4
|
||||
* Author: zengli
|
||||
*/
|
||||
|
||||
import jgsc.GstoreConnector;
|
||||
|
||||
// before run this example, you must start up the GStore server at first (use command ./gserver).
|
||||
public class JavaAPIExample
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// initialize the GStore server's IP address and port.
|
||||
GstoreConnector gc = new GstoreConnector("127.0.0.1", 9000);
|
||||
|
||||
// build a new database by a RDF file.
|
||||
// note that the relative path is related to gserver.
|
||||
gc.build("LUBM10", "data/LUBM_10.n3");
|
||||
gc.load("LUBM10");
|
||||
|
||||
// then you can execute SPARQL query on this database.
|
||||
String sparql = "select ?x where "
|
||||
+ "{"
|
||||
+ "?x <rdf:type> <ub:UndergraduateStudent>. "
|
||||
+ "?y <ub:name> <Course1>. "
|
||||
+ "?x <ub:takesCourse> ?y. "
|
||||
+ "?z <ub:teacherOf> ?y. "
|
||||
+ "?z <ub:name> <FullProfessor1>. "
|
||||
+ "?z <ub:worksFor> ?w. "
|
||||
+ "?w <ub:name> <Department0>. "
|
||||
+ "}";
|
||||
String answer = gc.query(sparql);
|
||||
System.out.println(answer);
|
||||
|
||||
// unload this database.
|
||||
gc.unload("LUBM10");
|
||||
|
||||
// also, you can load some exist database directly and then query.
|
||||
gc.load("LUBM10");
|
||||
answer = gc.query(sparql);
|
||||
System.out.println(answer);
|
||||
gc.unload("LUBM10");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,297 @@
|
|||
package jgsc;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.URLDecoder;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class GstoreConnector {
|
||||
|
||||
public static final String defaultServerIP = "127.0.0.1";
|
||||
public static final int defaultServerPort = 3305;
|
||||
|
||||
private String serverIP;
|
||||
private int serverPort;
|
||||
//private Socket socket = null;
|
||||
|
||||
public GstoreConnector() {
|
||||
this.serverIP = GstoreConnector.defaultServerIP;
|
||||
this.serverPort = GstoreConnector.defaultServerPort;
|
||||
}
|
||||
|
||||
public GstoreConnector(int _port) {
|
||||
this.serverIP = GstoreConnector.defaultServerIP;
|
||||
this.serverPort = _port;
|
||||
}
|
||||
|
||||
public GstoreConnector(String _ip, int _port) {
|
||||
this.serverIP = _ip;
|
||||
this.serverPort = _port;
|
||||
}
|
||||
|
||||
public String sendGet(String param) {
|
||||
String url = "http://" + this.serverIP + ":" + this.serverPort;
|
||||
String result = "";
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
String urlNameString = url + "/" + param;
|
||||
System.out.println("request: "+urlNameString);
|
||||
URL realUrl = new URL(urlNameString);
|
||||
// 打开和URL之间的连接
|
||||
URLConnection connection = realUrl.openConnection();
|
||||
// 设置通用的请求属性
|
||||
connection.setRequestProperty("accept", "*/*");
|
||||
connection.setRequestProperty("connection", "Keep-Alive");
|
||||
connection.setRequestProperty("user-agent",
|
||||
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
// 建立实际的连接
|
||||
connection.connect();
|
||||
// 获取所有响应头字段
|
||||
Map<String, List<String>> map = connection.getHeaderFields();
|
||||
// 遍历所有的响应头字段
|
||||
for (String key : map.keySet()) {
|
||||
System.out.println(key + "--->" + map.get(key));
|
||||
}
|
||||
// 定义 BufferedReader输入流来读取URL的响应
|
||||
in = new BufferedReader(new InputStreamReader(
|
||||
connection.getInputStream()));
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
result += line;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("error in get request: " + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 使用finally块来关闭输入流
|
||||
finally {
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
} catch (Exception e2) {
|
||||
e2.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//NOTICE: no need to connect now, HTTP connection is kept by default
|
||||
public boolean load(String _db_name) {
|
||||
boolean connect_return = this.connect();
|
||||
if (!connect_return) {
|
||||
System.err.println("connect to server error. @GstoreConnector.load");
|
||||
return false;
|
||||
}
|
||||
|
||||
String cmd = "load/" + _db_name;
|
||||
String msg = this.sendGet(cmd);
|
||||
//if (!send_return) {
|
||||
//System.err.println("send load command error. @GstoreConnector.load");
|
||||
//return false;
|
||||
//}
|
||||
|
||||
this.disconnect();
|
||||
|
||||
System.out.println(msg);
|
||||
if (msg.equals("load database done.")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean unload(String _db_name) {
|
||||
boolean connect_return = this.connect();
|
||||
if (!connect_return) {
|
||||
System.err.println("connect to server error. @GstoreConnector.unload");
|
||||
return false;
|
||||
}
|
||||
|
||||
//String cmd = "unload/" + _db_name;
|
||||
String cmd = "unload";
|
||||
String msg = this.sendGet(cmd);
|
||||
|
||||
this.disconnect();
|
||||
|
||||
System.out.println(msg);
|
||||
if (msg.equals("unload database done.")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean build(String _db_name, String _rdf_file_path) {
|
||||
boolean connect_return = this.connect();
|
||||
if (!connect_return) {
|
||||
System.err.println("connect to server error. @GstoreConnector.build");
|
||||
return false;
|
||||
}
|
||||
|
||||
String cmd = "build/" + _db_name + "/" + _rdf_file_path;
|
||||
String msg = this.sendGet(cmd);
|
||||
|
||||
this.disconnect();
|
||||
|
||||
System.out.println(msg);
|
||||
if (msg.equals("import RDF file to database done.")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean drop(String _db_name) {
|
||||
boolean connect_return = this.connect();
|
||||
if (!connect_return) {
|
||||
System.err.println("connect to server error. @GstoreConnector.drop");
|
||||
return false;
|
||||
}
|
||||
|
||||
String cmd = "drop/" + _db_name;
|
||||
String msg = this.sendGet(cmd);
|
||||
|
||||
this.disconnect();
|
||||
|
||||
System.out.println(msg);
|
||||
return msg.equals("drop database done.");
|
||||
}
|
||||
|
||||
public String query(String _sparql) {
|
||||
boolean connect_return = this.connect();
|
||||
if (!connect_return) {
|
||||
System.err.println("connect to server error. @GstoreConnector.query");
|
||||
return "connect to server error.";
|
||||
}
|
||||
|
||||
//URL encode should be used here
|
||||
try {
|
||||
_sparql = URLEncoder.encode("\""+_sparql+"\"", "UTF-8");
|
||||
}
|
||||
catch (UnsupportedEncodingException ex) {
|
||||
throw new RuntimeException("Broken VM does not support UTF-8");
|
||||
}
|
||||
String cmd = "query/" + _sparql;
|
||||
//String cmd = "query/\"" + _sparql + "\"";
|
||||
String msg = this.sendGet(cmd);
|
||||
|
||||
this.disconnect();
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
public String show() {
|
||||
return this.show(false);
|
||||
}
|
||||
|
||||
public String show(boolean _type) {
|
||||
boolean connect_return = this.connect();
|
||||
if (!connect_return) {
|
||||
System.err.println("connect to server error. @GstoreConnector.show");
|
||||
return "connect to server error.";
|
||||
}
|
||||
|
||||
String cmd;
|
||||
if (_type) {
|
||||
cmd = "show/all";
|
||||
}
|
||||
else {
|
||||
cmd = "show/databases";
|
||||
}
|
||||
String msg = this.sendGet(cmd);
|
||||
|
||||
this.disconnect();
|
||||
return msg;
|
||||
}
|
||||
|
||||
private boolean connect() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean disconnect() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static byte[] packageMsgData(String _msg) {
|
||||
//byte[] data_context = _msg.getBytes();
|
||||
byte[] data_context = null;
|
||||
try {
|
||||
data_context = _msg.getBytes("utf-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
System.err.println("utf-8 charset is unsupported.");
|
||||
data_context = _msg.getBytes();
|
||||
}
|
||||
int context_len = data_context.length + 1; // 1 byte for '\0' at the end of the context.
|
||||
int data_len = context_len + 4; // 4 byte for one int(data_len at the data's head).
|
||||
byte[] data = new byte[data_len];
|
||||
|
||||
// padding head(context_len).
|
||||
byte[] head = GstoreConnector.intToByte4(context_len);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
data[i] = head[i];
|
||||
}
|
||||
|
||||
// padding context.
|
||||
for (int i = 0; i < data_context.length; i++) {
|
||||
data[i + 4] = data_context[i];
|
||||
}
|
||||
// in C, there should be '\0' as the terminator at the end of a char array. so we need add '\0' at the end of sending message.
|
||||
data[data_len - 1] = 0;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
private static byte[] intToByte4(int _x) // with Little Endian format.
|
||||
{
|
||||
byte[] ret = new byte[4];
|
||||
ret[0] = (byte) (_x);
|
||||
ret[1] = (byte) (_x >>> 8);
|
||||
ret[2] = (byte) (_x >>> 16);
|
||||
ret[3] = (byte) (_x >>> 24);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static int byte4ToInt(byte[] _b) // with Little Endian format.
|
||||
{
|
||||
int byte0 = _b[0] & 0xFF, byte1 = _b[1] & 0xFF, byte2 = _b[2] & 0xFF, byte3 = _b[3] & 0xFF;
|
||||
int ret = (byte0) | (byte1 << 8) | (byte2 << 16) | (byte3 << 24);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// initialize the GStore server's IP address and port.
|
||||
GstoreConnector gc = new GstoreConnector("172.31.19.15", 3305);
|
||||
|
||||
// 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");
|
||||
String sparql = "select ?x where {"
|
||||
+ "?x <rdf:type> <cdblp.cn/class/Paper>. "
|
||||
+ "?x <cdblp.cn/schema/property/has_author> <cdblp.cn/author/wangshan>. "
|
||||
+ "}";
|
||||
|
||||
boolean flag = gc.load("db_cdblp");
|
||||
System.out.println(flag);
|
||||
String answer = gc.query(sparql);
|
||||
System.out.println(answer);
|
||||
|
||||
answer = gc.query(sparql);
|
||||
System.out.println(answer);
|
||||
|
||||
sparql = "select ?x where {"
|
||||
+ "?x <rdf:type> <cdblp.cn/class/Paper>. "
|
||||
+ "?x <cdblp.cn/schema/property/has_author> <cdblp.cn/author/yuge>. "
|
||||
+ "}";
|
||||
answer = gc.query(sparql);
|
||||
System.out.println(answer);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
/*=============================================================================
|
||||
# Filename: CppAPIExample.cpp
|
||||
# Author: Bookug Lobert
|
||||
# Mail: 1181955272@qq.com
|
||||
# Last Modified: 2016-02-21 21:32
|
||||
# Description: originally written by hanshuo, modified by zengli
|
||||
=============================================================================*/
|
||||
|
||||
#include "GstoreConnector.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
// before run this example, you must start up the GStore server at first (use command ./gserver).
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
// initialize the GStore server's IP address and port.
|
||||
GstoreConnector gc("127.0.0.1", 3305);
|
||||
|
||||
// build a new database by a RDF file.
|
||||
// note that the relative path is related to gserver.
|
||||
gc.build("LUBM10", "data/LUBM_10.n3");
|
||||
gc.load("LUBM10");
|
||||
|
||||
// then you can execute SPARQL query on this database.
|
||||
std::string sparql = "select ?x where \
|
||||
{ \
|
||||
?x <rdf:type> <ub:UndergraduateStudent>. \
|
||||
?y <ub:name> <Course1>. \
|
||||
?x <ub:takesCourse> ?y. \
|
||||
?z <ub:teacherOf> ?y. \
|
||||
?z <ub:name> <FullProfessor1>. \
|
||||
?z <ub:worksFor> ?w. \
|
||||
?w <ub:name> <Department0>. \
|
||||
}";
|
||||
std::string answer = gc.query(sparql);
|
||||
std::cout << answer << std::endl;
|
||||
|
||||
// unload this database.
|
||||
gc.unload("LUBM10");
|
||||
|
||||
// also, you can load some exist database directly and then query.
|
||||
gc.load("LUBM10");
|
||||
answer = gc.query(sparql);
|
||||
std::cout << answer << std::endl;
|
||||
gc.unload("LUBM10");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#CC=g++
|
||||
CC=ccache g++
|
||||
|
||||
example: CppAPIExample.o
|
||||
$(CC) -o example CppAPIExample.o -L../lib -lgstoreconnector
|
||||
|
||||
CppAPIExample.o: CppAPIExample.cpp
|
||||
$(CC) -c -I../src/ CppAPIExample.cpp -o CppAPIExample.o
|
||||
|
||||
clean:
|
||||
rm -rf *.o example
|
|
@ -0,0 +1,287 @@
|
|||
/*=============================================================================
|
||||
# Filename: GstoreConnector.cpp
|
||||
# Author: Bookug Lobert
|
||||
# Mail: 1181955272@qq.com
|
||||
# Last Modified: 2016-02-21 21:24
|
||||
# Description: achieve functions in GstoreConnector.h
|
||||
=============================================================================*/
|
||||
|
||||
#include "GstoreConnector.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const string GstoreConnector::defaultServerIP = "127.0.0.1";
|
||||
const unsigned short GstoreConnector::defaultServerPort = 3305;
|
||||
|
||||
GstoreConnector::GstoreConnector()
|
||||
{
|
||||
this->serverIP = GstoreConnector::defaultServerIP;
|
||||
}
|
||||
|
||||
GstoreConnector::GstoreConnector(string _ip)
|
||||
{
|
||||
this->serverIP = _ip;
|
||||
this->serverPort = GstoreConnector::defaultServerPort;
|
||||
}
|
||||
|
||||
GstoreConnector::GstoreConnector(unsigned short _port)
|
||||
{
|
||||
this->serverIP = GstoreConnector::defaultServerIP;
|
||||
this->serverPort = _port;
|
||||
}
|
||||
|
||||
GstoreConnector::GstoreConnector(string _ip, unsigned short _port)
|
||||
{
|
||||
if (_ip == "localhost")
|
||||
this->serverIP = "127.0.0.1";
|
||||
else
|
||||
this->serverIP = _ip;
|
||||
this->serverPort = _port;
|
||||
}
|
||||
|
||||
GstoreConnector::~GstoreConnector()
|
||||
{
|
||||
this->disconnect();
|
||||
}
|
||||
|
||||
bool GstoreConnector::test() {
|
||||
bool connect_return = this->connect();
|
||||
if (!connect_return) {
|
||||
cerr << "failed to connect to server. @GstoreConnector::test" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
string cmd = "test";
|
||||
bool send_return = this->socket.send(cmd);
|
||||
if (!send_return) {
|
||||
cerr << "send test command error. @GstoreConnector::test" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
string recv_msg;
|
||||
this->socket.recv(recv_msg);
|
||||
|
||||
this->disconnect();
|
||||
|
||||
cout << recv_msg << endl;
|
||||
if (recv_msg == "OK") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
GstoreConnector::load(string _db_name)
|
||||
{
|
||||
bool connect_return = this->connect();
|
||||
if (!connect_return)
|
||||
{
|
||||
cerr << "connect to server error. @GstoreConnector::load" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
string cmd = "load " + _db_name;
|
||||
bool send_return = this->socket.send(cmd);
|
||||
if (!send_return)
|
||||
{
|
||||
cerr << "send load command error. @GstoreConnector.load" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
string recv_msg;
|
||||
this->socket.recv(recv_msg);
|
||||
|
||||
this->disconnect();
|
||||
|
||||
cout << recv_msg << endl;
|
||||
if (recv_msg == "load database done.")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
GstoreConnector::unload(string _db_name)
|
||||
{
|
||||
bool connect_return = this->connect();
|
||||
if (!connect_return)
|
||||
{
|
||||
cerr << "connect to server error. @GstoreConnector::unload" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
string cmd = "unload " + _db_name;
|
||||
bool send_return = this->socket.send(cmd);
|
||||
if (!send_return)
|
||||
{
|
||||
cerr << "send unload command error. @GstoreConnector::unload" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
string recv_msg;
|
||||
this->socket.recv(recv_msg);
|
||||
|
||||
this->disconnect();
|
||||
|
||||
cout << recv_msg << endl;
|
||||
if (recv_msg == "unload database done.")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
GstoreConnector::build(string _db_name, string _rdf_file_path)
|
||||
{
|
||||
bool connect_return = this->connect();
|
||||
if (!connect_return)
|
||||
{
|
||||
cerr << "connect to server error. @GstoreConnector::build" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
string cmd = "import " + _db_name + " " + _rdf_file_path;
|
||||
bool send_return = this->socket.send(cmd);
|
||||
if (!send_return)
|
||||
{
|
||||
cerr << "send import command error. @GstoreConnector::build" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
string recv_msg;
|
||||
this->socket.recv(recv_msg);
|
||||
|
||||
this->disconnect();
|
||||
|
||||
cerr << recv_msg << endl;
|
||||
if (recv_msg == "import RDF file to database done.")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
GstoreConnector::drop(string _db_name)
|
||||
{
|
||||
bool connect_return = this->connect();
|
||||
if (!connect_return)
|
||||
{
|
||||
cerr << "connect to server error. @GstoreConnector::unload" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
string cmd = "drop " + _db_name;
|
||||
bool send_return = this->socket.send(cmd);
|
||||
if (!send_return)
|
||||
{
|
||||
cerr << "send unload command error. @GstoreConnector::unload" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
string recv_msg;
|
||||
this->socket.recv(recv_msg);
|
||||
|
||||
this->disconnect();
|
||||
|
||||
cout << recv_msg << endl;
|
||||
if (recv_msg == "drop database done.")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
string
|
||||
GstoreConnector::query(string _sparql, string _output)
|
||||
{
|
||||
bool connect_return = this->connect();
|
||||
if (!connect_return)
|
||||
{
|
||||
cerr << "connect to server error. @GstoreConnector::query" << endl;
|
||||
return "connect to server error.";
|
||||
}
|
||||
|
||||
string cmd = "query " + _sparql;
|
||||
bool send_return = this->socket.send(cmd);
|
||||
if (!send_return)
|
||||
{
|
||||
cerr << "send query command error. @GstoreConnector::query";
|
||||
return "send query command error.";
|
||||
}
|
||||
|
||||
string recv_msg;
|
||||
this->socket.recv(recv_msg);
|
||||
|
||||
this->disconnect();
|
||||
|
||||
return recv_msg;
|
||||
}
|
||||
|
||||
string
|
||||
GstoreConnector::show(bool _type)
|
||||
{
|
||||
bool connect_return = this->connect();
|
||||
if (!connect_return)
|
||||
{
|
||||
cerr << "connect to server error. @GstoreConnector::show" << endl;
|
||||
return "connect to server error.";
|
||||
}
|
||||
|
||||
string cmd;
|
||||
if (_type)
|
||||
{
|
||||
cmd = "show all";
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd = "show databases";
|
||||
}
|
||||
bool send_return = this->socket.send(cmd);
|
||||
if (!send_return)
|
||||
{
|
||||
cerr << "send show command error. @GstoreConnector::show";
|
||||
return "send show command error.";
|
||||
}
|
||||
|
||||
string recv_msg;
|
||||
this->socket.recv(recv_msg);
|
||||
this->disconnect();
|
||||
|
||||
return recv_msg;
|
||||
}
|
||||
|
||||
bool
|
||||
GstoreConnector::connect()
|
||||
{
|
||||
bool flag = this->socket.create();
|
||||
if (!flag)
|
||||
{
|
||||
cerr << "cannot create socket. @GstoreConnector::connect" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
flag = this->socket.connect(this->serverIP, this->serverPort);
|
||||
|
||||
if (!flag)
|
||||
{
|
||||
cerr << "cannot connect to server. @GstoreConnector::connect" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
GstoreConnector::disconnect()
|
||||
{
|
||||
bool flag = this->socket.close();
|
||||
|
||||
return flag;
|
||||
}
|
|
@ -9,7 +9,7 @@
|
|||
#ifndef _GSTORECONNECTOR_H
|
||||
#define _GSTORECONNECTOR_H
|
||||
|
||||
#include "../../../Server/Socket.h"
|
||||
#include "../../../../Server/Socket.h"
|
||||
#include <cstring>
|
||||
|
||||
class GstoreConnector
|
|
@ -0,0 +1,16 @@
|
|||
#CC=g++
|
||||
CC=ccache g++ -std=c++11
|
||||
|
||||
lib_dir=../lib/
|
||||
socket_obj_dir=../../../../.objs/
|
||||
|
||||
all: $(lib_dir)libgstoreconnector.a
|
||||
|
||||
$(lib_dir)libgstoreconnector.a: GstoreConnector.o $(socket_obj_dir)Socket.o
|
||||
ar -crv $(lib_dir)libgstoreconnector.a GstoreConnector.o $(socket_obj_dir)Socket.o
|
||||
|
||||
GstoreConnector.o: GstoreConnector.cpp GstoreConnector.h $(socket_obj_dir)Socket.o
|
||||
$(CC) -c GstoreConnector.cpp -o GstoreConnector.o
|
||||
|
||||
clean:
|
||||
rm -rf GstoreConnector.o $(lib_dir)libgstoreconnector.a
|
|
@ -0,0 +1,11 @@
|
|||
JavaAPIExample.class:
|
||||
javac -cp ../lib/GstoreJavaAPI.jar JavaAPIExample.java
|
||||
|
||||
.PHONY: clean run
|
||||
|
||||
run: JavaAPIExample.class
|
||||
java -cp ../lib/GstoreJavaAPI.jar:. JavaAPIExample
|
||||
|
||||
clean:
|
||||
rm -f JavaAPIExample.class
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
*
|
||||
!.gitignore
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
lib_dir=../lib/
|
||||
socket_obj_dir=../../../.objs/
|
||||
|
||||
all: $(lib_dir)GstoreJavaAPI.jar
|
||||
|
||||
$(lib_dir)GstoreJavaAPI.jar: jgsc/GstoreConnector.class
|
||||
jar -cvf $(lib_dir)GstoreJavaAPI.jar jgsc/GstoreConnector.java jgsc/GstoreConnector.class
|
||||
|
||||
jgsc/GstoreConnector.class:
|
||||
javac jgsc/GstoreConnector.java
|
||||
|
||||
clean:
|
||||
rm -rf $(lib_dir)GstoreJavaAPI.jar jgsc/GstoreConnector.class
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
*
|
||||
!.gitignore
|
||||
|
26
makefile
26
makefile
|
@ -62,9 +62,9 @@ exedir = bin/
|
|||
|
||||
lib_antlr = lib/libantlr.a
|
||||
|
||||
api_cpp = api/cpp/lib/libgstoreconnector.a
|
||||
api_cpp = api/socket/cpp/lib/libgstoreconnector.a
|
||||
|
||||
api_java = api/java/lib/GstoreJavaAPI.jar
|
||||
api_java = api/socket/java/lib/GstoreJavaAPI.jar
|
||||
|
||||
# objects
|
||||
|
||||
|
@ -138,7 +138,7 @@ $(exedir)gclient: $(lib_antlr) $(objdir)gclient.o $(objfile)
|
|||
$(CC) $(EXEFLAG) -o $(exedir)gclient $(objdir)gclient.o $(objfile) $(library)
|
||||
|
||||
$(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
|
||||
$(CC) $(EXEFLAG) -o $(exedir)gconsole $(objdir)gconsole.o $(objfile) $(library) -L./api/socket/cpp/lib -lgstoreconnector
|
||||
|
||||
$(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
|
||||
|
@ -165,8 +165,8 @@ $(objdir)gserver_backup_scheduler.o: Main/gserver_backup_scheduler.cpp Server/Se
|
|||
$(objdir)gclient.o: Main/gclient.cpp Server/Client.h Util/Util.h $(lib_antlr)
|
||||
$(CC) $(CFLAGS) Main/gclient.cpp $(inc) -o $(objdir)gclient.o #-DREADLINE_ON
|
||||
|
||||
$(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)gconsole.o: Main/gconsole.cpp Database/Database.h Util/Util.h api/socket/cpp/src/GstoreConnector.h $(lib_antlr)
|
||||
$(CC) $(CFLAGS) Main/gconsole.cpp $(inc) -o $(objdir)gconsole.o -I./api/socket/cpp/src/ #-DREADLINE_ON
|
||||
|
||||
$(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
|
||||
|
@ -429,19 +429,19 @@ $(lib_antlr):
|
|||
cd tools; tar -xzvf sparql.tar.gz; mv Sparql* ../Parser/;
|
||||
|
||||
$(api_cpp): $(objdir)Socket.o
|
||||
$(MAKE) -C api/cpp/src
|
||||
$(MAKE) -C api/socket/cpp/src
|
||||
|
||||
$(api_java):
|
||||
$(MAKE) -C api/java/src
|
||||
$(MAKE) -C api/socket/java/src
|
||||
|
||||
.PHONY: clean dist tarball api_example gtest sumlines
|
||||
|
||||
clean:
|
||||
rm -rf lib/libantlr.a
|
||||
$(MAKE) -C api/cpp/src clean
|
||||
$(MAKE) -C api/cpp/example clean
|
||||
$(MAKE) -C api/java/src clean
|
||||
$(MAKE) -C api/java/example clean
|
||||
$(MAKE) -C api/socket/cpp/src clean
|
||||
$(MAKE) -C api/socket/cpp/example clean
|
||||
$(MAKE) -C api/socket/java/src clean
|
||||
$(MAKE) -C api/socket/java/example clean
|
||||
#$(MAKE) -C KVstore clean
|
||||
rm -rf $(exedir)g* $(objdir)*.o $(exedir).gserver*
|
||||
#rm -rf .project .cproject .settings just for eclipse
|
||||
|
@ -460,8 +460,8 @@ tarball:
|
|||
Main Database KVstore Util Query Signature VSTree Parser Server README.md init.conf NOTES.md StringIndex COVERAGE
|
||||
|
||||
APIexample: $(api_cpp) $(api_java)
|
||||
$(MAKE) -C api/cpp/example
|
||||
$(MAKE) -C api/java/example
|
||||
$(MAKE) -C api/socket/cpp/example
|
||||
$(MAKE) -C api/socket/java/example
|
||||
|
||||
gtest: $(objdir)gtest.o $(objfile)
|
||||
$(CC) $(EXEFLAG) -o $(exedir)gtest $(objdir)gtest.o $(objfile) lib/libantlr.a $(library)
|
||||
|
|
Loading…
Reference in New Issue