From bf5112c6673d32cbaad33c388d38690264adf107 Mon Sep 17 00:00:00 2001 From: ck Date: Mon, 6 Jul 2009 10:20:02 +0200 Subject: Fixed race condition in search module. Task-number: 257441 Reviewed-by: kh --- tools/assistant/lib/qhelpsearchengine.cpp | 10 ++++------ .../lib/qhelpsearchindexreader_clucene.cpp | 21 +++++++++++++++------ .../lib/qhelpsearchindexreader_clucene_p.h | 5 ++--- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/tools/assistant/lib/qhelpsearchengine.cpp b/tools/assistant/lib/qhelpsearchengine.cpp index 9faafe0..15f7f9e 100644 --- a/tools/assistant/lib/qhelpsearchengine.cpp +++ b/tools/assistant/lib/qhelpsearchengine.cpp @@ -96,6 +96,7 @@ private: delete indexWriter; } + int hitsCount() const { int count = 0; @@ -107,12 +108,9 @@ private: QList hits(int start, int end) const { - QList returnValue; - if (indexReader) { - for (int i = start; i < end && i < hitsCount(); ++i) - returnValue.append(indexReader->hit(i)); - } - return returnValue; + return indexReader ? + indexReader->hits(start, end) : + QList(); } void updateIndex(bool reindex = false) diff --git a/tools/assistant/lib/qhelpsearchindexreader_clucene.cpp b/tools/assistant/lib/qhelpsearchindexreader_clucene.cpp index 227e558..867e060 100644 --- a/tools/assistant/lib/qhelpsearchindexreader_clucene.cpp +++ b/tools/assistant/lib/qhelpsearchindexreader_clucene.cpp @@ -86,8 +86,8 @@ void QHelpSearchIndexReader::cancelSearching() void QHelpSearchIndexReader::search(const QString &collectionFile, const QString &indexFilesFolder, const QList &queryList) { - QMutexLocker lock(&mutex); - + wait(); + this->hitList.clear(); this->m_cancel = false; this->m_query = queryList; @@ -99,12 +99,18 @@ void QHelpSearchIndexReader::search(const QString &collectionFile, const QString int QHelpSearchIndexReader::hitsCount() const { + QMutexLocker lock(&mutex); return hitList.count(); } -QHelpSearchEngine::SearchHit QHelpSearchIndexReader::hit(int index) const +QList QHelpSearchIndexReader::hits(int start, + int end) const { - return hitList.at(index); + QList hits; + QMutexLocker lock(&mutex); + for (int i = start; i < end && i < hitList.count(); ++i) + hits.append(hitList.at(i)); + return hits; } void QHelpSearchIndexReader::run() @@ -135,7 +141,7 @@ void QHelpSearchIndexReader::run() if(QCLuceneIndexReader::indexExists(indexPath)) { mutex.lock(); if (m_cancel) { - mutex.unlock(); + mutex.unlock(); return; } mutex.unlock(); @@ -213,7 +219,9 @@ void QHelpSearchIndexReader::run() #if !defined(QT_NO_EXCEPTIONS) } catch(...) { + mutex.lock(); hitList.clear(); + mutex.unlock(); emit searchingFinished(0); } #endif @@ -416,8 +424,9 @@ void QHelpSearchIndexReader::boostSearchHits(const QHelpEngineCore &engine, boostedList.append(it.value()); } while (it != hitMap.constBegin()); boostedList += hitList.mid(count, hitList.count()); - + mutex.lock(); hitList = boostedList; + mutex.unlock(); } } diff --git a/tools/assistant/lib/qhelpsearchindexreader_clucene_p.h b/tools/assistant/lib/qhelpsearchindexreader_clucene_p.h index 47af43f..e7ac0eb 100644 --- a/tools/assistant/lib/qhelpsearchindexreader_clucene_p.h +++ b/tools/assistant/lib/qhelpsearchindexreader_clucene_p.h @@ -85,9 +85,8 @@ public: void search(const QString &collectionFile, const QString &indexFilesFolder, const QList &queryList); - int hitsCount() const; - QHelpSearchEngine::SearchHit hit(int index) const; + QList hits(int start, int end) const; signals: void searchingStarted(); @@ -105,7 +104,7 @@ private: const QList &queryList); private: - QMutex mutex; + mutable QMutex mutex; QList hitList; QWaitCondition waitCondition; -- cgit v0.12