diff --git a/data/org.ukui.search.data.gschema.xml b/data/org.ukui.search.data.gschema.xml index 014f622..e5633a2 100644 --- a/data/org.ukui.search.data.gschema.xml +++ b/data/org.ukui.search.data.gschema.xml @@ -1,19 +1,29 @@ - - - false - file index switch - Enable or disable file index service. - - - "baidu" - web engine - Web engine to search keyword online. - - - false - content fuzzy search - Enable or disable fuzzy search for file content. - - + + + "0.0" + version of this config + Version of the config of ukui-search. + + + false + file index switch + Enable or disable file index service. + + + false + file content index switch + Enable or disable file content index service. + + + "baidu" + web engine + Web engine to search keyword online. + + + false + content fuzzy search + Enable or disable fuzzy search for file content. + + diff --git a/libsearch/index/batch-indexer.cpp b/libsearch/index/batch-indexer.cpp index d2d9bb9..f7e567d 100644 --- a/libsearch/index/batch-indexer.cpp +++ b/libsearch/index/batch-indexer.cpp @@ -32,10 +32,11 @@ #include "writable-database.h" #include "compatible-define.h" using namespace UkuiSearch; -BatchIndexer::BatchIndexer(const QStringList &folders, const QStringList &blackList, QAtomicInt& stop, WorkMode mode, Targets target) +BatchIndexer::BatchIndexer(const QStringList &folders, const QStringList &blackList, QAtomicInt& indexStop, QAtomicInt &contentIndexStop, WorkMode mode, Targets target) : m_folders(folders), m_blackList(blackList), - m_stop(&stop), + m_indexStop(&indexStop), + m_contentIndexStop(&contentIndexStop), m_mode(mode), m_target(target) { @@ -45,22 +46,24 @@ void BatchIndexer::run() { QElapsedTimer timer; timer.start(); - if(m_target == Target::None || m_stop->LOAD) { - Q_EMIT done(m_mode); + if(m_target == Target::None || (m_indexStop->LOAD && m_contentIndexStop->LOAD)) { + Q_EMIT done(m_mode, m_target); return; } fetch(); if(m_target & Target::Basic) { basicIndex(); + Q_EMIT basicIndexDone(m_mode); } if(m_target & Target::Content) { contentIndex(); + Q_EMIT contentIndexDone(m_mode); } m_cache.clear(); malloc_trim(0); qDebug() << "FirstRunIndexer: time :" << timer.elapsed() << "milliseconds"; - Q_EMIT done(m_mode); + Q_EMIT done(m_mode, m_target); } void BatchIndexer::fetch() @@ -164,7 +167,7 @@ void BatchIndexer::basicIndex() basicDb.commit(); Q_EMIT progress(IndexType::Basic, allSize, finishNum); //文件名索引很快 - if(m_stop->LOAD) { + if(m_indexStop->LOAD) { qDebug() << "Index stopped, abort basic index."; filesNeedIndex.clear(); return; @@ -175,7 +178,6 @@ void BatchIndexer::basicIndex() //TODO:xapian默认10000条自动commit一次,需要根据内存占用情况调整。 basicDb.commit(); Q_EMIT progress(IndexType::Basic, allSize, finishNum); - Q_EMIT basicIndexDone(finishNum); filesNeedIndex.clear(); qDebug() << "Finish basic index"; } @@ -183,7 +185,7 @@ void BatchIndexer::basicIndex() void BatchIndexer::contentIndex() { qDebug() << "Begin content index"; - if(m_stop->LOAD) { + if(m_indexStop->LOAD) { qDebug() << "Index stopped, abort content index."; return; } @@ -250,7 +252,7 @@ void BatchIndexer::contentIndex() uint batchSize = 0; uint finishNum = 0; for (QString path : filesNeedIndex) { - if(m_stop->LOAD) { + if(m_indexStop->LOAD) { qDebug() << "Index stopped, interrupt content index."; filesNeedIndex.clear(); filesNeedOCRIndex.clear(); @@ -288,7 +290,7 @@ void BatchIndexer::contentIndex() batchSize = 0; int ocrFinishNum = 0; for(QString path : filesNeedOCRIndex) { - if(m_stop->LOAD) { + if(m_indexStop->LOAD) { qDebug() << "Index stopped, interrupt content index."; filesNeedOCRIndex.clear(); return; @@ -314,6 +316,5 @@ void BatchIndexer::contentIndex() Q_EMIT progress(IndexType::Contents, allSize, finishNum + ocrFinishNum); filesNeedOCRIndex.clear(); qDebug() << "Finish OCR index."; - Q_EMIT contentIndexDone(finishNum + ocrFinishNum); qDebug() << "Finish content index"; } diff --git a/libsearch/index/batch-indexer.h b/libsearch/index/batch-indexer.h index 56361ab..eef2912 100644 --- a/libsearch/index/batch-indexer.h +++ b/libsearch/index/batch-indexer.h @@ -57,14 +57,19 @@ public: }; Q_DECLARE_FLAGS(Targets, Target) - BatchIndexer(const QStringList& folders, const QStringList& blackList, QAtomicInt& stop, WorkMode mode = WorkMode::Update, Targets target = Target::All); + BatchIndexer(const QStringList& folders, + const QStringList& blackList, + QAtomicInt& indexStop, + QAtomicInt& contentIndexStop, + WorkMode mode = WorkMode::Update, + Targets target = Target::All); void run() override; Q_SIGNALS: void progress(IndexType type, uint all, uint finished); - void basicIndexDone(int size); - void contentIndexDone(int size); - void done(WorkMode); + void basicIndexDone(WorkMode); + void contentIndexDone(WorkMode); + void done(WorkMode, Targets); private: void fetch(); @@ -73,7 +78,8 @@ private: QStringList m_folders; QStringList m_blackList; - QAtomicInt *m_stop = nullptr; + QAtomicInt *m_indexStop = nullptr; + QAtomicInt *m_contentIndexStop = nullptr; WorkMode m_mode; Targets m_target; QStringList m_cache; diff --git a/libsearch/index/file-indexer-config.cpp b/libsearch/index/file-indexer-config.cpp index 1d39e97..9cc0c1d 100644 --- a/libsearch/index/file-indexer-config.cpp +++ b/libsearch/index/file-indexer-config.cpp @@ -23,13 +23,14 @@ #include #include #define INDEX_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-service.conf" - +static const QString CONFIG_VERSION = QStringLiteral("1.0"); static const QByteArray UKUI_SEARCH_SCHEMAS = QByteArrayLiteral("org.ukui.search.settings"); static const QString FILE_INDEX_ENABLE_KEY = QStringLiteral("fileIndexEnable"); static const QString CONTENT_INDEX_ENABLE_KEY = QStringLiteral("contentIndexEnable"); static const QString CONTENT_FUZZY_SEARCH_KEY = QStringLiteral("contentFuzzySearch"); static const QString OCR_ENABLE_KEY = QStringLiteral("ocrEnable"); static const QString META_DATA_INDEX_ENABLE_KEY = QStringLiteral("metaDataIndexEnable"); +static const QString CONFIG_VERSION_KEY = QStringLiteral("version"); static std::once_flag flag; static FileIndexerConfig *global_intance = nullptr; @@ -52,9 +53,28 @@ FileIndexerConfig::FileIndexerConfig(QObject *parent) const QByteArray id(UKUI_SEARCH_SCHEMAS); if(QGSettings::isSchemaInstalled(id)) { m_gsettings = new QGSettings(id, QByteArray(), this); + //保留旧版本配置 + if(m_gsettings->keys().contains(CONFIG_VERSION_KEY)) { + QString oldVersion = m_gsettings->get(CONFIG_VERSION_KEY).toString(); + if(oldVersion == "0.0") { + bool fileIndexEnable = false; + if(m_gsettings->keys().contains(FILE_INDEX_ENABLE_KEY)) { + fileIndexEnable = m_gsettings->get(FILE_INDEX_ENABLE_KEY).toBool(); + } + if(fileIndexEnable) { + if(m_gsettings->keys().contains(CONTENT_INDEX_ENABLE_KEY)) { + m_gsettings->set(CONTENT_INDEX_ENABLE_KEY, true); + } + } + m_gsettings->set(CONFIG_VERSION_KEY, CONFIG_VERSION); + } + } connect(m_gsettings, &QGSettings::changed, this, [ = ](const QString &key) { if(key == FILE_INDEX_ENABLE_KEY) { Q_EMIT this->fileIndexEnableStatusChanged(m_gsettings->get(FILE_INDEX_ENABLE_KEY).toBool()); + } else if(key == CONTENT_INDEX_ENABLE_KEY) { + Q_EMIT this->contentIndexEnableStatusChanged(m_gsettings->get(CONTENT_INDEX_ENABLE_KEY).toBool()); + } }); } else { @@ -96,7 +116,17 @@ bool FileIndexerConfig::isFileIndexEnable() bool FileIndexerConfig::isContentIndexEnable() { - return m_settings->value(CONTENT_INDEX_ENABLE_KEY, true).toBool(); + if(m_gsettings) { + if(m_gsettings->keys().contains(CONTENT_INDEX_ENABLE_KEY)) { + return m_gsettings->get(CONTENT_INDEX_ENABLE_KEY).toBool(); + } else { + qWarning() << "FileIndexerConfig: Can not find key:" << CONTENT_INDEX_ENABLE_KEY << "in" << UKUI_SEARCH_SCHEMAS; + return false; + } + } else { + qWarning() << "FileIndexerConfig:" << UKUI_SEARCH_SCHEMAS << " is not found!"; + return false; + } } bool FileIndexerConfig::isFuzzySearchEnable() diff --git a/libsearch/index/file-indexer-config.h b/libsearch/index/file-indexer-config.h index ec16424..1613015 100644 --- a/libsearch/index/file-indexer-config.h +++ b/libsearch/index/file-indexer-config.h @@ -23,7 +23,6 @@ #include #include #include -#include #include "dir-watcher.h" class FileIndexerConfig : public QObject @@ -83,6 +82,11 @@ Q_SIGNALS: * 文件索引开关(基本索引) */ void fileIndexEnableStatusChanged(bool); + /** + * @brief fileIndexEnableStatusChanged + * 内容索引 + */ + void contentIndexEnableStatusChanged(bool); private: explicit FileIndexerConfig(QObject *parent = nullptr); @@ -91,7 +95,6 @@ private: DirWatcher *m_dirWatcher = nullptr; QGSettings *m_gsettings = nullptr; QSettings *m_settings = nullptr; - QAtomicInt m_stop; }; diff --git a/libsearch/index/file-search-plugin.cpp b/libsearch/index/file-search-plugin.cpp index f7732f9..774c213 100644 --- a/libsearch/index/file-search-plugin.cpp +++ b/libsearch/index/file-search-plugin.cpp @@ -466,7 +466,7 @@ void UkuiSearch::FileContengSearchPlugin::KeywordSearch(QString keyword, DataQue SearchManager::m_mutexContent.unlock(); m_keyWord = keyword; - if(FileIndexerConfig::getInstance()->isFileIndexEnable()) { + if(FileIndexerConfig::getInstance()->isContentIndexEnable()) { FileContentSearch *fileContentSearch; fileContentSearch = new FileContentSearch(searchResult, SearchManager::uniqueSymbolContent, keyword, FileIndexerConfig::getInstance()->isFuzzySearchEnable(), 0, 5); m_pool.start(fileContentSearch); diff --git a/libsearch/index/index-scheduler.cpp b/libsearch/index/index-scheduler.cpp index e33c182..81e2842 100644 --- a/libsearch/index/index-scheduler.cpp +++ b/libsearch/index/index-scheduler.cpp @@ -27,26 +27,38 @@ IndexScheduler::IndexScheduler(QObject *parent) : m_statusRecorder(IndexStatusRecorder::getInstance()), m_config(FileIndexerConfig::getInstance()), m_state(Startup), - m_stop(0) + m_indexStop(0), + m_contentIndexStop(0) { qRegisterMetaType("IndexerState"); qRegisterMetaType("BatchIndexer::WorkMode"); + qRegisterMetaType("WorkMode"); + qRegisterMetaType("Targets"); m_threadPool.setMaxThreadCount(1); connect(&m_fileWatcher, &FileWatcher::filesUpdate, this, &IndexScheduler::updateIndex); connect(m_config, &FileIndexerConfig::fileIndexEnableStatusChanged, this, &IndexScheduler::fileIndexEnable); + connect(m_config, &FileIndexerConfig::contentIndexEnableStatusChanged, this, &IndexScheduler::contentIndexEnable); + connect(m_config, &FileIndexerConfig::appendIndexDir, this, &IndexScheduler::addNewPath); connect(m_config, &FileIndexerConfig::removeIndexDir, this, &IndexScheduler::removeIndex); m_state = Startup; + BatchIndexer::Targets targets = BatchIndexer::Target::None; if(m_config->isFileIndexEnable()) { - start(); + targets |= BatchIndexer::Target::Basic; } else { - m_stop.fetchAndStoreRelaxed(1); + m_indexStop.fetchAndStoreRelaxed(1); } + if(m_config->isContentIndexEnable()) { + targets |= BatchIndexer::Target::Content; + } else { + m_contentIndexStop.fetchAndStoreRelaxed(1); + } + start(targets); } void IndexScheduler::addNewPath(const QString &folders, const QStringList &blackList) { - if(m_stop.LOAD) { + if(m_indexStop.LOAD && m_contentIndexStop.LOAD) { qDebug() << "Index Scheduler is being stopped, add operation will be executed when started up next time."; return; } @@ -70,54 +82,33 @@ void IndexScheduler::addNewPath(const QString &folders, const QStringList &black void IndexScheduler::removeIndex(const QString &folders) { - if(m_stop.LOAD) { + if(m_indexStop.LOAD && m_contentIndexStop.LOAD) { qDebug() << "Index Scheduler is being stopped, remove operation will be executed when started up next time."; return; } m_fileWatcher.removeWatch(folders, true); } -void IndexScheduler::stop() +void IndexScheduler::stop(BatchIndexer::Targets target) { - m_stop.fetchAndStoreRelaxed(1); - m_fileWatcher.removeWatch(); - m_threadPool.clear(); - m_threadPool.waitForDone(-1); - m_state = Stop; - qDebug() << "Index scheduler has been stopped."; - Q_EMIT stateChange(m_state); - m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Off); - m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Off); -} - -void IndexScheduler::start() -{ - qDebug() << "Index scheduler start."; - if(!m_isFirstRunFinished || !m_isRebuildFinished) { - qDebug() << "Index scheduler running, start operation ignored. FirstRun finished: " << m_isFirstRunFinished << "Rebuild finished: " << m_isRebuildFinished; - return; + if(target & BatchIndexer::Target::Basic) { + m_indexStop.fetchAndStoreRelaxed(1); + m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Off); + qDebug() << "File index has been stopped."; } - m_stop.fetchAndStoreRelaxed(0); - m_state = Running; - Q_EMIT stateChange(m_state); - BatchIndexer::Targets rebuiltTarget = checkAndRebuild(); - - BatchIndexer::WorkMode mode = BatchIndexer::WorkMode::Update; - BatchIndexer::Targets target = BatchIndexer::Target::None; - - //如果数据库被执行过重建,那么跳过增量更新步骤。 - if(m_config->isFileIndexEnable() && !(rebuiltTarget & BatchIndexer::Target::Basic)) { - target |= BatchIndexer::Target::Basic; - m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating); + if(target & BatchIndexer::Target::Content) { + m_contentIndexStop.fetchAndStoreRelaxed(1); + m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Off); + qDebug() << "File content index has been stopped."; } - if(m_config->isContentIndexEnable() && !(rebuiltTarget & BatchIndexer::Target::Content)) { - target |= BatchIndexer::Target::Content; - m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating); + if(m_indexStop.LOAD && m_contentIndexStop.LOAD) { + m_fileWatcher.removeWatch(); + m_threadPool.clear(); + m_threadPool.waitForDone(-1); + m_state = Stop; + qDebug() << "Index scheduler has been stopped."; + Q_EMIT stateChange(m_state); } - startIndexJob(m_config->currentIndexableDir(), m_config->currentBlackListOfIndex(), mode, target); - - //启动监听 - m_fileWatcher.installWatches(); } IndexScheduler::IndexerState IndexScheduler::getIndexState() @@ -125,27 +116,83 @@ IndexScheduler::IndexerState IndexScheduler::getIndexState() return m_state; } -BatchIndexer::Targets IndexScheduler::checkAndRebuild() +void IndexScheduler::start(BatchIndexer::Targets target) +{ + qDebug() << "Index scheduler start."; + //检查是否有任务未完成 + BatchIndexer::Targets tmpTargets = BatchIndexer::Target::None; + if(target & BatchIndexer::Basic) { + if(m_indexFirstRunFinished && m_indexRebuildFinished) { + tmpTargets |= BatchIndexer::Target::Basic; + } + } + if(target & BatchIndexer::Content) { + if(m_contentIndexFirstRunFinished && m_contentIndexRebuildFinished) { + tmpTargets |= BatchIndexer::Target::Content; + } + } + if(tmpTargets == BatchIndexer::Target::None) { + qDebug() << "Index scheduler running, start operation ignored." + << "FirstRun finished: " << m_indexFirstRunFinished + << "Rebuild finished: " << m_indexRebuildFinished + << "Content index firstRun finished: " << m_contentIndexFirstRunFinished + << "Content index rebuild finished: " << m_contentIndexRebuildFinished; + return; + } + + //打开异步控制开关 + if(tmpTargets & BatchIndexer::Basic) { + m_indexStop.fetchAndStoreRelaxed(0); + } + if(tmpTargets & BatchIndexer::Content) { + m_contentIndexStop.fetchAndStoreRelaxed(0); + } + //将索引调度器状态设置为运行中 + m_state = Running; + Q_EMIT stateChange(m_state); + + //检查是否有数据库需要重建并且执行重建 + BatchIndexer::Targets rebuiltTarget = checkAndRebuild(tmpTargets); + + BatchIndexer::WorkMode mode = BatchIndexer::WorkMode::Update; + BatchIndexer::Targets startTarget = BatchIndexer::Target::None; + + //如果数据库被执行过重建,那么跳过增量更新步骤。 + if((tmpTargets & BatchIndexer::Target::Basic) && !(rebuiltTarget & BatchIndexer::Target::Basic)) { + startTarget |= BatchIndexer::Target::Basic; + m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating); + } + if((tmpTargets & BatchIndexer::Target::Content) && !(rebuiltTarget & BatchIndexer::Target::Content)) { + startTarget |= BatchIndexer::Target::Content; + m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating); + } + startIndexJob(m_config->currentIndexableDir(), m_config->currentBlackListOfIndex(), mode, startTarget); + + //启动监听 + m_fileWatcher.installWatches(); +} + +BatchIndexer::Targets IndexScheduler::checkAndRebuild(BatchIndexer::Targets target) { BatchIndexer::WorkMode mode = BatchIndexer::WorkMode::Rebuild; - BatchIndexer::Targets target = BatchIndexer::Target::None; - if((m_statusRecorder->getStatus(INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error - || !m_statusRecorder->versionCheck(INDEX_DATABASE_VERSION_KEY, INDEX_DATABASE_VERSION)) - && m_config->isFileIndexEnable()) { + BatchIndexer::Targets rebuildTarget = BatchIndexer::Target::None; + if((target & BatchIndexer::Target::Basic) && m_config->isFileIndexEnable() && + (m_statusRecorder->getStatus(INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error + || !m_statusRecorder->versionCheck(INDEX_DATABASE_VERSION_KEY, INDEX_DATABASE_VERSION))) { qDebug() << "Basic database need rebuild"; - target |= BatchIndexer::Target::Basic; + rebuildTarget |= BatchIndexer::Target::Basic; m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Initializing); } - if((m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error - || !m_statusRecorder->versionCheck(CONTENT_DATABASE_VERSION_KEY, CONTENT_DATABASE_VERSION)) - && m_config->isFileIndexEnable()) { + if((target & BatchIndexer::Target::Content) && m_config->isContentIndexEnable() && + (m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error + || !m_statusRecorder->versionCheck(CONTENT_DATABASE_VERSION_KEY, CONTENT_DATABASE_VERSION))) { qDebug() << "Content database need rebuild"; - target |= BatchIndexer::Target::Content; + rebuildTarget |= BatchIndexer::Target::Content; m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Initializing); } - startIndexJob(m_config->currentIndexableDir(), m_config->currentBlackListOfIndex(), mode, target); - return target; + startIndexJob(m_config->currentIndexableDir(), m_config->currentBlackListOfIndex(), mode, rebuildTarget); + return rebuildTarget; } void IndexScheduler::startIndexJob(const QStringList& folders,const QStringList& blackList, BatchIndexer::WorkMode mode, BatchIndexer::Targets target) @@ -153,78 +200,66 @@ void IndexScheduler::startIndexJob(const QStringList& folders,const QStringList& if(BatchIndexer::Target::None != target) { switch (mode) { case BatchIndexer::WorkMode::Add: - m_isAddNewPathFinished = false; + m_addNewPathFinished = false; break; case BatchIndexer::WorkMode::Rebuild: - m_isRebuildFinished = false; + if(target & BatchIndexer::Basic) { + m_indexRebuildFinished = false; + } + if(target & BatchIndexer::Content) { + m_contentIndexRebuildFinished = false; + } break; case BatchIndexer::WorkMode::Update: - m_isFirstRunFinished = false; + if(target & BatchIndexer::Basic) { + m_indexFirstRunFinished = false; + } + if(target & BatchIndexer::Content) { + m_contentIndexFirstRunFinished = false; + } break; default: break; } - BatchIndexer *indexer = new BatchIndexer(folders, blackList, m_stop, mode, target); + BatchIndexer *indexer = new BatchIndexer(folders, blackList, m_indexStop, m_contentIndexStop, mode, target); connect(indexer, &BatchIndexer::done, this, &IndexScheduler::firstRunFinished, Qt::QueuedConnection); - connect(indexer, &BatchIndexer::progress, this, &IndexScheduler::process); - - connect(indexer, &BatchIndexer::basicIndexDone, this, [&](uint size){ - bool success = false; - if(!(m_statusRecorder->getStatus(INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error)) { - m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Ready); - success = true; - } - Q_EMIT basicIndexDone(size, success); - }); - - connect(indexer, &BatchIndexer::contentIndexDone, this, [&](uint size){ - bool success = false; - if(!(m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error)) { - m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Ready); - success = true; - } - Q_EMIT contentIndexDone(size, success); - }); - + connect(indexer, &BatchIndexer::progress, this, &IndexScheduler::process, Qt::QueuedConnection); + connect(indexer, &BatchIndexer::basicIndexDone, this, &IndexScheduler::onBasicIndexDone, Qt::QueuedConnection); + connect(indexer, &BatchIndexer::contentIndexDone, this, &IndexScheduler::onContentIndexDone, Qt::QueuedConnection); m_threadPool.start(indexer); } } void IndexScheduler::fileIndexEnable(bool enable) { - //Fix me: 快速反复开关会导致反复执行增量更新操作,可优化。 if(enable) { - start(); + start(BatchIndexer::Basic); } else { - stop(); + stop(BatchIndexer::Basic); + } +} + +void IndexScheduler::contentIndexEnable(bool enable) +{ + if(enable) { + start(BatchIndexer::Content); + } else { + stop(BatchIndexer::Content); } } void IndexScheduler::updateIndex(const QVector &files) { qDebug() << "updateIndex====="; - m_isUpdateFinished = false; + m_updateFinished = false; m_state = Running; - IndexUpdater *updateJob = new IndexUpdater(files, m_stop); + IndexUpdater *updateJob = new IndexUpdater(files, m_indexStop); connect(updateJob, &IndexUpdater::done, this, &IndexScheduler::updateFinished, Qt::QueuedConnection); m_threadPool.start(updateJob); } -void IndexScheduler::firstRunFinished(BatchIndexer::WorkMode mode) +void IndexScheduler::firstRunFinished() { - switch (mode) { - case BatchIndexer::WorkMode::Add: - m_isAddNewPathFinished = true; - break; - case BatchIndexer::WorkMode::Rebuild: - m_isRebuildFinished = true; - break; - case BatchIndexer::WorkMode::Update: - m_isFirstRunFinished = true; - break; - default: - break; - } if(isIdle()) { m_state = Idle; Q_EMIT stateChange(m_state); @@ -233,7 +268,7 @@ void IndexScheduler::firstRunFinished(BatchIndexer::WorkMode mode) void IndexScheduler::updateFinished() { - m_isUpdateFinished = true; + m_updateFinished = true; if(isIdle()) { m_state = Idle; Q_EMIT stateChange(m_state); @@ -242,5 +277,55 @@ void IndexScheduler::updateFinished() bool IndexScheduler::isIdle() { - return m_isFirstRunFinished && m_isAddNewPathFinished && m_isUpdateFinished && m_isRebuildFinished; + return m_indexFirstRunFinished && m_contentIndexFirstRunFinished + && m_addNewPathFinished + && m_updateFinished + && m_indexRebuildFinished && m_contentIndexRebuildFinished; +} + +void IndexScheduler::onBasicIndexDone(BatchIndexer::WorkMode mode) +{ + switch (mode) { + case BatchIndexer::WorkMode::Add: + m_addNewPathFinished = true; + break; + case BatchIndexer::WorkMode::Rebuild: + m_indexRebuildFinished = true; + break; + case BatchIndexer::WorkMode::Update: + m_indexFirstRunFinished = true; + break; + default: + break; + } + + bool success = false; + if(!(m_statusRecorder->getStatus(INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error)) { + m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Ready); + success = true; + } + Q_EMIT basicIndexDone(success); +} + +void IndexScheduler::onContentIndexDone(BatchIndexer::WorkMode mode) +{ + switch (mode) { + case BatchIndexer::WorkMode::Add: + m_addNewPathFinished = true; + break; + case BatchIndexer::WorkMode::Rebuild: + m_contentIndexRebuildFinished = true; + break; + case BatchIndexer::WorkMode::Update: + m_contentIndexFirstRunFinished = true; + break; + default: + break; + } + bool success = false; + if(!(m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error)) { + m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Ready); + success = true; + } + Q_EMIT contentIndexDone(success); } diff --git a/libsearch/index/index-scheduler.h b/libsearch/index/index-scheduler.h index 1a074a7..7e64400 100644 --- a/libsearch/index/index-scheduler.h +++ b/libsearch/index/index-scheduler.h @@ -54,23 +54,27 @@ public: * @param folders 要移除索引的目录 */ Q_SCRIPTABLE void removeIndex(const QString& folders); - Q_SCRIPTABLE void stop(); - Q_SCRIPTABLE void start(); + Q_SCRIPTABLE IndexerState getIndexState(); Q_SIGNALS: void stateChange(IndexerState); void process(IndexType type, uint all, uint finished); - void basicIndexDone(uint size, bool success); - void contentIndexDone(uint size, bool success); + void basicIndexDone(bool success); + void contentIndexDone(bool success); void done(); private Q_SLOTS: + void start(BatchIndexer::Targets target); + void stop(BatchIndexer::Targets target); void fileIndexEnable(bool enable); + void contentIndexEnable(bool enable); void updateIndex(const QVector& files); - void firstRunFinished(BatchIndexer::WorkMode mode); + void firstRunFinished(); void updateFinished(); bool isIdle(); + void onBasicIndexDone(BatchIndexer::WorkMode mode); + void onContentIndexDone(BatchIndexer::WorkMode mode); private: /** @@ -78,19 +82,24 @@ private: * 检查数据库状态,数据库状态处于 IndexStatusRecorder::State::Error 时,开始重建任务。 * @return 返回需要重建的数据库 */ - BatchIndexer::Targets checkAndRebuild(); + BatchIndexer::Targets checkAndRebuild(BatchIndexer::Targets target = BatchIndexer::All); void startIndexJob(const QStringList &folders, const QStringList &blackList, BatchIndexer::WorkMode mode, BatchIndexer::Targets target); FileWatcher m_fileWatcher; IndexStatusRecorder *m_statusRecorder = nullptr; FileIndexerConfig *m_config = nullptr; IndexerState m_state; - QAtomicInt m_stop; + QAtomicInt m_indexStop; + QAtomicInt m_contentIndexStop; QThreadPool m_threadPool; - bool m_isFirstRunFinished = true; - bool m_isRebuildFinished = true; - bool m_isUpdateFinished = true; - bool m_isAddNewPathFinished = true; + bool m_indexFirstRunFinished = true; + bool m_contentIndexFirstRunFinished = true; + + bool m_indexRebuildFinished = true; + bool m_contentIndexRebuildFinished = true; + + bool m_updateFinished = true; + bool m_addNewPathFinished = true; }; } #endif // INDEXSCHEDULER_H diff --git a/libsearch/index/index-updater.cpp b/libsearch/index/index-updater.cpp index e8632ba..1fd62a8 100644 --- a/libsearch/index/index-updater.cpp +++ b/libsearch/index/index-updater.cpp @@ -32,10 +32,10 @@ IndexUpdater::IndexUpdater(const QVector& files, QAtomicInt &stop) m_stop(&stop) { } -void IndexUpdater::UpdateIndex() +void IndexUpdater::updateIndex() { //fix me: How should I delete metadata of files below a folder - //that has been deleted(When a file watcher signal comes which only contains folder info)? + //which has been deleted(When a file watcher signal comes which only contains folder info)? if(FileIndexerConfig::getInstance()->isFileIndexEnable()) { WritableDatabase basicDb(DataBaseType::Basic); if(!basicDb.open()) { @@ -130,5 +130,5 @@ void IndexUpdater::UpdateIndex() void IndexUpdater::run() { - UpdateIndex(); + updateIndex(); } diff --git a/libsearch/index/index-updater.h b/libsearch/index/index-updater.h index 1c14819..eb69d6a 100644 --- a/libsearch/index/index-updater.h +++ b/libsearch/index/index-updater.h @@ -38,7 +38,7 @@ Q_SIGNALS: void done(); private: - void UpdateIndex(); + void updateIndex(); QVector m_cache; QAtomicInt *m_stop = nullptr; diff --git a/ukui-search-service/ukui-search-service.cpp b/ukui-search-service/ukui-search-service.cpp index 9eb6985..fef85ad 100644 --- a/ukui-search-service/ukui-search-service.cpp +++ b/ukui-search-service/ukui-search-service.cpp @@ -68,9 +68,6 @@ void UkuiSearchService::parseCmd(QString msg, bool isPrimary) QCommandLineOption quitOption(QStringList()<<"q"<<"quit", tr("Stop service")); parser.addOption(quitOption); - QCommandLineOption startOption(QStringList()<<"i"<<"index", tr("start or stop file index"), "option"); - parser.addOption(startOption); - QCommandLineOption monitorWindow(QStringList()<<"m"<<"monitor", tr("Show index monitor window")); parser.addOption(monitorWindow); @@ -80,15 +77,6 @@ void UkuiSearchService::parseCmd(QString msg, bool isPrimary) if (isPrimary) { const QStringList args = QString(msg).split(' '); parser.process(args); - if(parser.isSet(startOption)) { - qDebug() << "options!!!!" << parser.value(startOption); - if(parser.value(startOption) == "start") { - m_indexScheduler->start(); - } else if (parser.value(startOption) == "stop") { - m_indexScheduler->stop(); - } - } - if (parser.isSet(monitorWindow)) { loadMonitorWindow(); m_quickView->show();