OpenTREP Logo  0.6.0
C++ Open Travel Request Parsing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentrep-indexer.cpp
Go to the documentation of this file.
1 // STL
2 #include <cassert>
3 #include <iostream>
4 #include <sstream>
5 #include <fstream>
6 #include <vector>
7 #include <string>
8 // Boost (Extended STL)
9 #include <boost/date_time/posix_time/posix_time.hpp>
10 #include <boost/date_time/gregorian/gregorian.hpp>
11 #include <boost/program_options.hpp>
12 // OpenTREP
15 #include <opentrep/config/opentrep-paths.hpp>
16 
17 
18 // //////// Type definitions ///////
19 typedef std::vector<std::string> WordList_T;
20 
21 
22 // //////// Constants //////
26 const std::string K_OPENTREP_DEFAULT_LOG_FILENAME ("opentrep-indexer.log");
27 
28 
29 // ///////// Parsing of Options & Configuration /////////
32 
34 int readConfiguration (int argc, char* argv[],
35  std::string& ioPORFilepath,
36  std::string& ioXapianDBFilepath,
37  std::string& ioSQLiteDBFilepath,
38  std::string& ioLogFilename) {
39 
40  // Declare a group of options that will be allowed only on command line
41  boost::program_options::options_description generic ("Generic options");
42  generic.add_options()
43  ("prefix", "print installation prefix")
44  ("version,v", "print version string")
45  ("help,h", "produce help message");
46 
47  // Declare a group of options that will be allowed both on command
48  // line and in config file
49  boost::program_options::options_description config ("Configuration");
50  config.add_options()
51  ("porfile,p",
52  boost::program_options::value< std::string >(&ioPORFilepath)->default_value(OPENTREP::DEFAULT_OPENTREP_POR_FILEPATH),
53  "POR file-path (e.g., ori_por_public.csv)")
54  ("xapiandb,d",
55  boost::program_options::value< std::string >(&ioXapianDBFilepath)->default_value(OPENTREP::DEFAULT_OPENTREP_XAPIAN_DB_FILEPATH),
56  "Xapian database filepath (e.g., /tmp/opentrep/traveldb)")
57  ("sqlite,s",
58  boost::program_options::value< std::string >(&ioSQLiteDBFilepath)->default_value(OPENTREP::DEFAULT_OPENTREP_SQLITE_DB_FILEPATH),
59  "SQLite3 database filepath (e.g., ~/tmp/opentrep/traveldb/ori_por_public.db)")
60  ("log,l",
61  boost::program_options::value< std::string >(&ioLogFilename)->default_value(K_OPENTREP_DEFAULT_LOG_FILENAME),
62  "Filepath for the logs")
63  ;
64 
65  // Hidden options, will be allowed both on command line and
66  // in config file, but will not be shown to the user.
67  boost::program_options::options_description hidden ("Hidden options");
68  hidden.add_options()
69  ("copyright",
70  boost::program_options::value< std::vector<std::string> >(),
71  "Show the copyright (license)");
72 
73  boost::program_options::options_description cmdline_options;
74  cmdline_options.add(generic).add(config).add(hidden);
75 
76  boost::program_options::options_description config_file_options;
77  config_file_options.add(config).add(hidden);
78 
79  boost::program_options::options_description visible ("Allowed options");
80  visible.add(generic).add(config);
81 
82  boost::program_options::positional_options_description p;
83  p.add ("copyright", -1);
84 
85  boost::program_options::variables_map vm;
86  boost::program_options::
87  store (boost::program_options::command_line_parser (argc, argv).
88  options (cmdline_options).positional(p).run(), vm);
89 
90  std::ifstream ifs ("opentrep-indexer.cfg");
91  boost::program_options::store (parse_config_file (ifs, config_file_options),
92  vm);
93  boost::program_options::notify (vm);
94 
95  if (vm.count ("help")) {
96  std::cout << visible << std::endl;
98  }
99 
100  if (vm.count ("version")) {
101  std::cout << PACKAGE_NAME << ", version " << PACKAGE_VERSION << std::endl;
103  }
104 
105  if (vm.count ("prefix")) {
106  std::cout << "Installation prefix: " << PREFIXDIR << std::endl;
108  }
109 
110  if (vm.count ("porfile")) {
111  ioPORFilepath = vm["porfile"].as< std::string >();
112  std::cout << "POR file-path is: " << ioPORFilepath << std::endl;
113  }
114 
115  if (vm.count ("xapiandb")) {
116  ioXapianDBFilepath = vm["xapiandb"].as< std::string >();
117  std::cout << "Xapian database filepath is: " << ioXapianDBFilepath
118  << std::endl;
119  }
120 
121  if (vm.count ("sqlitedb")) {
122  ioSQLiteDBFilepath = vm["sqlitedb"].as< std::string >();
123  std::cout << "SQLite3 database filepath is: " << ioSQLiteDBFilepath
124  << std::endl;
125  }
126 
127  if (vm.count ("log")) {
128  ioLogFilename = vm["log"].as< std::string >();
129  std::cout << "Log filename is: " << ioLogFilename << std::endl;
130  }
131 
132  return 0;
133 }
134 
135 
136 // /////////////// M A I N /////////////////
137 int main (int argc, char* argv[]) {
138 
139  // Output log File
140  std::string lLogFilename;
141 
142  // File-path of POR (points of reference)
143  std::string lPORFilepathStr;
144 
145  // Xapian database name (directory of the index)
146  std::string lXapianDBNameStr;
147 
148  // SQLite3 database file-path
149  std::string lSQLiteDBFilePathStr;
150 
151  // Call the command-line option parser
152  const int lOptionParserStatus =
153  readConfiguration (argc, argv, lPORFilepathStr,
154  lXapianDBNameStr, lSQLiteDBFilePathStr, lLogFilename);
155 
156  if (lOptionParserStatus == K_OPENTREP_EARLY_RETURN_STATUS) {
157  return 0;
158  }
159 
160  // Set the log parameters
161  std::ofstream logOutputFile;
162  // open and clean the log outputfile
163  logOutputFile.open (lLogFilename.c_str());
164  logOutputFile.clear();
165 
166  //
167  std::cout << "Creating the Xapian index/database may take a few minutes "
168  << "on some architectures (and a few seconds on fastest ones)..."
169  << std::endl;
170 
171  // Initialise the context
172  const OPENTREP::PORFilePath_T lPORFilepath (lPORFilepathStr);
173  const OPENTREP::TravelDBFilePath_T lXapianDBName (lXapianDBNameStr);
174  const OPENTREP::SQLiteDBFilePath_T lSQLiteDBFilePath (lSQLiteDBFilePathStr);
175  OPENTREP::OPENTREP_Service opentrepService (logOutputFile, lPORFilepath,
176  lXapianDBName, lSQLiteDBFilePath);
177 
178  // Launch the indexation
179  const OPENTREP::NbOfDBEntries_T lNbOfEntries =
180  opentrepService.buildSearchIndex();
181 
182  // Close the Log outputFile
183  logOutputFile.close();
184 
185  //
186  std::cout << lNbOfEntries << " entries have been processed" << std::endl;
187 
188  return 0;
189 }
int main(int argc, char *argv[])
const std::string K_OPENTREP_DEFAULT_LOG_FILENAME("opentrep-indexer.log")
const std::string DEFAULT_OPENTREP_SQLITE_DB_FILEPATH
unsigned int NbOfDBEntries_T
Interface for the OPENTREP Services.
std::vector< std::string > WordList_T
int readConfiguration(int argc, char *argv[], std::string &ioPORFilepath, std::string &ioXapianDBFilepath, std::string &ioSQLiteDBFilepath, std::string &ioLogFilename)
NbOfDBEntries_T buildSearchIndex()
const std::string DEFAULT_OPENTREP_XAPIAN_DB_FILEPATH
const std::string DEFAULT_OPENTREP_POR_FILEPATH
const int K_OPENTREP_EARLY_RETURN_STATUS