From 7b158ec772c752ac87c8b43fbc970fbeeb7d432f Mon Sep 17 00:00:00 2001 From: iaom Date: Mon, 9 Oct 2023 10:27:29 +0800 Subject: [PATCH] =?UTF-8?q?fix(ukui-search-service):=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E7=8A=B6=E6=80=81=E7=9B=91=E6=8E=A7=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=E5=B4=A9=E6=BA=83=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libsearch/index/database.cpp | 57 ++++++++++++----- libsearch/index/database.h | 8 ++- ukui-search-service/CMakeLists.txt | 2 +- ukui-search-service/monitor.cpp | 68 ++++++++++++--------- ukui-search-service/monitor.h | 8 +++ ukui-search-service/ukui-search-service.cpp | 6 +- 6 files changed, 100 insertions(+), 49 deletions(-) diff --git a/libsearch/index/database.cpp b/libsearch/index/database.cpp index d80d464..958e386 100644 --- a/libsearch/index/database.cpp +++ b/libsearch/index/database.cpp @@ -18,23 +18,12 @@ * Authors: iaom */ #include "database.h" +#include using namespace UkuiSearch; -Database::Database(const DataBaseType &type) +Database::Database(const DataBaseType &type): m_type(type) { - switch (type) { - case DataBaseType::Basic: - m_database = new Xapian::Database(INDEX_PATH.toStdString()); - break; - case DataBaseType::Content: - m_database = new Xapian::Database(CONTENT_INDEX_PATH.toStdString()); - break; - case DataBaseType::OcrContent: - m_database = new Xapian::Database(OCR_CONTENT_INDEX_PATH.toStdString()); - break; - default: - break; - } + refresh(); } Database::~Database() @@ -45,7 +34,43 @@ Database::~Database() } } -uint Database::getIndexDocCount() const +uint Database::getIndexDocCount() { - return m_database->get_doccount(); + if(!valid()) { + refresh(); + } + if(m_database) { + return m_database->get_doccount(); + } else { + return 0; + } +} + +bool Database::valid() const +{ + return m_database != nullptr; +} + +bool Database::refresh() +{ + switch (m_type) { + case DataBaseType::Basic: + if(QDir(INDEX_PATH).exists()) { + m_database = new Xapian::Database(INDEX_PATH.toStdString()); + break; + } + case DataBaseType::Content: + if(QDir(CONTENT_INDEX_PATH).exists()) { + m_database = new Xapian::Database(CONTENT_INDEX_PATH.toStdString()); + break; + } + case DataBaseType::OcrContent: + if(QDir(OCR_CONTENT_INDEX_PATH).exists()) { + m_database = new Xapian::Database(OCR_CONTENT_INDEX_PATH.toStdString()); + break; + } + default: + break; + } + return m_database != nullptr; } diff --git a/libsearch/index/database.h b/libsearch/index/database.h index 99fe153..8953b17 100644 --- a/libsearch/index/database.h +++ b/libsearch/index/database.h @@ -28,10 +28,14 @@ class Database public: explicit Database(const DataBaseType &type); ~Database(); + bool valid() const; + bool refresh(); + + uint getIndexDocCount(); - uint getIndexDocCount() const; private: - Xapian::Database *m_database; + Xapian::Database *m_database = nullptr; + DataBaseType m_type; }; } #endif // DATABASE_H diff --git a/ukui-search-service/CMakeLists.txt b/ukui-search-service/CMakeLists.txt index c4e7528..fbc94ad 100644 --- a/ukui-search-service/CMakeLists.txt +++ b/ukui-search-service/CMakeLists.txt @@ -51,7 +51,7 @@ target_include_directories(ukui-search-service PRIVATE target_compile_definitions(ukui-search-service PRIVATE QT_DEPRECATED_WARNINGS - VERSION="UKUI_SEARCH_SERVICE_VERSION" + VERSION="${UKUI_SEARCH_SERVICE_VERSION}" QT_NO_KEYWORDS ) diff --git a/ukui-search-service/monitor.cpp b/ukui-search-service/monitor.cpp index 6118f5e..79d1939 100644 --- a/ukui-search-service/monitor.cpp +++ b/ukui-search-service/monitor.cpp @@ -32,10 +32,12 @@ Monitor::Monitor(IndexScheduler *scheduler, QObject *parent) : MonitorSource(par connect(scheduler, &IndexScheduler::basicIndexDone, this, &Monitor::basicIndexDone); connect(scheduler, &IndexScheduler::contentIndexDone, this, &Monitor::contentIndexDone); connect(FileIndexerConfig::getInstance(), &FileIndexerConfig::appendIndexDir, this, [&](){ - Q_EMIT currentIndexPathsChanged(FileIndexerConfig::getInstance()->currentIndexableDir()); + m_currentIndexPaths = FileIndexerConfig::getInstance()->currentIndexableDir(); + Q_EMIT currentIndexPathsChanged(m_currentIndexPaths); }); connect(FileIndexerConfig::getInstance(), &FileIndexerConfig::removeIndexDir, this, [&](){ - Q_EMIT currentIndexPathsChanged(FileIndexerConfig::getInstance()->currentIndexableDir()); + m_currentIndexPaths = FileIndexerConfig::getInstance()->currentIndexableDir(); + Q_EMIT currentIndexPathsChanged(m_currentIndexPaths); }); } @@ -82,24 +84,28 @@ uint Monitor::ocrIndexProgress() const uint Monitor::basicIndexDocNum() const { - return m_basicDatabase.getIndexDocCount(); + return m_basicIndexDocNum; } uint Monitor::contentIndexDocNum() const { - return m_contentDatabase.getIndexDocCount(); + return m_contentIndexDocNum; } uint Monitor::ocrContentIndexDocNum() const { - return m_ocrContentDatabase.getIndexDocCount(); + return m_ocrContentIndexDocNum; } void Monitor::onIndexStateChanged(IndexScheduler::IndexerState state) { - if(state == IndexScheduler::IndexerState::Idle) { - Q_EMIT basicIndexDocNumChanged(m_basicDatabase.getIndexDocCount()); - Q_EMIT contentIndexDocNumChanged(m_contentDatabase.getIndexDocCount()); + if(state == IndexScheduler::IndexerState::Idle || state == IndexScheduler::IndexerState::Stop) { + m_basicIndexDocNum = m_basicDatabase.getIndexDocCount(); + Q_EMIT basicIndexDocNumChanged(m_basicIndexDocNum); + m_contentIndexDocNum = m_contentDatabase.getIndexDocCount(); + Q_EMIT contentIndexDocNumChanged(m_contentIndexDocNum); + m_ocrContentIndexDocNum = m_ocrContentDatabase.getIndexDocCount(); + Q_EMIT ocrContentIndexDocNumChanged(m_ocrContentDatabase.getIndexDocCount()); } QMetaEnum metaEnum = QMetaEnum::fromType(); Q_EMIT indexStateChanged(QString::fromLocal8Bit(metaEnum.valueToKey(state))); @@ -108,25 +114,31 @@ void Monitor::onIndexStateChanged(IndexScheduler::IndexerState state) void Monitor::processUpdate(IndexType type, uint all, uint finished) { switch (type) { - case IndexType::Basic: - m_basicIndexSize = all; - Q_EMIT basicIndexSizeChanged(m_basicIndexSize); - m_basicIndexProgress = finished; - Q_EMIT basicIndexProgressChanged(m_basicIndexProgress); - break; - case IndexType::Contents: - m_contentIndexSize = all; - Q_EMIT contentIndexSizeChanged(m_contentIndexSize); - m_contentIndexProgress = finished; - Q_EMIT contentIndexProgressChanged(m_contentIndexProgress); - break; - case IndexType::OCR: - m_ocrIndexSize = all; - Q_EMIT ocrIndexSizeChanged(m_ocrIndexSize); - m_contentIndexProgress = finished; - Q_EMIT ocrIndexProgressChanged(m_contentIndexProgress); - break; - default: - break; + case IndexType::Basic: + m_basicIndexSize = all; + Q_EMIT basicIndexSizeChanged(m_basicIndexSize); + m_basicIndexProgress = finished; + Q_EMIT basicIndexProgressChanged(m_basicIndexProgress); + m_basicIndexDocNum = m_basicDatabase.getIndexDocCount(); + Q_EMIT basicIndexDocNumChanged(m_basicIndexDocNum); + break; + case IndexType::Contents: + m_contentIndexSize = all; + Q_EMIT contentIndexSizeChanged(m_contentIndexSize); + m_contentIndexProgress = finished; + Q_EMIT contentIndexProgressChanged(m_contentIndexProgress); + m_contentIndexDocNum = m_contentDatabase.getIndexDocCount(); + Q_EMIT contentIndexDocNumChanged(m_contentIndexDocNum); + break; + case IndexType::OCR: + m_ocrIndexSize = all; + Q_EMIT ocrIndexSizeChanged(m_ocrIndexSize); + m_contentIndexProgress = finished; + Q_EMIT ocrIndexProgressChanged(m_contentIndexProgress); + m_ocrContentIndexDocNum = m_ocrContentDatabase.getIndexDocCount(); + Q_EMIT ocrContentIndexDocNumChanged(m_ocrContentDatabase.getIndexDocCount()); + break; + default: + break; } } diff --git a/ukui-search-service/monitor.h b/ukui-search-service/monitor.h index 13d7cd8..df338eb 100644 --- a/ukui-search-service/monitor.h +++ b/ukui-search-service/monitor.h @@ -103,12 +103,17 @@ Q_SIGNALS: */ void contentIndexDone(); + void basicIndexDocNumChanged(uint); + void contentIndexDocNumChanged(uint); + void ocrContentIndexDocNumChanged(uint); + private Q_SLOTS: void onIndexStateChanged(IndexScheduler::IndexerState); void processUpdate(UkuiSearch::IndexType type, uint all, uint finished); private: IndexScheduler *m_scheduler = nullptr; + QStringList m_currentIndexPaths; Database m_basicDatabase; Database m_contentDatabase; Database m_ocrContentDatabase; @@ -118,6 +123,9 @@ private: uint m_basicIndexProgress = 0; uint m_contentIndexProgress = 0; uint m_ocrIndexProgress = 0; + uint m_basicIndexDocNum = 0; + uint m_contentIndexDocNum = 0; + uint m_ocrContentIndexDocNum = 0; }; } #endif // MONITOR_H diff --git a/ukui-search-service/ukui-search-service.cpp b/ukui-search-service/ukui-search-service.cpp index 6dc167c..e968de8 100644 --- a/ukui-search-service/ukui-search-service.cpp +++ b/ukui-search-service/ukui-search-service.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "ukui-search-service.h" #include "dir-watcher.h" #include "file-utils.h" @@ -125,6 +126,7 @@ void UkuiSearchService::showMonitorState() { m_qroNode.connectToNode(QUrl(QStringLiteral("local:ukui-search-service-monitor"))); auto *m = m_qroNode.acquire(); + connect(m, &QRemoteObjectReplica::initialized, [&, m](){ qDebug() << "QRemoteObjectReplica initialized"; QString state = m->indexState(); @@ -137,8 +139,8 @@ void UkuiSearchService::showMonitorState() } else { message += "Basic index progress: " + QString::number(m->basicIndexProgress()) + " of " + QString::number(m->basicIndexSize())+ " finished\n" - + "Content index size: " + QString::number(m->contentIndexProgress()) + " of " + QString::number(m->contentIndexSize())+ " finished\n" - + "Ocr content index size: " + QString::number(m->ocrIndexProgress()) + " of " + QString::number(m->ocrIndexSize())+ " finished\n"; + + "Content index progress: " + QString::number(m->contentIndexProgress()) + " of " + QString::number(m->contentIndexSize())+ " finished\n" + + "Ocr content index progress: " + QString::number(m->ocrIndexProgress()) + " of " + QString::number(m->ocrIndexSize())+ " finished\n"; } delete m;