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