From 5e586e297f8fa58740318ac2ff273c862ec84ecc Mon Sep 17 00:00:00 2001 From: rjawor Date: Tue, 4 Aug 2015 12:03:28 +0200 Subject: [PATCH] first working db --- CMakeLists.txt | 7 +++ concordia-server/CMakeLists.txt | 1 + concordia-server/concordia_server.cpp | 17 +++++-- concordia-server/config.hpp | 4 ++ concordia-server/config.hpp.in | 4 ++ concordia-server/db_connection.cpp | 71 +++++++++++++++++++++++++++ concordia-server/db_connection.hpp | 33 +++++++++++++ concordia-server/index_controller.cpp | 14 ++++-- concordia-server/index_controller.hpp | 10 +++- concordia-server/query_param.cpp | 71 +++++++++++++++++++++++++++ concordia-server/query_param.hpp | 17 +++++++ concordia-server/unit_dao.cpp | 11 ++++- concordia-server/unit_dao.hpp | 7 ++- tests/testCurl.sh | 4 +- 14 files changed, 256 insertions(+), 15 deletions(-) create mode 100644 concordia-server/db_connection.cpp create mode 100644 concordia-server/db_connection.hpp create mode 100644 concordia-server/query_param.cpp create mode 100644 concordia-server/query_param.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2361184..d0808a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" diff --git a/concordia-server/CMakeLists.txt b/concordia-server/CMakeLists.txt index a595eca..6abcb01 100644 --- a/concordia-server/CMakeLists.txt +++ b/concordia-server/CMakeLists.txt @@ -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) diff --git a/concordia-server/concordia_server.cpp b/concordia-server/concordia_server.cpp index 76b1714..4d5f1ac 100644 --- a/concordia-server/concordia_server.cpp +++ b/concordia-server/concordia_server.cpp @@ -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"); } diff --git a/concordia-server/config.hpp b/concordia-server/config.hpp index e11d020..3ee498f 100644 --- a/concordia-server/config.hpp +++ b/concordia-server/config.hpp @@ -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" diff --git a/concordia-server/config.hpp.in b/concordia-server/config.hpp.in index e27ceeb..6a03d4c 100644 --- a/concordia-server/config.hpp.in +++ b/concordia-server/config.hpp.in @@ -1 +1,5 @@ #define CONFIG_FILE_PATH "@CONFIG_FILE_PATH@" + +// database connection information +#define DB_NAME "@DB_NAME@" +#define DB_USER "@DB_USER@" diff --git a/concordia-server/db_connection.cpp b/concordia-server/db_connection.cpp new file mode 100644 index 0000000..f4bd8c3 --- /dev/null +++ b/concordia-server/db_connection.cpp @@ -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 params) throw(ConcordiaException) { +} + + diff --git a/concordia-server/db_connection.hpp b/concordia-server/db_connection.hpp new file mode 100644 index 0000000..cf8e7d9 --- /dev/null +++ b/concordia-server/db_connection.hpp @@ -0,0 +1,33 @@ +#ifndef DB_MANAGER_HDR +#define DB_MANAGER_HDR + +#include +#include +#include + +#include + +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 params) throw(ConcordiaException); +private: + void close(); + + PGconn * _connection; +}; + +#endif diff --git a/concordia-server/index_controller.cpp b/concordia-server/index_controller.cpp index 728d5c0..393dd6c 100644 --- a/concordia-server/index_controller.cpp +++ b/concordia-server/index_controller.cpp @@ -1,5 +1,6 @@ #include "index_controller.hpp" +#include #include "json_generator.hpp" IndexController::IndexController(boost::shared_ptr concordia) @@ -11,13 +12,18 @@ IndexController::~IndexController() { } -void IndexController::addSentence(rapidjson::Writer & jsonWriter, std::string & sentence) { +void IndexController::addSentence( + rapidjson::Writer & jsonWriter, + std::string & sourceSentence, + std::string & targetSentence, + int tmId) { try { - Example example(sentence, 0); - _concordia->addExample(example); + boost::shared_ptr 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"); diff --git a/concordia-server/index_controller.hpp b/concordia-server/index_controller.hpp index e004e5c..51a7692 100644 --- a/concordia-server/index_controller.hpp +++ b/concordia-server/index_controller.hpp @@ -6,6 +6,8 @@ #include #include +#include "unit_dao.hpp" + #include "rapidjson/writer.h" class IndexController { @@ -18,11 +20,15 @@ public: */ virtual ~IndexController(); - void addSentence(rapidjson::Writer & jsonWriter, std::string & sentence); + void addSentence(rapidjson::Writer & jsonWriter, + std::string & sourceSentence, + std::string & targetSentence, + int tmId); private: boost::shared_ptr _concordia; - + + UnitDAO _unitDAO; }; #endif diff --git a/concordia-server/query_param.cpp b/concordia-server/query_param.cpp new file mode 100644 index 0000000..f4bd8c3 --- /dev/null +++ b/concordia-server/query_param.cpp @@ -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 params) throw(ConcordiaException) { +} + + diff --git a/concordia-server/query_param.hpp b/concordia-server/query_param.hpp new file mode 100644 index 0000000..7972635 --- /dev/null +++ b/concordia-server/query_param.hpp @@ -0,0 +1,17 @@ +#ifndef QUERY_PARAM_HDR +#define QUERY_PARAM_HDR + +class QueryParam { +public: + /*! Constructor. + */ + QueryParam() throw(ConcordiaException); + /*! Destructor. + */ + virtual ~QueryParam(); + +private: + +}; + +#endif diff --git a/concordia-server/unit_dao.cpp b/concordia-server/unit_dao.cpp index f004bea..9a0bcc6 100644 --- a/concordia-server/unit_dao.cpp +++ b/concordia-server/unit_dao.cpp @@ -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 sourceSentence, + std::string & targetSentence, + int tmId) { + DBconnection connection; + connection.startTransaction(); + connection.execute("INSERT INTO unit(source_segment) values('just testing')"); + connection.endTransaction(); + } diff --git a/concordia-server/unit_dao.hpp b/concordia-server/unit_dao.hpp index 88bf868..f3aa37f 100644 --- a/concordia-server/unit_dao.hpp +++ b/concordia-server/unit_dao.hpp @@ -3,7 +3,9 @@ #include +#include #include +#include class UnitDAO { public: @@ -14,7 +16,10 @@ public: */ virtual ~UnitDAO(); - SUFFIX_MARKER_TYPE addSentence(std::string & sentence); + SUFFIX_MARKER_TYPE addSentence( + boost::shared_ptr sourceSentence, + std::string & targetSentence, + int tmId); private: }; diff --git a/tests/testCurl.sh b/tests/testCurl.sh index df73f45..882fb40 100755 --- a/tests/testCurl.sh +++ b/tests/testCurl.sh @@ -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