From 854623618cedaf4ab7e5ce60b092eb6ebb552a7b Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 22 Mar 2022 11:50:22 +0100 Subject: [PATCH] Use the newly introduced searcherCache for multizim searcher. --- include/library.h | 6 +++++- src/library.cpp | 16 ++++++++++------ src/server/internalServer.cpp | 16 +++++----------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/include/library.h b/include/library.h index 58e07c874..0d5d19f0f 100644 --- a/include/library.h +++ b/include/library.h @@ -153,6 +153,7 @@ class Library typedef uint64_t Revision; typedef std::vector BookIdCollection; typedef std::map AttributeCounts; + typedef std::set BookIdSet; public: Library(); @@ -208,7 +209,10 @@ class Library DEPRECATED std::shared_ptr getReaderById(const std::string& id); std::shared_ptr getArchiveById(const std::string& id); - std::shared_ptr getSearcherById(const std::string& id); + std::shared_ptr getSearcherById(const std::string& id) { + return getSearcherByIds(BookIdSet{id}); + } + std::shared_ptr getSearcherByIds(const BookIdSet& ids); /** * Remove a book from the library. diff --git a/src/library.cpp b/src/library.cpp index d57b4d450..7c5a63562 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -304,17 +304,21 @@ std::shared_ptr Library::getArchiveById(const std::string& id) } } -std::shared_ptr Library::getSearcherById(const std::string& id) +std::shared_ptr Library::getSearcherByIds(const BookIdSet& ids) { - std::set 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 archives; + for(auto& id:ids) { + auto archive = getArchiveById(id); + if(!archive) { + throw std::invalid_argument(""); + } + archives.push_back(*archive); } - return std::make_shared(*archive); + return std::make_shared(archives); }); } catch (std::invalid_argument&) { return nullptr; diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 8bc0fd8a2..7fad45d4e 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -596,21 +596,15 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re try { search = searchCache.getOrPut(searchInfo, [=](){ - std::shared_ptr 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(*currentArchive); - } else { - searcher->addArchive(*currentArchive); - } - } - } + bookIds.insert(bookId); + } } + auto searcher = mp_library->getSearcherByIds(bookIds); return make_shared(searcher->search(searchInfo.getZimQuery(m_verbose.load()))); } );