Check if a valuemaps metadata is available in the database and use it.

This way, we do not make assumption of where the values are stored.
This commit is contained in:
Matthieu Gautier 2017-03-21 16:26:03 +01:00
parent 83d27255cf
commit 9be2abedf3
2 changed files with 67 additions and 8 deletions

View File

@ -22,14 +22,18 @@
#include <xapian.h> #include <xapian.h>
#include "searcher.h" #include "searcher.h"
#include <map>
#include <string>
using namespace std; using namespace std;
namespace kiwix { namespace kiwix {
class XapianSearcher;
class XapianResult : public Result { class XapianResult : public Result {
public: public:
XapianResult(Xapian::MSetIterator& iterator); XapianResult(XapianSearcher* searcher, Xapian::MSetIterator& iterator);
virtual ~XapianResult() {}; virtual ~XapianResult() {};
virtual std::string get_url(); virtual std::string get_url();
@ -40,6 +44,7 @@ namespace kiwix {
virtual int get_size(); virtual int get_size();
private: private:
XapianSearcher* searcher;
Xapian::MSetIterator iterator; Xapian::MSetIterator iterator;
Xapian::Document document; Xapian::Document document;
}; };
@ -51,7 +56,7 @@ namespace kiwix {
}; };
class XapianSearcher : public Searcher { class XapianSearcher : public Searcher {
friend class XapianResult;
public: public:
XapianSearcher(const string &xapianDirectoryPath); XapianSearcher(const string &xapianDirectoryPath);
virtual ~XapianSearcher() {}; virtual ~XapianSearcher() {};
@ -68,6 +73,7 @@ namespace kiwix {
Xapian::Stem stemmer; Xapian::Stem stemmer;
Xapian::MSet results; Xapian::MSet results;
Xapian::MSetIterator current_result; Xapian::MSetIterator current_result;
std::map<std::string, int> valuesmap;
}; };
} }

View File

@ -25,8 +25,21 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <vector>
namespace kiwix { namespace kiwix {
std::map<std::string, int> read_valuesmap(const std::string &s) {
std::map<std::string, int> result;
std::vector<std::string> elems = split(s, ";");
for( auto elem: elems)
{
std::vector<std::string> tmp_elems = split(elem, ":");
result.insert( std::pair<std::string, int>(tmp_elems[0], atoi(tmp_elems[1].c_str())) );
}
return result;
}
/* Constructor */ /* Constructor */
XapianSearcher::XapianSearcher(const string &xapianDirectoryPath) XapianSearcher::XapianSearcher(const string &xapianDirectoryPath)
: Searcher(), : Searcher(),
@ -49,6 +62,7 @@ namespace kiwix {
} catch (...) { } catch (...) {
this->readableDatabase = Xapian::Database(directoryPath); this->readableDatabase = Xapian::Database(directoryPath);
} }
this->valuesmap = read_valuesmap(this->readableDatabase.get_metadata("valuesmap"));
} }
/* Close Xapian writable database */ /* Close Xapian writable database */
@ -78,7 +92,7 @@ namespace kiwix {
/* Get next result */ /* Get next result */
Result* XapianSearcher::getNextResult() { Result* XapianSearcher::getNextResult() {
if (this->current_result != this->results.end()) { if (this->current_result != this->results.end()) {
XapianResult* result = new XapianResult(this->current_result); XapianResult* result = new XapianResult(this, this->current_result);
this->current_result++; this->current_result++;
return result; return result;
} }
@ -89,7 +103,8 @@ namespace kiwix {
this->current_result = this->results.begin(); this->current_result = this->results.begin();
} }
XapianResult::XapianResult(Xapian::MSetIterator& iterator): XapianResult::XapianResult(XapianSearcher* searcher, Xapian::MSetIterator& iterator):
searcher(searcher),
iterator(iterator), iterator(iterator),
document(iterator.get_document()) document(iterator.get_document())
{ {
@ -100,7 +115,16 @@ namespace kiwix {
} }
std::string XapianResult::get_title() { std::string XapianResult::get_title() {
return document.get_value(0); if ( searcher->valuesmap.empty() )
{
/* This is the old legacy version. Guess and try */
return document.get_value(0);
}
else if ( searcher->valuesmap.find("title") != searcher->valuesmap.end() )
{
return document.get_value(searcher->valuesmap["title"]);
}
return "";
} }
int XapianResult::get_score() { int XapianResult::get_score() {
@ -108,15 +132,44 @@ namespace kiwix {
} }
std::string XapianResult::get_snippet() { std::string XapianResult::get_snippet() {
return document.get_value(1); if ( searcher->valuesmap.empty() )
{
/* This is the old legacy version. Guess and try */
return document.get_value(1);
}
else if ( searcher->valuesmap.find("snippet") != searcher->valuesmap.end() )
{
return document.get_value(searcher->valuesmap["snippet"]);
}
return "";
} }
int XapianResult::get_size() { int XapianResult::get_size() {
return document.get_value(2).empty() == true ? -1 : atoi(document.get_value(2).c_str()); if ( searcher->valuesmap.empty() )
{
/* This is the old legacy version. Guess and try */
return document.get_value(2).empty() == true ? -1 : atoi(document.get_value(2).c_str());
}
else if ( searcher->valuesmap.find("size") != searcher->valuesmap.end() )
{
return atoi(document.get_value(searcher->valuesmap["size"]).c_str());
}
/* The size is never used. Do we really want to get the content and
calculate the size ? */
return -1;
} }
int XapianResult::get_wordCount() { int XapianResult::get_wordCount() {
return document.get_value(3).empty() == true ? -1 : atoi(document.get_value(3).c_str()); if ( searcher->valuesmap.empty() )
{
/* This is the old legacy version. Guess and try */
return document.get_value(3).empty() == true ? -1 : atoi(document.get_value(3).c_str());
}
else if ( searcher->valuesmap.find("wordcount") != searcher->valuesmap.end() )
{
return atoi(document.get_value(searcher->valuesmap["wordcount"]).c_str());
}
return -1;
} }
} // Kiwix namespace } // Kiwix namespace