From bd18f13567f04676f13a3c0c30b11f8ff5da277d Mon Sep 17 00:00:00 2001 From: iaom Date: Sat, 20 Apr 2024 15:53:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(ai-index):=E4=BF=AE=E5=A4=8D=E6=9B=B4?= =?UTF-8?q?=E6=96=B0ai=E7=B4=A2=E5=BC=95=E6=97=B6=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E9=94=99=E8=AF=AF=EF=BC=8C=E4=BC=98=E5=8C=96=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增量更新和实时更新将共用一个session,避免多次创建session --- libsearch/index/ai-indexer.cpp | 47 +++++++++++++++++-------------- libsearch/index/index-updater.cpp | 2 +- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/libsearch/index/ai-indexer.cpp b/libsearch/index/ai-indexer.cpp index 8de52f2..17d156e 100644 --- a/libsearch/index/ai-indexer.cpp +++ b/libsearch/index/ai-indexer.cpp @@ -31,9 +31,11 @@ class AiIndexerPrivate { public: QMutex m_mutex; - bool m_sessionValid = false; - DataManagementSession m_session = nullptr; + static bool m_sessionValid; + static DataManagementSession m_session; }; +DataManagementSession AiIndexerPrivate::m_session = nullptr; +bool AiIndexerPrivate::m_sessionValid = false; AiIndexer::AiIndexer(QObject *parent) : QObject(parent), d(new AiIndexerPrivate) { d->m_mutex.lock(); @@ -41,7 +43,6 @@ AiIndexer::AiIndexer(QObject *parent) : QObject(parent), d(new AiIndexerPrivate) AiIndexer::~AiIndexer() { - destroySession(); d->m_mutex.unlock(); if(d) { delete d; @@ -51,30 +52,34 @@ AiIndexer::~AiIndexer() bool AiIndexer::creatSession() { - DataManagementResult result = data_management_create_session(&d->m_session, DataManagementType::SYS_SEARCH, getuid()); - qDebug() << "===creatSession===" <m_sessionValid = true; + if(!AiIndexerPrivate::m_sessionValid) { + DataManagementResult result = data_management_create_session(&AiIndexerPrivate::m_session, DataManagementType::SYS_SEARCH, getuid()); + qDebug() << "===creatSession===" <m_sessionValid = false; - return data_management_destroy_session(d->m_session) == DataManagementResult::DATA_MANAGEMENT_SUCCESS; + AiIndexerPrivate::m_sessionValid = false; + return data_management_destroy_session(AiIndexerPrivate::m_session) == DataManagementResult::DATA_MANAGEMENT_SUCCESS; } bool AiIndexer::checkFileSupported(const QString &filePath, QString &result) { - if(!d->m_sessionValid) { + if(!AiIndexerPrivate::m_sessionValid) { return false; } char *originalResult; - if(data_management_check_file_format(d->m_session, filePath.toLocal8Bit().data(), &originalResult) == DataManagementResult::DATA_MANAGEMENT_SUCCESS) { + if(data_management_check_file_format(AiIndexerPrivate::m_session, filePath.toLocal8Bit().data(), &originalResult) == DataManagementResult::DATA_MANAGEMENT_SUCCESS) { QJsonObject jo = QJsonDocument::fromJson(QByteArray(originalResult)).object(); - data_management_free_check_file_format_result(d->m_session, originalResult); + data_management_free_check_file_format_result(AiIndexerPrivate::m_session, originalResult); if(jo.value(QStringLiteral("supported")).toBool()) { result = jo.value(QStringLiteral("format")).toString(); return true; @@ -87,23 +92,23 @@ bool AiIndexer::checkFileSupported(const QString &filePath, QString &result) bool AiIndexer::addTextFileIndex(const QJsonArray &object) { - if(!d->m_sessionValid) { + if(!AiIndexerPrivate::m_sessionValid) { return false; } - return data_management_add_text_files(d->m_session, QJsonDocument(object).toJson().data()) == DataManagementResult::DATA_MANAGEMENT_SUCCESS; + return data_management_add_text_files(AiIndexerPrivate::m_session, QJsonDocument(object).toJson().data()) == DataManagementResult::DATA_MANAGEMENT_SUCCESS; } bool AiIndexer::addImageFileIndex(const QJsonArray &object) { - if(!d->m_sessionValid) { + if(!AiIndexerPrivate::m_sessionValid) { return false; } - return data_management_add_image_files(d->m_session, QJsonDocument(object).toJson().data()) == DataManagementResult::DATA_MANAGEMENT_SUCCESS; + return data_management_add_image_files(AiIndexerPrivate::m_session, QJsonDocument(object).toJson().data()) == DataManagementResult::DATA_MANAGEMENT_SUCCESS; } bool AiIndexer::deleteFileIndex(const QStringList &files) { - if(!d->m_sessionValid) { + if(!AiIndexerPrivate::m_sessionValid) { return false; } QJsonArray tmpArray; @@ -114,16 +119,16 @@ bool AiIndexer::deleteFileIndex(const QStringList &files) tmpArray.append(oneFile); } - return data_management_delete_files(d->m_session, QJsonDocument(tmpArray).toJson().data()) == DataManagementResult::DATA_MANAGEMENT_SUCCESS; + return data_management_delete_files(AiIndexerPrivate::m_session, QJsonDocument(tmpArray).toJson().data()) == DataManagementResult::DATA_MANAGEMENT_SUCCESS; } bool AiIndexer::getAllIndexedFiles(QJsonObject &object) { - if(!d->m_sessionValid) { + if(!AiIndexerPrivate::m_sessionValid) { return false; } char *originalResult; - DataManagementResult result = data_management_get_all_fileinfos(d->m_session, &originalResult); + DataManagementResult result = data_management_get_all_fileinfos(AiIndexerPrivate::m_session, &originalResult); if(result == DataManagementResult::DATA_MANAGEMENT_SUCCESS) { QJsonArray array = QJsonDocument::fromJson(QByteArray(originalResult)).array(); for(const QJsonValueRef value : array) { diff --git a/libsearch/index/index-updater.cpp b/libsearch/index/index-updater.cpp index 39dd1ce..cc9f6a5 100644 --- a/libsearch/index/index-updater.cpp +++ b/libsearch/index/index-updater.cpp @@ -230,7 +230,7 @@ void IndexUpdater::updateAiIndex() return; } qDebug() << "| index:" <