working database, adding sentence, logging
This commit is contained in:
parent
5e586e297f
commit
fa3f4db781
@ -6,6 +6,10 @@ add_executable(concordia_server_process
|
||||
json_generator.cpp
|
||||
unit_dao.cpp
|
||||
db_connection.cpp
|
||||
query_param.cpp
|
||||
string_param.cpp
|
||||
int_param.cpp
|
||||
logger.cpp
|
||||
)
|
||||
target_link_libraries(concordia_server_process fcgi fcgi++ pq concordia config++ log4cpp ${Boost_LIBRARIES} divsufsort utf8case)
|
||||
|
||||
|
@ -31,7 +31,6 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) {
|
||||
rapidjson::Writer<rapidjson::StringBuffer> jsonWriter(outputJson);
|
||||
|
||||
std::stringstream outputString;
|
||||
|
||||
try {
|
||||
outputString << "Content-type: application/json\r\n\r\n";
|
||||
rapidjson::Document d;
|
||||
|
@ -1,6 +1,11 @@
|
||||
#include "db_connection.hpp"
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
#include "config.hpp"
|
||||
#include "logger.hpp"
|
||||
|
||||
DBconnection::DBconnection() throw(ConcordiaException) {
|
||||
std::string connectionInfo = "dbname="DB_NAME" user="DB_USER;
|
||||
@ -53,9 +58,12 @@ PGresult * DBconnection::execute(std::string query) throw(ConcordiaException) {
|
||||
if (_connection != NULL) {
|
||||
PGresult * result = PQexec(_connection, query.c_str());
|
||||
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
|
||||
std::stringstream ss;
|
||||
ss << "query execution failed with message: ";
|
||||
ss << PQresultErrorMessage(result) << std::endl;
|
||||
PQclear(result);
|
||||
close();
|
||||
throw ConcordiaException("ending transaction failed");
|
||||
throw ConcordiaException(ss.str());
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
@ -65,7 +73,42 @@ PGresult * DBconnection::execute(std::string query) throw(ConcordiaException) {
|
||||
}
|
||||
|
||||
PGresult * DBconnection::execute(std::string query,
|
||||
std::vector<std::string> params) throw(ConcordiaException) {
|
||||
std::vector<QueryParam*> params) throw(ConcordiaException) {
|
||||
if (_connection != NULL) {
|
||||
const char * paramValues[params.size()];
|
||||
int paramLengths[params.size()];
|
||||
int paramFormats[params.size()];
|
||||
int index = 0;
|
||||
BOOST_FOREACH (QueryParam * param, params) {
|
||||
paramValues[index] = param->getValue();
|
||||
paramLengths[index] = param->getLength();
|
||||
paramFormats[index] = param->isBinary();
|
||||
index++;
|
||||
}
|
||||
|
||||
|
||||
PGresult * result = PQexecParams(_connection,
|
||||
query.c_str(),
|
||||
params.size(),
|
||||
NULL,
|
||||
paramValues,
|
||||
paramLengths,
|
||||
paramFormats,
|
||||
0
|
||||
);
|
||||
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
|
||||
std::stringstream ss;
|
||||
ss << "parametrized query execution failed with message: ";
|
||||
ss << PQresultErrorMessage(result) << std::endl;
|
||||
PQclear(result);
|
||||
close();
|
||||
throw ConcordiaException(ss.str());
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
} else {
|
||||
throw ConcordiaException("requested query execution but the database connection is not ready");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -7,6 +7,8 @@
|
||||
|
||||
#include <concordia/concordia_exception.hpp>
|
||||
|
||||
#include "query_param.hpp"
|
||||
|
||||
class DBconnection {
|
||||
public:
|
||||
/*! Constructor.
|
||||
@ -23,7 +25,7 @@ public:
|
||||
PGresult * execute(std::string query) throw(ConcordiaException);
|
||||
|
||||
PGresult * execute(std::string query,
|
||||
std::vector<std::string> params) throw(ConcordiaException);
|
||||
std::vector<QueryParam*> params) throw(ConcordiaException);
|
||||
private:
|
||||
void close();
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "index_controller.hpp"
|
||||
|
||||
#include <concordia/common/config.hpp>
|
||||
#include "json_generator.hpp"
|
||||
|
||||
IndexController::IndexController(boost::shared_ptr<Concordia> concordia)
|
||||
@ -20,7 +19,7 @@ void IndexController::addSentence(
|
||||
|
||||
try {
|
||||
boost::shared_ptr<TokenizedSentence> tokenizedSentence = _concordia->tokenize(sourceSentence);
|
||||
SUFFIX_MARKER_TYPE sentenceId = _unitDAO.addSentence(tokenizedSentence, targetSentence, tmId);
|
||||
int sentenceId = _unitDAO.addSentence(tokenizedSentence, targetSentence, tmId);
|
||||
_concordia->addTokenizedExample(tokenizedSentence, sentenceId);
|
||||
_concordia->refreshSAfromRAM();
|
||||
|
||||
|
23
concordia-server/int_param.cpp
Normal file
23
concordia-server/int_param.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include "int_param.hpp"
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
IntParam::IntParam(int value) {
|
||||
_value = htonl((unsigned long int) value);
|
||||
}
|
||||
|
||||
IntParam::~IntParam() {
|
||||
}
|
||||
|
||||
const char * IntParam::getValue() {
|
||||
return (const char *) &_value;
|
||||
}
|
||||
|
||||
const int IntParam::getLength() {
|
||||
return sizeof(_value);
|
||||
}
|
||||
|
||||
const int IntParam::isBinary() {
|
||||
return 1;
|
||||
}
|
24
concordia-server/int_param.hpp
Normal file
24
concordia-server/int_param.hpp
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef INT_PARAM_HDR
|
||||
#define INT_PARAM_HDR
|
||||
|
||||
#include "query_param.hpp"
|
||||
|
||||
class IntParam : public QueryParam {
|
||||
public:
|
||||
/*! Constructor.
|
||||
*/
|
||||
IntParam(int value);
|
||||
/*! Destructor.
|
||||
*/
|
||||
virtual ~IntParam();
|
||||
|
||||
const char * getValue();
|
||||
|
||||
const int getLength();
|
||||
|
||||
const int isBinary();
|
||||
private:
|
||||
unsigned int _value;
|
||||
};
|
||||
|
||||
#endif
|
31
concordia-server/logger.cpp
Normal file
31
concordia-server/logger.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include "logger.hpp"
|
||||
|
||||
#include "log4cpp/Category.hh"
|
||||
#include "log4cpp/Appender.hh"
|
||||
#include "log4cpp/FileAppender.hh"
|
||||
#include "log4cpp/BasicLayout.hh"
|
||||
#include "log4cpp/Priority.hh"
|
||||
|
||||
Logger::Logger() {
|
||||
}
|
||||
|
||||
Logger::~Logger() {
|
||||
}
|
||||
|
||||
int Logger::initialized = 0;
|
||||
|
||||
void Logger::log(std::string message) {
|
||||
log4cpp::Category & root = log4cpp::Category::getRoot();
|
||||
if (initialized == 0) {
|
||||
log4cpp::Appender *appender = new log4cpp::FileAppender("default", "/tmp/concordia-server.log");
|
||||
appender->setLayout(new log4cpp::BasicLayout());
|
||||
|
||||
root.setPriority(log4cpp::Priority::INFO);
|
||||
root.addAppender(appender);
|
||||
|
||||
initialized = 1;
|
||||
}
|
||||
root.info(message);
|
||||
}
|
||||
|
||||
|
21
concordia-server/logger.hpp
Normal file
21
concordia-server/logger.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef LOGGER_HDR
|
||||
#define LOGGER_HDR
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
class Logger {
|
||||
public:
|
||||
/*! Constructor.
|
||||
*/
|
||||
Logger();
|
||||
/*! Destructor.
|
||||
*/
|
||||
virtual ~Logger();
|
||||
|
||||
static void log(std::string message);
|
||||
private:
|
||||
static int initialized;
|
||||
};
|
||||
|
||||
#endif
|
@ -1,71 +1,16 @@
|
||||
#include "db_connection.hpp"
|
||||
#include "query_param.hpp"
|
||||
|
||||
#include "config.hpp"
|
||||
QueryParam::QueryParam() {
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
QueryParam::~QueryParam() {
|
||||
}
|
||||
|
||||
const char * QueryParam::getValue() {
|
||||
}
|
||||
|
||||
const int QueryParam::getLength() {
|
||||
}
|
||||
|
||||
DBconnection::~DBconnection() {
|
||||
close();
|
||||
const int QueryParam::isBinary() {
|
||||
}
|
||||
|
||||
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) {
|
||||
}
|
||||
|
||||
|
||||
|
@ -5,11 +5,16 @@ class QueryParam {
|
||||
public:
|
||||
/*! Constructor.
|
||||
*/
|
||||
QueryParam() throw(ConcordiaException);
|
||||
QueryParam();
|
||||
/*! Destructor.
|
||||
*/
|
||||
virtual ~QueryParam();
|
||||
|
||||
virtual const char * getValue();
|
||||
|
||||
virtual const int getLength();
|
||||
|
||||
virtual const int isBinary();
|
||||
private:
|
||||
|
||||
};
|
||||
|
19
concordia-server/string_param.cpp
Normal file
19
concordia-server/string_param.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "string_param.hpp"
|
||||
|
||||
StringParam::StringParam(std::string value) : _value(value) {
|
||||
}
|
||||
|
||||
StringParam::~StringParam() {
|
||||
}
|
||||
|
||||
const char * StringParam::getValue() {
|
||||
return _value.c_str();
|
||||
}
|
||||
|
||||
const int StringParam::getLength() {
|
||||
return _value.size();
|
||||
}
|
||||
|
||||
const int StringParam::isBinary() {
|
||||
return 0;
|
||||
}
|
26
concordia-server/string_param.hpp
Normal file
26
concordia-server/string_param.hpp
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef STRING_PARAM_HDR
|
||||
#define STRING_PARAM_HDR
|
||||
|
||||
#include "query_param.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
class StringParam : public QueryParam {
|
||||
public:
|
||||
/*! Constructor.
|
||||
*/
|
||||
StringParam(std::string value);
|
||||
/*! Destructor.
|
||||
*/
|
||||
virtual ~StringParam();
|
||||
|
||||
const char * getValue();
|
||||
|
||||
const int getLength();
|
||||
|
||||
const int isBinary();
|
||||
private:
|
||||
std::string _value;
|
||||
};
|
||||
|
||||
#endif
|
@ -1,6 +1,13 @@
|
||||
#include "unit_dao.hpp"
|
||||
|
||||
#include "db_connection.hpp"
|
||||
#include "query_param.hpp"
|
||||
#include "string_param.hpp"
|
||||
#include "int_param.hpp"
|
||||
#include "logger.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
UnitDAO::UnitDAO() {
|
||||
}
|
||||
@ -8,15 +15,28 @@ UnitDAO::UnitDAO() {
|
||||
UnitDAO::~UnitDAO() {
|
||||
}
|
||||
|
||||
SUFFIX_MARKER_TYPE UnitDAO::addSentence(
|
||||
int 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')");
|
||||
std::string query = "INSERT INTO unit(source_segment, target_segment, tm_id) values($1::text,$2::text,$3::integer)";
|
||||
std::vector<QueryParam*> params;
|
||||
params.push_back(new StringParam(sourceSentence->getSentence()));
|
||||
params.push_back(new StringParam(targetSentence));
|
||||
params.push_back(new IntParam(tmId));
|
||||
|
||||
connection.execute(query, params);
|
||||
connection.endTransaction();
|
||||
|
||||
BOOST_FOREACH (QueryParam * param, params) {
|
||||
delete param;
|
||||
}
|
||||
|
||||
//todo return new unit id
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,6 @@
|
||||
#include <string>
|
||||
|
||||
#include <concordia/tokenized_sentence.hpp>
|
||||
#include <concordia/common/config.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
class UnitDAO {
|
||||
@ -16,7 +15,7 @@ public:
|
||||
*/
|
||||
virtual ~UnitDAO();
|
||||
|
||||
SUFFIX_MARKER_TYPE addSentence(
|
||||
int addSentence(
|
||||
boost::shared_ptr<TokenizedSentence> sourceSentence,
|
||||
std::string & targetSentence,
|
||||
int tmId);
|
||||
|
@ -1,5 +1,7 @@
|
||||
#!/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":"addSentence", "sourceSentence":"zupełnie nowe zdanie", "targetSentence":"zażółć gęślą jaźńZAŻÓŁĆ GĘŚLĄ JAŹŃ", "tmId":1234782314}' http://localhost
|
||||
|
||||
|
||||
#curl -H "Content-Type: application/json" -X POST -d '{"operation":"simpleSearch", "sentence":"zupełnie nowe"}' http://localhost
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user