8#include <boost/date_time/gregorian/gregorian.hpp>
9#include <boost/date_time/posix_time/ptime.hpp>
33 OPENTREP_Service (std::ostream& ioLogStream,
const PORFilePath_T& iPORFilepath,
41 : _opentrepServiceContext (NULL) {
42 init (ioLogStream, iPORFilepath, iTravelDBFilePath, iSQLDBType,
43 iSQLDBConnStr, iDeploymentNumber, iShouldIndexNonIATAPOR,
44 iShouldIndexPORInXapian, iShouldAddPORInSQLDB);
49 OPENTREP_Service (std::ostream& ioLogStream,
54 : _opentrepServiceContext (NULL) {
55 init (ioLogStream, iTravelDBFilePath, iSQLDBType, iSQLDBConnStr,
65 OPENTREP_Service::OPENTREP_Service (
const OPENTREP_Service& iService) {
77 std::ostream& ioLogOutputFile) {
82 SQLDBConnectionString_T
88 std::string oSQLDBConnStr =
89 static_cast<const std::string
> (iSQLDBConnStr);
101 void OPENTREP_Service::init (std::ostream& ioLogStream,
102 const TravelDBFilePath_T& iTravelDBFilePath,
103 const DBType& iSQLDBType,
104 const SQLDBConnectionString_T& iSQLDBConnStr,
110 const SQLDBConnectionString_T& lSQLDBConnStr =
114 OPENTREP_ServiceContext& lOPENTREP_ServiceContext =
116 iSQLDBType, lSQLDBConnStr,
118 _opentrepServiceContext = &lOPENTREP_ServiceContext;
122 lOPENTREP_ServiceContext.setWorld (lWorld);
126 void OPENTREP_Service::
127 init (std::ostream& ioLogStream,
140 const SQLDBConnectionString_T& lSQLDBConnStr =
144 OPENTREP_ServiceContext& lOPENTREP_ServiceContext =
147 iSQLDBType, lSQLDBConnStr,
149 iShouldIndexNonIATAPOR,
150 iShouldIndexPORInXapian,
151 iShouldAddPORInSQLDB);
152 _opentrepServiceContext = &lOPENTREP_ServiceContext;
156 lOPENTREP_ServiceContext.setWorld (lWorld);
160 void OPENTREP_Service::finalise() {
165 if (_opentrepServiceContext == NULL) {
169 assert (_opentrepServiceContext != NULL);
176 return lDeploymentNumber;
181 if (_opentrepServiceContext == NULL) {
185 assert (_opentrepServiceContext != NULL);
200 lSQLDBConnectionString);
208 bool oExistXapianDBDir =
210 return oExistXapianDBDir;
217 if (_opentrepServiceContext == NULL) {
221 assert (_opentrepServiceContext != NULL);
230 oNbOfEntries = XapianIndexManager::getSize (lTravelDBFilePath);
231 const double lIndexSizeMeasure = lIndexSizeChronometer.
elapsed();
235 << lIndexSizeMeasure <<
" - "
236 << lOPENTREP_ServiceContext.
display());
247 if (_opentrepServiceContext == NULL) {
251 assert (_opentrepServiceContext != NULL);
260 oNbOfMatches = XapianIndexManager::drawRandomLocations (lTravelDBFilePath,
263 const double lRandomGetMeasure = lRandomGetChronometer.
elapsed();
267 << lRandomGetMeasure <<
" - "
268 << lOPENTREP_ServiceContext.
display());
275 bool oCreationSuccessful =
true;
277 if (_opentrepServiceContext == NULL) {
281 assert (_opentrepServiceContext != NULL);
292 <<
"Hence nothing is done at that stage. "
293 <<
" - " << lOPENTREP_ServiceContext.
display());
294 return oCreationSuccessful;
307 lDBCreationChronometer.
start();
311 oCreationSuccessful =
315 const double lDBCreationMeasure = lDBCreationChronometer.
elapsed();
319 <<
" - " << lOPENTREP_ServiceContext.
display());
321 return oCreationSuccessful;
327 if (_opentrepServiceContext == NULL) {
331 assert (_opentrepServiceContext != NULL);
339 << lOPENTREP_ServiceContext.
display());
344 if (_opentrepServiceContext == NULL) {
348 assert (_opentrepServiceContext != NULL);
359 <<
"Hence nothing is done at that stage. "
360 <<
" - " << lOPENTREP_ServiceContext.
display());
370 lDBCreationChronometer.
start();
373 soci::session* lSociSession_ptr =
375 assert (lSociSession_ptr != NULL);
376 soci::session& lSociSession = *lSociSession_ptr;
385 const double lDBCreationMeasure = lDBCreationChronometer.
elapsed();
389 <<
" - " << lOPENTREP_ServiceContext.
display());
394 if (_opentrepServiceContext == NULL) {
398 assert (_opentrepServiceContext != NULL);
409 <<
"Hence nothing is done at that stage. "
410 <<
" - " << lOPENTREP_ServiceContext.
display());
420 lDBCreationChronometer.
start();
423 soci::session* lSociSession_ptr =
425 assert (lSociSession_ptr != NULL);
426 soci::session& lSociSession = *lSociSession_ptr;
435 const double lDBCreationMeasure = lDBCreationChronometer.
elapsed();
439 << lDBCreationMeasure <<
" - "
440 << lOPENTREP_ServiceContext.
display());
447 if (_opentrepServiceContext == NULL) {
451 assert (_opentrepServiceContext != NULL);
468 << oDeploymentNumber <<
" - "
469 << lOPENTREP_ServiceContext.
display());
471 return oDeploymentNumber;
479 if (_opentrepServiceContext == NULL) {
483 assert (_opentrepServiceContext != NULL);
487 oShouldIndexNonIATAPOR =
491 oShouldIndexNonIATAPOR = !(oShouldIndexNonIATAPOR);
498 << oShouldIndexNonIATAPOR <<
" - "
499 << lOPENTREP_ServiceContext.
display());
501 return oShouldIndexNonIATAPOR;
509 if (_opentrepServiceContext == NULL) {
513 assert (_opentrepServiceContext != NULL);
517 oShouldIndexPORInXapian =
521 oShouldIndexPORInXapian = !(oShouldIndexPORInXapian);
524 lOPENTREP_ServiceContext.
525 setShouldIndexPORInXapianFlag (oShouldIndexPORInXapian);
529 << oShouldIndexPORInXapian <<
" - "
530 << lOPENTREP_ServiceContext.
display());
532 return oShouldIndexPORInXapian;
540 if (_opentrepServiceContext == NULL) {
544 assert (_opentrepServiceContext != NULL);
548 oShouldAddPORInSQLDB =
552 oShouldAddPORInSQLDB = !(oShouldAddPORInSQLDB);
559 << oShouldAddPORInSQLDB <<
" - "
560 << lOPENTREP_ServiceContext.
display());
562 return oShouldAddPORInSQLDB;
569 if (_opentrepServiceContext == NULL) {
573 assert (_opentrepServiceContext != NULL);
585 lDBListChronometer.
start();
588 soci::session* lSociSession_ptr =
590 assert (lSociSession_ptr != NULL);
591 soci::session& lSociSession = *lSociSession_ptr;
600 const double lDBListMeasure = lDBListChronometer.
elapsed();
604 <<
" - " << lOPENTREP_ServiceContext.
display());
616 if (_opentrepServiceContext == NULL) {
620 assert (_opentrepServiceContext != NULL);
632 lDBListChronometer.
start();
635 soci::session* lSociSession_ptr =
637 assert (lSociSession_ptr != NULL);
638 soci::session& lSociSession = *lSociSession_ptr;
641 const bool lUniqueEntry =
false;
643 ioLocationList, lUniqueEntry);
649 const double lDBListMeasure = lDBListChronometer.
elapsed();
653 <<
" - " << lOPENTREP_ServiceContext.
display());
665 if (_opentrepServiceContext == NULL) {
669 assert (_opentrepServiceContext != NULL);
681 lDBListChronometer.
start();
684 soci::session* lSociSession_ptr =
686 assert (lSociSession_ptr != NULL);
687 soci::session& lSociSession = *lSociSession_ptr;
697 const double lDBListMeasure = lDBListChronometer.
elapsed();
701 <<
" - " << lOPENTREP_ServiceContext.
display());
713 if (_opentrepServiceContext == NULL) {
717 assert (_opentrepServiceContext != NULL);
729 lDBListChronometer.
start();
732 soci::session* lSociSession_ptr =
734 assert (lSociSession_ptr != NULL);
735 soci::session& lSociSession = *lSociSession_ptr;
745 const double lDBListMeasure = lDBListChronometer.
elapsed();
749 <<
" - " << lOPENTREP_ServiceContext.
display());
761 if (_opentrepServiceContext == NULL) {
765 assert (_opentrepServiceContext != NULL);
777 lDBListChronometer.
start();
780 soci::session* lSociSession_ptr =
782 assert (lSociSession_ptr != NULL);
783 soci::session& lSociSession = *lSociSession_ptr;
786 const bool lUniqueEntry =
false;
795 const double lDBListMeasure = lDBListChronometer.
elapsed();
799 <<
" - " << lOPENTREP_ServiceContext.
display());
810 if (_opentrepServiceContext == NULL) {
814 assert (_opentrepServiceContext != NULL);
826 lDBListChronometer.
start();
829 soci::session* lSociSession_ptr =
831 assert (lSociSession_ptr != NULL);
832 soci::session& lSociSession = *lSociSession_ptr;
842 const double lDBListMeasure = lDBListChronometer.
elapsed();
846 <<
" - " << lOPENTREP_ServiceContext.
display());
858 if (_opentrepServiceContext == NULL) {
862 assert (_opentrepServiceContext != NULL);
874 lDBListChronometer.
start();
877 soci::session* lSociSession_ptr =
879 assert (lSociSession_ptr != NULL);
880 soci::session& lSociSession = *lSociSession_ptr;
890 const double lDBListMeasure = lDBListChronometer.
elapsed();
894 <<
" - " << lOPENTREP_ServiceContext.
display());
904 if (_opentrepServiceContext == NULL) {
908 assert (_opentrepServiceContext != NULL);
943 lInsertIntoXapianAndSQLDBChronometer.
start();
944 oNbOfEntries = IndexBuilder::buildSearchIndex (lPORFilePath,
947 lSQLDBConnectionString,
949 lShouldIndexPORInXapian,
950 lShouldAddPORInSQLDB,
952 const double lInsertIntoXapianAndSQLDBMeasure =
953 lInsertIntoXapianAndSQLDBChronometer.
elapsed();
957 << lInsertIntoXapianAndSQLDBMeasure <<
" - "
958 << lOPENTREP_ServiceContext.
display());
970 if (_opentrepServiceContext == NULL) {
974 assert (_opentrepServiceContext != NULL);
982 boost::posix_time::ptime lNowDateTime =
983 boost::posix_time::second_clock::local_time();
988 <<
"==================================================="
990 << lNowDateTime <<
" - Match query '" << iTravelQuery
991 <<
"' on Xapian database (index)");
994 if (iTravelQuery.empty() ==
true) {
995 std::ostringstream errorStr;
996 errorStr <<
"The travel query is empty.";
1007 if (lExistXapianDBDir ==
false) {
1008 std::ostringstream errorStr;
1009 errorStr <<
"The file-path to the Xapian database/index ('"
1010 << lTravelDBFilePath <<
"') does not exist or is not a "
1011 <<
"directory." << std::endl;
1012 errorStr <<
"That usually means that the OpenTREP indexer "
1013 <<
"(opentrep-indexer) has not been launched yet, "
1014 <<
"or that it has operated on a different Xapian "
1015 <<
"database/index file-path, for instance with a different "
1016 <<
"deployment number";
1031 lRequestInterpreterChronometer.
start();
1033 RequestInterpreter::interpretTravelRequest (lTravelDBFilePath,
1034 lSQLDBType, lSQLDBConnString,
1036 ioLocationList, ioWordList,
1038 const double lRequestInterpreterMeasure =
1039 lRequestInterpreterChronometer.
elapsed();
1043 << lRequestInterpreterMeasure <<
" - "
1044 << lOPENTREP_ServiceContext.
display());
#define OPENTREP_LOG_ERROR(iToBeLogged)
#define OPENTREP_LOG_DEBUG(iToBeLogged)
static void terminateSQLDBSession(const DBType &, const SQLDBConnectionString_T &, soci::session &)
static void createSQLDBTables(soci::session &)
static NbOfDBEntries_T getPORByUICCode(soci::session &, const UICCode_T &, LocationList_T &)
static NbOfDBEntries_T getPORByICAOCode(soci::session &, const ICAOCode_T &, LocationList_T &)
static soci::session * initSQLDBSession(const DBType &, const SQLDBConnectionString_T &)
static NbOfDBEntries_T getPORByFAACode(soci::session &, const FAACode_T &, LocationList_T &)
static void createSQLDBIndexes(soci::session &)
static NbOfDBEntries_T displayCount(soci::session &)
static NbOfDBEntries_T getPORByUNLOCode(soci::session &, const UNLOCode_T &, LocationList_T &, const bool iUniqueEntry)
static NbOfDBEntries_T getPORByGeonameID(soci::session &, const GeonamesID_T &, LocationList_T &)
static NbOfDBEntries_T getPORByIATACode(soci::session &, const IATACode_T &, LocationList_T &, const bool iUniqueEntry)
static bool createSQLDBUser(const DBType &, const SQLDBConnectionString_T &, const DeploymentNumber_T &)
OPENTREP_ServiceContext & create(const TravelDBFilePath_T &, const DBType &, const SQLDBConnectionString_T &, const DeploymentNumber_T &)
static FacOpenTrepServiceContext & instance()
static FacWorld & instance()
static bool checkXapianDBOnFileSystem(const TravelDBFilePath_T &)
static Logger & instance()
void setLogParameters(const LOG::EN_LogLevel iLogLevel, std::ostream &ioLogStream)
Class holding the context of the OpenTrep services.
const shouldAddPORInSQLDB_T & getShouldAddPORInSQLDB() const
const OTransliterator & getTransliterator() const
const SQLDBConnectionString_T & getSQLDBConnectionString() const
const DBType & getSQLDBType() const
void setSQLDBConnectionString(const std::string &iSQLDBConnStr)
void setShouldIncludeAllPORFlag(const shouldIndexNonIATAPOR_T &iShouldIndexNonIATAPOR)
void setShouldAddPORInSQLDB(const shouldAddPORInSQLDB_T &iShouldAddPORInSQLDB)
const shouldIndexPORInXapian_T & getShouldIndexPORInXapianFlag() const
void setDeploymentNumber(const DeploymentNumber_T &iDeploymentNumber)
const std::string display() const
const DeploymentNumber_T & getDeploymentNumber() const
const PORFilePath_T & getPORFilePath() const
const TravelDBFilePath_T & getTravelDBFilePath() const
const shouldIndexNonIATAPOR_T & getShouldIncludeAllPORFlag() const
NbOfMatches_T listByIataCode(const IATACode_T &, LocationList_T &)
NbOfMatches_T listByUNLOCode(const UNLOCode_T &, LocationList_T &)
void setSQLDBConnectString(const SQLDBConnectionString_T &)
bool checkXapianDBOnFileSystem(const TravelDBFilePath_T &) const
OPENTREP::shouldIndexNonIATAPOR_T toggleShouldIncludeAllPORFlag()
std::pair< const PORFilePath_T, const DBFilePathPair_T > FilePathSet_T
FilePathSet_T getFilePaths() const
NbOfMatches_T listByFaaCode(const FAACode_T &, LocationList_T &)
NbOfMatches_T listByUICCode(const UICCode_T &, LocationList_T &)
void createSQLDBIndexes()
NbOfMatches_T listByIcaoCode(const ICAOCode_T &, LocationList_T &)
NbOfMatches_T interpretTravelRequest(const std::string &iTravelQuery, LocationList_T &, WordList_T &)
NbOfMatches_T listByGeonameID(const GeonamesID_T &, LocationList_T &)
OPENTREP_Service(std::ostream &ioLogStream, const TravelDBFilePath_T &, const DBType &, const SQLDBConnectionString_T &, const DeploymentNumber_T &)
NbOfDBEntries_T getNbOfPORFromDB()
NbOfMatches_T drawRandomLocations(const NbOfMatches_T &iNbOfDraws, LocationList_T &)
const DeploymentNumber_T & getDeploymentNumber() const
NbOfDBEntries_T getIndexSize()
OPENTREP::shouldIndexPORInXapian_T toggleShouldIndexPORInXapianFlag()
std::pair< const TravelDBFilePath_T, const SQLDBConnectionString_T > DBFilePathPair_T
NbOfDBEntries_T insertIntoDBAndXapian()
OPENTREP::DeploymentNumber_T toggleDeploymentNumber()
OPENTREP::shouldAddPORInSQLDB_T toggleShouldAddPORInSQLDBFlag()
const std::string DEFAULT_OPENTREP_SQLITE_DB_FILEPATH
std::list< Word_T > WordList_T
const std::string DEFAULT_OPENTREP_PG_CONN_STRING
SQLDBConnectionString_T getSQLConnStr(const DBType &iSQLDBType, const SQLDBConnectionString_T &iSQLDBConnStr)
bool shouldAddPORInSQLDB_T
const unsigned short DEFAULT_OPENTREP_DEPLOYMENT_NUMBER_SIZE
unsigned int NbOfDBEntries_T
std::list< Location > LocationList_T
void logInit(const LOG::EN_LogLevel iLogLevel, std::ostream &ioLogOutputFile)
const unsigned short DEFAULT_OPENTREP_DEPLOYMENT_NUMBER
bool shouldIndexPORInXapian_T
const std::string DEFAULT_OPENTREP_MYSQL_CONN_STRING
unsigned short DeploymentNumber_T
unsigned short NbOfMatches_T
unsigned int GeonamesID_T
bool shouldIndexNonIATAPOR_T
Structure allowing measuring the time elapsed between two events.
Enumeration of database types.