working addSentence

This commit is contained in:
rjawor 2015-08-06 13:29:28 +02:00
parent fa3f4db781
commit de5d1f4a63
14 changed files with 147 additions and 115 deletions

View File

@ -10,6 +10,8 @@ add_executable(concordia_server_process
string_param.cpp
int_param.cpp
logger.cpp
int_array_param.cpp
simple_search_result.cpp
)
target_link_libraries(concordia_server_process fcgi fcgi++ pq concordia config++ log4cpp ${Boost_LIBRARIES} divsufsort utf8case)

View File

@ -2,7 +2,7 @@
#include <boost/foreach.hpp>
#include <sstream>
#include <stdlib.h>
#include "config.hpp"
#include "logger.hpp"
@ -57,7 +57,9 @@ void DBconnection::endTransaction() throw(ConcordiaException) {
PGresult * DBconnection::execute(std::string query) throw(ConcordiaException) {
if (_connection != NULL) {
PGresult * result = PQexec(_connection, query.c_str());
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
if (PQresultStatus(result) != PGRES_COMMAND_OK &&
PQresultStatus(result) != PGRES_TUPLES_OK) {
std::stringstream ss;
ss << "query execution failed with message: ";
ss << PQresultErrorMessage(result) << std::endl;
@ -96,7 +98,9 @@ PGresult * DBconnection::execute(std::string query,
paramFormats,
0
);
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
if (PQresultStatus(result) != PGRES_COMMAND_OK &&
PQresultStatus(result) != PGRES_TUPLES_OK) {
std::stringstream ss;
ss << "parametrized query execution failed with message: ";
ss << PQresultErrorMessage(result) << std::endl;
@ -111,4 +115,13 @@ PGresult * DBconnection::execute(std::string query,
}
}
void DBconnection::clearResult(PGresult * result) {
PQclear(result);
}
int DBconnection::getIntValue(PGresult * result, int row, int col) {
char * valueStr = PQgetvalue(result,row,col);
return strtol(valueStr, NULL, 10);
}

View File

@ -26,6 +26,11 @@ public:
PGresult * execute(std::string query,
std::vector<QueryParam*> params) throw(ConcordiaException);
void clearResult(PGresult * result);
int getIntValue(PGresult * result, int row, int col);
private:
void close();

View File

@ -1,96 +0,0 @@
#include <libpq-fe.h>
static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
throw ConcordiaException("Closing connection and shutting down");
}
const char *conninfo;
PGconn *conn;
PGresult *res;
int nFields;
int i,
j;
conninfo = "dbname=concordia_server user=concordia";
/* Make a connection to the database */
conn = PQconnectdb(conninfo);
/* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}
/*
* Our test case here involves using a cursor, for which we must be inside
* a transaction block. We could do the whole thing with a single
* PQexec() of "select * from pg_database", but that's too trivial to make
* a good example.
*/
/* Start a transaction block */
res = PQexec(conn, "BEGIN");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
/*
* Should PQclear PGresult whenever it is no longer needed to avoid memory
* leaks
*/
PQclear(res);
/*
* Fetch rows from pg_database, the system catalog of databases
*/
res = PQexec(conn, "SELECT * FROM language");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
jsonWriter.String("attributes");
jsonWriter.StartArray();
/* first, print out the attribute names */
nFields = PQnfields(res);
for (i = 0; i < nFields; i++)
jsonWriter.String(PQfname(res, i));
jsonWriter.EndArray();
jsonWriter.String("values");
jsonWriter.StartArray();
/* next, print out the rows */
for (i = 0; i < PQntuples(res); i++)
{
jsonWriter.StartArray();
for (j = 0; j < nFields; j++)
jsonWriter.String(PQgetvalue(res, i, j));
jsonWriter.EndArray();
}
jsonWriter.EndArray();
PQclear(res);
/* end the transaction */
res = PQexec(conn, "END");
PQclear(res);
/* close the connection to the database and cleanup */
PQfinish(conn);

View File

@ -0,0 +1,34 @@
#include "int_array_param.hpp"
#include <boost/foreach.hpp>
#include <sstream>
IntArrayParam::IntArrayParam(std::vector<int> array) {
std::stringstream ss;
ss << "{";
int index = 0;
BOOST_FOREACH(int & number, array) {
ss << number;
if (index < array.size() - 1) {
ss << ",";
}
index++;
}
ss << "}";
_arrayString = ss.str();
}
IntArrayParam::~IntArrayParam() {
}
const char * IntArrayParam::getValue() {
return _arrayString.c_str();
}
const int IntArrayParam::getLength() {
return _arrayString.size();
}
const int IntArrayParam::isBinary() {
return 0;
}

View File

@ -0,0 +1,27 @@
#ifndef INT_ARRAY_PARAM_HDR
#define INT_ARRAY_PARAM_HDR
#include "query_param.hpp"
#include <string>
#include <vector>
class IntArrayParam : public QueryParam {
public:
/*! Constructor.
*/
IntArrayParam(std::vector<int> array);
/*! Destructor.
*/
virtual ~IntArrayParam();
const char * getValue();
const int getLength();
const int isBinary();
private:
std::string _arrayString;
};
#endif

View File

@ -1,6 +1,5 @@
#include "searcher_controller.hpp"
#include <concordia/substring_occurence.hpp>
#include <vector>
SearcherController::SearcherController(boost::shared_ptr<Concordia> concordia)
@ -13,17 +12,15 @@ SearcherController::~SearcherController() {
void SearcherController::simpleSearch(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string & pattern) {
std::vector<SubstringOccurence> result = _concordia->simpleSearch(pattern);
std::vector<SubstringOccurence> results = _concordia->simpleSearch(pattern);
jsonWriter.StartObject();
jsonWriter.String("status");
jsonWriter.String("success");
jsonWriter.String("count");
jsonWriter.Int(result.size());
jsonWriter.String("firstId");
jsonWriter.Int(result.at(0).getId());
jsonWriter.String("firstOffset");
jsonWriter.Int(result.at(0).getOffset());
jsonWriter.String("results");
jsonWriter.StartArray();
jsonWriter.EndArray();
jsonWriter.EndObject();
}

View File

@ -0,0 +1,8 @@
#include "simple_search_result.hpp"
SimpleSearchResult::SimpleSearchResult() {
}
SimpleSearchResult::~SimpleSearchResult() {
}

View File

@ -0,0 +1,25 @@
#ifndef SIMPLE_SEARCH_RESULT_HDR
#define SIMPLE_SEARCH_RESULT_HDR
#include <string>
class SimpleSearchResult {
public:
/*! Constructor.
*/
SimpleSearchResult();
/*! Destructor.
*/
virtual ~SimpleSearchResult();
private:
int id;
std::string _matchedFragment;
std::string _sourceSegment;
std::string _targetSegment;
};
#endif

View File

@ -4,10 +4,12 @@
#include "query_param.hpp"
#include "string_param.hpp"
#include "int_param.hpp"
#include "int_array_param.hpp"
#include "logger.hpp"
#include <vector>
#include <libpq-fe.h>
#include <boost/foreach.hpp>
#include <concordia/token_annotation.hpp>
UnitDAO::UnitDAO() {
}
@ -22,21 +24,33 @@ int UnitDAO::addSentence(
DBconnection connection;
connection.startTransaction();
std::string query = "INSERT INTO unit(source_segment, target_segment, tm_id) values($1::text,$2::text,$3::integer)";
std::string query = "INSERT INTO unit(source_segment, target_segment, tm_id, source_tokens) values($1::text,$2::text,$3::integer,$4) RETURNING id";
std::vector<QueryParam*> params;
params.push_back(new StringParam(sourceSentence->getSentence()));
params.push_back(new StringParam(targetSentence));
params.push_back(new IntParam(tmId));
params.push_back(new IntArrayParam(_getTokenPositions(sourceSentence)));
connection.execute(query, params);
PGresult * result = connection.execute(query, params);
int newId = connection.getIntValue(result, 0, 0);
connection.clearResult(result);
connection.endTransaction();
BOOST_FOREACH (QueryParam * param, params) {
delete param;
}
//todo return new unit id
return 0;
return newId;
}
std::vector<int> UnitDAO::_getTokenPositions(boost::shared_ptr<TokenizedSentence> ts) {
std::vector<int> result;
BOOST_FOREACH(const TokenAnnotation & token, ts->getTokens()) {
result.push_back(token.getStart());
result.push_back(token.getEnd());
}
return result;
}

View File

@ -2,6 +2,7 @@
#define UNIT_DAO_HDR
#include <string>
#include <vector>
#include <concordia/tokenized_sentence.hpp>
#include <boost/shared_ptr.hpp>
@ -20,7 +21,7 @@ public:
std::string & targetSentence,
int tmId);
private:
std::vector<int> _getTokenPositions(boost::shared_ptr<TokenizedSentence> ts);
};
#endif

View File

@ -18,6 +18,7 @@ CREATE TABLE unit (
id SERIAL PRIMARY KEY,
tm_id integer,
source_segment text,
target_segment text
target_segment text,
source_tokens integer[]
);

1
db/query.txt Normal file
View File

@ -0,0 +1 @@
select substring(source_segment,source_tokens[start_token*2+1]+1,source_tokens[end_token*2+2]-source_tokens[start_token*2+1]) from unit where id = 3;

View File

@ -1,6 +1,6 @@
#!/bin/sh
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":"addSentence", "sourceSentence":"zu\"peł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