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); }