first working db

This commit is contained in:
rjawor 2015-08-04 12:03:28 +02:00
parent 0350616885
commit 5e586e297f
14 changed files with 256 additions and 15 deletions

View File

@ -45,6 +45,13 @@ configure_file (
)
set(CONFIG_FILE_PATH "${concordia-server_SOURCE_DIR}/concordia.cfg")
# --------------
# db settings
# --------------
set (DB_NAME "concordia_server")
set (DB_USER "concordia")
configure_file (
"${concordia-server_SOURCE_DIR}/concordia-server/config.hpp.in"
"${concordia-server_SOURCE_DIR}/concordia-server/config.hpp"

View File

@ -5,6 +5,7 @@ add_executable(concordia_server_process
searcher_controller.cpp
json_generator.cpp
unit_dao.cpp
db_connection.cpp
)
target_link_libraries(concordia_server_process fcgi fcgi++ pq concordia config++ log4cpp ${Boost_LIBRARIES} divsufsort utf8case)

View File

@ -6,7 +6,10 @@
#include "json_generator.hpp"
#define OPERATION_PARAM "operation"
#define SENTENCE_PARAM "sentence"
#define PATTERN_PARAM "pattern"
#define SOURCE_SENTENCE_PARAM "sourceSentence"
#define TARGET_SENTENCE_PARAM "targetSentence"
#define TM_ID_PARAM "tmId"
#define ADD_SENTENCE_OP "addSentence"
#define SIMPLE_SEARCH_OP "simpleSearch"
@ -41,13 +44,17 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) {
JsonGenerator::signalError(jsonWriter, errorstream.str());
} else { // json parsed
std::string operation = d[OPERATION_PARAM].GetString();
std::string sentence = d[SENTENCE_PARAM].GetString();
if (operation == ADD_SENTENCE_OP) {
_indexController->addSentence(jsonWriter, sentence);
std::string sourceSentence = d[SOURCE_SENTENCE_PARAM].GetString();
std::string targetSentence = d[TARGET_SENTENCE_PARAM].GetString();
int tmId = d[TM_ID_PARAM].GetInt();
_indexController->addSentence(jsonWriter, sourceSentence, targetSentence, tmId);
} else if (operation == SIMPLE_SEARCH_OP) {
_searcherController->simpleSearch(jsonWriter, sentence);
std::string pattern = d[PATTERN_PARAM].GetString();
_searcherController->simpleSearch(jsonWriter, pattern);
} else if (operation == CONCORDIA_SEARCH_OP) {
_searcherController->concordiaSearch(jsonWriter, sentence);
std::string pattern = d[PATTERN_PARAM].GetString();
_searcherController->concordiaSearch(jsonWriter, pattern);
} else {
JsonGenerator::signalError(jsonWriter, "no such operation");
}

View File

@ -1 +1,5 @@
#define CONFIG_FILE_PATH "/home/rafalj/projects/concordia-server/concordia.cfg"
// database connection information
#define DB_NAME "concordia_server"
#define DB_USER "concordia"

View File

@ -1 +1,5 @@
#define CONFIG_FILE_PATH "@CONFIG_FILE_PATH@"
// database connection information
#define DB_NAME "@DB_NAME@"
#define DB_USER "@DB_USER@"

View File

@ -0,0 +1,71 @@
#include "db_connection.hpp"
#include "config.hpp"
DBconnection::DBconnection() throw(ConcordiaException) {
std::string connectionInfo = "dbname="DB_NAME" user="DB_USER;
_connection = PQconnectdb(connectionInfo.c_str());
if (PQstatus(_connection) != CONNECTION_OK) {
close();
throw ConcordiaException("Could not establish connection with the database");
}
}
DBconnection::~DBconnection() {
close();
}
void DBconnection::close() {
if (_connection != NULL) {
PQfinish(_connection);
_connection = NULL;
}
}
void DBconnection::startTransaction() throw(ConcordiaException) {
if (_connection != NULL) {
PGresult * result = PQexec(_connection, "BEGIN");
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
PQclear(result);
close();
throw ConcordiaException("starting transaction failed");
}
} else {
throw ConcordiaException("requested start transaction but the database connection is not ready");
}
}
void DBconnection::endTransaction() throw(ConcordiaException) {
if (_connection != NULL) {
PGresult * result = PQexec(_connection, "END");
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
PQclear(result);
close();
throw ConcordiaException("ending transaction failed");
}
} else {
throw ConcordiaException("requested end transaction but the database connection is not ready");
}
}
PGresult * DBconnection::execute(std::string query) throw(ConcordiaException) {
if (_connection != NULL) {
PGresult * result = PQexec(_connection, query.c_str());
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
PQclear(result);
close();
throw ConcordiaException("ending transaction failed");
} else {
return result;
}
} else {
throw ConcordiaException("requested query execution but the database connection is not ready");
}
}
PGresult * DBconnection::execute(std::string query,
std::vector<std::string> params) throw(ConcordiaException) {
}

View File

@ -0,0 +1,33 @@
#ifndef DB_MANAGER_HDR
#define DB_MANAGER_HDR
#include <libpq-fe.h>
#include <string>
#include <vector>
#include <concordia/concordia_exception.hpp>
class DBconnection {
public:
/*! Constructor.
*/
DBconnection() throw(ConcordiaException);
/*! Destructor.
*/
virtual ~DBconnection();
void startTransaction() throw(ConcordiaException);
void endTransaction() throw(ConcordiaException);
PGresult * execute(std::string query) throw(ConcordiaException);
PGresult * execute(std::string query,
std::vector<std::string> params) throw(ConcordiaException);
private:
void close();
PGconn * _connection;
};
#endif

