From 8e689623936e8056ddff214ce272ae4521ae250c Mon Sep 17 00:00:00 2001 From: hewenfei Date: Fri, 25 Mar 2022 14:09:34 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=AF=8F=E4=B8=AAtask=E5=8F=AA=E5=8F=91?= =?UTF-8?q?=E9=80=81=E4=B8=80=E6=AC=A1finish=E4=BF=A1=E5=8F=B7=E3=80=822.?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=9B=B4=E6=8E=A5=E6=90=9C=E7=B4=A2=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E8=BF=94=E5=9B=9E=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../searchtasks/file-search-task.cpp | 24 +++++++++++++------ .../searchtasks/file-search-task.h | 11 +++++++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/libsearch/searchinterface/searchtasks/file-search-task.cpp b/libsearch/searchinterface/searchtasks/file-search-task.cpp index ff90075..a332bb7 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/file-search-task.cpp @@ -73,17 +73,18 @@ void FileSearchWorker::run() } } + bool finished = true; //TODO 还需要判断是否为不能建立索引的目录 if (IndexStatusRecorder::getInstance()->indexDatabaseEnable()) { qDebug() << "index ready"; - searchWithIndex(); + finished = searchWithIndex(); } else { qDebug() << "direct search"; - directSearch(); + finished = directSearch(); } - QMetaObject::invokeMethod(m_FileSearchTask, "searchFinished", Q_ARG(size_t, m_currentSearchId)); + if (finished) QMetaObject::invokeMethod(m_FileSearchTask, "searchFinished", Q_ARG(size_t, m_currentSearchId)); } Xapian::Query FileSearchWorker::creatQueryForFileSearch() { @@ -115,7 +116,7 @@ Xapian::Query FileSearchWorker::creatQueryForFileSearch() { return {Xapian::Query::OP_AND, {Xapian::Query::OP_AND, queries.begin(), queries.end()}, fileOrDir}; } -void FileSearchWorker::searchWithIndex() +bool FileSearchWorker::searchWithIndex() { try { Xapian::Database db(INDEX_PATH.toStdString()); @@ -137,17 +138,20 @@ void FileSearchWorker::searchWithIndex() } else { qDebug() << "Search id changed!"; m_searchController->finishSearchIdCheck(); - return; + return false; } } } catch(const Xapian::Error &e) { qWarning() << QString::fromStdString(e.get_description()); } + + return true; } -void FileSearchWorker::directSearch() +bool FileSearchWorker::directSearch() { + unsigned int maxResults = m_searchController->maxResults(); QQueue searchPathQueue; for (QString &dir : m_validDirectories) { searchPathQueue.enqueue(dir); @@ -194,15 +198,21 @@ void FileSearchWorker::directSearch() if (matched) { ResultItem ri(m_currentSearchId, fileInfo.absoluteFilePath()); m_searchController->getDataQueue()->enqueue(ri); + --maxResults; } m_searchController->finishSearchIdCheck(); + if (maxResults == 0) { + return true; + } } else { qDebug() << "Search id changed!"; m_searchController->finishSearchIdCheck(); - return; + return false; } } } + + return true; } FileSearchFilter::FileSearchFilter(FileSearchWorker *parent) : parent(parent) {} diff --git a/libsearch/searchinterface/searchtasks/file-search-task.h b/libsearch/searchinterface/searchtasks/file-search-task.h index 48b09e0..bbbe18a 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.h +++ b/libsearch/searchinterface/searchtasks/file-search-task.h @@ -47,8 +47,15 @@ protected: void run(); private: - void searchWithIndex(); - void directSearch(); + /** + * @brief 通过索引进行搜索,如果搜索过程正常,返回true + * 如果搜索被打断,返回false. + * 搜索被打断是指用户使用同一个task发起多次搜索,导致searchId发生变化,那么上一次搜索即被打断。 + * @return + */ + bool searchWithIndex(); + //同上 + bool directSearch(); Xapian::Query creatQueryForFileSearch(); private: