fix(ukui-search-service):修复索引状态监控导致的崩溃问题

This commit is contained in:
iaom 2023-10-09 10:27:29 +08:00 committed by Baijunjie
parent 524a43787b
commit 7b158ec772
6 changed files with 100 additions and 49 deletions

View File

@ -18,23 +18,12 @@
* Authors: iaom <zhangpengfei@kylinos.cn> * Authors: iaom <zhangpengfei@kylinos.cn>
*/ */
#include "database.h" #include "database.h"
#include <QDir>
using namespace UkuiSearch; using namespace UkuiSearch;
Database::Database(const DataBaseType &type) Database::Database(const DataBaseType &type): m_type(type)
{ {
switch (type) { refresh();
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;
}
} }
Database::~Database() Database::~Database()
@ -45,7 +34,43 @@ Database::~Database()
} }
} }
uint Database::getIndexDocCount() const uint Database::getIndexDocCount()
{ {
if(!valid()) {
refresh();
}
if(m_database) {
return m_database->get_doccount(); 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;
} }

View File

@ -28,10 +28,14 @@ class Database
public: public:
explicit Database(const DataBaseType &type); explicit Database(const DataBaseType &type);
~Database(); ~Database();
bool valid() const;
bool refresh();
uint getIndexDocCount();
uint getIndexDocCount() const;
private: private:
Xapian::Database *m_database; Xapian::Database *m_database = nullptr;
DataBaseType m_type;
}; };
} }
#endif // DATABASE_H #endif // DATABASE_H

View File

