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