2021-01-29 11:43:07 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2020, 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: zhangzihao <zhangzihao@kylinos.cn>
|
|
|
|
|
* Modified by: zhangpengfei <zhangpengfei@kylinos.cn>
|
|
|
|
|
*
|
|
|
|
|
*/
|
2021-01-09 11:25:07 +08:00
|
|
|
|
//#include <QtConcurrent>
|
|
|
|
|
#include "first-index.h"
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
2021-01-22 09:49:44 +08:00
|
|
|
|
#define NEW_QUEUE(a) a = new QQueue<QString>(); qDebug("---------------------------%s %s %s new at %d..",__FILE__,__FUNCTION__,#a,__LINE__);
|
2021-01-19 19:26:39 +08:00
|
|
|
|
//#define DELETE_QUEUE(a )
|
2021-04-30 16:28:50 +08:00
|
|
|
|
using namespace Zeeker;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
FirstIndex::FirstIndex() {
|
2021-06-02 15:17:27 +08:00
|
|
|
|
m_pool.setMaxThreadCount(2);
|
|
|
|
|
m_pool.setExpiryTimeout(100);
|
2021-04-20 16:24:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
FirstIndex::~FirstIndex() {
|
2021-04-20 16:24:07 +08:00
|
|
|
|
qDebug() << "~FirstIndex";
|
|
|
|
|
if(this->q_index)
|
|
|
|
|
delete this->q_index;
|
|
|
|
|
this->q_index = nullptr;
|
|
|
|
|
if(this->q_content_index)
|
|
|
|
|
delete this->q_content_index;
|
|
|
|
|
this->q_content_index = nullptr;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(this->p_indexGenerator)
|
2021-04-20 16:24:07 +08:00
|
|
|
|
delete this->p_indexGenerator;
|
|
|
|
|
this->p_indexGenerator = nullptr;
|
|
|
|
|
qDebug() << "~FirstIndex end";
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
void FirstIndex::DoSomething(const QFileInfo& fileInfo) {
|
2021-04-20 16:24:07 +08:00
|
|
|
|
// qDebug() << "there are some shit here"<<fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0");
|
|
|
|
|
this->q_index->enqueue(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString((fileInfo.isDir() && (!fileInfo.isSymLink())) ? "1" : "0"));
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if((fileInfo.fileName().split(".", QString::SkipEmptyParts).length() > 1) && (true == targetFileTypeMap[fileInfo.fileName().split(".").last()])) {
|
2021-05-22 09:18:35 +08:00
|
|
|
|
//this->q_content_index->enqueue(fileInfo.absoluteFilePath());
|
2021-06-07 15:37:06 +08:00
|
|
|
|
if (fileInfo.fileName().split(".").last() == "docx") {
|
2021-05-28 15:55:26 +08:00
|
|
|
|
QuaZip file(fileInfo.absoluteFilePath());
|
|
|
|
|
if(!file.open(QuaZip::mdUnzip))
|
|
|
|
|
return;
|
|
|
|
|
if(!file.setCurrentFile("word/document.xml", QuaZip::csSensitive))
|
|
|
|
|
return;
|
|
|
|
|
QuaZipFile fileR(&file);
|
|
|
|
|
this->q_content_index->enqueue(qMakePair(fileInfo.absoluteFilePath(),fileR.usize()));//docx解压缩后的xml文件为实际需要解析文件大小
|
|
|
|
|
file.close();
|
2021-06-07 15:37:06 +08:00
|
|
|
|
} else if (fileInfo.fileName().split(".").last() == "pptx") {
|
2021-05-28 15:55:26 +08:00
|
|
|
|
QuaZip file(fileInfo.absoluteFilePath());
|
|
|
|
|
if(!file.open(QuaZip::mdUnzip))
|
|
|
|
|
return;
|
|
|
|
|
QString prefix("ppt/slides/slide");
|
|
|
|
|
qint64 fileSize(0);
|
|
|
|
|
qint64 fileIndex(0);
|
|
|
|
|
for(QString i : file.getFileNameList()) {
|
|
|
|
|
if(i.startsWith(prefix)){
|
|
|
|
|
QString name = prefix + QString::number(fileIndex + 1) + ".xml";
|
|
|
|
|
fileIndex++;
|
|
|
|
|
if(!file.setCurrentFile(name)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
QuaZipFile fileR(&file);
|
|
|
|
|
fileSize += fileR.usize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
file.close();
|
|
|
|
|
this->q_content_index->enqueue(qMakePair(fileInfo.absoluteFilePath(),fileSize));//pptx解压缩后的xml文件为实际需要解析文件大小
|
2021-06-07 15:37:06 +08:00
|
|
|
|
} else if (fileInfo.fileName().split(".").last() == "xlsx") {
|
2021-05-28 15:55:26 +08:00
|
|
|
|
QuaZip file(fileInfo.absoluteFilePath());
|
|
|
|
|
if(!file.open(QuaZip::mdUnzip))
|
|
|
|
|
return;
|
|
|
|
|
if(!file.setCurrentFile("xl/sharedStrings.xml", QuaZip::csSensitive))
|
|
|
|
|
return;
|
|
|
|
|
QuaZipFile fileR(&file);
|
|
|
|
|
this->q_content_index->enqueue(qMakePair(fileInfo.absoluteFilePath(),fileR.usize()));//xlsx解压缩后的xml文件为实际解析文件大小
|
|
|
|
|
file.close();
|
2021-06-07 15:37:06 +08:00
|
|
|
|
} else {
|
2021-05-28 15:55:26 +08:00
|
|
|
|
this->q_content_index->enqueue(qMakePair(fileInfo.absoluteFilePath(),fileInfo.size()));
|
|
|
|
|
}
|
2021-04-20 16:24:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
void FirstIndex::run() {
|
2021-04-20 16:24:07 +08:00
|
|
|
|
QTime t1 = QTime::currentTime();
|
|
|
|
|
|
2021-04-16 16:09:50 +08:00
|
|
|
|
// Create a fifo at ~/.config/org.ukui/ukui-search, the fifo is used to control the order of child processes' running.
|
2021-04-26 15:06:47 +08:00
|
|
|
|
QDir fifoDir = QDir(QDir::homePath() + "/.config/org.ukui/ukui-search");
|
2021-04-16 16:09:50 +08:00
|
|
|
|
if(!fifoDir.exists())
|
2021-04-26 15:06:47 +08:00
|
|
|
|
qDebug() << "create fifo path" << fifoDir.mkpath(fifoDir.absolutePath());
|
2021-04-16 16:09:50 +08:00
|
|
|
|
|
|
|
|
|
unlink(UKUI_SEARCH_PIPE_PATH);
|
|
|
|
|
int retval = mkfifo(UKUI_SEARCH_PIPE_PATH, 0777);
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(retval == -1) {
|
|
|
|
|
qCritical() << "creat fifo error!!";
|
|
|
|
|
syslog(LOG_ERR, "creat fifo error!!\n");
|
2021-04-16 16:09:50 +08:00
|
|
|
|
assert(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-26 15:06:47 +08:00
|
|
|
|
qDebug() << "create fifo success\n";
|
2021-04-16 16:09:50 +08:00
|
|
|
|
|
2021-05-13 11:17:07 +08:00
|
|
|
|
QString indexDataBaseStatus = IndexStatusRecorder::getInstance()->getStatus(INDEX_DATABASE_STATE).toString();
|
|
|
|
|
QString contentIndexDataBaseStatus = IndexStatusRecorder::getInstance()->getStatus(CONTENT_INDEX_DATABASE_STATE).toString();
|
|
|
|
|
QString inotifyIndexStatus = IndexStatusRecorder::getInstance()->getStatus(INOTIFY_NORMAL_EXIT).toString();
|
2021-01-09 11:25:07 +08:00
|
|
|
|
|
|
|
|
|
qDebug() << "indexDataBaseStatus: " << indexDataBaseStatus;
|
|
|
|
|
qDebug() << "contentIndexDataBaseStatus: " << contentIndexDataBaseStatus;
|
2021-01-13 14:04:13 +08:00
|
|
|
|
qDebug() << "inotifyIndexStatus: " << inotifyIndexStatus;
|
2021-01-09 11:25:07 +08:00
|
|
|
|
|
2021-04-08 14:23:18 +08:00
|
|
|
|
/* || contentIndexDataBaseStatus == ""*/
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(indexDataBaseStatus == "") {
|
2021-01-09 11:25:07 +08:00
|
|
|
|
this->bool_dataBaseExist = false;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
} else {
|
2021-01-10 09:01:22 +08:00
|
|
|
|
this->bool_dataBaseExist = true;
|
|
|
|
|
}
|
2021-05-13 11:17:07 +08:00
|
|
|
|
if(indexDataBaseStatus != "2" || contentIndexDataBaseStatus != "2" || inotifyIndexStatus != "2") {
|
2021-01-09 11:25:07 +08:00
|
|
|
|
this->bool_dataBaseStatusOK = false;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
} else {
|
2021-01-10 09:01:22 +08:00
|
|
|
|
this->bool_dataBaseStatusOK = true;
|
|
|
|
|
}
|
2021-01-09 11:25:07 +08:00
|
|
|
|
|
|
|
|
|
this->q_index = new QQueue<QVector<QString>>();
|
2021-01-19 19:26:39 +08:00
|
|
|
|
//this->q_content_index = new QQueue<QString>();
|
2021-05-22 09:18:35 +08:00
|
|
|
|
//NEW_QUEUE(this->q_content_index);
|
2021-01-09 11:25:07 +08:00
|
|
|
|
// this->mlm = new MessageListManager();
|
2021-05-22 09:18:35 +08:00
|
|
|
|
this->q_content_index = new QQueue<QPair<QString,qint64>>();
|
2021-01-26 11:32:02 +08:00
|
|
|
|
|
2021-01-22 09:49:44 +08:00
|
|
|
|
int fifo_fd;
|
|
|
|
|
char buffer[2];
|
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
|
buffer[0] = 0x1;
|
|
|
|
|
buffer[1] = '\0';
|
|
|
|
|
fifo_fd = open(UKUI_SEARCH_PIPE_PATH, O_RDWR);
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(fifo_fd == -1) {
|
2021-01-22 09:49:44 +08:00
|
|
|
|
perror("open fifo error\n");
|
|
|
|
|
assert(false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 19:26:39 +08:00
|
|
|
|
// this->q_content_index->enqueue(QString("/home/zhangzihao/Desktop/qwerty/四库全书.txt"));
|
|
|
|
|
|
|
|
|
|
// this->p_indexGenerator->creatAllIndex(this->q_content_index);
|
|
|
|
|
|
|
|
|
|
|
2021-01-22 17:15:43 +08:00
|
|
|
|
++FileUtils::_index_status;
|
2021-01-21 13:50:21 +08:00
|
|
|
|
pid_t pid;
|
|
|
|
|
pid = fork();
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(pid == 0) {
|
2021-01-23 09:52:44 +08:00
|
|
|
|
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
2021-04-26 15:06:47 +08:00
|
|
|
|
prctl(PR_SET_NAME, "first-index");
|
|
|
|
|
if(this->bool_dataBaseExist) {
|
|
|
|
|
if(this->bool_dataBaseStatusOK) {
|
2021-02-06 15:13:05 +08:00
|
|
|
|
::_exit(0);
|
2021-04-26 15:06:47 +08:00
|
|
|
|
} else {
|
2021-01-26 11:32:02 +08:00
|
|
|
|
//if the parameter is false, index won't be rebuild
|
|
|
|
|
//if it is true, index will be rebuild
|
2021-04-26 15:06:47 +08:00
|
|
|
|
p_indexGenerator = IndexGenerator::getInstance(true, this);
|
2021-01-26 11:32:02 +08:00
|
|
|
|
}
|
2021-04-26 15:06:47 +08:00
|
|
|
|
} else {
|
2021-02-27 10:51:19 +08:00
|
|
|
|
// p_indexGenerator = IndexGenerator::getInstance(false,this);
|
2021-04-26 15:06:47 +08:00
|
|
|
|
p_indexGenerator = IndexGenerator::getInstance(true, this);
|
2021-02-27 10:51:19 +08:00
|
|
|
|
|
2021-01-26 11:32:02 +08:00
|
|
|
|
}
|
2021-06-02 15:17:27 +08:00
|
|
|
|
//TODO Fix these weird code.
|
2021-01-21 13:50:21 +08:00
|
|
|
|
QSemaphore sem(5);
|
|
|
|
|
QMutex mutex1, mutex2, mutex3;
|
|
|
|
|
mutex1.lock();
|
|
|
|
|
mutex2.lock();
|
|
|
|
|
mutex3.lock();
|
|
|
|
|
sem.acquire(4);
|
2021-04-21 09:31:03 +08:00
|
|
|
|
// QtConcurrent::run([&](){
|
|
|
|
|
sem.acquire(1);
|
|
|
|
|
mutex1.unlock();
|
|
|
|
|
this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
|
|
|
|
|
this->Traverse();
|
|
|
|
|
FileUtils::_max_index_count = this->q_index->length();
|
2021-04-26 15:06:47 +08:00
|
|
|
|
qDebug() << "max_index_count:" << FileUtils::_max_index_count;
|
2021-04-21 09:31:03 +08:00
|
|
|
|
sem.release(5);
|
|
|
|
|
// });
|
2021-06-02 15:17:27 +08:00
|
|
|
|
QtConcurrent::run(&m_pool, [&]() {
|
2021-01-21 13:50:21 +08:00
|
|
|
|
sem.acquire(2);
|
|
|
|
|
mutex2.unlock();
|
|
|
|
|
qDebug() << "index start;";
|
2021-06-02 15:17:27 +08:00
|
|
|
|
QQueue<QVector<QString>>* tmp1 = new QQueue<QVector<QString>>();
|
2021-04-26 15:06:47 +08:00
|
|
|
|
while(!this->q_index->empty()) {
|
|
|
|
|
for(size_t i = 0; (i < 8192) && (!this->q_index->empty()); ++i) {
|
2021-06-02 15:17:27 +08:00
|
|
|
|
tmp1->enqueue(this->q_index->dequeue());
|
2021-02-27 10:51:19 +08:00
|
|
|
|
}
|
2021-06-02 15:17:27 +08:00
|
|
|
|
this->p_indexGenerator->creatAllIndex(tmp1);
|
|
|
|
|
tmp1->clear();
|
2021-02-27 10:51:19 +08:00
|
|
|
|
}
|
2021-06-02 15:17:27 +08:00
|
|
|
|
delete tmp1;
|
2021-01-21 13:50:21 +08:00
|
|
|
|
qDebug() << "index end;";
|
|
|
|
|
sem.release(2);
|
|
|
|
|
});
|
2021-06-02 15:17:27 +08:00
|
|
|
|
QtConcurrent::run(&m_pool,[&]() {
|
2021-01-21 13:50:21 +08:00
|
|
|
|
sem.acquire(2);
|
|
|
|
|
mutex3.unlock();
|
2021-06-02 15:17:27 +08:00
|
|
|
|
QQueue<QString>* tmp2 = new QQueue<QString>();
|
2021-04-26 15:06:47 +08:00
|
|
|
|
qDebug() << "q_content_index:" << q_content_index->size();
|
|
|
|
|
while(!this->q_content_index->empty()) {
|
2021-06-02 15:17:27 +08:00
|
|
|
|
// for (size_t i = 0; (i < this->u_send_length) && (!this->q_content_index->empty()); ++i){
|
2021-05-22 09:18:35 +08:00
|
|
|
|
qint64 fileSize = 0;
|
|
|
|
|
//修改一次处理的数据量,从30个文件改为文件总大小为50M以下,50M为暂定值--jxx20210519
|
2021-06-07 15:37:06 +08:00
|
|
|
|
for(size_t i = 0;/* (i < 30) && (fileSize < 52428800) && */(!this->q_content_index->empty()); ++i) {
|
2021-05-22 09:18:35 +08:00
|
|
|
|
QPair<QString,qint64> tempPair = this->q_content_index->dequeue();
|
|
|
|
|
fileSize += tempPair.second;
|
2021-06-07 15:37:06 +08:00
|
|
|
|
if (fileSize > 52428800 ) {
|
|
|
|
|
if (tmp2->size() == 0) {
|
|
|
|
|
tmp2->enqueue(tempPair.first);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
this->q_content_index->enqueue(tempPair);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-06-02 15:17:27 +08:00
|
|
|
|
tmp2->enqueue(tempPair.first);
|
2021-02-27 10:51:19 +08:00
|
|
|
|
}
|
2021-06-02 15:17:27 +08:00
|
|
|
|
// qDebug() << ">>>>>>>>all fileSize:" << fileSize << "file num:" << tmp->size() << "<<<<<<<<<<<<<<<<<<<";
|
|
|
|
|
this->p_indexGenerator->creatAllIndex(tmp2);
|
|
|
|
|
tmp2->clear();
|
2021-02-27 10:51:19 +08:00
|
|
|
|
}
|
2021-06-02 15:17:27 +08:00
|
|
|
|
delete tmp2;
|
2021-01-21 13:50:21 +08:00
|
|
|
|
qDebug() << "content index end;";
|
|
|
|
|
sem.release(2);
|
|
|
|
|
});
|
2021-06-23 15:50:19 +08:00
|
|
|
|
|
2021-01-21 13:50:21 +08:00
|
|
|
|
mutex1.lock();
|
|
|
|
|
mutex2.lock();
|
|
|
|
|
mutex3.lock();
|
|
|
|
|
sem.acquire(5);
|
2021-01-09 11:25:07 +08:00
|
|
|
|
mutex1.unlock();
|
|
|
|
|
mutex2.unlock();
|
|
|
|
|
mutex3.unlock();
|
2021-01-11 16:59:50 +08:00
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(this->q_index)
|
2021-01-21 13:50:21 +08:00
|
|
|
|
delete this->q_index;
|
|
|
|
|
this->q_index = nullptr;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(this->q_content_index)
|
2021-01-21 13:50:21 +08:00
|
|
|
|
delete this->q_content_index;
|
|
|
|
|
this->q_content_index = nullptr;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(p_indexGenerator)
|
2021-01-21 21:05:53 +08:00
|
|
|
|
delete p_indexGenerator;
|
|
|
|
|
p_indexGenerator = nullptr;
|
2021-04-20 16:24:07 +08:00
|
|
|
|
// GlobalSettings::getInstance()->forceSync();
|
2021-05-13 11:17:07 +08:00
|
|
|
|
IndexStatusRecorder::getInstance()->setStatus(INDEX_DATABASE_STATE, "2");
|
|
|
|
|
IndexStatusRecorder::getInstance()->setStatus(CONTENT_INDEX_DATABASE_STATE, "2");
|
2021-02-06 15:13:05 +08:00
|
|
|
|
::_exit(0);
|
2021-04-26 15:06:47 +08:00
|
|
|
|
} else if(pid < 0) {
|
|
|
|
|
qWarning() << "First Index fork error!!";
|
|
|
|
|
} else {
|
|
|
|
|
waitpid(pid, NULL, 0);
|
2021-01-22 17:15:43 +08:00
|
|
|
|
--FileUtils::_index_status;
|
2021-01-21 13:50:21 +08:00
|
|
|
|
}
|
2021-01-11 16:59:50 +08:00
|
|
|
|
|
2021-05-13 11:17:07 +08:00
|
|
|
|
IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "2");
|
2021-04-20 16:24:07 +08:00
|
|
|
|
int retval1 = write(fifo_fd, buffer, strlen(buffer));
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(retval1 == -1) {
|
2021-01-26 11:32:02 +08:00
|
|
|
|
qWarning("write error\n");
|
2021-01-22 09:49:44 +08:00
|
|
|
|
}
|
2021-01-26 11:32:02 +08:00
|
|
|
|
qDebug("write data ok!\n");
|
2021-02-27 10:51:19 +08:00
|
|
|
|
QTime t2 = QTime::currentTime();
|
|
|
|
|
qWarning() << t1;
|
|
|
|
|
qWarning() << t2;
|
2021-02-25 14:40:44 +08:00
|
|
|
|
|
2021-01-26 11:32:02 +08:00
|
|
|
|
return;
|
2021-01-11 16:59:50 +08:00
|
|
|
|
|
2021-01-09 11:25:07 +08:00
|
|
|
|
}
|