diff --git a/include/meson.build b/include/meson.build index 608423170..0e890d707 100644 --- a/include/meson.build +++ b/include/meson.build @@ -5,10 +5,6 @@ headers = [ 'searcher.h' ] -if xapian_dep.found() - headers += ['xapianSearcher.h'] -endif - install_headers(headers, subdir:'kiwix') install_headers( diff --git a/include/searcher.h b/include/searcher.h index f8999501a..2a1dd3960 100644 --- a/include/searcher.h +++ b/include/searcher.h @@ -35,30 +35,31 @@ using namespace std; -class Result -{ - public: - virtual ~Result() {}; - virtual std::string get_url() = 0; - virtual std::string get_title() = 0; - virtual int get_score() = 0; - virtual std::string get_snippet() = 0; - virtual int get_wordCount() = 0; - virtual int get_size() = 0; -}; - namespace kiwix { + class Reader; + class Result { + public: + virtual ~Result() {}; + virtual std::string get_url() = 0; + virtual std::string get_title() = 0; + virtual int get_score() = 0; + virtual std::string get_snippet() = 0; + virtual int get_wordCount() = 0; + virtual int get_size() = 0; + }; + + struct SearcherInternal; class Searcher { public: - Searcher(); - virtual ~Searcher(); + Searcher(Reader* reader); + ~Searcher(); void search(std::string &search, unsigned int resultStart, unsigned int resultEnd, const bool verbose=false); - virtual Result* getNextResult() = 0; - virtual void restart_search() = 0; + Result* getNextResult(); + void restart_search(); unsigned int getEstimatedResultCount(); bool setProtocolPrefix(const std::string prefix); bool setSearchProtocolPrefix(const std::string prefix); @@ -71,10 +72,12 @@ namespace kiwix { protected: std::string beautifyInteger(const unsigned int number); - virtual void closeIndex() = 0; - virtual void searchInIndex(string &search, const unsigned int resultStart, - const unsigned int resultEnd, const bool verbose=false) = 0; + void closeIndex() ; + void searchInIndex(string &search, const unsigned int resultStart, + const unsigned int resultEnd, const bool verbose=false); + Reader* reader; + SearcherInternal* internal; std::string searchPattern; std::string protocolPrefix; std::string searchProtocolPrefix; diff --git a/meson.build b/meson.build index 75f256eef..19fce8c4f 100644 --- a/meson.build +++ b/meson.build @@ -61,9 +61,7 @@ else endif endif -xapian_dep = dependency('xapian-core', required:false) - -all_deps = [thread_dep, libicu_dep, libzim_dep, xapian_dep, pugixml_dep] +all_deps = [thread_dep, libicu_dep, libzim_dep, pugixml_dep] if has_ctpp2_dep all_deps += [ctpp2_dep] endif @@ -80,9 +78,6 @@ subdir('static') subdir('src') pkg_requires = ['libzim', 'icu-i18n', 'pugixml'] -if xapian_dep.found() - pkg_requires += ['xapian-core'] -endif extra_libs = [] extra_cflags = '' diff --git a/src/meson.build b/src/meson.build index 13a77332d..5cbac1f57 100644 --- a/src/meson.build +++ b/src/meson.build @@ -8,16 +8,10 @@ kiwix_sources = [ 'common/regexTools.cpp', 'common/stringTools.cpp', 'common/networkTools.cpp', - 'common/otherTools.cpp', - 'xapian/htmlparse.cc', - 'xapian/myhtmlparse.cc' + 'common/otherTools.cpp' ] kiwix_sources += lib_resources -if xapian_dep.found() - kiwix_sources += ['xapianSearcher.cpp'] -endif - if get_option('android') subdir('android') endif diff --git a/src/searcher.cpp b/src/searcher.cpp index f458c6129..380ecda4c 100644 --- a/src/searcher.cpp +++ b/src/searcher.cpp @@ -18,8 +18,11 @@ */ #include "searcher.h" +#include "reader.h" #include "kiwixlib-resources.h" +#include + #ifdef ENABLE_CTPP2 #include #include @@ -32,8 +35,39 @@ using namespace CTPP; namespace kiwix { + class _Result : public Result { + public: + _Result(Searcher* searcher, zim::Search::iterator& iterator); + virtual ~_Result() {}; + + virtual std::string get_url(); + virtual std::string get_title(); + virtual int get_score(); + virtual std::string get_snippet(); + virtual int get_wordCount(); + virtual int get_size(); + + private: + Searcher* searcher; + zim::Search::iterator iterator; + }; + + struct SearcherInternal { + const zim::Search *_search; + zim::Search::iterator current_iterator; + + SearcherInternal() : _search(NULL) {} + ~SearcherInternal() { + if ( _search != NULL ) + delete _search; + } + + }; + /* Constructor */ - Searcher::Searcher() : + Searcher::Searcher(Reader* reader) : + reader(reader), + internal(new SearcherInternal()), searchPattern(""), protocolPrefix("zim://"), searchProtocolPrefix("search://?"), @@ -47,7 +81,9 @@ namespace kiwix { } /* Destructor */ - Searcher::~Searcher() {} + Searcher::~Searcher() { + delete internal; + } /* Search strings in the database */ void Searcher::search(std::string &search, unsigned int resultStart, @@ -80,12 +116,28 @@ namespace kiwix { this->resultStart = resultStart; this->resultEnd = resultEnd; string unaccentedSearch = removeAccents(search); - searchInIndex(unaccentedSearch, resultStart, resultEnd, verbose); + internal->_search = this->reader->getZimFileHandler()->search(unaccentedSearch, resultStart, resultEnd); + internal->current_iterator = internal->_search->begin(); + this->estimatedResultCount = internal->_search->get_matches_estimated(); } return; } + void Searcher::restart_search() { + internal->current_iterator = internal->_search->begin(); + } + + Result* Searcher::getNextResult() { + if (internal->current_iterator != internal->_search->end()) { + Result* result = new _Result(this, internal->current_iterator); + internal->current_iterator++; + return result; + } + return NULL; + } + + /* Reset the results */ void Searcher::reset() { this->estimatedResultCount = 0; @@ -112,6 +164,36 @@ namespace kiwix { this->contentHumanReadableId = contentHumanReadableId; } + _Result::_Result(Searcher* searcher, zim::Search::iterator& iterator): + searcher(searcher), + iterator(iterator) + { + } + + std::string _Result::get_url() { + return iterator.get_url(); + } + + std::string _Result::get_title() { + return iterator.get_title(); + } + + int _Result::get_score() { + return iterator.get_score(); + } + + std::string _Result::get_snippet() { + return iterator.get_snippet(); + } + + int _Result::get_size() { + return iterator.get_size(); + } + + int _Result::get_wordCount() { + return iterator.get_wordCount(); + } + #ifdef ENABLE_CTPP2 string Searcher::getHtml() {