View File

@ -1,5 +1,6 @@
#include "index_controller.hpp"
#include <concordia/common/config.hpp>
#include "json_generator.hpp"
IndexController::IndexController(boost::shared_ptr<Concordia> concordia)
@ -11,13 +12,18 @@ IndexController::~IndexController() {
}
void IndexController::addSentence(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string & sentence) {
void IndexController::addSentence(
rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter,
std::string & sourceSentence,
std::string & targetSentence,
int tmId) {
try {
Example example(sentence, 0);
_concordia->addExample(example);
boost::shared_ptr<TokenizedSentence> tokenizedSentence = _concordia->tokenize(sourceSentence);
SUFFIX_MARKER_TYPE sentenceId = _unitDAO.addSentence(tokenizedSentence, targetSentence, tmId);
_concordia->addTokenizedExample(tokenizedSentence, sentenceId);
_concordia->refreshSAfromRAM();
jsonWriter.StartObject();
jsonWriter.String("status");
jsonWriter.String("success");

View File

@ -6,6 +6,8 @@
#include <concordia/concordia.hpp>
#include <concordia/concordia_exception.hpp>
#include "unit_dao.hpp"
#include "rapidjson/writer.h"
class IndexController {
@ -18,11 +20,15 @@ public:
*/
virtual ~IndexController();
void addSentence(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string & sentence);
void addSentence(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter,
std::string & sourceSentence,
std::string & targetSentence,
int tmId);
private:
boost::shared_ptr<Concordia> _concordia;
UnitDAO _unitDAO;
};
#endif

View File

@ -0,0 +1,71 @@
#include "db_connection.hpp"
#include "config.hpp"
DBconnection::DBconnection() throw(ConcordiaException) {
std::string connectionInfo = "dbname="DB_NAME" user="DB_USER;
_connection = PQconnectdb(connectionInfo.c_str());
if (PQstatus(_connection) != CONNECTION_OK) {
close();
throw ConcordiaException("Could not establish connection with the database");
}
}
DBconnection::~DBconnection() {
close();
}
void DBconnection::close() {
if (_connection != NULL) {
PQfinish(_connection);
_connection = NULL;
}
}
void DBconnection::startTransaction() throw(ConcordiaException) {
if (_connection != NULL) {
PGresult * result = PQexec(_connection, "BEGIN");
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
PQclear(result);
close();
throw ConcordiaException("starting transaction failed");
}
} else {
throw ConcordiaException("requested start transaction but the database connection is not ready");
}
}
void DBconnection::endTransaction() throw(ConcordiaException) {
if (_connection != NULL) {
PGresult * result = PQexec(_connection, "END");
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
PQclear(result);
close();
throw ConcordiaException("ending transaction failed");
}
} else {
throw ConcordiaException("requested end transaction but the database connection is not ready");
}
}
PGresult * DBconnection::execute(std::string query) throw(ConcordiaException) {
if (_connection != NULL) {
PGresult * result = PQexec(_connection, query.c_str());
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
PQclear(result);
close();
throw ConcordiaException("ending transaction failed");
} else {
return result;
}
} else {
throw ConcordiaException("requested query execution but the database connection is not ready");
}
}
PGresult * DBconnection::execute(std::string query,
std::vector<std::string> params) throw(ConcordiaException) {
}

View File

@ -0,0 +1,17 @@
#ifndef QUERY_PARAM_HDR
#define QUERY_PARAM_HDR
class QueryParam {
public:
/*! Constructor.
*/
QueryParam() throw(ConcordiaException);
/*! Destructor.
*/
virtual ~QueryParam();
private:
};
#endif

View File

@ -1,5 +1,6 @@
#include "unit_dao.hpp"
#include "db_connection.hpp"
UnitDAO::UnitDAO() {
}
@ -7,7 +8,15 @@ UnitDAO::UnitDAO() {
UnitDAO::~UnitDAO() {
}
SUFFIX_MARKER_TYPE UnitDAO::addSentence(std::string & sentence) {
SUFFIX_MARKER_TYPE UnitDAO::addSentence(
boost::shared_ptr<TokenizedSentence> sourceSentence,
std::string & targetSentence,
int tmId) {
DBconnection connection;
connection.startTransaction();
connection.execute("INSERT INTO unit(source_segment) values('just testing')");
connection.endTransaction();
}

View File

@ -3,7 +3,9 @@
#include <string>
#include <concordia/tokenized_sentence.hpp>
#include <concordia/common/config.hpp>
#include <boost/shared_ptr.hpp>
class UnitDAO {
public:
@ -14,7 +16,10 @@ public:
*/
virtual ~UnitDAO();
SUFFIX_MARKER_TYPE addSentence(std::string & sentence);
SUFFIX_MARKER_TYPE addSentence(
boost::shared_ptr<TokenizedSentence> sourceSentence,
std::string & targetSentence,
int tmId);
private:
};

View File

@ -1,5 +1,5 @@
#!/bin/sh
#curl -H "Content-Type: application/json" -X POST -d '{"operation":"addSentence", "sentence":"zupełnie nowe zdanie"}' http://localhost
curl -H "Content-Type: application/json" -X POST -d '{"operation":"simpleSearch", "sentence":"zupełnie nowe"}' http://localhost
curl -H "Content-Type: application/json" -X POST -d '{"operation":"addSentence", "sentence":"zupełnie nowe zdanie"}' http://localhost
#curl -H "Content-Type: application/json" -X POST -d '{"operation":"simpleSearch", "sentence":"zupełnie nowe"}' http://localhost