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"
|
2024-01-23 11:18:55 +08:00
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QMetaEnum>
|
2022-10-26 18:01:40 +08:00
|
|
|
|
#include "index-updater.h"
|
2022-12-15 16:13:16 +08:00
|
|
|
|
#include "compatible-define.h"
|
2024-04-17 17:43:50 +08:00
|
|
|
|
#include "ai-indexer.h"
|
2022-12-02 13:48:59 +08:00
|
|
|
|
|
2022-10-26 18:01:40 +08:00
|
|
|
|
using namespace UkuiSearch;
|
|
|
|
|
IndexScheduler::IndexScheduler(QObject *parent) :
|
2023-09-28 10:54:58 +08:00
|
|
|
|
QObject(parent),
|
|
|
|
|
m_statusRecorder(IndexStatusRecorder::getInstance()),
|
|
|
|
|
m_config(FileIndexerConfig::getInstance()),
|
|
|
|
|
m_state(Startup),
|
|
|
|
|
m_indexStop(0),
|
|
|
|
|
m_contentIndexStop(0),
|
2024-04-17 17:43:50 +08:00
|
|
|
|
m_ocrContentIndexStop(0),
|
|
|
|
|
m_aiIndexStop(0)
|
2022-10-26 18:01:40 +08:00
|
|
|
|
{
|
|
|
|
|
qRegisterMetaType<IndexerState>("IndexerState");
|
2022-12-15 16:13:16 +08:00
|
|
|
|
qRegisterMetaType<BatchIndexer::WorkMode>("BatchIndexer::WorkMode");
|
2023-08-03 09:09:18 +08:00
|
|
|
|
qRegisterMetaType<BatchIndexer::WorkMode>("WorkMode");
|
|
|
|
|
qRegisterMetaType<BatchIndexer::Targets>("Targets");
|
2022-10-26 18:01:40 +08:00
|
|
|
|
m_threadPool.setMaxThreadCount(1);
|
2024-03-08 16:58:09 +08:00
|
|
|
|
m_threadPoolForRTUpdate.setMaxThreadCount(1);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
connect(&m_fileWatcher, &FileWatcher::filesUpdate, this, &IndexScheduler::updateIndex);
|
|
|
|
|
connect(m_config, &FileIndexerConfig::fileIndexEnableStatusChanged, this, &IndexScheduler::fileIndexEnable);
|
2023-08-03 09:09:18 +08:00
|
|
|
|
connect(m_config, &FileIndexerConfig::contentIndexEnableStatusChanged, this, &IndexScheduler::contentIndexEnable);
|
2023-09-28 10:54:58 +08:00
|
|
|
|
connect(m_config, &FileIndexerConfig::contentIndexEnableOcrStatusChanged, this, &IndexScheduler::ocrContentIndexEnable);
|
2024-04-17 17:43:50 +08:00
|
|
|
|
connect(m_config, &FileIndexerConfig::aiIndexEnableStatusChanged, this, &IndexScheduler::aiIndexEnable);
|
2023-08-03 09:09:18 +08:00
|
|
|
|
|
2022-10-26 18:01:40 +08:00
|
|
|
|
connect(m_config, &FileIndexerConfig::appendIndexDir, this, &IndexScheduler::addNewPath);
|
|
|
|
|
connect(m_config, &FileIndexerConfig::removeIndexDir, this, &IndexScheduler::removeIndex);
|
|
|
|
|
m_state = Startup;
|
2023-08-03 09:09:18 +08:00
|
|
|
|
BatchIndexer::Targets targets = BatchIndexer::Target::None;
|
2022-10-26 18:01:40 +08:00
|
|
|
|
if(m_config->isFileIndexEnable()) {
|
2023-08-03 09:09:18 +08:00
|
|
|
|
targets |= BatchIndexer::Target::Basic;
|
|
|
|
|
} else {
|
|
|
|
|
m_indexStop.fetchAndStoreRelaxed(1);
|
|
|
|
|
}
|
|
|
|
|
if(m_config->isContentIndexEnable()) {
|
|
|
|
|
targets |= BatchIndexer::Target::Content;
|
2022-10-26 18:01:40 +08:00
|
|
|
|
} else {
|
2023-08-03 09:09:18 +08:00
|
|
|
|
m_contentIndexStop.fetchAndStoreRelaxed(1);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if(m_config->isOCREnable()) {
|
|
|
|
|
targets |= BatchIndexer::Target::Ocr;
|
|
|
|
|
} else {
|
|
|
|
|
m_ocrContentIndexStop.fetchAndStoreRelaxed(1);
|
|
|
|
|
}
|
2024-04-17 17:43:50 +08:00
|
|
|
|
if(m_config->isAiIndexEnable()) {
|
|
|
|
|
targets |= BatchIndexer::Target::Ai;
|
|
|
|
|
} else {
|
|
|
|
|
m_aiIndexStop.fetchAndStoreRelaxed(1);
|
|
|
|
|
}
|
2023-08-03 09:09:18 +08:00
|
|
|
|
start(targets);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexScheduler::addNewPath(const QString &folders, const QStringList &blackList)
|
|
|
|
|
{
|
2024-04-20 16:29:29 +08:00
|
|
|
|
if(m_indexStop.LOAD && m_contentIndexStop.LOAD && m_ocrContentIndexStop.LOAD && m_aiIndexStartWaiting) {
|
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;
|
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
|
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
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
if(m_config->isOCREnable()) {
|
|
|
|
|
target |= BatchIndexer::Target::Ocr;
|
|
|
|
|
m_statusRecorder->setStatus(OCR_CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating);
|
|
|
|
|
}
|
2024-04-20 16:29:29 +08:00
|
|
|
|
if(m_config->isAiIndexEnable()) {
|
|
|
|
|
target |= BatchIndexer::Target::Ai;
|
|
|
|
|
m_statusRecorder->setStatus(AI_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating);
|
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if(m_indexStop.LOAD && m_contentIndexStop.LOAD && m_ocrContentIndexStop.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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 09:09:18 +08:00
|
|
|
|
void IndexScheduler::stop(BatchIndexer::Targets target)
|
2022-10-26 18:01:40 +08:00
|
|
|
|
{
|
2023-08-03 09:09:18 +08:00
|
|
|
|
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.";
|
|
|
|
|
}
|
|
|
|
|
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.";
|
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
if(target & BatchIndexer::Target::Ocr) {
|
|
|
|
|
m_ocrContentIndexStop.fetchAndStoreRelaxed(1);
|
|
|
|
|
m_statusRecorder->setStatus(OCR_CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Off);
|
|
|
|
|
qDebug() << "File ocr content index has been stopped.";
|
|
|
|
|
}
|
2024-04-17 17:43:50 +08:00
|
|
|
|
if(target & BatchIndexer::Target::Ai) {
|
|
|
|
|
m_aiIndexStop.fetchAndStoreRelaxed(1);
|
|
|
|
|
m_statusRecorder->setStatus(AI_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Off);
|
|
|
|
|
qDebug() << "Ai index has been stopped.";
|
|
|
|
|
}
|
|
|
|
|
if(m_indexStop.LOAD && m_contentIndexStop.LOAD && m_ocrContentIndexStop.LOAD && m_aiIndexStop.LOAD) {
|
2023-08-03 09:09:18 +08:00
|
|
|
|
m_fileWatcher.removeWatch();
|
2024-03-08 17:58:12 +08:00
|
|
|
|
m_watchInstalled = false;
|
2023-08-03 09:09:18 +08:00
|
|
|
|
m_threadPool.clear();
|
|
|
|
|
m_threadPool.waitForDone(-1);
|
2024-03-08 16:58:09 +08:00
|
|
|
|
m_threadPoolForRTUpdate.clear();
|
|
|
|
|
m_threadPoolForRTUpdate.waitForDone(-1);
|
2023-08-03 09:09:18 +08:00
|
|
|
|
m_state = Stop;
|
|
|
|
|
qDebug() << "Index scheduler has been stopped.";
|
|
|
|
|
Q_EMIT stateChange(m_state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IndexScheduler::IndexerState IndexScheduler::getIndexState()
|
|
|
|
|
{
|
|
|
|
|
return m_state;
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 11:18:55 +08:00
|
|
|
|
void IndexScheduler::forceUpdate(BatchIndexer::Targets target)
|
2022-10-26 18:01:40 +08:00
|
|
|
|
{
|
2024-04-17 17:43:50 +08:00
|
|
|
|
m_config->syncIndexFileTypes();
|
2024-01-23 11:18:55 +08:00
|
|
|
|
stop(target);
|
2023-08-03 09:09:18 +08:00
|
|
|
|
if(target & BatchIndexer::Basic) {
|
2024-01-23 11:18:55 +08:00
|
|
|
|
fileIndexEnable(true);
|
2023-08-03 09:09:18 +08:00
|
|
|
|
}
|
|
|
|
|
if(target & BatchIndexer::Content) {
|
2024-01-23 11:18:55 +08:00
|
|
|
|
contentIndexEnable(true);
|
2023-08-03 09:09:18 +08:00
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
if(target & BatchIndexer::Ocr) {
|
2024-01-23 11:18:55 +08:00
|
|
|
|
ocrContentIndexEnable(true);
|
2023-09-28 10:54:58 +08:00
|
|
|
|
}
|
2024-04-17 17:43:50 +08:00
|
|
|
|
if(target & BatchIndexer::Ai) {
|
|
|
|
|
aiIndexEnable(true);
|
|
|
|
|
}
|
2024-01-23 11:18:55 +08:00
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
|
2024-01-23 11:18:55 +08:00
|
|
|
|
void IndexScheduler::start(BatchIndexer::Targets target)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "Index scheduler start." << QMetaEnum::fromType<BatchIndexer::Targets>().valueToKeys(target) ;
|
2023-08-03 09:09:18 +08:00
|
|
|
|
|
|
|
|
|
//打开异步控制开关
|
2023-09-05 18:41:28 +08:00
|
|
|
|
if(target & BatchIndexer::Basic) {
|
2023-08-03 09:09:18 +08:00
|
|
|
|
m_indexStop.fetchAndStoreRelaxed(0);
|
|
|
|
|
}
|
2023-09-05 18:41:28 +08:00
|
|
|
|
if(target & BatchIndexer::Content) {
|
2023-08-03 09:09:18 +08:00
|
|
|
|
m_contentIndexStop.fetchAndStoreRelaxed(0);
|
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
if(target & BatchIndexer::Ocr) {
|
|
|
|
|
m_ocrContentIndexStop.fetchAndStoreRelaxed(0);
|
|
|
|
|
}
|
2024-04-17 17:43:50 +08:00
|
|
|
|
if(target & BatchIndexer::Ai) {
|
|
|
|
|
m_aiIndexStop.fetchAndStoreRelaxed(0);
|
|
|
|
|
}
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if(target == BatchIndexer::None) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-03 09:09:18 +08:00
|
|
|
|
|
|
|
|
|
//检查是否有数据库需要重建并且执行重建
|
2024-01-23 11:18:55 +08:00
|
|
|
|
BatchIndexer::Targets rebuiltTarget = checkAndRebuild(target);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
|
2022-12-15 16:13:16 +08:00
|
|
|
|
BatchIndexer::WorkMode mode = BatchIndexer::WorkMode::Update;
|
2023-08-03 09:09:18 +08:00
|
|
|
|
BatchIndexer::Targets startTarget = BatchIndexer::Target::None;
|
2022-10-26 18:01:40 +08:00
|
|
|
|
|
|
|
|
|
//如果数据库被执行过重建,那么跳过增量更新步骤。
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if((target & BatchIndexer::Target::Basic) && !(rebuiltTarget & BatchIndexer::Target::Basic)) {
|
2023-08-03 09:09:18 +08:00
|
|
|
|
startTarget |= 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
|
|
|
|
}
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if((target & BatchIndexer::Target::Content) && !(rebuiltTarget & BatchIndexer::Target::Content)) {
|
2023-08-03 09:09:18 +08:00
|
|
|
|
startTarget |= 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
|
|
|
|
}
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if(target & BatchIndexer::Ocr && !(rebuiltTarget & BatchIndexer::Target::Ocr)) {
|
2023-09-28 10:54:58 +08:00
|
|
|
|
startTarget |= BatchIndexer::Target::Ocr;
|
|
|
|
|
m_statusRecorder->setStatus(OCR_CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating);
|
|
|
|
|
}
|
2024-04-17 17:43:50 +08:00
|
|
|
|
if(target & BatchIndexer::Ai && !(rebuiltTarget & BatchIndexer::Target::Ai)) {
|
|
|
|
|
startTarget |= BatchIndexer::Target::Ai;
|
|
|
|
|
m_statusRecorder->setStatus(AI_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating);
|
|
|
|
|
}
|
2023-08-03 09:09:18 +08:00
|
|
|
|
startIndexJob(m_config->currentIndexableDir(), m_config->currentBlackListOfIndex(), mode, startTarget);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
|
|
|
|
|
//启动监听
|
2024-01-23 11:18:55 +08:00
|
|
|
|
installWatches();
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 09:09:18 +08:00
|
|
|
|
BatchIndexer::Targets IndexScheduler::checkAndRebuild(BatchIndexer::Targets target)
|
2022-10-26 18:01:40 +08:00
|
|
|
|
{
|
2022-12-15 16:13:16 +08:00
|
|
|
|
BatchIndexer::WorkMode mode = BatchIndexer::WorkMode::Rebuild;
|
2023-08-03 09:09:18 +08:00
|
|
|
|
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))) {
|
2022-12-02 13:48:59 +08:00
|
|
|
|
qDebug() << "Basic database need rebuild";
|
2023-08-03 09:09:18 +08:00
|
|
|
|
rebuildTarget |= 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
|
|
|
|
}
|
2023-08-03 09:09:18 +08:00
|
|
|
|
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))) {
|
2022-12-02 13:48:59 +08:00
|
|
|
|
qDebug() << "Content database need rebuild";
|
2023-08-03 09:09:18 +08:00
|
|
|
|
rebuildTarget |= 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
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
if((target & BatchIndexer::Target::Ocr) && m_config->isOCREnable() &&
|
|
|
|
|
(m_statusRecorder->getStatus(OCR_CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Error
|
|
|
|
|
|| !m_statusRecorder->versionCheck(OCR_CONTENT_DATABASE_VERSION_KEY, OCR_CONTENT_DATABASE_VERSION))) {
|
|
|
|
|
qDebug() << "Ocr content database need rebuild";
|
|
|
|
|
rebuildTarget |= BatchIndexer::Target::Ocr;
|
|
|
|
|
m_statusRecorder->setStatus(OCR_CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Initializing);
|
|
|
|
|
}
|
2023-08-03 09:09:18 +08:00
|
|
|
|
startIndexJob(m_config->currentIndexableDir(), m_config->currentBlackListOfIndex(), mode, rebuildTarget);
|
|
|
|
|
return rebuildTarget;
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2023-09-28 10:54:58 +08:00
|
|
|
|
if(mode == BatchIndexer::WorkMode::Add) {
|
|
|
|
|
m_addNewPathPendingWorkCount++;
|
2022-12-15 16:13:16 +08:00
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
if(target & BatchIndexer::Basic) {
|
2024-01-23 11:18:55 +08:00
|
|
|
|
m_basicIndexPendingWorkCount++;
|
2023-09-28 10:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
if(target & BatchIndexer::Content) {
|
|
|
|
|
m_contentIndexPendingWorkCount++;
|
|
|
|
|
}
|
|
|
|
|
if(target & BatchIndexer::Ocr) {
|
|
|
|
|
m_ocrContentIndexPendingWorkCount++;
|
|
|
|
|
}
|
2024-04-17 17:43:50 +08:00
|
|
|
|
if(target & BatchIndexer::Ai) {
|
|
|
|
|
m_aiIndexPendingWorkCount++;
|
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
|
|
|
|
|
m_state = Running;
|
|
|
|
|
Q_EMIT stateChange(m_state);
|
|
|
|
|
|
2024-04-17 17:43:50 +08:00
|
|
|
|
auto *indexer = new BatchIndexer(folders, blackList,
|
|
|
|
|
m_indexStop, m_contentIndexStop, m_ocrContentIndexStop, m_aiIndexStop,
|
|
|
|
|
mode, target);
|
2023-09-28 10:54:58 +08:00
|
|
|
|
connect(indexer, &BatchIndexer::done, this, &IndexScheduler::batchIndexerFinished, Qt::QueuedConnection);
|
2023-08-03 09:09:18 +08:00
|
|
|
|
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);
|
2023-09-28 10:54:58 +08:00
|
|
|
|
connect(indexer, &BatchIndexer::ocrContentIndexDone, this, &IndexScheduler::onOcrContentIndexDone, Qt::QueuedConnection);
|
2024-04-17 17:43:50 +08:00
|
|
|
|
connect(indexer, &BatchIndexer::aiIndexDone, this, &IndexScheduler::onAiIndexDone, Qt::QueuedConnection);
|
2024-04-15 09:52:06 +08:00
|
|
|
|
connect(indexer, &BatchIndexer::basicIndexStart, this, &IndexScheduler::basicIndexStart, Qt::QueuedConnection);
|
|
|
|
|
connect(indexer, &BatchIndexer::contentIndexStart, this, &IndexScheduler::contentIndexStart, Qt::QueuedConnection);
|
|
|
|
|
connect(indexer, &BatchIndexer::ocrContentIndexStart, this, &IndexScheduler::ocrContentIndexStart, Qt::QueuedConnection);
|
2024-04-17 17:43:50 +08:00
|
|
|
|
connect(indexer, &BatchIndexer::aiIndexStart, this, &IndexScheduler::aiIndexStart, Qt::QueuedConnection);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
m_threadPool.start(indexer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexScheduler::fileIndexEnable(bool enable)
|
|
|
|
|
{
|
|
|
|
|
if(enable) {
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if(m_basicIndexPendingWorkCount == 0) {
|
|
|
|
|
start(BatchIndexer::Basic);
|
|
|
|
|
} else {
|
|
|
|
|
m_basicIndexStartWaiting = true;
|
|
|
|
|
}
|
2023-08-03 09:09:18 +08:00
|
|
|
|
} else {
|
|
|
|
|
stop(BatchIndexer::Basic);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexScheduler::contentIndexEnable(bool enable)
|
|
|
|
|
{
|
|
|
|
|
if(enable) {
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if(m_contentIndexPendingWorkCount == 0) {
|
|
|
|
|
start(BatchIndexer::Content);
|
|
|
|
|
} else {
|
|
|
|
|
m_contentIndexStartWaiting = true;
|
|
|
|
|
}
|
2022-10-26 18:01:40 +08:00
|
|
|
|
} else {
|
2023-08-03 09:09:18 +08:00
|
|
|
|
stop(BatchIndexer::Content);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 10:54:58 +08:00
|
|
|
|
void IndexScheduler::ocrContentIndexEnable(bool enable)
|
|
|
|
|
{
|
|
|
|
|
if(enable) {
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if(m_ocrContentIndexPendingWorkCount == 0) {
|
|
|
|
|
start(BatchIndexer::Ocr);
|
|
|
|
|
} else {
|
|
|
|
|
m_ocrContentIndexStartWaiting = true;
|
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
} else {
|
|
|
|
|
stop(BatchIndexer::Ocr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 17:43:50 +08:00
|
|
|
|
void IndexScheduler::aiIndexEnable(bool enable)
|
|
|
|
|
{
|
|
|
|
|
if(enable) {
|
|
|
|
|
if(m_aiIndexPendingWorkCount == 0) {
|
|
|
|
|
start(BatchIndexer::Ai);
|
|
|
|
|
} else {
|
|
|
|
|
m_aiIndexStartWaiting = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
stop(BatchIndexer::Ai);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 18:01:40 +08:00
|
|
|
|
void IndexScheduler::updateIndex(const QVector<PendingFile> &files)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "updateIndex=====";
|
2023-09-28 10:54:58 +08:00
|
|
|
|
m_updatePendingWorkCount++;
|
|
|
|
|
|
2022-10-26 18:01:40 +08:00
|
|
|
|
m_state = Running;
|
2023-09-28 10:54:58 +08:00
|
|
|
|
Q_EMIT stateChange(m_state);
|
|
|
|
|
|
2024-04-17 17:43:50 +08:00
|
|
|
|
auto *updateJob = new IndexUpdater(files, m_indexStop, m_contentIndexStop, m_ocrContentIndexStop, m_aiIndexStop);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
connect(updateJob, &IndexUpdater::done, this, &IndexScheduler::updateFinished, Qt::QueuedConnection);
|
2024-03-08 16:58:09 +08:00
|
|
|
|
m_threadPoolForRTUpdate.start(updateJob);
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 10:54:58 +08:00
|
|
|
|
void IndexScheduler::batchIndexerFinished(BatchIndexer::WorkMode mode, BatchIndexer::Targets targets)
|
2022-10-26 18:01:40 +08:00
|
|
|
|
{
|
2023-09-28 10:54:58 +08:00
|
|
|
|
if(mode == BatchIndexer::WorkMode::Add) {
|
|
|
|
|
m_addNewPathPendingWorkCount--;
|
|
|
|
|
}
|
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()
|
|
|
|
|
{
|
2023-09-28 10:54:58 +08:00
|
|
|
|
m_updatePendingWorkCount--;
|
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
|
|
|
|
{
|
2024-01-23 11:18:55 +08:00
|
|
|
|
return m_basicIndexPendingWorkCount == 0
|
2023-09-28 10:54:58 +08:00
|
|
|
|
&& m_contentIndexPendingWorkCount == 0
|
|
|
|
|
&& m_ocrContentIndexPendingWorkCount == 0
|
2024-04-17 17:43:50 +08:00
|
|
|
|
&& m_aiIndexPendingWorkCount == 0
|
2023-09-28 10:54:58 +08:00
|
|
|
|
&& m_updatePendingWorkCount == 0
|
|
|
|
|
&& m_addNewPathPendingWorkCount == 0;
|
2023-08-03 09:09:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexScheduler::onBasicIndexDone(BatchIndexer::WorkMode mode)
|
|
|
|
|
{
|
2023-09-28 10:54:58 +08:00
|
|
|
|
Q_UNUSED(mode)
|
2024-01-23 11:18:55 +08:00
|
|
|
|
m_basicIndexPendingWorkCount--;
|
2023-08-03 09:09:18 +08:00
|
|
|
|
|
|
|
|
|
bool success = false;
|
2024-03-08 16:58:09 +08:00
|
|
|
|
if(m_statusRecorder->getStatus(INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Initializing ||
|
|
|
|
|
m_statusRecorder->getStatus(INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Updating) {
|
2023-08-03 09:09:18 +08:00
|
|
|
|
m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Ready);
|
|
|
|
|
success = true;
|
2024-04-15 09:52:06 +08:00
|
|
|
|
} else if (m_statusRecorder->getStatus(INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::RealTimeUpdating ) {
|
|
|
|
|
success = true;
|
2023-08-03 09:09:18 +08:00
|
|
|
|
}
|
|
|
|
|
Q_EMIT basicIndexDone(success);
|
2024-01-23 11:18:55 +08:00
|
|
|
|
checkIfStartsWaiting();
|
2023-08-03 09:09:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexScheduler::onContentIndexDone(BatchIndexer::WorkMode mode)
|
|
|
|
|
{
|
2023-09-28 10:54:58 +08:00
|
|
|
|
Q_UNUSED(mode)
|
|
|
|
|
m_contentIndexPendingWorkCount--;
|
|
|
|
|
|
2023-08-03 09:09:18 +08:00
|
|
|
|
bool success = false;
|
2024-03-08 16:58:09 +08:00
|
|
|
|
if(m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Initializing ||
|
|
|
|
|
m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Updating) {
|
2023-08-03 09:09:18 +08:00
|
|
|
|
m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Ready);
|
|
|
|
|
success = true;
|
2024-04-15 09:52:06 +08:00
|
|
|
|
} else if (m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::RealTimeUpdating ) {
|
|
|
|
|
success = true;
|
2023-08-03 09:09:18 +08:00
|
|
|
|
}
|
|
|
|
|
Q_EMIT contentIndexDone(success);
|
2024-01-23 11:18:55 +08:00
|
|
|
|
checkIfStartsWaiting();
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
|
|
|
|
|
void IndexScheduler::onOcrContentIndexDone(BatchIndexer::WorkMode mode)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(mode)
|
|
|
|
|
m_ocrContentIndexPendingWorkCount--;
|
|
|
|
|
|
|
|
|
|
bool success = false;
|
2024-03-08 16:58:09 +08:00
|
|
|
|
if(m_statusRecorder->getStatus(OCR_CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Initializing ||
|
|
|
|
|
m_statusRecorder->getStatus(OCR_CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Updating) {
|
2023-09-28 10:54:58 +08:00
|
|
|
|
m_statusRecorder->setStatus(OCR_CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Ready);
|
|
|
|
|
success = true;
|
2024-04-15 09:52:06 +08:00
|
|
|
|
} else if (m_statusRecorder->getStatus(OCR_CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::RealTimeUpdating ) {
|
|
|
|
|
success = true;
|
2023-09-28 10:54:58 +08:00
|
|
|
|
}
|
2024-04-15 09:52:06 +08:00
|
|
|
|
Q_EMIT ocrContentIndexDone(success);
|
2024-01-23 11:18:55 +08:00
|
|
|
|
checkIfStartsWaiting();
|
|
|
|
|
}
|
2024-04-17 17:43:50 +08:00
|
|
|
|
void IndexScheduler::onAiIndexDone(BatchIndexer::WorkMode mode)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(mode)
|
|
|
|
|
m_aiIndexPendingWorkCount--;
|
|
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
|
if(m_statusRecorder->getStatus(AI_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Initializing ||
|
|
|
|
|
m_statusRecorder->getStatus(AI_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Updating) {
|
|
|
|
|
m_statusRecorder->setStatus(AI_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Ready);
|
|
|
|
|
success = true;
|
|
|
|
|
} else if (m_statusRecorder->getStatus(AI_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::RealTimeUpdating ) {
|
|
|
|
|
success = true;
|
|
|
|
|
}
|
|
|
|
|
Q_EMIT aiIndexDone(success);
|
|
|
|
|
checkIfStartsWaiting();
|
|
|
|
|
}
|
2024-01-23 11:18:55 +08:00
|
|
|
|
|
|
|
|
|
void IndexScheduler::checkIfStartsWaiting()
|
|
|
|
|
{
|
|
|
|
|
BatchIndexer::Targets targets = BatchIndexer::None;
|
|
|
|
|
if(m_basicIndexStartWaiting && !m_basicIndexPendingWorkCount) {
|
|
|
|
|
targets |= BatchIndexer::Basic;
|
|
|
|
|
m_basicIndexStartWaiting = false;
|
|
|
|
|
}
|
|
|
|
|
if(m_contentIndexStartWaiting && !m_contentIndexPendingWorkCount) {
|
|
|
|
|
targets |= BatchIndexer::Content;
|
|
|
|
|
m_contentIndexStartWaiting = false;
|
|
|
|
|
}
|
|
|
|
|
if(m_ocrContentIndexStartWaiting && !m_ocrContentIndexPendingWorkCount) {
|
|
|
|
|
targets |= BatchIndexer::Ocr;
|
|
|
|
|
m_ocrContentIndexStartWaiting = false;
|
|
|
|
|
}
|
2024-04-17 17:43:50 +08:00
|
|
|
|
if(m_aiIndexStartWaiting && !m_aiIndexPendingWorkCount) {
|
|
|
|
|
targets |= BatchIndexer::Ai;
|
|
|
|
|
m_aiIndexStartWaiting = false;
|
|
|
|
|
}
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if(targets != BatchIndexer::None) {
|
|
|
|
|
start(targets);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexScheduler::installWatches()
|
|
|
|
|
{
|
2024-04-18 19:19:53 +08:00
|
|
|
|
if(m_config->isFileIndexEnable() || m_config->isContentIndexEnable() || m_config->isOCREnable() || m_config->isAiIndexEnable()) {
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if(!m_watchInstalled) {
|
|
|
|
|
m_fileWatcher.installWatches();
|
|
|
|
|
m_watchInstalled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
}
|