From de5d1f4a6347e8d52bb7408cc48a818956138569 Mon Sep 17 00:00:00 2001 From: rjawor Date: Thu, 6 Aug 2015 13:29:28 +0200 Subject: [PATCH] working addSentence --- concordia-server/CMakeLists.txt | 2 + concordia-server/db_connection.cpp | 19 ++++- concordia-server/db_connection.hpp | 5 ++ concordia-server/db_tests.cpp | 96 ----------------------- concordia-server/int_array_param.cpp | 34 ++++++++ concordia-server/int_array_param.hpp | 27 +++++++ concordia-server/searcher_controller.cpp | 13 ++- concordia-server/simple_search_result.cpp | 8 ++ concordia-server/simple_search_result.hpp | 25 ++++++ concordia-server/unit_dao.cpp | 24 ++++-- concordia-server/unit_dao.hpp | 3 +- db/concordia_server.sql | 3 +- db/query.txt | 1 + tests/testCurl.sh | 2 +- 14 files changed, 147 insertions(+), 115 deletions(-) delete mode 100644 concordia-server/db_tests.cpp create mode 100644 concordia-server/int_array_param.cpp create mode 100644 concordia-server/int_array_param.hpp create mode 100644 concordia-server/simple_search_result.cpp create mode 100644 concordia-server/simple_search_result.hpp create mode 100644 db/query.txt diff --git a/concordia-server/CMakeLists.txt b/concordia-server/CMakeLists.txt index 548e426..c5e9856 100644 --- a/concordia-server/CMakeLists.txt +++ b/concordia-server/CMakeLists.txt @@ -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) diff --git a/concordia-server/db_connection.cpp b/concordia-server/db_connection.cpp index 5ffaba3..3996d31 100644 --- a/concordia-server/db_connection.cpp +++ b/concordia-server/db_connection.cpp @@ -2,7 +2,7 @@ #include #include - +#include #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); +} + diff --git a/concordia-server/db_connection.hpp b/concordia-server/db_connection.hpp index 2c04183..a9e0cd7 100644 --- a/concordia-server/db_connection.hpp +++ b/concordia-server/db_connection.hpp @@ -26,6 +26,11 @@ public: PGresult * execute(std::string query, std::vector params) throw(ConcordiaException); + + void clearResult(PGresult * result); + + int getIntValue(PGresult * result, int row, int col); + private: void close(); diff --git a/concordia-server/db_tests.cpp b/concordia-server/db_tests.cpp deleted file mode 100644 index da8f43e..0000000 --- a/concordia-server/db_tests.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include - -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); - - diff --git a/concordia-server/int_array_param.cpp b/concordia-server/int_array_param.cpp new file mode 100644 index 0000000..8a15c5d --- /dev/null +++ b/concordia-server/int_array_param.cpp @@ -0,0 +1,34 @@ +#include "int_array_param.hpp" + +#include +#include + +IntArrayParam::IntArrayParam(std::vector 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; +} diff --git a/concordia-server/int_array_param.hpp b/concordia-server/int_array_param.hpp new file mode 100644 index 0000000..a7b7e89 --- /dev/null +++ b/concordia-server/int_array_param.hpp @@ -0,0 +1,27 @@ +#ifndef INT_ARRAY_PARAM_HDR +#define INT_ARRAY_PARAM_HDR + +#include "query_param.hpp" + +#include +#include + +class IntArrayParam : public QueryParam { +public: + /*! Constructor. + */ + IntArrayParam(std::vector array); + /*! Destructor. + */ + virtual ~IntArrayParam(); + + const char * getValue(); + + const int getLength(); + + const int isBinary(); +private: + std::string _arrayString; +}; + +#endif diff --git a/concordia-server/searcher_controller.cpp b/concordia-server/searcher_controller.cpp index 1d3d949..0d4b15a 100644 --- a/concordia-server/searcher_controller.cpp +++ b/concordia-server/searcher_controller.cpp @@ -1,6 +1,5 @@ #include "searcher_controller.hpp" -#include #include SearcherController::SearcherController(boost::shared_ptr concordia) @@ -13,17 +12,15 @@ SearcherController::~SearcherController() { void SearcherController::simpleSearch(rapidjson::Writer & jsonWriter, std::string & pattern) { - std::vector result = _concordia->simpleSearch(pattern); + std::vector 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(); } diff --git a/concordia-server/simple_search_result.cpp b/concordia-server/simple_search_result.cpp new file mode 100644 index 0000000..968285c --- /dev/null +++ b/concordia-server/simple_search_result.cpp @@ -0,0 +1,8 @@ +#include "simple_search_result.hpp" + +SimpleSearchResult::SimpleSearchResult() { +} + +SimpleSearchResult::~SimpleSearchResult() { +} + diff --git a/concordia-server/simple_search_result.hpp b/concordia-server/simple_search_result.hpp new file mode 100644 index 0000000..bf114a2 --- /dev/null +++ b/concordia-server/simple_search_result.hpp @@ -0,0 +1,25 @@ +#ifndef SIMPLE_SEARCH_RESULT_HDR +#define SIMPLE_SEARCH_RESULT_HDR + +#include + +class SimpleSearchResult { +public: + /*! Constructor. + */ + SimpleSearchResult(); + /*! Destructor. + */ + virtual ~SimpleSearchResult(); + +private: + int id; + + std::string _matchedFragment; + + std::string _sourceSegment; + + std::string _targetSegment; +}; + +#endif diff --git a/concordia-server/unit_dao.cpp b/concordia-server/unit_dao.cpp index 453115d..d407bfa 100644 --- a/concordia-server/unit_dao.cpp +++ b/concordia-server/unit_dao.cpp @@ -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 +#include #include +#include 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 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 UnitDAO::_getTokenPositions(boost::shared_ptr ts) { + std::vector result; + BOOST_FOREACH(const TokenAnnotation & token, ts->getTokens()) { + result.push_back(token.getStart()); + result.push_back(token.getEnd()); + } + return result; +} + + diff --git a/concordia-server/unit_dao.hpp b/concordia-server/unit_dao.hpp index 97fa334..dee0bab 100644 --- a/concordia-server/unit_dao.hpp +++ b/concordia-server/unit_dao.hpp @@ -2,6 +2,7 @@ #define UNIT_DAO_HDR #include +#include #include #include @@ -20,7 +21,7 @@ public: std::string & targetSentence, int tmId); private: - + std::vector _getTokenPositions(boost::shared_ptr ts); }; #endif diff --git a/db/concordia_server.sql b/db/concordia_server.sql index d563cbc..f0106c8 100644 --- a/db/concordia_server.sql +++ b/db/concordia_server.sql @@ -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[] ); diff --git a/db/query.txt b/db/query.txt new file mode 100644 index 0000000..26b5d6b --- /dev/null +++ b/db/query.txt @@ -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; diff --git a/tests/testCurl.sh b/tests/testCurl.sh index c7b1750..2661baf 100755 --- a/tests/testCurl.sh +++ b/tests/testCurl.sh @@ -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