2021-06-10 20:43:57 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2021, 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: zhangpengfei <zhangpengfei@kylinos.cn>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "pending-file-queue.h"
|
2021-07-06 16:53:32 +08:00
|
|
|
#include "file-utils.h"
|
2021-06-10 20:43:57 +08:00
|
|
|
#include <malloc.h>
|
2021-12-14 14:43:35 +08:00
|
|
|
using namespace UkuiSearch;
|
2021-06-10 20:43:57 +08:00
|
|
|
static PendingFileQueue *global_instance_pending_file_queue = nullptr;
|
2022-03-15 13:53:38 +08:00
|
|
|
PendingFileQueue::PendingFileQueue(QObject *parent) : QThread(parent), m_semaphore(INDEX_SEM, 0, QSystemSemaphore::AccessMode::Open)
|
2021-06-10 20:43:57 +08:00
|
|
|
{
|
|
|
|
this->start();
|
|
|
|
|
|
|
|
m_cacheTimer = new QTimer;
|
|
|
|
m_minProcessTimer = new QTimer;
|
|
|
|
|
|
|
|
m_cacheTimer->setInterval(10*1000);
|
|
|
|
m_cacheTimer->setSingleShot(true);
|
|
|
|
m_minProcessTimer->setInterval(500);
|
|
|
|
m_minProcessTimer->setSingleShot(true);
|
|
|
|
|
|
|
|
m_cacheTimer->moveToThread(this);
|
|
|
|
m_minProcessTimer->moveToThread(this);
|
|
|
|
|
|
|
|
// connect(this, &PendingFileQueue::cacheTimerStart, m_cacheTimer, f, Qt::DirectConnection);
|
|
|
|
// connect(this, &PendingFileQueue::minProcessTimerStart, m_minProcessTimer, f,Qt::DirectConnection);
|
|
|
|
connect(this, SIGNAL(cacheTimerStart()), m_cacheTimer, SLOT(start()));
|
|
|
|
connect(this, SIGNAL(minProcessTimerStart()), m_minProcessTimer, SLOT(start()));
|
2021-06-11 19:58:32 +08:00
|
|
|
connect(this, &PendingFileQueue::timerStop, m_cacheTimer, &QTimer::stop);
|
|
|
|
connect(this, &PendingFileQueue::timerStop, m_minProcessTimer, &QTimer::stop);
|
2021-06-10 20:43:57 +08:00
|
|
|
|
|
|
|
connect(m_cacheTimer, &QTimer::timeout, this, &PendingFileQueue::processCache, Qt::DirectConnection);
|
|
|
|
connect(m_minProcessTimer, &QTimer::timeout, this, &PendingFileQueue::processCache, Qt::DirectConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
PendingFileQueue *PendingFileQueue::getInstance(QObject *parent)
|
|
|
|
{
|
|
|
|
if (!global_instance_pending_file_queue) {
|
|
|
|
global_instance_pending_file_queue = new PendingFileQueue(parent);
|
|
|
|
}
|
|
|
|
return global_instance_pending_file_queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PendingFileQueue::~PendingFileQueue()
|
|
|
|
{
|
|
|
|
if(m_cacheTimer) {
|
|
|
|
delete m_cacheTimer;
|
|
|
|
m_cacheTimer = nullptr;
|
|
|
|
}
|
|
|
|
if(m_minProcessTimer) {
|
|
|
|
delete m_minProcessTimer;
|
|
|
|
m_minProcessTimer = nullptr;
|
|
|
|
}
|
2022-06-02 09:23:53 +08:00
|
|
|
global_instance_pending_file_queue = nullptr;
|
2021-06-10 20:43:57 +08:00
|
|
|
|
|
|
|
IndexGenerator::getInstance()->~IndexGenerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PendingFileQueue::forceFinish()
|
|
|
|
{
|
2021-06-11 19:58:32 +08:00
|
|
|
Q_EMIT timerStop();
|
2021-06-10 20:43:57 +08:00
|
|
|
this->quit();
|
|
|
|
this->wait();
|
2022-03-15 13:53:38 +08:00
|
|
|
processCache();
|
|
|
|
m_semaphore.release(1);
|
2021-06-10 20:43:57 +08:00
|
|
|
}
|
|
|
|
void PendingFileQueue::enqueue(const PendingFile &file)
|
|
|
|
{
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug() << "enqueuq file: " << file.path();
|
2021-06-10 20:43:57 +08:00
|
|
|
m_mutex.lock();
|
2021-06-11 19:58:32 +08:00
|
|
|
m_enqueuetimes++;
|
|
|
|
if(m_cache.isEmpty()) {
|
|
|
|
IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "0");
|
|
|
|
}
|
2021-06-10 20:43:57 +08:00
|
|
|
// Remove all indexs of files under a dir which is to about be deleted,but keep delete signals.
|
|
|
|
// Because our datebase need to delete those indexs one by one.
|
|
|
|
if(file.shouldRemoveIndex() && file.isDir()) {
|
|
|
|
const auto keepFile = [&file](const PendingFile& pending) {
|
2021-07-06 16:53:32 +08:00
|
|
|
return (!FileUtils::isOrUnder(pending.path(), file.path()) || pending.shouldRemoveIndex());
|
2021-06-10 20:43:57 +08:00
|
|
|
};
|
|
|
|
const auto end = m_cache.end();
|
|
|
|
const auto droppedFilesBegin = std::stable_partition(m_cache.begin(), end, keepFile);
|
|
|
|
m_cache.erase(droppedFilesBegin, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(file.shouldRemoveIndex()) {
|
|
|
|
m_cache.removeOne(file);
|
|
|
|
}
|
|
|
|
int i = m_cache.indexOf(file);
|
|
|
|
if (i == -1) {
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug() << "insert file" << file.path() << file.shouldRemoveIndex();
|
2021-06-10 20:43:57 +08:00
|
|
|
m_cache << file;
|
|
|
|
} else {
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug() << "merge file" << file.path() << file.shouldRemoveIndex();
|
2021-06-10 20:43:57 +08:00
|
|
|
m_cache[i].merge(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!m_cacheTimer->isActive()) {
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug()<<"m_cacheTimer-----start!!";
|
2021-06-10 20:43:57 +08:00
|
|
|
// m_cacheTimer->start();
|
|
|
|
Q_EMIT cacheTimerStart();
|
|
|
|
}
|
|
|
|
Q_EMIT minProcessTimerStart();
|
|
|
|
// m_minProcessTimer->start();
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug()<<"m_minProcessTimer-----start!!";
|
2021-06-10 20:43:57 +08:00
|
|
|
m_mutex.unlock();
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug() << "Current cache-------------";
|
|
|
|
// for(PendingFile i : m_cache) {
|
|
|
|
// qDebug() << "|" << i.path();
|
|
|
|
// qDebug() << "|" <<i.shouldRemoveIndex();
|
|
|
|
// }
|
|
|
|
// qDebug() << "Current cache-------------";
|
|
|
|
// qDebug()<<"enqueuq file finish!!"<<file.path();
|
2021-06-10 20:43:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PendingFileQueue::run()
|
|
|
|
{
|
2022-03-15 13:53:38 +08:00
|
|
|
//阻塞线程直到first-index进程结束
|
|
|
|
m_semaphore.acquire();
|
2021-06-10 20:43:57 +08:00
|
|
|
exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PendingFileQueue::processCache()
|
|
|
|
{
|
2021-06-11 19:58:32 +08:00
|
|
|
qDebug()<< "Begin processCache!" ;
|
2021-06-10 20:43:57 +08:00
|
|
|
m_mutex.lock();
|
2021-06-11 19:58:32 +08:00
|
|
|
qDebug() << "Events:" << m_enqueuetimes;
|
|
|
|
m_enqueuetimes = 0;
|
2021-06-10 20:43:57 +08:00
|
|
|
m_cache.swap(m_pendingFiles);
|
|
|
|
// m_pendingFiles = m_cache;
|
|
|
|
// m_cache.clear();
|
|
|
|
// m_cache.squeeze();
|
|
|
|
m_mutex.unlock();
|
2021-06-11 19:58:32 +08:00
|
|
|
qDebug() << "Current process-------------";
|
|
|
|
for(PendingFile i : m_pendingFiles) {
|
|
|
|
qDebug() << "|" << i.path();
|
|
|
|
qDebug() << "|" <<i.shouldRemoveIndex();
|
|
|
|
}
|
|
|
|
qDebug() << "Current process-------------";
|
2021-06-10 20:43:57 +08:00
|
|
|
if(m_pendingFiles.isEmpty()) {
|
|
|
|
qDebug()<< "Empty, finish processCache!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
IndexGenerator::getInstance()->updateIndex(&m_pendingFiles);
|
|
|
|
m_mutex.lock();
|
|
|
|
if(m_cache.isEmpty()) {
|
|
|
|
IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "2");
|
|
|
|
}
|
|
|
|
m_mutex.unlock();
|
|
|
|
m_pendingFiles.clear();
|
|
|
|
m_pendingFiles.squeeze();
|
|
|
|
malloc_trim(0);
|
|
|
|
qDebug()<< "Finish processCache!";
|
|
|
|
return;
|
|
|
|
}
|