From ffb51b9d1777311c244b7e5a96dd8040bc6cda6c Mon Sep 17 00:00:00 2001 From: Mouse Zhang Date: Fri, 16 Apr 2021 15:35:54 +0800 Subject: [PATCH] Add direct search without index. --- libsearch/file-utils.cpp | 1 + libsearch/file-utils.h | 4 +- libsearch/index/search-manager.cpp | 220 +++++++++++++++++------------ libsearch/index/search-manager.h | 14 ++ src/main.cpp | 10 +- 5 files changed, 148 insertions(+), 101 deletions(-) diff --git a/libsearch/file-utils.cpp b/libsearch/file-utils.cpp index 6c89018..91944e1 100644 --- a/libsearch/file-utils.cpp +++ b/libsearch/file-utils.cpp @@ -25,6 +25,7 @@ size_t FileUtils::_max_index_count = 0; size_t FileUtils::_current_index_count = 0; unsigned short FileUtils::_index_status = 0; +FileUtils::SearchMethod FileUtils::searchMethod = FileUtils::SearchMethod::DIRECTSEARCH; QMap FileUtils::map_chinese2pinyin = QMap(); FileUtils::FileUtils() diff --git a/libsearch/file-utils.h b/libsearch/file-utils.h index 223be43..77c2808 100644 --- a/libsearch/file-utils.h +++ b/libsearch/file-utils.h @@ -88,8 +88,8 @@ public: static size_t _current_index_count; //this one has been Abandoned,do not use it. static unsigned short _index_status; -// enum class SearchMethod{ DIRECTSEARCH = 0, INDEXSEARCH = 1}; -// static SearchMethod searchMethod = FileUtils::SearchMethod::DIRECTSEARCH; + enum class SearchMethod{ DIRECTSEARCH = 0, INDEXSEARCH = 1}; + static SearchMethod searchMethod; private: FileUtils(); diff --git a/libsearch/index/search-manager.cpp b/libsearch/index/search-manager.cpp index b8202cb..c3651c3 100644 --- a/libsearch/index/search-manager.cpp +++ b/libsearch/index/search-manager.cpp @@ -37,14 +37,11 @@ SearchManager::~SearchManager() int SearchManager::getCurrentIndexCount() { - try - { + try { Xapian::Database db(INDEX_PATH); return db.get_doccount(); - } - catch(const Xapian::Error &e) - { - qWarning() < *searchResul ++uniqueSymbol3; m_mutex3.unlock(); + if (FileUtils::SearchMethod::DIRECTSEARCH == FileUtils::searchMethod) { + DirectSearch *directSearch; + directSearch = new DirectSearch(keyword, searchResultFile, searchResultDir, uniqueSymbol1); + m_pool.start(directSearch); + } else if (FileUtils::SearchMethod::INDEXSEARCH == FileUtils::searchMethod) { + FileSearch *filesearch; + filesearch = new FileSearch(searchResultFile,uniqueSymbol1,keyword,"0",1,0,5); + m_pool.start(filesearch); + FileSearch *dirsearch; + dirsearch = new FileSearch(searchResultDir,uniqueSymbol2,keyword,"1",1,0,5); + m_pool.start(dirsearch); - FileSearch *filesearch; - filesearch = new FileSearch(searchResultFile,uniqueSymbol1,keyword,"0",1,0,5); - m_pool.start(filesearch); - - FileSearch *dirsearch; - dirsearch = new FileSearch(searchResultDir,uniqueSymbol2,keyword,"1",1,0,5); - m_pool.start(dirsearch); - - FileContentSearch *contentSearch; - contentSearch = new FileContentSearch(searchResultContent,uniqueSymbol3,keyword,0,5); - m_pool.start(contentSearch); + FileContentSearch *contentSearch; + contentSearch = new FileContentSearch(searchResultContent,uniqueSymbol3,keyword,0,5); + m_pool.start(contentSearch); + } else { + qWarning() << "Unknown search method! FileUtils::searchMethod: " << static_cast(FileUtils::searchMethod); + } return; } @@ -109,14 +112,14 @@ FileSearch::~FileSearch() void FileSearch::run() { - if(!m_search_result->isEmpty()) + if (!m_search_result->isEmpty()){ m_search_result->clear(); + } int resultCount = 0; int total = 0; - while(total < 100) - { + while (total < 100) { resultCount = keywordSearchfile(); - if(resultCount == 0 || resultCount == -1) + if (resultCount == 0 || resultCount == -1) break; total += resultCount; m_begin += m_num; @@ -126,22 +129,18 @@ void FileSearch::run() int FileSearch::keywordSearchfile() { - try - { + try { qDebug() << "--keywordSearchfile start--"; Xapian::Database db(INDEX_PATH); Xapian::Query query = creatQueryForFileSearch(db); Xapian::Enquire enquire(db); Xapian::Query queryFile; - if(!m_value.isEmpty()) - { + if (!m_value.isEmpty()) { std::string slotValue = m_value.toStdString(); Xapian::Query queryValue = Xapian::Query(Xapian::Query::OP_VALUE_RANGE,m_slot,slotValue,slotValue); queryFile = Xapian::Query(Xapian::Query::OP_AND,query,queryValue); - } - else - { + } else { queryFile = query; } @@ -150,19 +149,17 @@ int FileSearch::keywordSearchfile() enquire.set_query(queryFile); Xapian::MSet result = enquire.get_mset(m_begin, m_num); int resultCount = result.size(); - qDebug()<< "keywordSearchfile results count=" < v; - for(int i=0;iappend(QString::fromStdString(data)); + if (!info.exists()) { +// pathTobeDelete->append(QString::fromStdString(data)); qDebug()<enqueue(path); SearchManager::m_mutex1.unlock(); - } - else - { + } else { SearchManager::m_mutex1.unlock(); return -1; } @@ -221,13 +210,10 @@ int FileSearch::getResult(Xapian::MSet &result) break; case 0: SearchManager::m_mutex2.lock(); - if(m_uniqueSymbol == SearchManager::uniqueSymbol1) - { + if(m_uniqueSymbol == SearchManager::uniqueSymbol1) { m_search_result->enqueue(path); SearchManager::m_mutex2.unlock(); - } - else - { + } else { SearchManager::m_mutex2.unlock(); return -1; } @@ -237,7 +223,7 @@ int FileSearch::getResult(Xapian::MSet &result) } // searchResult.append(path); } - qDebug()<< "doc="<< path << ",weight=" < sKeyWord = ChineseSegmentation::getInstance()->callSegement(m_keyword); //Creat a query std::string words; - for(int i=0;iappend(QString::fromStdString(data)); - qDebug()< 6 + QString::fromStdString(keyWord).size()) + if (snippet.size() > 6 + QString::fromStdString(keyWord).size()){ snippet.replace(0,3,"...").replace(snippet.size()-3,3,"..."); - else + } else { snippet.append("...").prepend("..."); + } snippets.append(snippet); QString().swap(snippet); std::string().swap(s); @@ -418,23 +402,71 @@ int FileContentSearch::getResult(Xapian::MSet &result, std::string &keyWord) // } SearchManager::m_mutex3.lock(); - if(m_uniqueSymbol == SearchManager::uniqueSymbol3) - { - + if (m_uniqueSymbol == SearchManager::uniqueSymbol3) { m_search_result->enqueue(qMakePair(path,snippets)); SearchManager::m_mutex3.unlock(); snippets.clear(); QStringList().swap(snippets); - } - else - { + } else { SearchManager::m_mutex3.unlock(); return -1; } // searchResult.insert(path,snippets); - qDebug()<< "path="<< path << ",weight=" <start(); +// InotifyIndex* ii = InotifyIndex::getInstance("/home"); +// ii->start(); return app.exec(); }