diff --git a/libsearch/common.h b/libsearch/common.h index 1b0c9e7..2a1638f 100644 --- a/libsearch/common.h +++ b/libsearch/common.h @@ -19,6 +19,10 @@ static const QString INDEX_SEM = QStringLiteral("ukui-search-index-sem"); static const int OCR_MIN_SIZE = 200; static const QByteArray UKUI_SEARCH_SCHEMAS = QByteArrayLiteral("org.ukui.search.settings"); static const QString SEARCH_METHOD_KEY = QStringLiteral("fileIndexEnable"); +static const QString INDEX_DATABASE_VERSION = QStringLiteral("1.0.0"); +static const QString CONTENT_DATABASE_VERSION = QStringLiteral("1.0.0"); + + static const QStringList allAppPath = { {HOME_PATH + "/.local/share/applications/"}, diff --git a/libsearch/index/index-scheduler.cpp b/libsearch/index/index-scheduler.cpp index a3af901..180f565 100644 --- a/libsearch/index/index-scheduler.cpp +++ b/libsearch/index/index-scheduler.cpp @@ -19,7 +19,7 @@ */ #include "index-scheduler.h" #include "index-updater.h" -#include "first-run-indexer.h" + using namespace UkuiSearch; IndexScheduler::IndexScheduler(QObject *parent) : QObject(parent), @@ -101,11 +101,11 @@ void IndexScheduler::scheduleIndexing() //如果数据库被执行过重建,那么跳过增量更新步骤。 if(m_config->isFileIndexEnable() && !(rebuiltTarget & FirstRunIndexer::Target::Basic)) { target |= FirstRunIndexer::Target::Basic; - m_statusRecorder->setStatus(INDEX_DATABASE_STATE, IndexStatusRecorder::State::Updating); + m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating); } if(m_config->isContentIndexEnable() && !(rebuiltTarget & FirstRunIndexer::Target::Content)) { target |= FirstRunIndexer::Target::Content; - m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE, IndexStatusRecorder::State::Updating); + m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Updating); } startIndexJob(mode, target); @@ -122,16 +122,18 @@ FirstRunIndexer::Targets IndexScheduler::checkAndRebuild() { FirstRunIndexer::WorkModes mode = FirstRunIndexer::WorkMode::Rebuild; FirstRunIndexer::Targets target = FirstRunIndexer::Target::None; - if(m_statusRecorder->getStatus(INDEX_DATABASE_STATE).toInt() == IndexStatusRecorder::State::Error && m_config->isFileIndexEnable()) { - qDebug() << "Basic database error,need rebuild"; + 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()) { + qDebug() << "Basic database need rebuild"; target |= FirstRunIndexer::Target::Basic; - m_statusRecorder->setStatus(INDEX_DATABASE_STATE, IndexStatusRecorder::State::Initializing); + m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Initializing); } - if(m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE).toInt() == IndexStatusRecorder::State::Error && m_config->isFileIndexEnable()) { - qDebug() << "Content database error,need rebuild"; + 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()) { + qDebug() << "Content database need rebuild"; target |= FirstRunIndexer::Target::Content; - m_statusRecorder->setStatus(INDEX_DATABASE_STATE, IndexStatusRecorder::State::Initializing); + m_statusRecorder->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Initializing); } startIndexJob(mode, target); return target; @@ -146,8 +148,8 @@ void IndexScheduler::startIndexJob(FirstRunIndexer::WorkModes &mode, FirstRunInd connect(indexer, &FirstRunIndexer::basicIndexDone, this, [&](uint size){ bool success = false; - if(!(m_statusRecorder->getStatus(INDEX_DATABASE_STATE).toInt() == IndexStatusRecorder::State::Error)) { - m_statusRecorder->setStatus(INDEX_DATABASE_STATE, IndexStatusRecorder::State::Ready); + 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); @@ -155,8 +157,8 @@ void IndexScheduler::startIndexJob(FirstRunIndexer::WorkModes &mode, FirstRunInd connect(indexer, &FirstRunIndexer::contentIndexDone, this, [&](uint size){ bool success = false; - if(!(m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE).toInt() == IndexStatusRecorder::State::Error)) { - m_statusRecorder->setStatus(CONTENT_INDEX_DATABASE_STATE, IndexStatusRecorder::State::Ready); + 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); @@ -187,8 +189,8 @@ void IndexScheduler::updateIndex(const QVector &files) void IndexScheduler::firstRunFinished() { - if((m_statusRecorder->getStatus(INDEX_DATABASE_STATE).toInt() == IndexStatusRecorder::State::Ready) - && m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE).toInt() == IndexStatusRecorder::State::Ready) { + if((m_statusRecorder->getStatus(INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Ready) + && m_statusRecorder->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt() == IndexStatusRecorder::State::Ready) { m_isFirstRunFinished = true; } if(m_isFirstRunFinished && m_isAddNewPathFinished && m_isUpdateFinished) { diff --git a/libsearch/index/index-status-recorder.cpp b/libsearch/index/index-status-recorder.cpp index 1b9ca55..77d012f 100644 --- a/libsearch/index/index-status-recorder.cpp +++ b/libsearch/index/index-status-recorder.cpp @@ -46,8 +46,28 @@ void IndexStatusRecorder::setStatus(const QString &key, State state) const QVariant IndexStatusRecorder::getStatus(const QString &key) { + m_mutex.lock(); m_status->sync(); - return m_status->value(key); + m_mutex.unlock(); + QVariant value = m_status->value(key); + return value; +} + +void IndexStatusRecorder::setVersion(const QString &key, const QString &version) +{ + m_mutex.lock(); + m_status->setValue(key, version); + m_status->sync(); + m_mutex.unlock(); +} + +bool IndexStatusRecorder::versionCheck(const QString &key, const QString &version) +{ + if(getStatus(key).toString() == version) { + return true; + } else { + return false; + } } bool IndexStatusRecorder::indexDatabaseEnable() @@ -55,7 +75,7 @@ bool IndexStatusRecorder::indexDatabaseEnable() m_mutex.lock(); m_status->sync(); m_mutex.unlock(); - if(m_status->value(INDEX_DATABASE_STATE, 0).toInt() == State::Ready) { + if(m_status->value(INDEX_DATABASE_STATE_KEY, 0).toInt() == State::Ready) { return true; } else { return false; @@ -68,7 +88,7 @@ bool IndexStatusRecorder::contentIndexDatabaseEnable() m_mutex.lock(); m_status->sync(); m_mutex.unlock(); - if(m_status->value(CONTENT_INDEX_DATABASE_STATE, 0).toInt() == State::Ready) { + if(m_status->value(CONTENT_INDEX_DATABASE_STATE_KEY, 0).toInt() == State::Ready) { return true; } else { return false; diff --git a/libsearch/index/index-status-recorder.h b/libsearch/index/index-status-recorder.h index bf72776..157bc15 100644 --- a/libsearch/index/index-status-recorder.h +++ b/libsearch/index/index-status-recorder.h @@ -24,9 +24,11 @@ #include #include #include -#define CONTENT_INDEX_DATABASE_STATE "content_index_database_state" -#define INDEX_DATABASE_STATE "index_database_state" +#define CONTENT_INDEX_DATABASE_STATE_KEY "content_index_database_state" +#define INDEX_DATABASE_STATE_KEY "index_database_state" #define INDEX_STATUS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-index-status.conf" +#define INDEX_DATABASE_VERSION_KEY "index_database_version" +#define CONTENT_DATABASE_VERSION_KEY "content_database_version" namespace UkuiSearch { //fixme: we need a better way to record index status. class IndexStatusRecorder : public QObject @@ -44,6 +46,9 @@ public: static IndexStatusRecorder *getInstance(); void setStatus(const QString& key, UkuiSearch::IndexStatusRecorder::State state); const QVariant getStatus(const QString& key); + void setVersion(const QString& key, const QString& version); + + bool versionCheck(const QString& key, const QString& version); bool indexDatabaseEnable(); bool contentIndexDatabaseEnable(); diff --git a/libsearch/index/writable-database.cpp b/libsearch/index/writable-database.cpp index efb6c7e..c52c1e2 100644 --- a/libsearch/index/writable-database.cpp +++ b/libsearch/index/writable-database.cpp @@ -24,6 +24,8 @@ #include #include "file-utils.h" #include "index-status-recorder.h" +#include "common.h" + using namespace UkuiSearch; static QMutex g_basicDatabaseMutex; @@ -96,6 +98,18 @@ void WritableDatabase::rebuild() } else { qDebug() << "Dababase rebuild, create" << m_path << database.mkpath(m_path); } + + //更新版本号 + switch (m_type) { + case DataBaseType::Basic: + IndexStatusRecorder::getInstance()->setVersion(INDEX_DATABASE_VERSION_KEY, INDEX_DATABASE_VERSION); + break; + case DataBaseType::Content: + IndexStatusRecorder::getInstance()->setVersion(CONTENT_DATABASE_VERSION_KEY, CONTENT_DATABASE_VERSION); + break; + default: + break; + } } void WritableDatabase::beginTransation() @@ -166,10 +180,10 @@ void WritableDatabase::errorRecord() { switch (m_type) { case DataBaseType::Basic: - IndexStatusRecorder::getInstance()->setStatus(INDEX_DATABASE_STATE, IndexStatusRecorder::State::Error); + IndexStatusRecorder::getInstance()->setStatus(INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Error); break; case DataBaseType::Content: - IndexStatusRecorder::getInstance()->setStatus(CONTENT_INDEX_DATABASE_STATE, IndexStatusRecorder::State::Error); + IndexStatusRecorder::getInstance()->setStatus(CONTENT_INDEX_DATABASE_STATE_KEY, IndexStatusRecorder::State::Error); break; default: break;