feat(ai-index):文件修改时使用更新接口更新索引

This commit is contained in:
iaom 2024-04-28 10:10:09 +08:00
parent 5c7a4ec1f9
commit 9843d43c0b
4 changed files with 34 additions and 5 deletions

View File

@ -151,4 +151,18 @@ bool AiIndexer::getAllIndexedFiles(QJsonObject &object)
return false; return false;
} }
bool AiIndexer::update(const QJsonArray &object)
{
if(!AiIndexerPrivate::m_sessionValid) {
return false;
}
DataManagementResult result = data_management_update_files_content(AiIndexerPrivate::m_session, QJsonDocument(object).toJson().data());
if(result != DataManagementResult::DATA_MANAGEMENT_SUCCESS) {
qWarning() << "Fail to call data_management_update_files_content, input: " << QJsonDocument(object).toJson().data()
<< "result:" << result;
return false;
}
return true;
}
} // UkuiSearch } // UkuiSearch

View File

@ -36,6 +36,7 @@ public:
bool addImageFileIndex(const QJsonArray &object); bool addImageFileIndex(const QJsonArray &object);
bool deleteFileIndex(const QStringList &files); bool deleteFileIndex(const QStringList &files);
bool getAllIndexedFiles(QJsonObject &object); bool getAllIndexedFiles(QJsonObject &object);
bool update(const QJsonArray &object);
private: private:
bool destroySession(); bool destroySession();

View File

@ -409,6 +409,7 @@ void BatchIndexer::aiIndex()
} }
QJsonObject filesNeedAiIndex; QJsonObject filesNeedAiIndex;
QJsonObject imagesNeedAiIndex; QJsonObject imagesNeedAiIndex;
QJsonObject filesNeedUpdate;
QFileInfo info; QFileInfo info;
if (m_mode == WorkMode::Add) { if (m_mode == WorkMode::Add) {
for (const QString &path: m_cache) { for (const QString &path: m_cache) {
@ -441,18 +442,28 @@ void BatchIndexer::aiIndex()
} }
QString type; QString type;
if (FileIndexerConfig::getInstance()->aiIndexFileTarget()[info.suffix()]) { if (FileIndexerConfig::getInstance()->aiIndexFileTarget()[info.suffix()]) {
if(indexTimes.value(path).toInt() != info.lastModified().toSecsSinceEpoch()) { int modifiedTime = indexTimes.value(path).toInt(0);
if(modifiedTime != info.lastModified().toSecsSinceEpoch()) {
if(indexer.checkFileSupported(path, type)) { if(indexer.checkFileSupported(path, type)) {
if(modifiedTime) {
filesNeedUpdate.insert(path, type); //修改时间不为空时执行更新
} else {
filesNeedAiIndex.insert(path, type); filesNeedAiIndex.insert(path, type);
}
indexTimes.remove(path); indexTimes.remove(path);
} }
} else { } else {
indexTimes.remove(path); indexTimes.remove(path);
} }
} else if (FileIndexerConfig::getInstance()->aiIndexImageTarget()[info.suffix()]) { } else if (FileIndexerConfig::getInstance()->aiIndexImageTarget()[info.suffix()]) {
if(indexTimes.value(path).toInt() != info.lastModified().toSecsSinceEpoch()) { int modifiedTime = indexTimes.value(path).toInt(0);
if(modifiedTime != info.lastModified().toSecsSinceEpoch()) {
if(indexer.checkFileSupported(path, type)) { if(indexer.checkFileSupported(path, type)) {
if(modifiedTime) {
filesNeedUpdate.insert(path, type); //修改时间不为空时执行更新
} else {
imagesNeedAiIndex.insert(path, type); imagesNeedAiIndex.insert(path, type);
}
indexTimes.remove(path); indexTimes.remove(path);
} }
} else { } else {
@ -467,7 +478,7 @@ void BatchIndexer::aiIndex()
} }
} }
uint allSize = filesNeedAiIndex.size() + imagesNeedAiIndex.size(); uint allSize = filesNeedAiIndex.size() + imagesNeedAiIndex.size() + filesNeedUpdate.size();
qDebug() << allSize << "files need ai index."; qDebug() << allSize << "files need ai index.";
Q_EMIT progress(IndexType::Ai, allSize, 0); Q_EMIT progress(IndexType::Ai, allSize, 0);
@ -504,5 +515,6 @@ void BatchIndexer::aiIndex()
}; };
creatIndex(&AiIndexer::addImageFileIndex, imagesNeedAiIndex); creatIndex(&AiIndexer::addImageFileIndex, imagesNeedAiIndex);
creatIndex(&AiIndexer::addTextFileIndex, filesNeedAiIndex); creatIndex(&AiIndexer::addTextFileIndex, filesNeedAiIndex);
creatIndex(&AiIndexer::update, filesNeedUpdate);
} }

View File

@ -245,6 +245,8 @@ void IndexUpdater::updateAiIndex()
qDebug() << "| remove:" <<file.path(); qDebug() << "| remove:" <<file.path();
indexer.deleteFileIndex({file.path()}); indexer.deleteFileIndex({file.path()});
} }
} else if(file.isModified()) {
update(&AiIndexer::update);
} else if(FileIndexerConfig::getInstance()->aiIndexFileTarget()[suffix] && !file.isDir()) { } else if(FileIndexerConfig::getInstance()->aiIndexFileTarget()[suffix] && !file.isDir()) {
update(&AiIndexer::addTextFileIndex); update(&AiIndexer::addTextFileIndex);
} else if (FileIndexerConfig::getInstance()->aiIndexImageTarget()[suffix] && !file.isDir()) { } else if (FileIndexerConfig::getInstance()->aiIndexImageTarget()[suffix] && !file.isDir()) {