From f5af0633ececb27cdb4511e946645ff26a82236f Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 27 Apr 2022 15:18:09 +0200 Subject: [PATCH] Move the searcher cache into the Library --- include/library.h | 2 ++ src/library.cpp | 23 ++++++++++++++++++++++- src/server/internalServer.cpp | 15 ++++++--------- src/server/internalServer.h | 2 -- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/include/library.h b/include/library.h index 4a2bc787e..58e07c874 100644 --- a/include/library.h +++ b/include/library.h @@ -26,6 +26,7 @@ #include #include #include +#include #include "book.h" #include "bookmark.h" @@ -207,6 +208,7 @@ 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); /** * Remove a book from the library. diff --git a/src/library.cpp b/src/library.cpp index b28533a4a..4369f58a7 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -66,13 +66,14 @@ struct Library::Impl struct Entry : Book { Library::Revision lastUpdatedRevision = 0; - }; Library::Revision m_revision; std::map m_books; using ArchiveCache = ConcurrentCache>; std::unique_ptr mp_archiveCache; + using SearcherCache = ConcurrentCache>; + std::unique_ptr mp_searcherCache; std::vector m_bookmarks; Xapian::WritableDatabase m_bookDB; @@ -87,6 +88,7 @@ struct Library::Impl Library::Impl::Impl() : mp_archiveCache(new ArchiveCache(std::max(getEnvVar("ARCHIVE_CACHE_SIZE", 1), 1))), + mp_searcherCache(new SearcherCache(std::max(getEnvVar("SEARCHER_CACHE_SIZE", 1), 1))), m_bookDB("", Xapian::DB_BACKEND_INMEMORY) { } @@ -156,6 +158,9 @@ bool Library::addBook(const Book& book) if (getEnvVar("ARCHIVE_CACHE_SIZE", -1) <= 0) { mp_impl->mp_archiveCache->setMaxSize(new_cache_size); } + if (getEnvVar("SEARCHER_CACHE_SIZE", -1) <= 0) { + mp_impl->mp_searcherCache->setMaxSize(new_cache_size); + } return true; } } @@ -182,6 +187,7 @@ bool Library::removeBookmark(const std::string& zimId, const std::string& url) void Library::dropCache(const std::string& id) { mp_impl->mp_archiveCache->drop(id); + mp_impl->mp_searcherCache->drop(id); } bool Library::removeBookById(const std::string& id) @@ -275,7 +281,22 @@ std::shared_ptr Library::getArchiveById(const std::string& id) } catch (std::invalid_argument&) { return nullptr; } +} +std::shared_ptr Library::getSearcherById(const std::string& id) +{ + try { + return mp_impl->mp_searcherCache->getOrPut(id, + [&](){ + auto archive = getArchiveById(id); + if(!archive) { + throw std::invalid_argument(""); + } + return std::make_shared(*archive); + }); + } catch (std::invalid_argument&) { + return nullptr; + } } unsigned int Library::getBookCount(const bool localBooks, diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 1ed43db94..8bc0fd8a2 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -182,7 +182,6 @@ InternalServer::InternalServer(Library* library, mp_daemon(nullptr), mp_library(library), mp_nameMapper(nameMapper ? nameMapper : &defaultNameMapper), - searcherCache(getEnvVar("SEARCHER_CACHE_SIZE", std::max((unsigned int) (mp_library->getBookCount(true, true)*0.1), 1U))), searchCache(getEnvVar("SEARCH_CACHE_SIZE", DEFAULT_CACHE_SIZE)), suggestionSearcherCache(getEnvVar("SUGGESTION_SEARCHER_CACHE_SIZE", std::max((unsigned int) (mp_library->getBookCount(true, true)*0.1), 1U))) {} @@ -582,11 +581,9 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re auto searchInfo = SearchInfo(request); std::string bookId; - std::shared_ptr archive; if (!searchInfo.bookName.empty()) { try { bookId = mp_nameMapper->getIdForName(searchInfo.bookName); - archive = mp_library->getArchiveById(bookId); } catch (const std::out_of_range&) { throw std::invalid_argument("The requested book doesn't exist."); } @@ -600,8 +597,8 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re search = searchCache.getOrPut(searchInfo, [=](){ std::shared_ptr searcher; - if (archive) { - searcher = searcherCache.getOrPut(bookId, [=](){ return std::make_shared(*archive);}); + if(!bookId.empty()) { + searcher = mp_library->getSearcherById(bookId); } else { for (auto& bookId: mp_library->filter(kiwix::Filter().local(true).valid(true))) { auto currentArchive = mp_library->getArchiveById(bookId); @@ -622,11 +619,11 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re // (in case of zim file not containing a index) const auto cssUrl = renderUrl(m_root, RESOURCE::templates::url_of_search_results_css); HTTPErrorHtmlResponse response(*this, request, MHD_HTTP_NOT_FOUND, - "fulltext-search-unavailable", - "404-page-heading", + "fulltext-search-unavailable", + "404-page-heading", cssUrl); response += nonParameterizedMessage("no-search-results"); - response += TaskbarInfo(searchInfo.bookName, archive.get()); + response += TaskbarInfo(searchInfo.bookName, bookId.empty()?nullptr:mp_library->getArchiveById(bookId).get()); return response; } @@ -656,7 +653,7 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re renderer.setSearchProtocolPrefix(m_root + "/search"); renderer.setPageLength(pageLength); auto response = ContentResponse::build(*this, renderer.getHtml(), "text/html; charset=utf-8"); - response->set_taskbar(searchInfo.bookName, archive.get()); + response->set_taskbar(searchInfo.bookName, bookId.empty()?nullptr:mp_library->getArchiveById(bookId).get()); return std::move(response); } catch (const std::invalid_argument& e) { return HTTP400HtmlResponse(*this, request) diff --git a/src/server/internalServer.h b/src/server/internalServer.h index 69e8166c4..2d6da476b 100644 --- a/src/server/internalServer.h +++ b/src/server/internalServer.h @@ -88,7 +88,6 @@ class SearchInfo { typedef kainjow::mustache::data MustacheData; -typedef ConcurrentCache> SearcherCache; typedef ConcurrentCache> SearchCache; typedef ConcurrentCache> SuggestionSearcherCache; @@ -167,7 +166,6 @@ class InternalServer { Library* mp_library; NameMapper* mp_nameMapper; - SearcherCache searcherCache; SearchCache searchCache; SuggestionSearcherCache suggestionSearcherCache;