OpenTREP Logo  0.07.1
C++ Open Travel Request Parsing Library
OPENTREP_Service.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <ostream>
7 // Boost
8 #include <boost/date_time/gregorian/gregorian.hpp>
9 #include <boost/date_time/posix_time/ptime.hpp>
10 // SOCI
11 #include <soci/soci.h>
12 // OpenTrep
13 #include <opentrep/Location.hpp>
14 #include <opentrep/CityDetails.hpp>
27 
28 namespace OPENTREP {
29 
30  // //////////////////////////////////////////////////////////////////////
31  OPENTREP_Service::
32  OPENTREP_Service (std::ostream& ioLogStream, const PORFilePath_T& iPORFilepath,
33  const TravelDBFilePath_T& iTravelDBFilePath,
34  const DBType& iSQLDBType,
35  const SQLDBConnectionString_T& iSQLDBConnStr,
36  const DeploymentNumber_T& iDeploymentNumber,
37  const shouldIndexNonIATAPOR_T& iShouldIndexNonIATAPOR,
38  const shouldIndexPORInXapian_T& iShouldIndexPORInXapian,
39  const shouldAddPORInSQLDB_T& iShouldAddPORInSQLDB)
40  : _opentrepServiceContext (NULL) {
41  init (ioLogStream, iPORFilepath, iTravelDBFilePath, iSQLDBType,
42  iSQLDBConnStr, iDeploymentNumber, iShouldIndexNonIATAPOR,
43  iShouldIndexPORInXapian, iShouldAddPORInSQLDB);
44  }
45 
46  // //////////////////////////////////////////////////////////////////////
47  OPENTREP_Service::
48  OPENTREP_Service (std::ostream& ioLogStream,
49  const TravelDBFilePath_T& iTravelDBFilePath,
50  const DBType& iSQLDBType,
51  const SQLDBConnectionString_T& iSQLDBConnStr,
52  const DeploymentNumber_T& iDeploymentNumber)
53  : _opentrepServiceContext (NULL) {
54  init (ioLogStream, iTravelDBFilePath, iSQLDBType, iSQLDBConnStr,
55  iDeploymentNumber);
56  }
57 
58  // //////////////////////////////////////////////////////////////////////
59  OPENTREP_Service::OPENTREP_Service() : _opentrepServiceContext (NULL) {
60  assert (false);
61  }
62 
63  // //////////////////////////////////////////////////////////////////////
64  OPENTREP_Service::OPENTREP_Service (const OPENTREP_Service& iService) {
65  assert (false);
66  }
67 
68  // //////////////////////////////////////////////////////////////////////
70  // Delete/Clean all the objects from memory
71  finalise();
72  }
73 
74  // //////////////////////////////////////////////////////////////////////
75  void logInit (const LOG::EN_LogLevel iLogLevel,
76  std::ostream& ioLogOutputFile) {
77  Logger::instance().setLogParameters (iLogLevel, ioLogOutputFile);
78  }
79 
80  // //////////////////////////////////////////////////////////////////////
81  SQLDBConnectionString_T
82  getSQLConnStr (const DBType& iSQLDBType,
83  const SQLDBConnectionString_T& iSQLDBConnStr) {
84  // When the SQL database is MariaDB/MySQL and the connection string
85  // is equal to the default SQLite one, override it
86  std::string oSQLDBConnStr =
87  static_cast<const std::string> (iSQLDBConnStr);
88  if (iSQLDBType == DBType::MYSQL
89  && oSQLDBConnStr == DEFAULT_OPENTREP_SQLITE_DB_FILEPATH) {
90  oSQLDBConnStr = DEFAULT_OPENTREP_MYSQL_CONN_STRING;
91  }
92  return SQLDBConnectionString_T (oSQLDBConnStr);
93  }
94 
95  // //////////////////////////////////////////////////////////////////////
96  void OPENTREP_Service::init (std::ostream& ioLogStream,
97  const TravelDBFilePath_T& iTravelDBFilePath,
98  const DBType& iSQLDBType,
99  const SQLDBConnectionString_T& iSQLDBConnStr,
100  const DeploymentNumber_T& iDeploymentNumber) {
101  // Set the log file
102  logInit (LOG::DEBUG, ioLogStream);
103 
104  // Fix the SQL database connection string, if needed
105  const SQLDBConnectionString_T& lSQLDBConnStr =
106  getSQLConnStr (iSQLDBType, iSQLDBConnStr);
107 
108  // Initialise the context
109  OPENTREP_ServiceContext& lOPENTREP_ServiceContext =
110  FacOpenTrepServiceContext::instance().create (iTravelDBFilePath,
111  iSQLDBType, lSQLDBConnStr,
112  iDeploymentNumber);
113  _opentrepServiceContext = &lOPENTREP_ServiceContext;
114 
115  // Instanciate an empty World object
116  World& lWorld = FacWorld::instance().create();
117  lOPENTREP_ServiceContext.setWorld (lWorld);
118  }
119 
120  // //////////////////////////////////////////////////////////////////////
121  void OPENTREP_Service::
122  init (std::ostream& ioLogStream,
123  const PORFilePath_T& iPORFilepath,
124  const TravelDBFilePath_T& iTravelDBFilePath,
125  const DBType& iSQLDBType,
126  const SQLDBConnectionString_T& iSQLDBConnStr,
127  const DeploymentNumber_T& iDeploymentNumber,
128  const shouldIndexNonIATAPOR_T& iShouldIndexNonIATAPOR,
129  const shouldIndexPORInXapian_T& iShouldIndexPORInXapian,
130  const shouldAddPORInSQLDB_T& iShouldAddPORInSQLDB) {
131  // Set the log file
132  logInit (LOG::DEBUG, ioLogStream);
133 
134  // Fix the SQL database connection string, if needed
135  const SQLDBConnectionString_T& lSQLDBConnStr =
136  getSQLConnStr (iSQLDBType, iSQLDBConnStr);
137 
138  // Initialise the context
139  OPENTREP_ServiceContext& lOPENTREP_ServiceContext =
141  iTravelDBFilePath,
142  iSQLDBType, lSQLDBConnStr,
143  iDeploymentNumber,
144  iShouldIndexNonIATAPOR,
145  iShouldIndexPORInXapian,
146  iShouldAddPORInSQLDB);
147  _opentrepServiceContext = &lOPENTREP_ServiceContext;
148 
149  // Instanciate an empty World object
150  World& lWorld = FacWorld::instance().create();
151  lOPENTREP_ServiceContext.setWorld (lWorld);
152  }
153 
154  // //////////////////////////////////////////////////////////////////////
155  void OPENTREP_Service::finalise() {
156  }
157 
158  // //////////////////////////////////////////////////////////////////////
160  if (_opentrepServiceContext == NULL) {
161  throw NonInitialisedServiceException ("The OpenTREP service has not been"
162  " initialised");
163  }
164  assert (_opentrepServiceContext != NULL);
165  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
166 
167  // Retrieve the file-path of the POR (points of reference) file
168  const PORFilePath_T& lPORFilePath= lOPENTREP_ServiceContext.getPORFilePath();
169 
170  // Retrieve the Xapian database name (directorty of the index)
171  const TravelDBFilePath_T& lTravelDBFilePath =
172  lOPENTREP_ServiceContext.getTravelDBFilePath();
173 
174  // Retrieve the SQL database connection string
175  const SQLDBConnectionString_T& lSQLDBConnectionString =
176  lOPENTREP_ServiceContext.getSQLDBConnectionString();
177 
178  const DBFilePathPair_T lDBFilePathPair (lTravelDBFilePath,
179  lSQLDBConnectionString);
180  FilePathSet_T oFilePathSet (lPORFilePath, lDBFilePathPair);
181  return oFilePathSet;
182  }
183 
184  // //////////////////////////////////////////////////////////////////////
186  NbOfDBEntries_T oNbOfEntries = 0;
187 
188  if (_opentrepServiceContext == NULL) {
189  throw NonInitialisedServiceException ("The OpenTREP service has not been"
190  " initialised");
191  }
192  assert (_opentrepServiceContext != NULL);
193  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
194 
195  // Retrieve the Xapian database name (directorty of the index)
196  const TravelDBFilePath_T& lTravelDBFilePath =
197  lOPENTREP_ServiceContext.getTravelDBFilePath();
198 
199  // Delegate the query execution to the dedicated command
200  BasChronometer lIndexSizeChronometer; lIndexSizeChronometer.start();
201  oNbOfEntries = XapianIndexManager::getSize (lTravelDBFilePath);
202  const double lIndexSizeMeasure = lIndexSizeChronometer.elapsed();
203 
204  // DEBUG
205  OPENTREP_LOG_DEBUG ("Size retrieval of the Xapian database (index): "
206  << lIndexSizeMeasure << " - "
207  << lOPENTREP_ServiceContext.display());
208 
209  return oNbOfEntries;
210  }
211 
212  // //////////////////////////////////////////////////////////////////////
215  LocationList_T& ioLocationList) {
216  NbOfMatches_T oNbOfMatches = 0;
217 
218  if (_opentrepServiceContext == NULL) {
219  throw NonInitialisedServiceException ("The OpenTREP service has not been"
220  " initialised");
221  }
222  assert (_opentrepServiceContext != NULL);
223  OPENTREP_ServiceContext& lOPENTREP_ServiceContext= *_opentrepServiceContext;
224 
225  // Retrieve the Xapian database name (directorty of the index)
226  const TravelDBFilePath_T& lTravelDBFilePath =
227  lOPENTREP_ServiceContext.getTravelDBFilePath();
228 
229  // Delegate the query execution to the dedicated command
230  BasChronometer lRandomGetChronometer; lRandomGetChronometer.start();
231  oNbOfMatches = XapianIndexManager::drawRandomLocations (lTravelDBFilePath,
232  iNbOfDraws,
233  ioLocationList);
234  const double lRandomGetMeasure = lRandomGetChronometer.elapsed();
235 
236  // DEBUG
237  OPENTREP_LOG_DEBUG ("Random retrieval of locations (index): "
238  << lRandomGetMeasure << " - "
239  << lOPENTREP_ServiceContext.display());
240 
241  return oNbOfMatches;
242  }
243 
244  // //////////////////////////////////////////////////////////////////////
246  bool oCreationSuccessful = true;
247 
248  if (_opentrepServiceContext == NULL) {
249  throw NonInitialisedServiceException ("The OpenTREP service has not been"
250  " initialised");
251  }
252  assert (_opentrepServiceContext != NULL);
253  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
254 
255  // Retrieve the SQL database type
256  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
257 
258  // Sanity check: no need to perform any action when the current option
259  // is to not use any SQL database
260  if (lSQLDBType == DBType::NODB) {
261  // DEBUG
262  OPENTREP_LOG_DEBUG ("The current option is to not use any SQL database. "
263  << "Hence nothing is done at that stage. "
264  << " - " << lOPENTREP_ServiceContext.display());
265  return oCreationSuccessful;
266  }
267 
268  // Retrieve the SQL database connection string
269  const SQLDBConnectionString_T& lSQLDBConnectionString =
270  lOPENTREP_ServiceContext.getSQLDBConnectionString();
271 
272  // Retrieve the deployment number/version
273  const DeploymentNumber_T& lDeploymentNumber =
274  lOPENTREP_ServiceContext.getDeploymentNumber();
275 
276  // Delegate the database creation to the dedicated command
277  BasChronometer lDBCreationChronometer;
278  lDBCreationChronometer.start();
279 
280  // Create the SQL database user ('trep' on MySQL database)
281  // and database ('trep_trep' on MySQL database)
282  oCreationSuccessful =
283  DBManager::createSQLDBUser (lSQLDBType, lSQLDBConnectionString,
284  lDeploymentNumber);
285 
286  const double lDBCreationMeasure = lDBCreationChronometer.elapsed();
287 
288  // DEBUG
289  OPENTREP_LOG_DEBUG ("Created the SQL database: " << lDBCreationMeasure
290  << " - " << lOPENTREP_ServiceContext.display());
291 
292  return oCreationSuccessful;
293  }
294 
295  // //////////////////////////////////////////////////////////////////////
296  void OPENTREP_Service::
297  setSQLDBConnectString (const SQLDBConnectionString_T& iSQLDBConnectionString) {
298  if (_opentrepServiceContext == NULL) {
299  throw NonInitialisedServiceException ("The OpenTREP service has not been"
300  " initialised");
301  }
302  assert (_opentrepServiceContext != NULL);
303  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
304 
305  // Set the SQL database connection string
306  lOPENTREP_ServiceContext.setSQLDBConnectionString (iSQLDBConnectionString);
307 
308  // DEBUG
309  OPENTREP_LOG_DEBUG ("Reset of the SQL database connection string: "
310  << lOPENTREP_ServiceContext.display());
311  }
312 
313  // //////////////////////////////////////////////////////////////////////
315  if (_opentrepServiceContext == NULL) {
316  throw NonInitialisedServiceException ("The OpenTREP service has not been"
317  " initialised");
318  }
319  assert (_opentrepServiceContext != NULL);
320  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
321 
322  // Retrieve the SQL database type
323  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
324 
325  // Sanity check: no need to perform any action when the current option
326  // is to not use any SQL database
327  if (lSQLDBType == DBType::NODB) {
328  // DEBUG
329  OPENTREP_LOG_DEBUG ("The current option is to not use any SQL database. "
330  << "Hence nothing is done at that stage. "
331  << " - " << lOPENTREP_ServiceContext.display());
332  return;
333  }
334 
335  // Retrieve the SQL database connection string
336  const SQLDBConnectionString_T& lSQLDBConnectionString =
337  lOPENTREP_ServiceContext.getSQLDBConnectionString();
338 
339  // Delegate the database creation to the dedicated command
340  BasChronometer lDBCreationChronometer;
341  lDBCreationChronometer.start();
342 
343  // Connect to the SQLite3/MySQL database
344  soci::session* lSociSession_ptr =
345  DBManager::initSQLDBSession (lSQLDBType, lSQLDBConnectionString);
346  assert (lSociSession_ptr != NULL);
347  soci::session& lSociSession = *lSociSession_ptr;
348 
349  // Create the SQL database tables
350  DBManager::createSQLDBTables (lSociSession);
351 
352  // Release the SQL database connection
353  DBManager::terminateSQLDBSession (lSQLDBType, lSQLDBConnectionString,
354  lSociSession);
355 
356  const double lDBCreationMeasure = lDBCreationChronometer.elapsed();
357 
358  // DEBUG
359  OPENTREP_LOG_DEBUG ("Created the SQL database tables: " << lDBCreationMeasure
360  << " - " << lOPENTREP_ServiceContext.display());
361  }
362 
363  // //////////////////////////////////////////////////////////////////////
365  if (_opentrepServiceContext == NULL) {
366  throw NonInitialisedServiceException ("The OpenTREP service has not been"
367  " initialised");
368  }
369  assert (_opentrepServiceContext != NULL);
370  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
371 
372  // Retrieve the SQL database type
373  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
374 
375  // Sanity check: no need to perform any action when the current option
376  // is to not use any SQL database
377  if (lSQLDBType == DBType::NODB) {
378  // DEBUG
379  OPENTREP_LOG_DEBUG ("The current option is to not use any SQL database. "
380  << "Hence nothing is done at that stage. "
381  << " - " << lOPENTREP_ServiceContext.display());
382  return;
383  }
384 
385  // Retrieve the SQL database connection string
386  const SQLDBConnectionString_T& lSQLDBConnectionString =
387  lOPENTREP_ServiceContext.getSQLDBConnectionString();
388 
389  // Delegate the database creation to the dedicated command
390  BasChronometer lDBCreationChronometer;
391  lDBCreationChronometer.start();
392 
393  // Connect to the SQLite3/MySQL database
394  soci::session* lSociSession_ptr =
395  DBManager::initSQLDBSession (lSQLDBType, lSQLDBConnectionString);
396  assert (lSociSession_ptr != NULL);
397  soci::session& lSociSession = *lSociSession_ptr;
398 
399  // Create the SQL database tables
400  DBManager::createSQLDBIndexes (lSociSession);
401 
402  // Release the SQL database connection
403  DBManager::terminateSQLDBSession (lSQLDBType, lSQLDBConnectionString,
404  lSociSession);
405 
406  const double lDBCreationMeasure = lDBCreationChronometer.elapsed();
407 
408  // DEBUG
409  OPENTREP_LOG_DEBUG ("Created the SQL database indexes: "
410  << lDBCreationMeasure << " - "
411  << lOPENTREP_ServiceContext.display());
412  }
413 
414  // //////////////////////////////////////////////////////////////////////
416  DeploymentNumber_T oDeploymentNumber = 0;
417 
418  if (_opentrepServiceContext == NULL) {
419  throw NonInitialisedServiceException ("The OpenTREP service has not been"
420  " initialised");
421  }
422  assert (_opentrepServiceContext != NULL);
423  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
424 
425  // Retrieve the deployment number/version
426  oDeploymentNumber = lOPENTREP_ServiceContext.getDeploymentNumber();
427 
428  // Toggle the number
429  ++oDeploymentNumber;
430  if (oDeploymentNumber >= DEFAULT_OPENTREP_DEPLOYMENT_NUMBER_SIZE) {
431  oDeploymentNumber = DEFAULT_OPENTREP_DEPLOYMENT_NUMBER;
432  }
433 
434  // Store back the toggled flag
435  lOPENTREP_ServiceContext.setDeploymentNumber (oDeploymentNumber);
436 
437  // DEBUG
438  OPENTREP_LOG_DEBUG ("The new deployment number/version is: "
439  << oDeploymentNumber << " - "
440  << lOPENTREP_ServiceContext.display());
441 
442  return oDeploymentNumber;
443  }
444 
445  // //////////////////////////////////////////////////////////////////////
448  shouldIndexNonIATAPOR_T oShouldIndexNonIATAPOR = false;
449 
450  if (_opentrepServiceContext == NULL) {
451  throw NonInitialisedServiceException ("The OpenTREP service has not been"
452  " initialised");
453  }
454  assert (_opentrepServiceContext != NULL);
455  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
456 
457  // Retrieve the flag
458  oShouldIndexNonIATAPOR =
459  lOPENTREP_ServiceContext.getShouldIncludeAllPORFlag();
460 
461  // Toggle the flag
462  oShouldIndexNonIATAPOR = !(oShouldIndexNonIATAPOR);
463 
464  // Store back the toggled flag
465  lOPENTREP_ServiceContext.setShouldIncludeAllPORFlag (oShouldIndexNonIATAPOR);
466 
467  // DEBUG
468  OPENTREP_LOG_DEBUG ("The new non-IATA-referenced POR flag is: "
469  << oShouldIndexNonIATAPOR << " - "
470  << lOPENTREP_ServiceContext.display());
471 
472  return oShouldIndexNonIATAPOR;
473  }
474 
475  // //////////////////////////////////////////////////////////////////////
478  shouldIndexPORInXapian_T oShouldIndexPORInXapian = true;
479 
480  if (_opentrepServiceContext == NULL) {
481  throw NonInitialisedServiceException ("The OpenTREP service has not been"
482  " initialised");
483  }
484  assert (_opentrepServiceContext != NULL);
485  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
486 
487  // Retrieve the flag
488  oShouldIndexPORInXapian =
489  lOPENTREP_ServiceContext.getShouldIndexPORInXapianFlag();
490 
491  // Toggle the flag
492  oShouldIndexPORInXapian = !(oShouldIndexPORInXapian);
493 
494  // Store back the toggled flag
495  lOPENTREP_ServiceContext.
496  setShouldIndexPORInXapianFlag (oShouldIndexPORInXapian);
497 
498  // DEBUG
499  OPENTREP_LOG_DEBUG ("The new index-in-Xapian-POR flag is: "
500  << oShouldIndexPORInXapian << " - "
501  << lOPENTREP_ServiceContext.display());
502 
503  return oShouldIndexPORInXapian;
504  }
505 
506  // //////////////////////////////////////////////////////////////////////
509  shouldAddPORInSQLDB_T oShouldAddPORInSQLDB = false;
510 
511  if (_opentrepServiceContext == NULL) {
512  throw NonInitialisedServiceException ("The OpenTREP service has not been"
513  " initialised");
514  }
515  assert (_opentrepServiceContext != NULL);
516  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
517 
518  // Retrieve the flag
519  oShouldAddPORInSQLDB =
520  lOPENTREP_ServiceContext.getShouldAddPORInSQLDB();
521 
522  // Toggle the flag
523  oShouldAddPORInSQLDB = !(oShouldAddPORInSQLDB);
524 
525  // Store back the toggled flag
526  lOPENTREP_ServiceContext.setShouldAddPORInSQLDB (oShouldAddPORInSQLDB);
527 
528  // DEBUG
529  OPENTREP_LOG_DEBUG ("The new insert-in-DB-POR flag is: "
530  << oShouldAddPORInSQLDB << " - "
531  << lOPENTREP_ServiceContext.display());
532 
533  return oShouldAddPORInSQLDB;
534  }
535 
536  // //////////////////////////////////////////////////////////////////////
538  NbOfDBEntries_T nbOfMatches = 0;
539 
540  if (_opentrepServiceContext == NULL) {
541  throw NonInitialisedServiceException ("The OpenTREP service has not been"
542  " initialised");
543  }
544  assert (_opentrepServiceContext != NULL);
545  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
546 
547  // Retrieve the SQL database type
548  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
549 
550  // Retrieve the SQL database connection string
551  const SQLDBConnectionString_T& lSQLDBConnectionString =
552  lOPENTREP_ServiceContext.getSQLDBConnectionString();
553 
554  // Delegate the database look up to the dedicated command
555  BasChronometer lDBListChronometer;
556  lDBListChronometer.start();
557 
558  // Connect to the SQLite3/MySQL database
559  soci::session* lSociSession_ptr =
560  DBManager::initSQLDBSession (lSQLDBType, lSQLDBConnectionString);
561  assert (lSociSession_ptr != NULL);
562  soci::session& lSociSession = *lSociSession_ptr;
563 
564  // Get the number of POR stored within the SQLite3/MySQL database
565  nbOfMatches = DBManager::displayCount (lSociSession);
566 
567  // Release the SQL database connection
568  DBManager::terminateSQLDBSession (lSQLDBType, lSQLDBConnectionString,
569  lSociSession);
570 
571  const double lDBListMeasure = lDBListChronometer.elapsed();
572 
573  // DEBUG
574  OPENTREP_LOG_DEBUG ("Look up in the SQL database: " << lDBListMeasure
575  << " - " << lOPENTREP_ServiceContext.display());
576 
577  //
578  return nbOfMatches;
579  }
580 
581  // //////////////////////////////////////////////////////////////////////
583  listByIataCode (const IATACode_T& iIataCode,
584  LocationList_T& ioLocationList) {
585  NbOfMatches_T nbOfMatches = 0;
586 
587  if (_opentrepServiceContext == NULL) {
588  throw NonInitialisedServiceException ("The OpenTREP service has not been"
589  " initialised");
590  }
591  assert (_opentrepServiceContext != NULL);
592  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
593 
594  // Retrieve the SQL database type
595  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
596 
597  // Retrieve the SQL database connection string
598  const SQLDBConnectionString_T& lSQLDBConnectionString =
599  lOPENTREP_ServiceContext.getSQLDBConnectionString();
600 
601  // Delegate the database look up to the dedicated command
602  BasChronometer lDBListChronometer;
603  lDBListChronometer.start();
604 
605  // Connect to the SQLite3/MySQL database
606  soci::session* lSociSession_ptr =
607  DBManager::initSQLDBSession (lSQLDBType, lSQLDBConnectionString);
608  assert (lSociSession_ptr != NULL);
609  soci::session& lSociSession = *lSociSession_ptr;
610 
611  // Get the list of POR corresponding to the given IATA code
612  const bool lSeveralEntries = false;
613  nbOfMatches = DBManager::getPORByIATACode (lSociSession, iIataCode,
614  ioLocationList, lSeveralEntries);
615 
616  // Release the SQL database connection
617  DBManager::terminateSQLDBSession (lSQLDBType, lSQLDBConnectionString,
618  lSociSession);
619 
620  const double lDBListMeasure = lDBListChronometer.elapsed();
621 
622  // DEBUG
623  OPENTREP_LOG_DEBUG ("Look up in the SQL database: " << lDBListMeasure
624  << " - " << lOPENTREP_ServiceContext.display());
625 
626  //
627  return nbOfMatches;
628  }
629 
630  // //////////////////////////////////////////////////////////////////////
632  listByIcaoCode (const ICAOCode_T& iIcaoCode,
633  LocationList_T& ioLocationList) {
634  NbOfMatches_T nbOfMatches = 0;
635 
636  if (_opentrepServiceContext == NULL) {
637  throw NonInitialisedServiceException ("The OpenTREP service has not been"
638  " initialised");
639  }
640  assert (_opentrepServiceContext != NULL);
641  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
642 
643  // Retrieve the SQL database type
644  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
645 
646  // Retrieve the SQL database connection string
647  const SQLDBConnectionString_T& lSQLDBConnectionString =
648  lOPENTREP_ServiceContext.getSQLDBConnectionString();
649 
650  // Delegate the database look up to the dedicated command
651  BasChronometer lDBListChronometer;
652  lDBListChronometer.start();
653 
654  // Connect to the SQLite3/MySQL database
655  soci::session* lSociSession_ptr =
656  DBManager::initSQLDBSession (lSQLDBType, lSQLDBConnectionString);
657  assert (lSociSession_ptr != NULL);
658  soci::session& lSociSession = *lSociSession_ptr;
659 
660  // Get the list of POR corresponding to the given ICAO code
661  nbOfMatches =
662  DBManager::getPORByICAOCode (lSociSession, iIcaoCode, ioLocationList);
663 
664  // Release the SQL database connection
665  DBManager::terminateSQLDBSession (lSQLDBType, lSQLDBConnectionString,
666  lSociSession);
667 
668  const double lDBListMeasure = lDBListChronometer.elapsed();
669 
670  // DEBUG
671  OPENTREP_LOG_DEBUG ("Look up in the SQL database: " << lDBListMeasure
672  << " - " << lOPENTREP_ServiceContext.display());
673 
674  //
675  return nbOfMatches;
676  }
677 
678  // //////////////////////////////////////////////////////////////////////
680  listByFaaCode (const FAACode_T& iFaaCode,
681  LocationList_T& ioLocationList) {
682  NbOfMatches_T nbOfMatches = 0;
683 
684  if (_opentrepServiceContext == NULL) {
685  throw NonInitialisedServiceException ("The OpenTREP service has not been"
686  " initialised");
687  }
688  assert (_opentrepServiceContext != NULL);
689  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
690 
691  // Retrieve the SQL database type
692  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
693 
694  // Retrieve the SQL database connection string
695  const SQLDBConnectionString_T& lSQLDBConnectionString =
696  lOPENTREP_ServiceContext.getSQLDBConnectionString();
697 
698  // Delegate the database look up to the dedicated command
699  BasChronometer lDBListChronometer;
700  lDBListChronometer.start();
701 
702  // Connect to the SQLite3/MySQL database
703  soci::session* lSociSession_ptr =
704  DBManager::initSQLDBSession (lSQLDBType, lSQLDBConnectionString);
705  assert (lSociSession_ptr != NULL);
706  soci::session& lSociSession = *lSociSession_ptr;
707 
708  // Get the list of POR corresponding to the given FAA code
709  nbOfMatches =
710  DBManager::getPORByFAACode (lSociSession, iFaaCode, ioLocationList);
711 
712  // Release the SQL database connection
713  DBManager::terminateSQLDBSession (lSQLDBType, lSQLDBConnectionString,
714  lSociSession);
715 
716  const double lDBListMeasure = lDBListChronometer.elapsed();
717 
718  // DEBUG
719  OPENTREP_LOG_DEBUG ("Look up in the SQL database: " << lDBListMeasure
720  << " - " << lOPENTREP_ServiceContext.display());
721 
722  //
723  return nbOfMatches;
724  }
725 
726  // //////////////////////////////////////////////////////////////////////
728  listByUNLOCode (const UNLOCode_T& iUNLOCode,
729  LocationList_T& ioLocationList) {
730  NbOfMatches_T nbOfMatches = 0;
731 
732  if (_opentrepServiceContext == NULL) {
733  throw NonInitialisedServiceException ("The OpenTREP service has not been"
734  " initialised");
735  }
736  assert (_opentrepServiceContext != NULL);
737  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
738 
739  // Retrieve the SQL database type
740  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
741 
742  // Retrieve the SQL database connection string
743  const SQLDBConnectionString_T& lSQLDBConnectionString =
744  lOPENTREP_ServiceContext.getSQLDBConnectionString();
745 
746  // Delegate the database look up to the dedicated command
747  BasChronometer lDBListChronometer;
748  lDBListChronometer.start();
749 
750  // Connect to the SQLite3/MySQL database
751  soci::session* lSociSession_ptr =
752  DBManager::initSQLDBSession (lSQLDBType, lSQLDBConnectionString);
753  assert (lSociSession_ptr != NULL);
754  soci::session& lSociSession = *lSociSession_ptr;
755 
756  // Get the list of POR corresponding to the given UN/LOCODE code
757  nbOfMatches =
758  DBManager::getPORByUNLOCode (lSociSession, iUNLOCode, ioLocationList);
759 
760  // Release the SQL database connection
761  DBManager::terminateSQLDBSession (lSQLDBType, lSQLDBConnectionString,
762  lSociSession);
763 
764  const double lDBListMeasure = lDBListChronometer.elapsed();
765 
766  // DEBUG
767  OPENTREP_LOG_DEBUG ("Look up in the SQL database: " << lDBListMeasure
768  << " - " << lOPENTREP_ServiceContext.display());
769 
770  //
771  return nbOfMatches;
772  }
773 
774  // //////////////////////////////////////////////////////////////////////
776  listByUICCode (const UICCode_T& iUICCode, LocationList_T& ioLocationList) {
777  NbOfMatches_T nbOfMatches = 0;
778 
779  if (_opentrepServiceContext == NULL) {
780  throw NonInitialisedServiceException ("The OpenTREP service has not been"
781  " initialised");
782  }
783  assert (_opentrepServiceContext != NULL);
784  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
785 
786  // Retrieve the SQL database type
787  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
788 
789  // Retrieve the SQL database connection string
790  const SQLDBConnectionString_T& lSQLDBConnectionString =
791  lOPENTREP_ServiceContext.getSQLDBConnectionString();
792 
793  // Delegate the database look up to the dedicated command
794  BasChronometer lDBListChronometer;
795  lDBListChronometer.start();
796 
797  // Connect to the SQLite3/MySQL database
798  soci::session* lSociSession_ptr =
799  DBManager::initSQLDBSession (lSQLDBType, lSQLDBConnectionString);
800  assert (lSociSession_ptr != NULL);
801  soci::session& lSociSession = *lSociSession_ptr;
802 
803  // Get the list of POR corresponding to the given UIC code
804  nbOfMatches =
805  DBManager::getPORByUICCode (lSociSession, iUICCode, ioLocationList);
806 
807  // Release the SQL database connection
808  DBManager::terminateSQLDBSession (lSQLDBType, lSQLDBConnectionString,
809  lSociSession);
810 
811  const double lDBListMeasure = lDBListChronometer.elapsed();
812 
813  // DEBUG
814  OPENTREP_LOG_DEBUG ("Look up in the SQL database: " << lDBListMeasure
815  << " - " << lOPENTREP_ServiceContext.display());
816 
817  //
818  return nbOfMatches;
819  }
820 
821  // //////////////////////////////////////////////////////////////////////
823  listByGeonameID (const GeonamesID_T& iGeonameID,
824  LocationList_T& ioLocationList) {
825  NbOfMatches_T nbOfMatches = 0;
826 
827  if (_opentrepServiceContext == NULL) {
828  throw NonInitialisedServiceException ("The OpenTREP service has not been"
829  " initialised");
830  }
831  assert (_opentrepServiceContext != NULL);
832  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
833 
834  // Retrieve the SQL database type
835  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
836 
837  // Retrieve the SQL database connection string
838  const SQLDBConnectionString_T& lSQLDBConnectionString =
839  lOPENTREP_ServiceContext.getSQLDBConnectionString();
840 
841  // Delegate the database look up to the dedicated command
842  BasChronometer lDBListChronometer;
843  lDBListChronometer.start();
844 
845  // Connect to the SQLite3/MySQL database
846  soci::session* lSociSession_ptr =
847  DBManager::initSQLDBSession (lSQLDBType, lSQLDBConnectionString);
848  assert (lSociSession_ptr != NULL);
849  soci::session& lSociSession = *lSociSession_ptr;
850 
851  // Get the list of POR corresponding to the given Geoname ID
852  nbOfMatches =
853  DBManager::getPORByGeonameID (lSociSession, iGeonameID, ioLocationList);
854 
855  // Release the SQL database connection
856  DBManager::terminateSQLDBSession (lSQLDBType, lSQLDBConnectionString,
857  lSociSession);
858 
859  const double lDBListMeasure = lDBListChronometer.elapsed();
860 
861  // DEBUG
862  OPENTREP_LOG_DEBUG ("Look up in the SQL database: " << lDBListMeasure
863  << " - " << lOPENTREP_ServiceContext.display());
864 
865  //
866  return nbOfMatches;
867  }
868 
869  // //////////////////////////////////////////////////////////////////////
871  NbOfDBEntries_T oNbOfEntries = 0;
872 
873  if (_opentrepServiceContext == NULL) {
874  throw NonInitialisedServiceException ("The OpenTREP service has not been"
875  " initialised");
876  }
877  assert (_opentrepServiceContext != NULL);
878  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
879 
880  // Retrieve the file-path of the POR (points of reference) file
881  const PORFilePath_T& lPORFilePath= lOPENTREP_ServiceContext.getPORFilePath();
882 
883  // Retrieve the Xapian database name (directorty of the index)
884  const TravelDBFilePath_T& lTravelDBFilePath =
885  lOPENTREP_ServiceContext.getTravelDBFilePath();
886 
887  // Retrieve the SQL database type
888  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
889 
890  // Retrieve the SQL database connection string
891  const SQLDBConnectionString_T& lSQLDBConnectionString =
892  lOPENTREP_ServiceContext.getSQLDBConnectionString();
893 
894  // Retrieve whether or not all the POR should be indexed
895  const OPENTREP::shouldIndexNonIATAPOR_T& lIncludeNonIATAPOR =
896  lOPENTREP_ServiceContext.getShouldIncludeAllPORFlag();
897 
898  // Retrieve whether or not the POR should be indexed in Xapian
899  const OPENTREP::shouldIndexPORInXapian_T& lShouldIndexPORInXapian =
900  lOPENTREP_ServiceContext.getShouldIndexPORInXapianFlag();
901 
902  // Retrieve whether or not the POR should be indexed in Xapian
903  const OPENTREP::shouldAddPORInSQLDB_T& lShouldAddPORInSQLDB =
904  lOPENTREP_ServiceContext.getShouldAddPORInSQLDB();
905 
906  // Retrieve the Unicode transliterator
907  const OTransliterator& lTransliterator =
908  lOPENTREP_ServiceContext.getTransliterator();
909 
910  // Delegate the index building to the dedicated command
911  BasChronometer lInsertIntoXapianAndSQLDBChronometer;
912  lInsertIntoXapianAndSQLDBChronometer.start();
913  oNbOfEntries = IndexBuilder::buildSearchIndex (lPORFilePath,
914  lTravelDBFilePath,
915  lSQLDBType,
916  lSQLDBConnectionString,
917  lIncludeNonIATAPOR,
918  lShouldIndexPORInXapian,
919  lShouldAddPORInSQLDB,
920  lTransliterator);
921  const double lInsertIntoXapianAndSQLDBMeasure =
922  lInsertIntoXapianAndSQLDBChronometer.elapsed();
923 
924  // DEBUG
925  OPENTREP_LOG_DEBUG ("Built Xapian database/index and filled SQL database: "
926  << lInsertIntoXapianAndSQLDBMeasure << " - "
927  << lOPENTREP_ServiceContext.display());
928 
929  return oNbOfEntries;
930  }
931 
932  // //////////////////////////////////////////////////////////////////////
934  interpretTravelRequest (const std::string& iTravelQuery,
935  LocationList_T& ioLocationList,
936  WordList_T& ioWordList) {
937  NbOfMatches_T nbOfMatches = 0;
938 
939  if (_opentrepServiceContext == NULL) {
940  throw NonInitialisedServiceException ("The OpenTREP service has not been"
941  " initialised");
942  }
943  assert (_opentrepServiceContext != NULL);
944  OPENTREP_ServiceContext& lOPENTREP_ServiceContext= *_opentrepServiceContext;
945 
946  // Retrieve the Unicode transliterator
947  const OTransliterator& lTransliterator =
948  lOPENTREP_ServiceContext.getTransliterator();
949 
950  // Get the date-time for the present time
951  boost::posix_time::ptime lNowDateTime =
952  boost::posix_time::second_clock::local_time();
953  // boost::gregorian::date lNowDate = lNowDateTime.date();
954 
955  // DEBUG
956  OPENTREP_LOG_DEBUG (std::endl
957  << "==================================================="
958  << std::endl
959  << lNowDateTime << " - Match query '" << iTravelQuery
960  << "' on Xapian database (index)");
961 
962  // Check that the travel query is not empty
963  if (iTravelQuery.empty() == true) {
964  std::ostringstream errorStr;
965  errorStr << "The travel query is empty.";
966  OPENTREP_LOG_ERROR (errorStr.str());
967  throw TravelRequestEmptyException (errorStr.str());
968  }
969 
970  // Retrieve the Xapian database name (directorty of the index)
971  const TravelDBFilePath_T& lTravelDBFilePath =
972  lOPENTREP_ServiceContext.getTravelDBFilePath();
973 
974  // Retrieve the SQL database type
975  const DBType& lSQLDBType = lOPENTREP_ServiceContext.getSQLDBType();
976 
977  // Retrieve the SQL database connection string
978  const SQLDBConnectionString_T& lSQLDBConnString =
979  lOPENTREP_ServiceContext.getSQLDBConnectionString();
980 
981  // Delegate the query execution to the dedicated command
982  BasChronometer lRequestInterpreterChronometer;
983  lRequestInterpreterChronometer.start();
984  nbOfMatches =
985  RequestInterpreter::interpretTravelRequest (lTravelDBFilePath,
986  lSQLDBType, lSQLDBConnString,
987  iTravelQuery,
988  ioLocationList, ioWordList,
989  lTransliterator);
990  const double lRequestInterpreterMeasure =
991  lRequestInterpreterChronometer.elapsed();
992 
993  // DEBUG
994  OPENTREP_LOG_DEBUG ("Match query on Xapian database (index): "
995  << lRequestInterpreterMeasure << " - "
996  << lOPENTREP_ServiceContext.display());
997 
998  return nbOfMatches;
999  }
1000 
1001 }
Structure allowing measuring the time elapsed between two events.
#define OPENTREP_LOG_ERROR(iToBeLogged)
Definition: Logger.hpp:24
const unsigned short DEFAULT_OPENTREP_DEPLOYMENT_NUMBER
#define OPENTREP_LOG_DEBUG(iToBeLogged)
Definition: Logger.hpp:33
void setSQLDBConnectionString(const std::string &iSQLDBConnStr)
const TravelDBFilePath_T & getTravelDBFilePath() const
void setSQLDBConnectString(const SQLDBConnectionString_T &)
unsigned int GeonamesID_T
unsigned short NbOfMatches_T
const std::string DEFAULT_OPENTREP_MYSQL_CONN_STRING
FilePathSet_T getFilePaths() const
Class holding the context of the OpenTrep services.
void setShouldIncludeAllPORFlag(const shouldIndexNonIATAPOR_T &iShouldIndexNonIATAPOR)
bool shouldIndexNonIATAPOR_T
OPENTREP::shouldIndexPORInXapian_T toggleShouldIndexPORInXapianFlag()
const std::string DEFAULT_OPENTREP_SQLITE_DB_FILEPATH
NbOfDBEntries_T insertIntoDBAndXapian()
static NbOfDBEntries_T getPORByFAACode(soci::session &, const FAACode_T &, LocationList_T &)
Definition: DBManager.cpp:1345
static FacWorld & instance()
Definition: FacWorld.cpp:26
const DeploymentNumber_T & getDeploymentNumber() const
void setLogParameters(const LOG::EN_LogLevel iLogLevel, std::ostream &ioLogStream)
Definition: Logger.cpp:47
unsigned int NbOfDBEntries_T
NbOfMatches_T listByIataCode(const IATACode_T &, LocationList_T &)
World & create()
Definition: FacWorld.cpp:38
const shouldAddPORInSQLDB_T & getShouldAddPORInSQLDB() const
static bool createSQLDBUser(const DBType &, const SQLDBConnectionString_T &, const DeploymentNumber_T &)
Definition: DBManager.cpp:35
void setShouldAddPORInSQLDB(const shouldAddPORInSQLDB_T &iShouldAddPORInSQLDB)
std::list< Word_T > WordList_T
NbOfMatches_T listByFaaCode(const FAACode_T &, LocationList_T &)
NbOfDBEntries_T getNbOfPORFromDB()
static void createSQLDBTables(soci::session &)
Definition: DBManager.cpp:460
NbOfMatches_T listByIcaoCode(const ICAOCode_T &, LocationList_T &)
static void terminateSQLDBSession(const DBType &, const SQLDBConnectionString_T &, soci::session &)
Definition: DBManager.cpp:406
static FacOpenTrepServiceContext & instance()
static NbOfDBEntries_T getPORByUICCode(soci::session &, const UICCode_T &, LocationList_T &)
Definition: DBManager.cpp:1466
NbOfMatches_T drawRandomLocations(const NbOfMatches_T &iNbOfDraws, LocationList_T &)
Enumeration of database types.
Definition: DBType.hpp:17
unsigned int UICCode_T
static NbOfDBEntries_T getPORByIATACode(soci::session &, const IATACode_T &, LocationList_T &, const bool iUniqueEntry)
Definition: DBManager.cpp:1190
const shouldIndexNonIATAPOR_T & getShouldIncludeAllPORFlag() const
static NbOfDBEntries_T displayCount(soci::session &)
Definition: DBManager.cpp:1124
const SQLDBConnectionString_T & getSQLDBConnectionString() const
std::list< Location > LocationList_T
NbOfMatches_T listByUICCode(const UICCode_T &, LocationList_T &)
static NbOfDBEntries_T getPORByUNLOCode(soci::session &, const UNLOCode_T &, LocationList_T &)
Definition: DBManager.cpp:1405
NbOfMatches_T interpretTravelRequest(const std::string &iTravelQuery, LocationList_T &, WordList_T &)
OPENTREP::shouldAddPORInSQLDB_T toggleShouldAddPORInSQLDBFlag()
bool shouldAddPORInSQLDB_T
bool shouldIndexPORInXapian_T
const OTransliterator & getTransliterator() const
std::pair< const PORFilePath_T, const DBFilePathPair_T > FilePathSet_T
static Logger & instance()
Definition: Logger.cpp:54
OPENTREP_ServiceContext & create(const TravelDBFilePath_T &, const DBType &, const SQLDBConnectionString_T &, const DeploymentNumber_T &)
static NbOfDBEntries_T getPORByGeonameID(soci::session &, const GeonamesID_T &, LocationList_T &)
Definition: DBManager.cpp:1529
NbOfMatches_T listByGeonameID(const GeonamesID_T &, LocationList_T &)
OPENTREP::DeploymentNumber_T toggleDeploymentNumber()
SQLDBConnectionString_T getSQLConnStr(const DBType &iSQLDBType, const SQLDBConnectionString_T &iSQLDBConnStr)
OPENTREP::shouldIndexNonIATAPOR_T toggleShouldIncludeAllPORFlag()
static NbOfDBEntries_T getPORByICAOCode(soci::session &, const ICAOCode_T &, LocationList_T &)
Definition: DBManager.cpp:1285
void setDeploymentNumber(const DeploymentNumber_T &iDeploymentNumber)
static void createSQLDBIndexes(soci::session &)
Definition: DBManager.cpp:595
static soci::session * initSQLDBSession(const DBType &, const SQLDBConnectionString_T &)
Definition: DBManager.cpp:318
const shouldIndexPORInXapian_T & getShouldIndexPORInXapianFlag() const
void logInit(const LOG::EN_LogLevel iLogLevel, std::ostream &ioLogOutputFile)
unsigned short DeploymentNumber_T
const PORFilePath_T & getPORFilePath() const
NbOfMatches_T listByUNLOCode(const UNLOCode_T &, LocationList_T &)
const unsigned short DEFAULT_OPENTREP_DEPLOYMENT_NUMBER_SIZE
std::pair< const TravelDBFilePath_T, const SQLDBConnectionString_T > DBFilePathPair_T