Skip to content

Commit

Permalink
Retrieve Searcher and Search from LRU Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
maneeshpm committed Oct 12, 2021
1 parent 4312297 commit e642d41
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ extern "C" {

#include <zim/uuid.h>
#include <zim/error.h>
#include <zim/search.h>
#include <zim/suggestion.h>
#include <zim/entry.h>
#include <zim/item.h>

Expand All @@ -79,6 +77,7 @@ extern "C" {

#define MAX_SEARCH_LEN 140
#define KIWIX_MIN_CONTENT_SIZE_TO_DEFLATE 100
#define SEARCH_CACHE_SIZE 2

namespace kiwix {

Expand Down Expand Up @@ -139,7 +138,13 @@ InternalServer::InternalServer(Library* library,
m_blockExternalLinks(blockExternalLinks),
mp_daemon(nullptr),
mp_library(library),
mp_nameMapper(nameMapper ? nameMapper : &defaultNameMapper)
mp_nameMapper(nameMapper ? nameMapper : &defaultNameMapper),
searcherCache((std::getenv("SEARCHER_CACHE_SIZE") != NULL)
? atoi(std::getenv("SEARCHER_CACHE_SIZE"))
: std::max(1, (int) (mp_library->getBookCount(true, true)*0.1))),
searchCache((std::getenv("SEARCH_CACHE_SIZE"))
? atoi(std::getenv("SEARCH_CACHE_SIZE"))
: SEARCH_CACHE_SIZE)
{}

bool InternalServer::start() {
Expand Down Expand Up @@ -583,7 +588,12 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re

std::shared_ptr<zim::Searcher> searcher;
if (archive) {
searcher = std::make_shared<zim::Searcher>(*archive);
if (searcherCache.exist(bookId)) {
searcher = searcherCache.get(bookId);
} else {
searcher = std::make_shared<zim::Searcher>(*archive);
searcherCache.put(bookId, searcher);
}
} else {
for (auto& bookId: mp_library->filter(kiwix::Filter().local(true).valid(true))) {
auto currentArchive = mp_library->getArchiveById(bookId);
Expand Down Expand Up @@ -614,6 +624,7 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
}

/* Get the results */
std::string queryString;
try {
zim::Query query;
if (patternString.empty()) {
Expand All @@ -623,20 +634,29 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
}

query.setQuery("");
queryString = to_string(latitude) + to_string(longitude) + to_string(distance);
query.setGeorange(latitude, longitude, distance);
} else {
// Execute Ft search
if (m_verbose.load()) {
cout << "Performing query `" << patternString << "'" << endl;
}

std::string queryString = removeAccents(patternString);
queryString = removeAccents(patternString);
query.setQuery(queryString);
}
queryString = bookId + queryString;

std::shared_ptr<zim::Search> search;
if (searchCache.exist(queryString)) {
search = searchCache.get(queryString);
} else {
search = make_shared<zim::Search>(searcher->search(query));
searchCache.put(queryString, search);
}

zim::Search search = searcher->search(query);
SearchRenderer renderer(search.getResults(start, pageLength), mp_nameMapper, start,
search.getEstimatedMatches());
SearchRenderer renderer(search->getResults(start, pageLength), mp_nameMapper, start,
search->getEstimatedMatches());
renderer.setSearchPattern(patternString);
renderer.setSearchContent(bookName);
renderer.setProtocolPrefix(m_root + "/");
Expand Down
8 changes: 8 additions & 0 deletions src/server/internalServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ extern "C" {
#include "library.h"
#include "name_mapper.h"

#include <zim/search.h>
#include <zim/suggestion.h>

#include <mustache.hpp>

#include <atomic>
Expand All @@ -36,6 +39,8 @@ extern "C" {
#include "server/request_context.h"
#include "server/response.h"

#include "tools/cache.cpp"

namespace kiwix {

typedef kainjow::mustache::data MustacheData;
Expand Down Expand Up @@ -108,6 +113,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;

std::string m_server_id;
std::string m_library_id;

Expand Down

0 comments on commit e642d41

Please sign in to comment.