10#include <boost/filesystem.hpp>
11#include <boost/filesystem/fstream.hpp>
12#include <boost/tokenizer.hpp>
13#include <boost/iostreams/device/file.hpp>
14#include <boost/iostreams/filtering_stream.hpp>
15#include <boost/iostreams/filter/gzip.hpp>
16#include <boost/iostreams/filter/bzip2.hpp>
41 Xapian::WritableDatabase& ioDatabase) {
48 Xapian::TermGenerator lTermGenerator;
49 lTermGenerator.set_database (ioDatabase);
50 lTermGenerator.set_document (ioDocument);
56 for (Place::TermSetMap_T::const_iterator itStringSet = lTermSetMap.begin();
57 itStringSet != lTermSetMap.end(); ++itStringSet) {
59 const Weight_T& lWeight = itStringSet->first;
60 const Xapian::termcount lWDFInc =
61 static_cast<const Xapian::termcount
> (lWeight);
65 for (Place::StringSet_T::const_iterator itString = lTermSet.begin();
66 itString != lTermSet.end(); ++itString) {
67 const std::string& lString = *itString;
68 lTermGenerator.index_text (lString, lWDFInc);
77 for (Place::StringSet_T::const_iterator itTerm = lSpellingSet.begin();
78 itTerm != lSpellingSet.end(); ++itTerm) {
79 const std::string& lTerm = *itTerm;
82 if (lTerm.size() <= 254) {
83 ioDatabase.add_spelling (lTerm);
90 <<
" into " << ioDocument.get_description());
94 void IndexBuilder::addDocumentToIndex(Xapian::WritableDatabase& ioDatabase,
96 const OTransliterator& iTransliterator) {
99 Xapian::Document lDocument;
103 const RawDataString_T& lRawDataString = ioPlace.getRawDataString();
108 lDocument.set_data (lRawDataString);
112 ioPlace.buildIndexSets (iTransliterator);
118 const Xapian::docid& lDocID = ioDatabase.add_document (lDocument);
122 ioPlace.setDocID (lDocID);
127 buildSearchIndex (Xapian::WritableDatabase* ioXapianDB_ptr,
128 const DBType& iSQLDBType, soci::session* ioSociSessionPtr,
129 std::istream& iPORFileStream,
137 std::string itReadLine;
138 while (std::getline (iPORFileStream, itReadLine)) {
147 if (!iIncludeNonIATAPOR) {
148 const unsigned short lFirstSeparatorPos = itReadLine.find_first_of (
"^");
149 if (lFirstSeparatorPos != 3) {
158 ++oNbOfEntriesInPORFile;
166 PORStringParser lStringParser (itReadLine);
169 const Location& lLocation = lStringParser.generateLocation();
181 const std::string& lCommonName = lLocation.getCommonName();
182 if (lCommonName ==
"NotAvailable") {
187 lPlace.setLocation (lLocation);
191 if (ioXapianDB_ptr != NULL) {
193 IndexBuilder::addDocumentToIndex (*ioXapianDB_ptr, lPlace,
195 }
catch (
const Xapian::Error& iError) {
196 std::ostringstream errorStr;
197 errorStr <<
"Xapian failed while indexing '"
198 << lPlace.describeKey() <<
"': "
199 << iError.get_description();
201 throw XapianDatabaseFailureException (errorStr.str());
206 if (ioSociSessionPtr != NULL) {
217 ++oNbOfEntries; ++oNbOfEntriesInPORFile;
220 if (oNbOfEntries % 1000 == 0) {
221 std::cout.imbue( std::locale (std::locale::classic(),
new NumSep));
222 std::cout <<
"Number of actually parsed records: " << oNbOfEntries
223 <<
", out of " << oNbOfEntriesInPORFile
224 <<
" records in the POR data file so far" << std::endl;
231 lPlace.resetMatrix();
232 lPlace.resetIndexSets();
249 soci::session* lSociSession_ptr = NULL;
250 Xapian::WritableDatabase* lXapianDatabase_ptr = NULL;
259 if (iShouldIndexPORInXapian) {
265 lXapianDatabase_ptr =
267 assert (lXapianDatabase_ptr != NULL);
271 << iTravelIndexFilePath
272 <<
"') has been re-created, checked and opened");
282 if (iShouldAddPORInSQLDB) {
303 if (lSociSession_ptr == NULL) {
304 std::ostringstream errorStr;
305 errorStr <<
"Error when trying to connect to the SQL database ('"
306 << iSQLDBConnStr <<
"')";
308 throw SQLDatabaseImpossibleConnectionException (errorStr.str());
310 assert (lSociSession_ptr != NULL);
324 const PORFileHelper lPORFileHelper (iPORFilePath);
325 std::istream& lPORFileStream = lPORFileHelper.getFileStreamRef();
330 oNbOfEntries = buildSearchIndex (lXapianDatabase_ptr, iSQLDBType,
331 lSociSession_ptr, lPORFileStream,
332 iIncludeNonIATAPOR, iTransliterator);
338 if (iShouldIndexPORInXapian) {
339 assert (lXapianDatabase_ptr != NULL);
343 lXapianDatabase_ptr->commit();
356 if (iShouldIndexPORInXapian) {
357 assert (lXapianDatabase_ptr != NULL);
358 lXapianDatabase_ptr->close();
362 if (iShouldAddPORInSQLDB) {
367 assert (lSociSession_ptr != NULL);
375 assert (lSociSession_ptr != NULL);
#define OPENTREP_LOG_ERROR(iToBeLogged)
#define OPENTREP_LOG_DEBUG(iToBeLogged)
static void terminateSQLDBSession(const DBType &, const SQLDBConnectionString_T &, soci::session &)
static void createSQLDBTables(soci::session &)
static soci::session * initSQLDBSession(const DBType &, const SQLDBConnectionString_T &)
static void createSQLDBIndexes(soci::session &)
static void insertPlaceInDB(soci::session &, const Place &)
static FacPlace & instance()
static FacXapianDB & instance()
Xapian::WritableDatabase * create(const TravelDBFilePath_T &, const int &iXapianActionFlag)
static void recreateXapianDirectory(const std::string &iTravelDBFilePath)
Class modelling a place/POR (point of reference).
std::map< const Weight_T, StringSet_T > TermSetMap_T
std::string describeSets() const
const StringSet_T & getSpellingSet() const
std::set< std::string > StringSet_T
std::string describeKey() const
const TermSetMap_T & getTermSetMap() const
void addToXapian(const Place &iPlace, Xapian::Document &ioDocument, Xapian::WritableDatabase &ioDatabase)
bool shouldAddPORInSQLDB_T
unsigned int NbOfDBEntries_T
bool shouldIndexPORInXapian_T
bool shouldIndexNonIATAPOR_T
Enumeration of database types.