2022-10-26 18:01:40 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2022, KylinSoft Co., Ltd.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
* Authors: iaom <zhangpengfei@kylinos.cn>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include "index-scheduler.h"
|
|
|
|
|
#include "index-updater.h"
|
2022-12-15 16:13:16 +08:00
|
|
|
|
#include "compatible-define.h"
|
2022-12-02 13:48:59 +08:00
|
|
|
|
|
2022-10-26 18:01:40 +08:00
|
|
|
|
using namespace UkuiSearch;
|
|
|
|
|
IndexScheduler::IndexScheduler(QObject *parent) :
|
|
|
|
|
QObject(parent),
|
|
|
|
|
m_statusRecorder(IndexStatusRecorder::getInstance()),
|
|
|
|
|
m_config(FileIndexerConfig::getInstance()),
|
|
|
|
|
m_state(Startup),
|
|
|
|
|
m_stop(0)
|
|
|
|
|
{
|
|
|
|
|
qRegisterMetaType<IndexerState>("IndexerState");
|
2022-12-15 16:13:16 +08:00
|
|
|
|
qRegisterMetaType<BatchIndexer::WorkMode>("BatchIndexer::WorkMode");
|
2022-10-26 18:01:40 +08:00
|
|
|
|
m_threadPool.setMaxThreadCount(1);
|
|
|
|
|
connect(&m_fileWatcher, &FileWatcher::filesUpdate, this, &IndexScheduler::updateIndex);
|
|
|
|
|
connect(m_config, &FileIndexerConfig::fileIndexEnableStatusChanged, this, &IndexScheduler::fileIndexEnable);
|
|
|
|
|
connect(m_config, &FileIndexerConfig::appendIndexDir, this, &IndexScheduler::addNewPath);
|
|
|
|
|
connect(m_config, &FileIndexerConfig::removeIndexDir, this, &IndexScheduler::removeIndex);
|
|
|
|
|
m_state = Startup;
|
|
|
|
|
if(m_config->isFileIndexEnable()) {
|
2022-12-15 16:13:16 +08:00
|
|
|
|
start();
|
2022-10-26 18:01:40 +08:00
|
|
|
|
} else {
|
|
|
|
|
m_stop.fetchAndStoreRelaxed(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexScheduler::addNewPath(const QString &folders, const QStringList &blackList)
|
|
|
|
|
{
|
2022-12-15 16:13:16 +08:00
|
|
|
|
if(m_stop.LOAD) {
|
2022-10-26 18:01:40 +08:00
|
|
|
|
qDebug() << "Index Scheduler is being stopped, add operation will be executed when started up next time.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_state = Running;
|
2022-12-09 16:30:58 +08:00
|
|
|
|
Q_EMIT stateChange(m_state);
|
2022-12-15 16:13:16 +08:00
|
|
|
|
BatchIndexer::Targets target = BatchIndexer::Target::None;
|
2022-10-26 18:01:40 +08:00
|
|
|
|
if(m_config->isFileIndexEnable()) {
|
2022-12-15 16:13:16 +08:00
|
|
|
|
target |= BatchIndexer::Target::Basic;
|
2023-05-15 11:35:06 +08:00
|
|
|
|
m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
if(m_config->isContentIndexEnable()) {
|
2022-12-15 16:13:16 +08:00
|
|
|
|
target |= BatchIndexer::Target::Content;
|
2023-05-15 11:35:06 +08:00
|
|
|
|
m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
2022-12-15 16:13:16 +08:00
|
|
|
|
BatchIndexer::WorkMode mode = BatchIndexer::WorkMode::Add;
|
2022-12-09 16:30:58 +08:00
|
|
|
|
startIndexJob(QStringList() << folders, blackList, mode, target);
|
2022-12-15 16:13:16 +08:00
|
|
|
|
if(BatchIndexer::Target::None != target) {
|
2022-12-08 10:34:29 +08:00
|
|
|
|
m_fileWatcher.addWatch(folders, blackList);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexScheduler::removeIndex(const QString &folders)
|
|
|
|
|
{
|
2022-12-15 16:13:16 +08:00
|
|
|
|
if(m_stop.LOAD) {
|
2022-10-26 18:01:40 +08:00
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
m_stop.fetchAndStoreRelaxed(1);
|
|
|
|
|
m_fileWatcher.removeWatch();
|
|
|
|
|
m_threadPool.clear();
|
2022-12-15 16:13:16 +08:00
|
|
|
|
m_threadPool.waitForDone(-1);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
m_state = Stop;
|
|
|
|
|
qDebug() << "Index scheduler has been stopped.";
|
|
|
|
|
Q_EMIT stateChange(m_state);
|
2023-03-15 16:39:38 +08:00
|
|
|
|
m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Off);
|
|
|
|
|
m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Off);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 16:13:16 +08:00
|
|
|
|
void IndexScheduler::start()
|
2022-10-26 18:01:40 +08:00
|
|
|
|
{
|
2022-12-15 16:13:16 +08:00
|
|
|
|
qDebug() << "Index scheduler start.";
|
|
|
|
|
if(!m_isFirstRunFinished || !m_isRebuildFinished) {
|
|
|
|
|
qDebug() << "Index scheduler running, start operation ignored. FirstRun finished: " << m_isFirstRunFinished << "Rebuild finished: " << m_isRebuildFinished;
|
2022-10-26 18:01:40 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_stop.fetchAndStoreRelaxed(0);
|
|
|
|
|
m_state = Running;
|
|
|
|
|
Q_EMIT stateChange(m_state);
|
2022-12-15 16:13:16 +08:00
|
|
|
|
BatchIndexer::Targets rebuiltTarget = checkAndRebuild();
|
2022-10-26 18:01:40 +08:00
|
|
|
|
|
2022-12-15 16:13:16 +08:00
|
|
|
|
BatchIndexer::WorkMode mode = BatchIndexer::WorkMode::Update;
|
|
|
|
|
BatchIndexer::Targets target = BatchIndexer::Target::None;
|
2022-10-26 18:01:40 +08:00
|
|
|
|
|
|
|
|
|
//如果数据库被执行过重建,那么跳过增量更新步骤。
|
2022-12-15 16:13:16 +08:00
|
|
|
|
if(m_config->isFileIndexEnable() && !(rebuiltTarget & BatchIndexer::Target::Basic)) {
|
|
|
|
|
target |= BatchIndexer::Target::Basic;
|
2022-12-02 13:48:59 +08:00
|
|
|
|
m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
2022-12-15 16:13:16 +08:00
|
|
|
|
if(m_config->isContentIndexEnable() && !(rebuiltTarget & BatchIndexer::Target::Content)) {
|
|
|
|
|
target |= BatchIndexer::Target::Content;
|
2022-12-02 13:48:59 +08:00
|
|
|
|
m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
2022-12-09 16:30:58 +08:00
|
|
|
|
startIndexJob(m_config->currentIndexableDir(), m_config->currentBlackListOfIndex(), mode, target);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
|
|
|
|
|
//启动监听
|
|
|
|
|
m_fileWatcher.installWatches();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IndexScheduler::IndexerState IndexScheduler::getIndexState()
|
|
|
|
|
{
|
|
|
|
|
return m_state;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 16:13:16 +08:00
|
|
|
|
BatchIndexer::Targets IndexScheduler::checkAndRebuild()
|
2022-10-26 18:01:40 +08:00
|
|
|
|
{
|
2022-12-15 16:13:16 +08:00
|
|
|
|
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))
|
2022-12-02 13:48:59 +08:00
|
|
|
|
&& m_config->isFileIndexEnable()) {
|
|
|
|
|
qDebug() << "Basic database need rebuild";
|
2022-12-15 16:13:16 +08:00
|
|
|
|
target |= BatchIndexer::Target::Basic;
|
2022-12-02 13:48:59 +08:00
|
|
|
|
m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Initializing);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 16:13:16 +08:00
|
|
|
|
if((m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error
|
|
|
|
|
|| !m_statusRecorder->versionCheck(CONTENT_DATABASE_VERSION_KEY, CONTENT_DATABASE_VERSION))
|
2022-12-02 13:48:59 +08:00
|
|
|
|
&& m_config->isFileIndexEnable()) {
|
|
|
|
|
qDebug() << "Content database need rebuild";
|
2022-12-15 16:13:16 +08:00
|
|
|
|
target |= BatchIndexer::Target::Content;
|
2022-12-05 15:58:33 +08:00
|
|
|
|
m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Initializing);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
2022-12-09 16:30:58 +08:00
|
|
|
|
startIndexJob(m_config->currentIndexableDir(), m_config->currentBlackListOfIndex(), mode, target);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
return target;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 16:13:16 +08:00
|
|
|
|
void IndexScheduler::startIndexJob(const QStringList& folders,const QStringList& blackList, BatchIndexer::WorkMode mode, BatchIndexer::Targets target)
|
2022-10-26 18:01:40 +08:00
|
|
|
|
{
|
2022-12-15 16:13:16 +08:00
|
|
|
|
if(BatchIndexer::Target::None != target) {
|
|
|
|
|
switch (mode) {
|
|
|
|
|
case BatchIndexer::WorkMode::Add:
|
|
|
|
|
m_isAddNewPathFinished = false;
|
|
|
|
|
break;
|
|
|
|
|
case BatchIndexer::WorkMode::Rebuild:
|
|
|
|
|
m_isRebuildFinished = false;
|
|
|
|
|
break;
|
|
|
|
|
case BatchIndexer::WorkMode::Update:
|
|
|
|
|
m_isFirstRunFinished = false;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
BatchIndexer *indexer = new BatchIndexer(folders, blackList, m_stop, mode, target);
|
|
|
|
|
connect(indexer, &BatchIndexer::done, this, &IndexScheduler::firstRunFinished, Qt::QueuedConnection);
|
|
|
|
|
connect(indexer, &BatchIndexer::progress, this, &IndexScheduler::process);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
|
2022-12-15 16:13:16 +08:00
|
|
|
|
connect(indexer, &BatchIndexer::basicIndexDone, this, [&](uint size){
|
2022-10-26 18:01:40 +08:00
|
|
|
|
bool success = false;
|
2022-12-02 13:48:59 +08:00
|
|
|
|
if(!(m_statusRecorder->getStatus(INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error)) {
|
|
|
|
|
m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Ready);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
success = true;
|
|
|
|
|
}
|
|
|
|
|
Q_EMIT basicIndexDone(size, success);
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-15 16:13:16 +08:00
|
|
|
|
connect(indexer, &BatchIndexer::contentIndexDone, this, [&](uint size){
|
2022-10-26 18:01:40 +08:00
|
|
|
|
bool success = false;
|
2022-12-02 13:48:59 +08:00
|
|
|
|
if(!(m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error)) {
|
|
|
|
|
m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Ready);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
success = true;
|
|
|
|
|
}
|
|
|
|
|
Q_EMIT contentIndexDone(size, success);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m_threadPool.start(indexer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexScheduler::fileIndexEnable(bool enable)
|
|
|
|
|
{
|
2022-12-15 16:13:16 +08:00
|
|
|
|
//Fix me: 快速反复开关会导致反复执行增量更新操作,可优化。
|
2022-10-26 18:01:40 +08:00
|
|
|
|
if(enable) {
|
2022-12-15 16:13:16 +08:00
|
|
|
|
start();
|
2022-10-26 18:01:40 +08:00
|
|
|
|
} else {
|
|
|
|
|
stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexScheduler::updateIndex(const QVector<PendingFile> &files)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "updateIndex=====";
|
|
|
|
|
m_isUpdateFinished = false;
|
|
|
|
|
m_state = Running;
|
|
|
|
|
IndexUpdater *updateJob = new IndexUpdater(files, m_stop);
|
|
|
|
|
connect(updateJob, &IndexUpdater::done, this, &IndexScheduler::updateFinished, Qt::QueuedConnection);
|
|
|
|
|
m_threadPool.start(updateJob);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 16:13:16 +08:00
|
|
|
|
void IndexScheduler::firstRunFinished(BatchIndexer::WorkMode mode)
|
2022-10-26 18:01:40 +08:00
|
|
|
|
{
|
2022-12-15 16:13:16 +08:00
|
|
|
|
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;
|
2022-12-09 16:30:58 +08:00
|
|
|
|
}
|
|
|
|
|
if(isIdle()) {
|
2022-10-26 18:01:40 +08:00
|
|
|
|
m_state = Idle;
|
|
|
|
|
Q_EMIT stateChange(m_state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexScheduler::updateFinished()
|
|
|
|
|
{
|
|
|
|
|
m_isUpdateFinished = true;
|
2022-12-09 16:30:58 +08:00
|
|
|
|
if(isIdle()) {
|
2022-10-26 18:01:40 +08:00
|
|
|
|
m_state = Idle;
|
|
|
|
|
Q_EMIT stateChange(m_state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 16:30:58 +08:00
|
|
|
|
bool IndexScheduler::isIdle()
|
2022-10-26 18:01:40 +08:00
|
|
|
|
{
|
2022-12-09 16:30:58 +08:00
|
|
|
|
return m_isFirstRunFinished && m_isAddNewPathFinished && m_isUpdateFinished && m_isRebuildFinished;
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|