feat(index):增加内容索引开关

内容索引现在可以通过新增的gsettings(content-index-enable)单独控制开关,
新增了version键用于判断gsettings版本,兼容旧版用户配置:
当file-index-enable值为true,升级之后file-index-enable和
content-index-enable都置为true,否则都置为false。
备注:此提交需要配合设置页面的改动一起食用。
This commit is contained in:
iaom 2023-08-03 09:09:18 +08:00
parent f97213ff60
commit 490459291e
11 changed files with 296 additions and 164 deletions

View File

@ -1,19 +1,29 @@
<schemalist gettext-domain="ukui-search">
<schema id="org.ukui.search.settings" path="/org/ukui/ukui-search/settings/">
<key name="file-index-enable" type="b">
<default>false</default>
<summary>file index switch</summary>
<description>Enable or disable file index service.</description>
</key>
<key name="web-engine" type="s">
<default>"baidu"</default>
<summary>web engine</summary>
<description>Web engine to search keyword online.</description>
</key>
<key name="content-fuzzy-search" type="b">
<default>false</default>
<summary>content fuzzy search</summary>
<description>Enable or disable fuzzy search for file content.</description>
</key>
</schema>
<schema id="org.ukui.search.settings" path="/org/ukui/ukui-search/settings/">
<key name="version" type="s">
<default>"0.0"</default>
<summary>version of this config</summary>
<description>Version of the config of ukui-search.</description>
</key>
<key name="file-index-enable" type="b">
<default>false</default>
<summary>file index switch</summary>
<description>Enable or disable file index service.</description>
</key>
<key name="content-index-enable" type="b">
<default>false</default>
<summary>file content index switch</summary>
<description>Enable or disable file content index service.</description>
</key>
<key name="web-engine" type="s">
<default>"baidu"</default>
<summary>web engine</summary>
<description>Web engine to search keyword online.</description>
</key>
<key name="content-fuzzy-search" type="b">
<default>false</default>
<summary>content fuzzy search</summary>
<description>Enable or disable fuzzy search for file content.</description>
</key>
</schema>
</schemalist>

View File

@ -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";
}

View File

@ -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;

View File

@ -23,13 +23,14 @@
#include <QDebug>
#include <QDir>
#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()

View File

@ -23,7 +23,6 @@
#include <QObject>
#include <QSettings>
#include <QGSettings>
#include <QAtomicInt>
#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;
};

View File

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

View File

@ -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>("IndexerState");
qRegisterMetaType<BatchIndexer::WorkMode>("BatchIndexer::WorkMode");
qRegisterMetaType<BatchIndexer::WorkMode>("WorkMode");
qRegisterMetaType<BatchIndexer::Targets>("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<PendingFile> &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);
}

View File

@ -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<PendingFile>& 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

View File

@ -32,10 +32,10 @@ IndexUpdater::IndexUpdater(const QVector<PendingFile>& 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();
}

View File

@ -38,7 +38,7 @@ Q_SIGNALS:
void done();
private:
void UpdateIndex();
void updateIndex();
QVector<PendingFile> m_cache;
QAtomicInt *m_stop = nullptr;

View File

@ -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();