8 #include <boost/date_time/gregorian/gregorian.hpp>
9 #include <boost/date_time/posix_time/ptime.hpp>
11 #include <soci/soci.h>
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,
60 OPENTREP_Service::OPENTREP_Service() : _opentrepServiceContext (NULL) {
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,
124 const PORFilePath_T& iPORFilepath,
125 const TravelDBFilePath_T& iTravelDBFilePath,
126 const DBType& iSQLDBType,
127 const SQLDBConnectionString_T& iSQLDBConnStr,
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());