From e526026407c60c1481d7c17c09ba7982ba574cfc Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 28 Sep 2016 17:21:20 +0200 Subject: [PATCH 1/2] Properly fail when creating XapianSearcher on a zim without embedded index. The XapianSearcher creation must fail with a exception if we cannot open the xapian database. So, if we try to open a zim and there is no embedded index, we must fail. We raise the custom exception NoXapianIndexInZim in this case. --- src/common/kiwix/xapianSearcher.cpp | 14 ++++++-------- src/common/kiwix/xapianSearcher.h | 8 ++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/common/kiwix/xapianSearcher.cpp b/src/common/kiwix/xapianSearcher.cpp index 80dfedf21..923bc19d6 100644 --- a/src/common/kiwix/xapianSearcher.cpp +++ b/src/common/kiwix/xapianSearcher.cpp @@ -36,18 +36,16 @@ namespace kiwix { /* Open Xapian readable database */ void XapianSearcher::openIndex(const string &directoryPath) { - bool indexInZim = false; try { zim::File zimFile = zim::File(directoryPath); zim::Article xapianArticle = zimFile.getArticle('Z', "/Z/fulltextIndex/xapian"); - if (xapianArticle.good()) - { - zim::offset_type dbOffset = xapianArticle.getOffset(); - int databasefd = open(directoryPath.c_str(), O_RDONLY); - lseek(databasefd, dbOffset, SEEK_SET); - this->readableDatabase = Xapian::Database(databasefd); - } + if ( ! xapianArticle.good()) + throw NoXapianIndexInZim(); + zim::offset_type dbOffset = xapianArticle.getOffset(); + int databasefd = open(directoryPath.c_str(), O_RDONLY); + lseek(databasefd, dbOffset, SEEK_SET); + this->readableDatabase = Xapian::Database(databasefd); } catch (zim::ZimFileFormatError) { this->readableDatabase = Xapian::Database(directoryPath); diff --git a/src/common/kiwix/xapianSearcher.h b/src/common/kiwix/xapianSearcher.h index e01283be7..3fb24b29e 100644 --- a/src/common/kiwix/xapianSearcher.h +++ b/src/common/kiwix/xapianSearcher.h @@ -27,6 +27,14 @@ using namespace std; namespace kiwix { + class NoXapianIndexInZim: public exception + { + virtual const char* what() const throw() + { + return "There is no fulltext index in the zim file"; + } + }; + class XapianSearcher : public Searcher { public: From 657fc0522571edc654886915918ab847715a23ba Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Mon, 3 Oct 2016 15:39:00 +0200 Subject: [PATCH 2/2] Fix url of the fulltext index. The fulltext index in the zim is at the url /Z/fulltextIndex/xapian. We do not need to specifie the Z in the url as this is automatically add with the namespace. --- src/common/kiwix/xapianSearcher.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/kiwix/xapianSearcher.cpp b/src/common/kiwix/xapianSearcher.cpp index 923bc19d6..6d895aa6d 100644 --- a/src/common/kiwix/xapianSearcher.cpp +++ b/src/common/kiwix/xapianSearcher.cpp @@ -39,7 +39,7 @@ namespace kiwix { try { zim::File zimFile = zim::File(directoryPath); - zim::Article xapianArticle = zimFile.getArticle('Z', "/Z/fulltextIndex/xapian"); + zim::Article xapianArticle = zimFile.getArticle('Z', "/fulltextIndex/xapian"); if ( ! xapianArticle.good()) throw NoXapianIndexInZim(); zim::offset_type dbOffset = xapianArticle.getOffset();