@ -51,7 +51,7 @@ target_include_directories(ukui-search-service PRIVATE
target_compile_definitions(ukui-search-service PRIVATE target_compile_definitions(ukui-search-service PRIVATE
QT_DEPRECATED_WARNINGS QT_DEPRECATED_WARNINGS
VERSION="UKUI_SEARCH_SERVICE_VERSION" VERSION="${UKUI_SEARCH_SERVICE_VERSION}"
QT_NO_KEYWORDS QT_NO_KEYWORDS
) )

View File

@ -32,10 +32,12 @@ Monitor::Monitor(IndexScheduler *scheduler, QObject *parent) : MonitorSource(par
connect(scheduler, &IndexScheduler::basicIndexDone, this, &Monitor::basicIndexDone); connect(scheduler, &IndexScheduler::basicIndexDone, this, &Monitor::basicIndexDone);
connect(scheduler, &IndexScheduler::contentIndexDone, this, &Monitor::contentIndexDone); connect(scheduler, &IndexScheduler::contentIndexDone, this, &Monitor::contentIndexDone);
connect(FileIndexerConfig::getInstance(), &FileIndexerConfig::appendIndexDir, this, [&](){ 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, [&](){ 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 uint Monitor::basicIndexDocNum() const
{ {
return m_basicDatabase.getIndexDocCount(); return m_basicIndexDocNum;
} }
uint Monitor::contentIndexDocNum() const uint Monitor::contentIndexDocNum() const
{ {
return m_contentDatabase.getIndexDocCount(); return m_contentIndexDocNum;
} }
uint Monitor::ocrContentIndexDocNum() const uint Monitor::ocrContentIndexDocNum() const
{ {
return m_ocrContentDatabase.getIndexDocCount(); return m_ocrContentIndexDocNum;
} }
void Monitor::onIndexStateChanged(IndexScheduler::IndexerState state) void Monitor::onIndexStateChanged(IndexScheduler::IndexerState state)
{ {
if(state == IndexScheduler::IndexerState::Idle) { if(state == IndexScheduler::IndexerState::Idle || state == IndexScheduler::IndexerState::Stop) {
Q_EMIT basicIndexDocNumChanged(m_basicDatabase.getIndexDocCount()); m_basicIndexDocNum = m_basicDatabase.getIndexDocCount();
Q_EMIT contentIndexDocNumChanged(m_contentDatabase.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<IndexScheduler::IndexerState>(); QMetaEnum metaEnum = QMetaEnum::fromType<IndexScheduler::IndexerState>();
Q_EMIT indexStateChanged(QString::fromLocal8Bit(metaEnum.valueToKey(state))); Q_EMIT indexStateChanged(QString::fromLocal8Bit(metaEnum.valueToKey(state)));
@ -113,18 +119,24 @@ void Monitor::processUpdate(IndexType type, uint all, uint finished)
Q_EMIT basicIndexSizeChanged(m_basicIndexSize); Q_EMIT basicIndexSizeChanged(m_basicIndexSize);
m_basicIndexProgress = finished; m_basicIndexProgress = finished;
Q_EMIT basicIndexProgressChanged(m_basicIndexProgress); Q_EMIT basicIndexProgressChanged(m_basicIndexProgress);
m_basicIndexDocNum = m_basicDatabase.getIndexDocCount();
Q_EMIT basicIndexDocNumChanged(m_basicIndexDocNum);
break; break;
case IndexType::Contents: case IndexType::Contents:
m_contentIndexSize = all; m_contentIndexSize = all;
Q_EMIT contentIndexSizeChanged(m_contentIndexSize); Q_EMIT contentIndexSizeChanged(m_contentIndexSize);
m_contentIndexProgress = finished; m_contentIndexProgress = finished;
Q_EMIT contentIndexProgressChanged(m_contentIndexProgress); Q_EMIT contentIndexProgressChanged(m_contentIndexProgress);
m_contentIndexDocNum = m_contentDatabase.getIndexDocCount();
Q_EMIT contentIndexDocNumChanged(m_contentIndexDocNum);
break; break;
case IndexType::OCR: case IndexType::OCR:
m_ocrIndexSize = all; m_ocrIndexSize = all;
Q_EMIT ocrIndexSizeChanged(m_ocrIndexSize); Q_EMIT ocrIndexSizeChanged(m_ocrIndexSize);
m_contentIndexProgress = finished; m_contentIndexProgress = finished;
Q_EMIT ocrIndexProgressChanged(m_contentIndexProgress); Q_EMIT ocrIndexProgressChanged(m_contentIndexProgress);
m_ocrContentIndexDocNum = m_ocrContentDatabase.getIndexDocCount();
Q_EMIT ocrContentIndexDocNumChanged(m_ocrContentDatabase.getIndexDocCount());
break; break;
default: default:
break; break;

View File

@ -103,12 +103,17 @@ Q_SIGNALS:
*/ */
void contentIndexDone(); void contentIndexDone();
void basicIndexDocNumChanged(uint);
void contentIndexDocNumChanged(uint);
void ocrContentIndexDocNumChanged(uint);
private Q_SLOTS: private Q_SLOTS:
void onIndexStateChanged(IndexScheduler::IndexerState); void onIndexStateChanged(IndexScheduler::IndexerState);
void processUpdate(UkuiSearch::IndexType type, uint all, uint finished); void processUpdate(UkuiSearch::IndexType type, uint all, uint finished);
private: private:
IndexScheduler *m_scheduler = nullptr; IndexScheduler *m_scheduler = nullptr;
QStringList m_currentIndexPaths;
Database m_basicDatabase; Database m_basicDatabase;
Database m_contentDatabase; Database m_contentDatabase;
Database m_ocrContentDatabase; Database m_ocrContentDatabase;
@ -118,6 +123,9 @@ private:
uint m_basicIndexProgress = 0; uint m_basicIndexProgress = 0;
uint m_contentIndexProgress = 0; uint m_contentIndexProgress = 0;
uint m_ocrIndexProgress = 0; uint m_ocrIndexProgress = 0;
uint m_basicIndexDocNum = 0;
uint m_contentIndexDocNum = 0;
uint m_ocrContentIndexDocNum = 0;
}; };
} }
#endif // MONITOR_H #endif // MONITOR_H

View File

@ -23,6 +23,7 @@
#include <utility> #include <utility>
#include <stdio.h> #include <stdio.h>
#include <QVariant>
#include "ukui-search-service.h" #include "ukui-search-service.h"
#include "dir-watcher.h" #include "dir-watcher.h"
#include "file-utils.h" #include "file-utils.h"
@ -125,6 +126,7 @@ void UkuiSearchService::showMonitorState()
{ {
m_qroNode.connectToNode(QUrl(QStringLiteral("local:ukui-search-service-monitor"))); m_qroNode.connectToNode(QUrl(QStringLiteral("local:ukui-search-service-monitor")));
auto *m = m_qroNode.acquire<MonitorReplica>(); auto *m = m_qroNode.acquire<MonitorReplica>();
connect(m, &QRemoteObjectReplica::initialized, [&, m](){ connect(m, &QRemoteObjectReplica::initialized, [&, m](){
qDebug() << "QRemoteObjectReplica initialized"; qDebug() << "QRemoteObjectReplica initialized";
QString state = m->indexState(); QString state = m->indexState();
@ -137,8 +139,8 @@ void UkuiSearchService::showMonitorState()
} else { } else {
message += "Basic index progress: " + QString::number(m->basicIndexProgress()) + " of " + QString::number(m->basicIndexSize())+ " finished\n" 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" + "Content index progress: " + 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"; + "Ocr content index progress: " + QString::number(m->ocrIndexProgress()) + " of " + QString::number(m->ocrIndexSize())+ " finished\n";
} }
delete m; delete m;