From 1a440bf78fede285fa403c4af3e71419b2580fa4 Mon Sep 17 00:00:00 2001 From: iaom Date: Wed, 11 May 2022 18:05:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=B4=A2=E5=BC=95=E5=BB=BA?= =?UTF-8?q?=E7=AB=8B=E5=92=8C=E5=A2=9E=E5=8A=A0=E7=9A=84=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E4=BB=A5=E5=AE=9E=E7=8E=B0=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E9=87=8D=E5=BB=BA=E6=9F=90=E4=B8=AA=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libsearch/index/file-index-manager.cpp | 3 +- libsearch/index/first-index.cpp | 60 ++++++++++++++++++----- libsearch/index/first-index.h | 13 +++-- libsearch/index/index-status-recorder.cpp | 1 + 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/libsearch/index/file-index-manager.cpp b/libsearch/index/file-index-manager.cpp index 219138f..fbc20f7 100644 --- a/libsearch/index/file-index-manager.cpp +++ b/libsearch/index/file-index-manager.cpp @@ -26,8 +26,7 @@ void FileIndexManager::searchMethod(FileUtils::SearchMethod sm) { } if(FileUtils::SearchMethod::INDEXSEARCH == sm && 0 == FileUtils::indexStatus) { qDebug() << "start first index"; - m_semaphore.acquire(); - m_fi->start(); + m_fi->rebuildDatebase(); qDebug() << "start inotify index"; if(!this->m_iw->isRunning()) { this->m_iw->start(); diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index 4198084..1a938a6 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -35,6 +35,7 @@ FirstIndex::FirstIndex() : m_semaphore(INDEX_SEM, 1, QSystemSemaphore::AccessMod { m_pool.setMaxThreadCount(2); m_pool.setExpiryTimeout(100); + connect(this, &FirstIndex::needRebuild, this, &FirstIndex::rebuildDatebase, Qt::QueuedConnection); } FirstIndex *FirstIndex::getInstance() @@ -118,9 +119,18 @@ void FirstIndex::work(const QFileInfo& fileInfo) { } } +void FirstIndex::rebuildDatebase() +{ + m_semaphore.acquire(); + m_isRebuildProcess = true; + this->wait(); + this->start(); +} + void FirstIndex::addIndexPath(const QString path, const QStringList blockList) { m_semaphore.acquire(); + m_isRebuildProcess = false; setPath(QStringList() << path); setBlockPath(blockList); this->wait(); @@ -139,20 +149,34 @@ void FirstIndex::run() { // qInfo() << "ocrIndexDatabaseStatus: " << ocrIndexDatabaseStatus; qInfo() << "inotifyIndexStatus: " << inotifyIndexStatus; - m_allDatadaseStatus = inotifyIndexStatus == "2" ? true : false; + m_inotifyIndexStatus = inotifyIndexStatus == "2" ? true : false; m_indexDatabaseStatus = indexDataBaseStatus == "2" ? true : false; m_contentIndexDatabaseStatus = contentIndexDataBaseStatus == "2" ? true : false; // m_ocrIndexDatabaseStatus = ocrIndexDatabaseStatus == "2" ? true : false; - if(m_allDatadaseStatus && m_indexDatabaseStatus && m_contentIndexDatabaseStatus /*&& m_ocrIndexDatabaseStatus*/) { - if(m_isFirstIndex) { - m_isFirstIndex = false; + if(m_inotifyIndexStatus && m_indexDatabaseStatus && m_contentIndexDatabaseStatus /*&& m_ocrIndexDatabaseStatus*/) { + m_needRebuild = false; + if(m_isRebuildProcess) { + m_isRebuildProcess = false; m_semaphore.release(1); return; } } else { - setPath(DirWatcher::getDirWatcher()->currentIndexableDir()); - setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex()); + if(m_isRebuildProcess) { + setPath(DirWatcher::getDirWatcher()->currentIndexableDir()); + setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex()); + } else { + if(m_inotifyIndexStatus && (!m_indexDatabaseStatus || !m_contentIndexDatabaseStatus)) { + m_needRebuild = true; + } + if(!m_inotifyIndexStatus || (!m_indexDatabaseStatus && !m_contentIndexDatabaseStatus)) { + m_needRebuild = false; + qInfo() << "Entering rebuild procedure"; + Q_EMIT needRebuild(); + m_semaphore.release(1); + return; + } + } } @@ -187,12 +211,15 @@ void FirstIndex::run() { QtConcurrent::run(&m_pool, [&]() { sem.acquire(2); mutex1.unlock(); - if(m_isFirstIndex && m_allDatadaseStatus && m_indexDatabaseStatus) { + if(m_isRebuildProcess && m_inotifyIndexStatus && m_indexDatabaseStatus) { //重建索引且无异常 sem.release(2); return; - } - if (!m_allDatadaseStatus || !m_indexDatabaseStatus || !m_contentIndexDatabaseStatus) { + } else if(m_isRebuildProcess) { //重建索引且有异常 IndexGenerator::getInstance()->rebuildIndexDatabase(); + } else if(!m_inotifyIndexStatus || !m_indexDatabaseStatus) { //添加目录且有异常 + qWarning() << "Index database need rebuild!"; + sem.release(2); + return; } qDebug() << "index start;" << m_indexData->size(); @@ -218,12 +245,15 @@ void FirstIndex::run() { QtConcurrent::run(&m_pool,[&]() { sem.acquire(2); mutex2.unlock(); - if(m_isFirstIndex && m_allDatadaseStatus && m_contentIndexDatabaseStatus) { + if(m_isRebuildProcess && m_inotifyIndexStatus && m_contentIndexDatabaseStatus) { sem.release(2); return; - } - if (!m_allDatadaseStatus || !m_contentIndexDatabaseStatus || !m_indexDatabaseStatus) { + } else if(m_isRebuildProcess) { //重建索引且有异常 IndexGenerator::getInstance()->rebuildContentIndexDatabase(); + } else if(!m_inotifyIndexStatus || !m_contentIndexDatabaseStatus) { //添加目录且有异常 + qWarning() << "Content index database need rebuild!"; + sem.release(2); + return; } qDebug() << "content index start:" << m_contentIndexData->size(); QQueue* tmp2 = new QQueue(); @@ -323,8 +353,12 @@ void FirstIndex::run() { --FileUtils::indexStatus; } - m_isFirstIndex = false; //首次索引后置为false,后续start为添加索引目录时新建索引。 IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "2"); + if(m_needRebuild) { + m_needRebuild = false; + qInfo() << "Entering rebuild procedure"; + Q_EMIT needRebuild(); + } m_semaphore.release(1); // int retval1 = write(fifo_fd, buffer, strlen(buffer)); // if(retval1 == -1) { diff --git a/libsearch/index/first-index.h b/libsearch/index/first-index.h index c06a58b..14f72d5 100644 --- a/libsearch/index/first-index.h +++ b/libsearch/index/first-index.h @@ -42,13 +42,17 @@ #include "file-utils.h" #include "common.h" namespace UkuiSearch { -class FirstIndex : public QThread, public Traverse_BFS { +class FirstIndex : public QThread, public Traverse_BFS +{ + Q_OBJECT public: static FirstIndex* getInstance(); ~FirstIndex(); virtual void work(const QFileInfo &) final; + void rebuildDatebase(); void addIndexPath(const QString path, const QStringList blockList); - +Q_SIGNALS: + void needRebuild(); protected: void run() override; private: @@ -61,8 +65,9 @@ private: bool m_indexDatabaseStatus = false; bool m_contentIndexDatabaseStatus = false; bool m_ocrIndexDatabaseStatus = false; - bool m_allDatadaseStatus = false; - bool m_isFirstIndex = true; + bool m_inotifyIndexStatus = false; + bool m_isRebuildProcess = true; + bool m_needRebuild = false; QThreadPool m_pool; QQueue>* m_indexData = nullptr; diff --git a/libsearch/index/index-status-recorder.cpp b/libsearch/index/index-status-recorder.cpp index fa295ab..f09dcda 100644 --- a/libsearch/index/index-status-recorder.cpp +++ b/libsearch/index/index-status-recorder.cpp @@ -22,6 +22,7 @@ void IndexStatusRecorder::setStatus(const QString &key, const QVariant &value) const QVariant IndexStatusRecorder::getStatus(const QString &key) { + m_status->sync(); return m_status->value(key); }