fix(ai-index):修复更新ai索引时的逻辑错误,优化更新速度

增量更新和实时更新将共用一个session,避免多次创建session
This commit is contained in:
iaom 2024-04-20 15:53:01 +08:00
parent 81ab29bb64
commit 42d3242553
2 changed files with 27 additions and 22 deletions

View File

@ -31,9 +31,11 @@ class AiIndexerPrivate
{ {
public: public:
QMutex m_mutex; QMutex m_mutex;
bool m_sessionValid = false; static bool m_sessionValid;
DataManagementSession m_session = nullptr; static DataManagementSession m_session;
}; };
DataManagementSession AiIndexerPrivate::m_session = nullptr;
bool AiIndexerPrivate::m_sessionValid = false;
AiIndexer::AiIndexer(QObject *parent) : QObject(parent), d(new AiIndexerPrivate) AiIndexer::AiIndexer(QObject *parent) : QObject(parent), d(new AiIndexerPrivate)
{ {
d->m_mutex.lock(); d->m_mutex.lock();
@ -41,7 +43,6 @@ AiIndexer::AiIndexer(QObject *parent) : QObject(parent), d(new AiIndexerPrivate)
AiIndexer::~AiIndexer() AiIndexer::~AiIndexer()
{ {
destroySession();
d->m_mutex.unlock(); d->m_mutex.unlock();
if(d) { if(d) {
delete d; delete d;
@ -51,30 +52,34 @@ AiIndexer::~AiIndexer()
bool AiIndexer::creatSession() bool AiIndexer::creatSession()
{ {
DataManagementResult result = data_management_create_session(&d->m_session, DataManagementType::SYS_SEARCH, getuid()); if(!AiIndexerPrivate::m_sessionValid) {
DataManagementResult result = data_management_create_session(&AiIndexerPrivate::m_session, DataManagementType::SYS_SEARCH, getuid());
qDebug() << "===creatSession===" <<result; qDebug() << "===creatSession===" <<result;
if(result == DataManagementResult::DATA_MANAGEMENT_SUCCESS) { if(result == DataManagementResult::DATA_MANAGEMENT_SUCCESS) {
d->m_sessionValid = true; AiIndexerPrivate::m_sessionValid = true;
return true; return true;
} }
return false; return false;
} else {
return true;
}
} }
bool AiIndexer::destroySession() bool AiIndexer::destroySession()
{ {
d->m_sessionValid = false; AiIndexerPrivate::m_sessionValid = false;
return data_management_destroy_session(d->m_session) == DataManagementResult::DATA_MANAGEMENT_SUCCESS; return data_management_destroy_session(AiIndexerPrivate::m_session) == DataManagementResult::DATA_MANAGEMENT_SUCCESS;
} }
bool AiIndexer::checkFileSupported(const QString &filePath, QString &result) bool AiIndexer::checkFileSupported(const QString &filePath, QString &result)
{ {
if(!d->m_sessionValid) { if(!AiIndexerPrivate::m_sessionValid) {
return false; return false;
} }
char *originalResult; 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(); 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()) { if(jo.value(QStringLiteral("supported")).toBool()) {
result = jo.value(QStringLiteral("format")).toString(); result = jo.value(QStringLiteral("format")).toString();
return true; return true;
@ -87,23 +92,23 @@ bool AiIndexer::checkFileSupported(const QString &filePath, QString &result)
bool AiIndexer::addTextFileIndex(const QJsonArray &object) bool AiIndexer::addTextFileIndex(const QJsonArray &object)
{ {
if(!d->m_sessionValid) { if(!AiIndexerPrivate::m_sessionValid) {
return false; 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) bool AiIndexer::addImageFileIndex(const QJsonArray &object)
{ {
if(!d->m_sessionValid) { if(!AiIndexerPrivate::m_sessionValid) {
return false; 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) bool AiIndexer::deleteFileIndex(const QStringList &files)
{ {
if(!d->m_sessionValid) { if(!AiIndexerPrivate::m_sessionValid) {
return false; return false;
} }
QJsonArray tmpArray; QJsonArray tmpArray;
@ -114,16 +119,16 @@ bool AiIndexer::deleteFileIndex(const QStringList &files)
tmpArray.append(oneFile); 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) bool AiIndexer::getAllIndexedFiles(QJsonObject &object)
{ {
if(!d->m_sessionValid) { if(!AiIndexerPrivate::m_sessionValid) {
return false; return false;
} }
char *originalResult; 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) { if(result == DataManagementResult::DATA_MANAGEMENT_SUCCESS) {
QJsonArray array = QJsonDocument::fromJson(QByteArray(originalResult)).array(); QJsonArray array = QJsonDocument::fromJson(QByteArray(originalResult)).array();
for(const QJsonValueRef value : array) { for(const QJsonValueRef value : array) {

View File

@ -230,7 +230,7 @@ void IndexUpdater::updateAiIndex()
return; return;
} }
qDebug() << "| index:" <<file.path(); qDebug() << "| index:" <<file.path();
if(indexer.addTextFileIndex(QJsonArray{ if((indexer.*creat)(QJsonArray{
QJsonObject{ QJsonObject{
{QStringLiteral("filepath"), file.path()}, {QStringLiteral("filepath"), file.path()},
{QStringLiteral("filetype"), type} {QStringLiteral("filetype"), type}