131 lines
4.6 KiB
C++
131 lines
4.6 KiB
C++
/*
|
|
* Copyright (C) 2022, KylinSoft Co., Ltd.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*
|
|
* Authors: iaom <zhangpengfei@kylinos.cn>
|
|
*
|
|
*/
|
|
#include "index-updater.h"
|
|
#include <malloc.h>
|
|
#include "writable-database.h"
|
|
#include "basic-indexer.h"
|
|
#include "file-indexer-config.h"
|
|
#include "file-content-indexer.h"
|
|
#include "common.h"
|
|
#include "file-utils.h"
|
|
using namespace UkuiSearch;
|
|
IndexUpdater::IndexUpdater(const QVector<PendingFile>& files, QAtomicInt &stop)
|
|
: m_cache(files),
|
|
m_stop(&stop)
|
|
{
|
|
}
|
|
void IndexUpdater::UpdateIndex()
|
|
{
|
|
//fix me: How should I delete metadata of files below a folder
|
|
//that has been deleted(When a file watcher signal comes which only contains folder info)?
|
|
if(FileIndexerConfig::getInstance()->isFileIndexEnable()) {
|
|
WritableDatabase basicDb(DataBaseType::Basic);
|
|
if(!basicDb.open()) {
|
|
qWarning() << "Basic db open failed, fail to update index";
|
|
return;
|
|
}
|
|
qDebug() << "===update basic index===";
|
|
for(PendingFile file : m_cache) {
|
|
if(file.shouldRemoveIndex()) {
|
|
qDebug() << "| remove:" <<file.path();
|
|
basicDb.removeDocument(file.path());
|
|
if(file.isDir()) {
|
|
basicDb.removeChildrenDocument(file.path());
|
|
}
|
|
} else {
|
|
qDebug() << "| index:" <<file.path();
|
|
BasicIndexer indexer(file.path());
|
|
if(indexer.index()) {
|
|
basicDb.addDocument(indexer.document());
|
|
}
|
|
}
|
|
|
|
}
|
|
basicDb.commit();
|
|
qDebug() << "===finish update basic index===";
|
|
}
|
|
if(FileIndexerConfig::getInstance()->isContentIndexEnable()) {
|
|
if(m_stop->load()) {
|
|
qDebug() << "Index stopped, abort update content index.";
|
|
return;
|
|
}
|
|
WritableDatabase contentDb(DataBaseType::Content);
|
|
if(!contentDb.open()) {
|
|
qWarning() << "Content db open failed, fail to update index";
|
|
return;
|
|
}
|
|
|
|
QMap<QString, bool> suffixMap = targetFileTypeMap;
|
|
//ocr
|
|
if(FileIndexerConfig::getInstance()->isOCREnable()) {
|
|
suffixMap.unite(targetPhotographTypeMap);
|
|
}
|
|
qDebug() << "===update content index===";
|
|
int size = 0;
|
|
for(PendingFile file : m_cache) {
|
|
QString suffix = QFileInfo(file.path()).suffix();
|
|
if(file.shouldRemoveIndex()) {
|
|
qDebug() << "| remove:" <<file.path();
|
|
if(file.isDir()) {
|
|
contentDb.removeChildrenDocument(file.path());
|
|
} else if(true == suffixMap[suffix]) {
|
|
contentDb.removeDocument(file.path());
|
|
}
|
|
} else if(true == suffixMap[suffix] && !file.isDir()) {
|
|
if(FileUtils::isEncrypedOrUnsupport(file.path(), suffix) && file.isModified()) {
|
|
contentDb.removeDocument(file.path());
|
|
continue;
|
|
}
|
|
qDebug() << "| index:" <<file.path();
|
|
fileContentIndexer indexer(file.path());
|
|
if(indexer.index()) {
|
|
contentDb.addDocument(indexer.document());
|
|
++size;
|
|
} else if(file.isModified()){
|
|
contentDb.removeDocument(file.path());
|
|
}
|
|
}
|
|
if(size >= 30) {
|
|
contentDb.commit();
|
|
qDebug() << "30 finished.";
|
|
size = 0;
|
|
}
|
|
if(m_stop->load()) {
|
|
qDebug() << "Index stopped, content index update interrupted";
|
|
m_cache.clear();
|
|
m_cache.shrink_to_fit();
|
|
malloc_trim(0);
|
|
return;
|
|
}
|
|
}
|
|
contentDb.commit();
|
|
qDebug() << "===finish update content index===";
|
|
}
|
|
m_cache.clear();
|
|
m_cache.shrink_to_fit();
|
|
malloc_trim(0);
|
|
Q_EMIT done();
|
|
}
|
|
|
|
void IndexUpdater::run()
|
|
{
|
|
UpdateIndex();
|
|
}
|