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
87 std::string oSQLDBConnStr =
88 static_cast<const std::string
> (iSQLDBConnStr);
97 void OPENTREP_Service::init (std::ostream& ioLogStream,
98 const TravelDBFilePath_T& iTravelDBFilePath,
99 const DBType& iSQLDBType,
100 const SQLDBConnectionString_T& iSQLDBConnStr,
106 const SQLDBConnectionString_T& lSQLDBConnStr =
110 OPENTREP_ServiceContext& lOPENTREP_ServiceContext =
112 iSQLDBType, lSQLDBConnStr,
114 _opentrepServiceContext = &lOPENTREP_ServiceContext;
118 lOPENTREP_ServiceContext.setWorld (lWorld);
122 void OPENTREP_Service::
123 init (std::ostream& ioLogStream,
136 const SQLDBConnectionString_T& lSQLDBConnStr =
140 OPENTREP_ServiceContext& lOPENTREP_ServiceContext =
143 iSQLDBType, lSQLDBConnStr,
145 iShouldIndexNonIATAPOR,
146 iShouldIndexPORInXapian,
147 iShouldAddPORInSQLDB);
148 _opentrepServiceContext = &lOPENTREP_ServiceContext;
152 lOPENTREP_ServiceContext.setWorld (lWorld);
156 void OPENTREP_Service::finalise() {
161 if (_opentrepServiceContext == NULL) {
165 assert (_opentrepServiceContext != NULL);
172 return lDeploymentNumber;
177 if (_opentrepServiceContext == NULL) {
181 assert (_opentrepServiceContext != NULL);
196 lSQLDBConnectionString);
204 bool oExistXapianDBDir =
206 return oExistXapianDBDir;
213 if (_opentrepServiceContext == NULL) {
217 assert (_opentrepServiceContext != NULL);
226 oNbOfEntries = XapianIndexManager::getSize (lTravelDBFilePath);
227 const double lIndexSizeMeasure = lIndexSizeChronometer.
elapsed();
231 << lIndexSizeMeasure <<
" - "
232 << lOPENTREP_ServiceContext.
display());
243 if (_opentrepServiceContext == NULL) {
247 assert (_opentrepServiceContext != NULL);
256 oNbOfMatches = XapianIndexManager::drawRandomLocations (lTravelDBFilePath,
259 const double lRandomGetMeasure = lRandomGetChronometer.
elapsed();
263 << lRandomGetMeasure <<
" - "
264 << lOPENTREP_ServiceContext.
display());
271 bool oCreationSuccessful =
true;
273 if (_opentrepServiceContext == NULL) {
277 assert (_opentrepServiceContext != NULL);
288 <<
"Hence nothing is done at that stage. "
289 <<
" - " << lOPENTREP_ServiceContext.
display());
290 return oCreationSuccessful;
303 lDBCreationChronometer.
start();
307 oCreationSuccessful =
311 const double lDBCreationMeasure = lDBCreationChronometer.
elapsed();
315 <<
" - " << lOPENTREP_ServiceContext.
display());
317 return oCreationSuccessful;
323 if (_opentrepServiceContext == NULL) {
327 assert (_opentrepServiceContext != NULL);
335 << lOPENTREP_ServiceContext.
display());
340 if (_opentrepServiceContext == NULL) {
344 assert (_opentrepServiceContext != NULL);
355 <<
"Hence nothing is done at that stage. "
356 <<
" - " << lOPENTREP_ServiceContext.
display());
366 lDBCreationChronometer.
start();
369 soci::session* lSociSession_ptr =
371 assert (lSociSession_ptr != NULL);
372 soci::session& lSociSession = *lSociSession_ptr;
381 const double lDBCreationMeasure = lDBCreationChronometer.
elapsed();
385 <<
" - " << lOPENTREP_ServiceContext.
display());
390 if (_opentrepServiceContext == NULL) {
394 assert (_opentrepServiceContext != NULL);
405 <<
"Hence nothing is done at that stage. "
406 <<
" - " << lOPENTREP_ServiceContext.
display());
416 lDBCreationChronometer.
start();
419 soci::session* lSociSession_ptr =
421 assert (lSociSession_ptr != NULL);
422 soci::session& lSociSession = *lSociSession_ptr;
431 const double lDBCreationMeasure = lDBCreationChronometer.
elapsed();
435 << lDBCreationMeasure <<
" - "
436 << lOPENTREP_ServiceContext.
display());
443 if (_opentrepServiceContext == NULL) {
447 assert (_opentrepServiceContext != NULL);
464 << oDeploymentNumber <<
" - "
465 << lOPENTREP_ServiceContext.
display());
467 return oDeploymentNumber;
475 if (_opentrepServiceContext == NULL) {
479 assert (_opentrepServiceContext != NULL);
483 oShouldIndexNonIATAPOR =
487 oShouldIndexNonIATAPOR = !(oShouldIndexNonIATAPOR);
494 << oShouldIndexNonIATAPOR <<
" - "
495 << lOPENTREP_ServiceContext.
display());
497 return oShouldIndexNonIATAPOR;
505 if (_opentrepServiceContext == NULL) {
509 assert (_opentrepServiceContext != NULL);
513 oShouldIndexPORInXapian =
517 oShouldIndexPORInXapian = !(oShouldIndexPORInXapian);
520 lOPENTREP_ServiceContext.
521 setShouldIndexPORInXapianFlag (oShouldIndexPORInXapian);
525 << oShouldIndexPORInXapian <<
" - "
526 << lOPENTREP_ServiceContext.
display());
528 return oShouldIndexPORInXapian;
536 if (_opentrepServiceContext == NULL) {
540 assert (_opentrepServiceContext != NULL);
544 oShouldAddPORInSQLDB =
548 oShouldAddPORInSQLDB = !(oShouldAddPORInSQLDB);
555 << oShouldAddPORInSQLDB <<
" - "
556 << lOPENTREP_ServiceContext.
display());
558 return oShouldAddPORInSQLDB;
565 if (_opentrepServiceContext == NULL) {
569 assert (_opentrepServiceContext != NULL);
581 lDBListChronometer.
start();
584 soci::session* lSociSession_ptr =
586 assert (lSociSession_ptr != NULL);
587 soci::session& lSociSession = *lSociSession_ptr;
596 const double lDBListMeasure = lDBListChronometer.
elapsed();
600 <<
" - " << lOPENTREP_ServiceContext.
display());
612 if (_opentrepServiceContext == NULL) {
616 assert (_opentrepServiceContext != NULL);
628 lDBListChronometer.
start();
631 soci::session* lSociSession_ptr =
633 assert (lSociSession_ptr != NULL);
634 soci::session& lSociSession = *lSociSession_ptr;
637 const bool lUniqueEntry =
false;
639 ioLocationList, lUniqueEntry);
645 const double lDBListMeasure = lDBListChronometer.
elapsed();
649 <<
" - " << lOPENTREP_ServiceContext.
display());
661 if (_opentrepServiceContext == NULL) {
665 assert (_opentrepServiceContext != NULL);
677 lDBListChronometer.
start();
680 soci::session* lSociSession_ptr =
682 assert (lSociSession_ptr != NULL);
683 soci::session& lSociSession = *lSociSession_ptr;
693 const double lDBListMeasure = lDBListChronometer.
elapsed();
697 <<
" - " << lOPENTREP_ServiceContext.
display());
709 if (_opentrepServiceContext == NULL) {
713 assert (_opentrepServiceContext != NULL);
725 lDBListChronometer.
start();
728 soci::session* lSociSession_ptr =
730 assert (lSociSession_ptr != NULL);
731 soci::session& lSociSession = *lSociSession_ptr;
741 const double lDBListMeasure = lDBListChronometer.
elapsed();
745 <<
" - " << lOPENTREP_ServiceContext.
display());
757 if (_opentrepServiceContext == NULL) {
761 assert (_opentrepServiceContext != NULL);
773 lDBListChronometer.
start();
776 soci::session* lSociSession_ptr =
778 assert (lSociSession_ptr != NULL);
779 soci::session& lSociSession = *lSociSession_ptr;
782 const bool lUniqueEntry =
false;
791 const double lDBListMeasure = lDBListChronometer.
elapsed();
795 <<
" - " << lOPENTREP_ServiceContext.
display());
806 if (_opentrepServiceContext == NULL) {
810 assert (_opentrepServiceContext != NULL);
822 lDBListChronometer.
start();
825 soci::session* lSociSession_ptr =
827 assert (lSociSession_ptr != NULL);
828 soci::session& lSociSession = *lSociSession_ptr;
838 const double lDBListMeasure = lDBListChronometer.
elapsed();
842 <<
" - " << lOPENTREP_ServiceContext.
display());
854 if (_opentrepServiceContext == NULL) {
858 assert (_opentrepServiceContext != NULL);
870 lDBListChronometer.
start();
873 soci::session* lSociSession_ptr =
875 assert (lSociSession_ptr != NULL);
876 soci::session& lSociSession = *lSociSession_ptr;
886 const double lDBListMeasure = lDBListChronometer.
elapsed();
890 <<
" - " << lOPENTREP_ServiceContext.
display());
900 if (_opentrepServiceContext == NULL) {
904 assert (_opentrepServiceContext != NULL);
939 lInsertIntoXapianAndSQLDBChronometer.
start();
940 oNbOfEntries = IndexBuilder::buildSearchIndex (lPORFilePath,
943 lSQLDBConnectionString,
945 lShouldIndexPORInXapian,
946 lShouldAddPORInSQLDB,
948 const double lInsertIntoXapianAndSQLDBMeasure =
949 lInsertIntoXapianAndSQLDBChronometer.
elapsed();
953 << lInsertIntoXapianAndSQLDBMeasure <<
" - "
954 << lOPENTREP_ServiceContext.
display());
966 if (_opentrepServiceContext == NULL) {
970 assert (_opentrepServiceContext != NULL);
978 boost::posix_time::ptime lNowDateTime =
979 boost::posix_time::second_clock::local_time();
984 <<
"==================================================="
986 << lNowDateTime <<
" - Match query '" << iTravelQuery
987 <<
"' on Xapian database (index)");
990 if (iTravelQuery.empty() ==
true) {
991 std::ostringstream errorStr;
992 errorStr <<
"The travel query is empty.";
1003 if (lExistXapianDBDir ==
false) {
1004 std::ostringstream errorStr;
1005 errorStr <<
"The file-path to the Xapian database/index ('"
1006 << lTravelDBFilePath <<
"') does not exist or is not a "
1007 <<
"directory." << std::endl;
1008 errorStr <<
"That usually means that the OpenTREP indexer "
1009 <<
"(opentrep-indexer) has not been launched yet, "
1010 <<
"or that it has operated on a different Xapian "
1011 <<
"database/index file-path, for instance with a different "
1012 <<
"deployment number";
1027 lRequestInterpreterChronometer.
start();
1029 RequestInterpreter::interpretTravelRequest (lTravelDBFilePath,
1030 lSQLDBType, lSQLDBConnString,
1032 ioLocationList, ioWordList,
1034 const double lRequestInterpreterMeasure =
1035 lRequestInterpreterChronometer.
elapsed();
1039 << lRequestInterpreterMeasure <<
" - "
1040 << 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
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.