Skip to content

Commit

Permalink
Retrieve SuggestionSearcher from LRU Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
maneeshpm committed Nov 27, 2021
1 parent 6036854 commit 008db4e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
23 changes: 16 additions & 7 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ InternalServer::InternalServer(Library* library,
mp_library(library),
mp_nameMapper(nameMapper ? nameMapper : &defaultNameMapper),
searcherCache(std::max(getCacheLength("SEARCHER_CACHE_SIZE", mp_library->getBookCount(true, true)*0.1), 1U)),
searchCache(std::max(getCacheLength("SEARCH_CACHE_SIZE", SEARCH_CACHE_SIZE), 1U))
searchCache(std::max(getCacheLength("SEARCH_CACHE_SIZE", SEARCH_CACHE_SIZE), 1U)),
suggestionSearcherCache(std::max(getCacheLength("SUGGESTION_SEARCHER_CACHE_SIZE", mp_library->getBookCount(true, true)*0.1), 1U))
{}

bool InternalServer::start() {
Expand Down Expand Up @@ -356,13 +357,20 @@ std::unique_ptr<Response> InternalServer::build_homepage(const RequestContext& r
**/

// TODO: retrieve searcher from caching mechanism
SuggestionsList_t getSuggestions(const zim::Archive* const archive,
const std::string& queryString, int start, int suggestionCount)
SuggestionsList_t getSuggestions(SuggestionSearcherCache& cache, const zim::Archive* const archive,
const std::string& bookId, const std::string& queryString, int start, int suggestionCount)
{
SuggestionsList_t suggestions;
auto searcher = zim::SuggestionSearcher(*archive);
std::shared_ptr<zim::SuggestionSearcher> searcher;
try {
searcher = cache.get(bookId);
} catch (std::runtime_error&) {
searcher = make_shared<zim::SuggestionSearcher>(*archive);
cache.put(bookId, searcher);
}

if (archive->hasTitleIndex()) {
auto search = searcher.suggest(queryString);
auto search = searcher->suggest(queryString);
auto srs = search.getResults(start, suggestionCount);

for (auto it : srs) {
Expand All @@ -375,7 +383,7 @@ SuggestionsList_t getSuggestions(const zim::Archive* const archive,
std::vector<std::string> variants = getTitleVariants(queryString);
int currCount = 0;
for (auto it = variants.begin(); it != variants.end() && currCount < suggestionCount; it++) {
auto search = searcher.suggest(queryString);
auto search = searcher->suggest(queryString);
auto srs = search.getResults(0, suggestionCount);
for (auto it : srs) {
SuggestionItem suggestion(it.getTitle(), kiwix::normalize(it.getTitle()),
Expand Down Expand Up @@ -492,7 +500,8 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
bool first = true;

/* Get the suggestions */
SuggestionsList_t suggestions = getSuggestions(archive.get(), queryString, start, count);
SuggestionsList_t suggestions = getSuggestions(suggestionSearcherCache, archive.get(),
bookId, queryString, start, count);
for(auto& suggestion:suggestions) {
MustacheData result;
result.set("label", suggestion.getTitle());
Expand Down
8 changes: 6 additions & 2 deletions src/server/internalServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ extern "C" {
namespace kiwix {

typedef kainjow::mustache::data MustacheData;
typedef LRUCache<string, std::shared_ptr<zim::Searcher>> SearcherCache;
typedef LRUCache<string, std::shared_ptr<zim::Search>> SearchCache;
typedef LRUCache<string, std::shared_ptr<zim::SuggestionSearcher>> SuggestionSearcherCache;

class Entry;
class OPDSDumper;
Expand Down Expand Up @@ -115,8 +118,9 @@ class InternalServer {
Library* mp_library;
NameMapper* mp_nameMapper;

LRUCache<string, std::shared_ptr<zim::Searcher>> searcherCache;
LRUCache<string, std::shared_ptr<zim::Search>> searchCache;
SearcherCache searcherCache;
SearchCache searchCache;
SuggestionSearcherCache suggestionSearcherCache;

std::string m_server_id;
std::string m_library_id;
Expand Down

0 comments on commit 008db4e

Please sign in to comment.