mirror of
https://github.com/kiwix/kiwix-tools.git
synced 2025-09-22 19:38:53 -04:00
Use only one lock to acces zim library.
Search can now use reader. So there is no need to have to different locks.
This commit is contained in:
parent
08f2373360
commit
4b9dc39c49
@ -92,10 +92,9 @@ static bool verboseFlag = false;
|
|||||||
static std::map<std::string, std::string> extMimeTypes;
|
static std::map<std::string, std::string> extMimeTypes;
|
||||||
static std::map<std::string, kiwix::Reader*> readers;
|
static std::map<std::string, kiwix::Reader*> readers;
|
||||||
static std::map<std::string, kiwix::Searcher*> searchers;
|
static std::map<std::string, kiwix::Searcher*> searchers;
|
||||||
static pthread_mutex_t readerLock = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t zimLock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
static pthread_mutex_t mapLock = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t mapLock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
static pthread_mutex_t welcomeLock = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t welcomeLock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
static pthread_mutex_t searcherLock = PTHREAD_MUTEX_INITIALIZER;
|
|
||||||
static pthread_mutex_t compressorLock = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t compressorLock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
static pthread_mutex_t verboseFlagLock = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t verboseFlagLock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
static pthread_mutex_t mimeTypeLock = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t mimeTypeLock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
@ -259,31 +258,31 @@ ssize_t callback_reader_from_blob(void* cls,
|
|||||||
size_t max)
|
size_t max)
|
||||||
{
|
{
|
||||||
zim::Blob* blob = static_cast<zim::Blob*>(cls);
|
zim::Blob* blob = static_cast<zim::Blob*>(cls);
|
||||||
pthread_mutex_lock(&readerLock);
|
pthread_mutex_lock(&zimLock);
|
||||||
size_t max_size_to_set = min<size_t>(max, blob->size() - pos);
|
size_t max_size_to_set = min<size_t>(max, blob->size() - pos);
|
||||||
|
|
||||||
if (max_size_to_set <= 0) {
|
if (max_size_to_set <= 0) {
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&zimLock);
|
||||||
return MHD_CONTENT_READER_END_WITH_ERROR;
|
return MHD_CONTENT_READER_END_WITH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(buf, blob->data() + pos, max_size_to_set);
|
memcpy(buf, blob->data() + pos, max_size_to_set);
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&zimLock);
|
||||||
return max_size_to_set;
|
return max_size_to_set;
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_free_blob(void* cls)
|
void callback_free_blob(void* cls)
|
||||||
{
|
{
|
||||||
zim::Blob* blob = static_cast<zim::Blob*>(cls);
|
zim::Blob* blob = static_cast<zim::Blob*>(cls);
|
||||||
pthread_mutex_lock(&readerLock);
|
pthread_mutex_lock(&zimLock);
|
||||||
delete blob;
|
delete blob;
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&zimLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct MHD_Response* build_callback_response_from_blob(
|
static struct MHD_Response* build_callback_response_from_blob(
|
||||||
zim::Blob& blob, const std::string& mimeType)
|
zim::Blob& blob, const std::string& mimeType)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&readerLock);
|
pthread_mutex_lock(&zimLock);
|
||||||
zim::Blob* p_blob = new zim::Blob(blob);
|
zim::Blob* p_blob = new zim::Blob(blob);
|
||||||
struct MHD_Response* response
|
struct MHD_Response* response
|
||||||
= MHD_create_response_from_callback(blob.size(),
|
= MHD_create_response_from_callback(blob.size(),
|
||||||
@ -291,7 +290,7 @@ static struct MHD_Response* build_callback_response_from_blob(
|
|||||||
callback_reader_from_blob,
|
callback_reader_from_blob,
|
||||||
p_blob,
|
p_blob,
|
||||||
callback_free_blob);
|
callback_free_blob);
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&zimLock);
|
||||||
/* Tell the client that byte ranges are accepted */
|
/* Tell the client that byte ranges are accepted */
|
||||||
MHD_add_response_header(response, MHD_HTTP_HEADER_ACCEPT_RANGES, "bytes");
|
MHD_add_response_header(response, MHD_HTTP_HEADER_ACCEPT_RANGES, "bytes");
|
||||||
|
|
||||||
@ -333,6 +332,7 @@ static struct MHD_Response* handle_suggest(
|
|||||||
std::cout << "Searching suggestions for: \"" << term << "\"" << endl;
|
std::cout << "Searching suggestions for: \"" << term << "\"" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_mutex_lock(&zimLock);
|
||||||
/* Get the suggestions */
|
/* Get the suggestions */
|
||||||
content = "[";
|
content = "[";
|
||||||
reader->searchSuggestionsSmart(term, maxSuggestionCount);
|
reader->searchSuggestionsSmart(term, maxSuggestionCount);
|
||||||
@ -343,6 +343,7 @@ static struct MHD_Response* handle_suggest(
|
|||||||
+ "\"}";
|
+ "\"}";
|
||||||
suggestionCount++;
|
suggestionCount++;
|
||||||
}
|
}
|
||||||
|
pthread_mutex_unlock(&zimLock);
|
||||||
|
|
||||||
/* Propose the fulltext search if possible */
|
/* Propose the fulltext search if possible */
|
||||||
if (searcher != NULL) {
|
if (searcher != NULL) {
|
||||||
@ -399,12 +400,12 @@ static struct MHD_Response* handle_search(
|
|||||||
std::vector<std::string> variants = reader->getTitleVariants(patternString);
|
std::vector<std::string> variants = reader->getTitleVariants(patternString);
|
||||||
std::vector<std::string>::iterator variantsItr = variants.begin();
|
std::vector<std::string>::iterator variantsItr = variants.begin();
|
||||||
|
|
||||||
pthread_mutex_lock(&readerLock);
|
pthread_mutex_lock(&zimLock);
|
||||||
while (patternCorrespondingUrl.empty() && variantsItr != variants.end()) {
|
while (patternCorrespondingUrl.empty() && variantsItr != variants.end()) {
|
||||||
reader->getPageUrlFromTitle(*variantsItr, patternCorrespondingUrl);
|
reader->getPageUrlFromTitle(*variantsItr, patternCorrespondingUrl);
|
||||||
variantsItr++;
|
variantsItr++;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&zimLock);
|
||||||
|
|
||||||
/* If article found then redirect directly to it */
|
/* If article found then redirect directly to it */
|
||||||
if (!patternCorrespondingUrl.empty()) {
|
if (!patternCorrespondingUrl.empty()) {
|
||||||
@ -425,16 +426,14 @@ static struct MHD_Response* handle_search(
|
|||||||
unsigned int endNumber = end != NULL ? atoi(end) : 25;
|
unsigned int endNumber = end != NULL ? atoi(end) : 25;
|
||||||
|
|
||||||
/* Get the results */
|
/* Get the results */
|
||||||
pthread_mutex_lock(&searcherLock);
|
pthread_mutex_lock(&zimLock);
|
||||||
pthread_mutex_lock(&readerLock);
|
|
||||||
try {
|
try {
|
||||||
searcher->search(patternString, startNumber, endNumber, isVerbose());
|
searcher->search(patternString, startNumber, endNumber, isVerbose());
|
||||||
content = searcher->getHtml();
|
content = searcher->getHtml();
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&zimLock);
|
||||||
pthread_mutex_unlock(&searcherLock);
|
|
||||||
} else {
|
} else {
|
||||||
content = "<!DOCTYPE html>\n<html><head><meta content=\"text/html;charset=UTF-8\" http-equiv=\"content-type\" /><title>Fulltext search unavailable</title></head><body><h1>Not Found</h1><p>There is no article with the title <b>\"" + kiwix::encodeDiples(patternString) + "\"</b> and the fulltext search engine is not available for this content.</p></body></html>";
|
content = "<!DOCTYPE html>\n<html><head><meta content=\"text/html;charset=UTF-8\" http-equiv=\"content-type\" /><title>Fulltext search unavailable</title></head><body><h1>Not Found</h1><p>There is no article with the title <b>\"" + kiwix::encodeDiples(patternString) + "\"</b> and the fulltext search engine is not available for this content.</p></body></html>";
|
||||||
httpResponseCode = MHD_HTTP_NOT_FOUND;
|
httpResponseCode = MHD_HTTP_NOT_FOUND;
|
||||||
@ -465,9 +464,9 @@ static struct MHD_Response* handle_random(
|
|||||||
std::string httpRedirection;
|
std::string httpRedirection;
|
||||||
httpResponseCode = MHD_HTTP_FOUND;
|
httpResponseCode = MHD_HTTP_FOUND;
|
||||||
if (reader != NULL) {
|
if (reader != NULL) {
|
||||||
pthread_mutex_lock(&readerLock);
|
pthread_mutex_lock(&zimLock);
|
||||||
std::string randomUrl = reader->getRandomPageUrl();
|
std::string randomUrl = reader->getRandomPageUrl();
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&zimLock);
|
||||||
httpRedirection
|
httpRedirection
|
||||||
= "/" + humanReadableBookId + "/" + kiwix::urlEncode(randomUrl);
|
= "/" + humanReadableBookId + "/" + kiwix::urlEncode(randomUrl);
|
||||||
}
|
}
|
||||||
@ -489,7 +488,7 @@ static struct MHD_Response* handle_content(
|
|||||||
|
|
||||||
bool found = false;
|
bool found = false;
|
||||||
zim::Article article;
|
zim::Article article;
|
||||||
pthread_mutex_lock(&readerLock);
|
pthread_mutex_lock(&zimLock);
|
||||||
try {
|
try {
|
||||||
found = reader->getArticleObjectByDecodedUrl(urlStr, article);
|
found = reader->getArticleObjectByDecodedUrl(urlStr, article);
|
||||||
|
|
||||||
@ -508,7 +507,7 @@ static struct MHD_Response* handle_content(
|
|||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
found = false;
|
found = false;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&zimLock);
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
if (isVerbose())
|
if (isVerbose())
|
||||||
@ -530,9 +529,9 @@ static struct MHD_Response* handle_content(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
pthread_mutex_lock(&readerLock);
|
pthread_mutex_lock(&zimLock);
|
||||||
mimeType = article.getMimeType();
|
mimeType = article.getMimeType();
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&zimLock);
|
||||||
} catch (exception& e) {
|
} catch (exception& e) {
|
||||||
mimeType = "application/octet-stream";
|
mimeType = "application/octet-stream";
|
||||||
}
|
}
|
||||||
@ -542,16 +541,16 @@ static struct MHD_Response* handle_content(
|
|||||||
cout << "mimeType: " << mimeType << endl;
|
cout << "mimeType: " << mimeType << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_lock(&readerLock);
|
pthread_mutex_lock(&zimLock);
|
||||||
zim::Blob raw_content = article.getData();
|
zim::Blob raw_content = article.getData();
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&zimLock);
|
||||||
|
|
||||||
if (mimeType.find("text/") != string::npos
|
if (mimeType.find("text/") != string::npos
|
||||||
|| mimeType.find("application/javascript") != string::npos
|
|| mimeType.find("application/javascript") != string::npos
|
||||||
|| mimeType.find("application/json") != string::npos) {
|
|| mimeType.find("application/json") != string::npos) {
|
||||||
pthread_mutex_lock(&readerLock);
|
pthread_mutex_lock(&zimLock);
|
||||||
content = string(raw_content.data(), raw_content.size());
|
content = string(raw_content.data(), raw_content.size());
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&zimLock);
|
||||||
|
|
||||||
/* Special rewrite URL in case of ZIM file use intern *asbolute* url like
|
/* Special rewrite URL in case of ZIM file use intern *asbolute* url like
|
||||||
* /A/Kiwix */
|
* /A/Kiwix */
|
||||||
@ -1001,10 +1000,9 @@ int main(int argc, char** argv)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Mutex init */
|
/* Mutex init */
|
||||||
pthread_mutex_init(&readerLock, NULL);
|
pthread_mutex_init(&zimLock, NULL);
|
||||||
pthread_mutex_init(&mapLock, NULL);
|
pthread_mutex_init(&mapLock, NULL);
|
||||||
pthread_mutex_init(&welcomeLock, NULL);
|
pthread_mutex_init(&welcomeLock, NULL);
|
||||||
pthread_mutex_init(&searcherLock, NULL);
|
|
||||||
pthread_mutex_init(&compressorLock, NULL);
|
pthread_mutex_init(&compressorLock, NULL);
|
||||||
pthread_mutex_init(&verboseFlagLock, NULL);
|
pthread_mutex_init(&verboseFlagLock, NULL);
|
||||||
pthread_mutex_init(&mimeTypeLock, NULL);
|
pthread_mutex_init(&mimeTypeLock, NULL);
|
||||||
@ -1147,8 +1145,7 @@ int main(int argc, char** argv)
|
|||||||
MHD_stop_daemon(daemon);
|
MHD_stop_daemon(daemon);
|
||||||
|
|
||||||
/* Mutex destroy */
|
/* Mutex destroy */
|
||||||
pthread_mutex_destroy(&readerLock);
|
pthread_mutex_destroy(&zimLock);
|
||||||
pthread_mutex_destroy(&searcherLock);
|
|
||||||
pthread_mutex_destroy(&compressorLock);
|
pthread_mutex_destroy(&compressorLock);
|
||||||
pthread_mutex_destroy(&mapLock);
|
pthread_mutex_destroy(&mapLock);
|
||||||
pthread_mutex_destroy(&welcomeLock);
|
pthread_mutex_destroy(&welcomeLock);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user