#include #include #include #include #include #include "file-searcher.h" #include "global-settings.h" size_t FileSearcher::uniqueSymbol1 = 0; size_t FileSearcher::uniqueSymbol2 = 0; size_t FileSearcher::uniqueSymbol3 = 0; QMutex FileSearcher::m_mutex1; QMutex FileSearcher::m_mutex2; QMutex FileSearcher::m_mutex3; FileSearcher::FileSearcher(QObject *parent) : QObject(parent) { } FileSearcher::~FileSearcher() { } int FileSearcher::getCurrentIndexCount() { Xapian::Database db(INDEX_PATH); return db.get_doccount(); } void FileSearcher::onKeywordSearch(QString keyword,QQueue *searchResultFile,QQueue *searchResultDir,QQueue> *searchResultContent) { m_mutex1.lock(); ++uniqueSymbol1; m_mutex1.unlock(); m_mutex2.lock(); ++uniqueSymbol2; m_mutex2.unlock(); m_mutex3.lock(); ++uniqueSymbol3; m_mutex3.unlock(); m_search_result_file = searchResultFile; m_search_result_dir = searchResultDir; m_search_result_content = searchResultContent; //file QtConcurrent::run([&, uniqueSymbol1, keyword](){ if(!m_search_result_file->isEmpty()) m_search_result_file->clear(); int begin = 0; int num = 5; int resultCount = 0; int total = 0; while(total<20) { resultCount = keywordSearchfile(uniqueSymbol1,keyword,"0",1,begin,num); if(resultCount == 0 || resultCount == -1) break; total += resultCount; begin += num; } }); // Q_EMIT this->resultFile(m_search_result_file); //dir QtConcurrent::run([&, uniqueSymbol2, keyword](){ if(!m_search_result_dir->isEmpty()) m_search_result_dir->clear(); int begin = 0; int num = 5; int resultCount = 0; int total = 0; while(total<20) { resultCount = keywordSearchfile(uniqueSymbol2,keyword,"1",1,begin,num); if(resultCount == 0 || resultCount == -1) break; total += resultCount; begin += num; } }); // Q_EMIT this->resultDir(m_search_result_dir); //content QtConcurrent::run([&, uniqueSymbol3, keyword](){ if(!m_search_result_content->isEmpty()) m_search_result_content->clear(); int begin = 0; int num = 5; int resultCount = 0; int total = 0; while(total<20) { keywordSearchContent(uniqueSymbol3,keyword,begin,num); if(resultCount == 0 || resultCount == -1) break; total += resultCount; begin += num; } }); // Q_EMIT this->resultContent(m_search_result_content); } int FileSearcher::keywordSearchfile(size_t uniqueSymbol, QString keyword, QString value, unsigned slot, int begin, int num) { try { qDebug()<<"--search start--"; Xapian::Database db(INDEX_PATH); Xapian::Query query = creatQueryForFileSearch(keyword,db); Xapian::Enquire enquire(db); Xapian::Query queryFile; if(!value.isEmpty()) { std::string slotValue = value.toStdString(); Xapian::Query queryValue = Xapian::Query(Xapian::Query::OP_VALUE_RANGE,slot,slotValue,slotValue); queryFile = Xapian::Query(Xapian::Query::OP_AND,query,queryValue); } else { queryFile = query; } qDebug()<(result.get_matches_estimated()); qDebug()<< "find results count=" < sKeyWord = ChineseSegmentation::getInstance()->callSegement(&keyword); //Creat a query std::string words; for(int i=0;i v; // for(int i=0;i(result.get_matches_estimated()); if(result.size() == 0) return 0; qDebug()<< "find results count=" < v; for(int i=0;iappend(QString::fromStdString(data)); qDebug()<enqueue(path); m_mutex1.unlock(); } else { m_mutex1.unlock(); return -1; } break; case 0: m_mutex2.lock(); if(uniqueSymbol == FileSearcher::uniqueSymbol2) { m_search_result_file->enqueue(path); m_mutex2.unlock(); } else { m_mutex2.unlock(); return -1; } break; default: break; } // searchResult.append(path); } qDebug()<< "doc="<< path << ",weight=" < searchResult; for (auto it = result.begin(); it != result.end(); ++it) { Xapian::Document doc = it.get_document(); std::string data = doc.get_data(); double docScoreWeight = it.get_weight(); Xapian::percent docScorePercent = it.get_percent(); QString path = QString::fromStdString(doc.get_value(1)); if(isBlocked(path)) continue; QFileInfo info(path); if(!info.exists()) { // pathTobeDelete->append(QString::fromStdString(data)); qDebug()<enqueue(qMakePair(path,snippets)); m_mutex3.unlock(); } else { m_mutex3.unlock(); return -1; } // searchResult.insert(path,snippets); qDebug()<< "path="<< path << ",weight=" <