Integrate blacklist function into Xapian database.

This commit is contained in:
iaom 2021-11-10 10:20:16 +08:00
parent ca6850f5fc
commit b50d51dfc8
6 changed files with 55 additions and 36 deletions

View File

@ -65,7 +65,7 @@ void ConstructDocumentForPath::run() {
doc.setData(sourcePath); doc.setData(sourcePath);
doc.setUniqueTerm(uniqueterm); doc.setUniqueTerm(uniqueterm);
doc.addTerm(upTerm); doc.addTerm(upTerm);
doc.addValue(m_list.at(2)); doc.addValue(1, m_list.at(2));
/* QStringList temp; /* QStringList temp;
// temp.append(index_text); // temp.append(index_text);
temp.append(pinyin_text_list)*/; temp.append(pinyin_text_list)*/;
@ -107,7 +107,7 @@ void ConstructDocumentForContent::run() {
Document doc; Document doc;
doc.setUniqueTerm(FileUtils::makeDocUterm(m_path)); doc.setUniqueTerm(FileUtils::makeDocUterm(m_path));
doc.addTerm("ZEEKERUPTERM" + FileUtils::makeDocUterm(m_path.section("/", 0, -2, QString::SectionIncludeLeadingSep))); doc.addTerm("ZEEKERUPTERM" + FileUtils::makeDocUterm(m_path.section("/", 0, -2, QString::SectionIncludeLeadingSep)));
doc.addValue(m_path); doc.addValue(1, m_path);
if(content.isEmpty()) { if(content.isEmpty()) {
doc.reuireDeleted(); doc.reuireDeleted();

View File

@ -69,8 +69,9 @@ void Document::addTerm(std::string term) {
m_document.add_term(term); m_document.add_term(term);
} }
void Document::addValue(QString value) { void Document::addValue(unsigned slot, QString value)
m_document.add_value(1, value.toStdString()); {
m_document.add_value(slot, value.toStdString());
} }
void Document::setUniqueTerm(QString term) { void Document::setUniqueTerm(QString term) {

View File

@ -47,7 +47,7 @@ public:
void addPosting(std::string term, unsigned int offset, int weight = 1); void addPosting(std::string term, unsigned int offset, int weight = 1);
void addTerm(QString term); void addTerm(QString term);
void addTerm(std::string term); void addTerm(std::string term);
void addValue(QString value); void addValue(unsigned slot, QString value);
void setUniqueTerm(QString term); void setUniqueTerm(QString term);
void setUniqueTerm(std::string term); void setUniqueTerm(std::string term);
std::string getUniqueTerm(); std::string getUniqueTerm();

View File

@ -326,7 +326,7 @@ Document IndexGenerator::GenerateDocument(const QVector<QString> &list) {
doc.setData(sourcePath); doc.setData(sourcePath);
doc.setUniqueTerm(uniqueterm); doc.setUniqueTerm(uniqueterm);
doc.addTerm(upTerm); doc.addTerm(upTerm);
doc.addValue(list.at(2)); doc.addValue(1, list.at(2));
QStringList temp; QStringList temp;
temp.append(index_text); temp.append(index_text);
temp.append(pinyin_text_list); temp.append(pinyin_text_list);
@ -352,7 +352,7 @@ Document IndexGenerator::GenerateContentDocument(const QString &path) {
doc.setData(content); doc.setData(content);
doc.setUniqueTerm(uniqueterm); doc.setUniqueTerm(uniqueterm);
doc.addTerm(upTerm); doc.addTerm(upTerm);
doc.addValue(path); doc.addValue(1, path);
for(int i = 0; i < term.size(); ++i) { for(int i = 0; i < term.size(); ++i) {
doc.addPosting(term.at(i).word, term.at(i).offsets, static_cast<int>(term.at(i).weight)); doc.addPosting(term.at(i).word, term.at(i).offsets, static_cast<int>(term.at(i).weight));

View File

@ -110,10 +110,13 @@ FileSearch::FileSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, s
m_slot = slot; m_slot = slot;
m_begin = begin; m_begin = begin;
m_num = num; m_num = num;
m_matchDecider = new FileMatchDecider();
} }
FileSearch::~FileSearch() { FileSearch::~FileSearch() {
m_search_result = nullptr; m_search_result = nullptr;
if(m_matchDecider)
delete m_matchDecider;
} }
void FileSearch::run() { void FileSearch::run() {
@ -130,16 +133,9 @@ void FileSearch::run() {
} }
SearchManager::m_mutexDir.unlock(); SearchManager::m_mutexDir.unlock();
} }
m_begin = 0;
int resultCount = 0; m_num = -1;
int total = 0; keywordSearchfile();
while(total < 100) {
resultCount = keywordSearchfile();
if(resultCount == 0 || resultCount == -1)
break;
total += resultCount;
m_begin += m_num;
}
return; return;
} }
@ -162,7 +158,7 @@ int FileSearch::keywordSearchfile() {
qDebug() << "keywordSearchfile:" << QString::fromStdString(queryFile.get_description()); qDebug() << "keywordSearchfile:" << QString::fromStdString(queryFile.get_description());
enquire.set_query(queryFile); enquire.set_query(queryFile);
Xapian::MSet result = enquire.get_mset(m_begin, m_num); Xapian::MSet result = enquire.get_mset(m_begin, m_num, 0, m_matchDecider);
int resultCount = result.size(); int resultCount = result.size();
qDebug() << "keywordSearchfile results count=" << resultCount; qDebug() << "keywordSearchfile results count=" << resultCount;
if(resultCount == 0) if(resultCount == 0)
@ -199,9 +195,6 @@ int FileSearch::getResult(Xapian::MSet &result) {
QString path = QString::fromStdString(data); QString path = QString::fromStdString(data);
std::string().swap(data); std::string().swap(data);
if(SearchManager::isBlocked(path)) {
continue;
}
SearchPluginIface::ResultInfo ri; SearchPluginIface::ResultInfo ri;
if(SearchManager::creatResultInfo(ri, path)) { if(SearchManager::creatResultInfo(ri, path)) {
switch(m_value.toInt()) { switch(m_value.toInt()) {
@ -245,10 +238,13 @@ FileContentSearch::FileContentSearch(DataQueue<SearchPluginIface::ResultInfo> *s
m_keyword = keyword; m_keyword = keyword;
m_begin = begin; m_begin = begin;
m_num = num; m_num = num;
m_matchDecider = new FileContentMatchDecider();
} }
FileContentSearch::~FileContentSearch() { FileContentSearch::~FileContentSearch() {
m_search_result = nullptr; m_search_result = nullptr;
if(m_matchDecider)
delete m_matchDecider;
} }
void FileContentSearch::run() { void FileContentSearch::run() {
@ -257,17 +253,10 @@ void FileContentSearch::run() {
m_search_result->clear(); m_search_result->clear();
} }
SearchManager::m_mutexContent.unlock(); SearchManager::m_mutexContent.unlock();
int resultCount = 0;
int total = 0;
while(total < 50) { m_begin = 0;
resultCount = keywordSearchContent(); m_num = -1;
if(resultCount == 0 || resultCount == -1) { keywordSearchContent();
break;
}
total += resultCount;
m_begin += m_num;
}
return; return;
} }
@ -314,7 +303,7 @@ int FileContentSearch::keywordSearchContent() {
enquire.set_query(query); enquire.set_query(query);
Xapian::MSet result = enquire.get_mset(m_begin, m_num); Xapian::MSet result = enquire.get_mset(m_begin, m_num, 0, m_matchDecider);
int resultCount = result.size(); int resultCount = result.size();
if(result.size() == 0) { if(result.size() == 0) {
return 0; return 0;
@ -342,10 +331,6 @@ int FileContentSearch::getResult(Xapian::MSet &result, std::string &keyWord) {
Xapian::percent docScorePercent = it.get_percent(); Xapian::percent docScorePercent = it.get_percent();
QString path = QString::fromStdString(doc.get_value(1)); QString path = QString::fromStdString(doc.get_value(1));
if(SearchManager::isBlocked(path)) {
continue;
}
SearchPluginIface::ResultInfo ri; SearchPluginIface::ResultInfo ri;
if(!SearchManager::creatResultInfo(ri, path)) { if(!SearchManager::creatResultInfo(ri, path)) {
continue; continue;
@ -501,3 +486,21 @@ void DirectSearch::run() {
} }
} }
} }
bool FileMatchDecider::operator ()(const Xapian::Document &doc) const
{
QString path = QString::fromStdString(doc.get_data());
if(SearchManager::isBlocked(path)) {
return false;
}
return true;
}
bool FileContentMatchDecider::operator ()(const Xapian::Document &doc) const
{
QString path = QString::fromStdString(doc.get_value(1));
if(SearchManager::isBlocked(path)) {
return false;
}
return true;
}

View File

@ -46,10 +46,14 @@
#define INDEX_PATH (QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/org.ukui/ukui-search/index_data").toStdString() #define INDEX_PATH (QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/org.ukui/ukui-search/index_data").toStdString()
#define CONTENT_INDEX_PATH (QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/org.ukui/ukui-search/content_index_data").toStdString() #define CONTENT_INDEX_PATH (QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/org.ukui/ukui-search/content_index_data").toStdString()
namespace Zeeker { namespace Zeeker {
class FileMatchDecider;
class FileContentMatchDecider;
class LIBSEARCH_EXPORT SearchManager : public QObject { class LIBSEARCH_EXPORT SearchManager : public QObject {
friend class FileSearch; friend class FileSearch;
friend class FileContentSearch; friend class FileContentSearch;
friend class DirectSearch; friend class DirectSearch;
friend class FileMatchDecider;
friend class FileContentMatchDecider;
Q_OBJECT Q_OBJECT
public: public:
explicit SearchManager(QObject *parent = nullptr); explicit SearchManager(QObject *parent = nullptr);
@ -90,6 +94,7 @@ private:
int getResult(Xapian::MSet &result); int getResult(Xapian::MSet &result);
DataQueue<SearchPluginIface::ResultInfo> *m_search_result = nullptr; DataQueue<SearchPluginIface::ResultInfo> *m_search_result = nullptr;
FileMatchDecider *m_matchDecider;
QString m_value; QString m_value;
unsigned m_slot = 1; unsigned m_slot = 1;
size_t m_uniqueSymbol; size_t m_uniqueSymbol;
@ -109,6 +114,7 @@ private:
int getResult(Xapian::MSet &result, std::string &keyWord); int getResult(Xapian::MSet &result, std::string &keyWord);
DataQueue<SearchPluginIface::ResultInfo> *m_search_result = nullptr; DataQueue<SearchPluginIface::ResultInfo> *m_search_result = nullptr;
FileContentMatchDecider *m_matchDecider;
size_t m_uniqueSymbol; size_t m_uniqueSymbol;
QString m_keyword; QString m_keyword;
int m_begin = 0; int m_begin = 0;
@ -127,5 +133,14 @@ private:
QString m_value; QString m_value;
}; };
class FileMatchDecider :public Xapian::MatchDecider {
public:
bool operator ()(const Xapian::Document &doc) const;
};
class FileContentMatchDecider :public Xapian::MatchDecider {
public:
bool operator ()(const Xapian::Document &doc) const;
};
} }
#endif // SEARCHMANAGER_H #endif // SEARCHMANAGER_H