Use the newly introduced searcherCache for multizim searcher.

This commit is contained in:
Matthieu Gautier 2022-03-22 11:50:22 +01:00
parent fd0edbba80
commit 854623618c
3 changed files with 20 additions and 18 deletions

View File

@ -153,6 +153,7 @@ class Library
typedef uint64_t Revision;
typedef std::vector<std::string> BookIdCollection;
typedef std::map<std::string, int> AttributeCounts;
typedef std::set<std::string> BookIdSet;
public:
Library();
@ -208,7 +209,10 @@ class Library
DEPRECATED std::shared_ptr<Reader> getReaderById(const std::string& id);
std::shared_ptr<zim::Archive> getArchiveById(const std::string& id);
std::shared_ptr<zim::Searcher> getSearcherById(const std::string& id);
std::shared_ptr<zim::Searcher> getSearcherById(const std::string& id) {
return getSearcherByIds(BookIdSet{id});
}
std::shared_ptr<zim::Searcher> getSearcherByIds(const BookIdSet& ids);
/**
* Remove a book from the library.

View File

@ -304,17 +304,21 @@ std::shared_ptr<zim::Archive> Library::getArchiveById(const std::string& id)
}
}
std::shared_ptr<zim::Searcher> Library::getSearcherById(const std::string& id)
std::shared_ptr<zim::Searcher> Library::getSearcherByIds(const BookIdSet& ids)
{
std::set<std::string> ids {id};
assert(!ids.empty());
try {
return mp_impl->mp_searcherCache->getOrPut(ids,
[&](){
auto archive = getArchiveById(id);
if(!archive) {
throw std::invalid_argument("");
std::vector<zim::Archive> archives;
for(auto& id:ids) {
auto archive = getArchiveById(id);
if(!archive) {
throw std::invalid_argument("");
}
archives.push_back(*archive);
}
return std::make_shared<zim::Searcher>(*archive);
return std::make_shared<zim::Searcher>(archives);
});
} catch (std::invalid_argument&) {
return nullptr;

View File

@ -596,21 +596,15 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
try {
search = searchCache.getOrPut(searchInfo,
[=](){
std::shared_ptr<zim::Searcher> searcher;
Library::BookIdSet bookIds;
if(!bookId.empty()) {
searcher = mp_library->getSearcherById(bookId);
bookIds.insert(bookId);
} else {
for (auto& bookId: mp_library->filter(kiwix::Filter().local(true).valid(true))) {
auto currentArchive = mp_library->getArchiveById(bookId);
if (currentArchive) {
if (! searcher) {
searcher = std::make_shared<zim::Searcher>(*currentArchive);
} else {
searcher->addArchive(*currentArchive);
}
}
}
bookIds.insert(bookId);
}
}
auto searcher = mp_library->getSearcherByIds(bookIds);
return make_shared<zim::Search>(searcher->search(searchInfo.getZimQuery(m_verbose.load())));
}
);