Optimize code logic.

This commit is contained in:
Mouse Zhang 2021-04-07 22:28:49 +08:00
parent 9ca9f47320
commit 3dc0eb4af2
4 changed files with 73 additions and 198 deletions

View File

@ -75,11 +75,9 @@ FirstIndex::~FirstIndex()
void FirstIndex::DoSomething(const QFileInfo& fileInfo){ void FirstIndex::DoSomething(const QFileInfo& fileInfo){
// qDebug() << "there are some shit here"<<fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0"); // 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")); this->q_index->enqueue(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString((fileInfo.isDir() && (!fileInfo.isSymLink())) ? "1" : "0"));
for (auto i : this->targetFileTypeVec) { if ((!fileInfo.fileName().split(".").isEmpty()) && (true == targetFileTypeMap[fileInfo.fileName().split(".").last()])){
if (fileInfo.fileName().endsWith(i)) {
this->q_content_index->enqueue(fileInfo.absoluteFilePath()); this->q_content_index->enqueue(fileInfo.absoluteFilePath());
} }
}
} }
void FirstIndex::run(){ void FirstIndex::run(){

View File

@ -62,19 +62,21 @@ private:
//test //test
QQueue<QVector<QString>>* q_index; QQueue<QVector<QString>>* q_index;
QQueue<QString>* q_content_index; QQueue<QString>* q_content_index;
const QVector<QString> targetFileTypeVec ={
QString(".doc"), const QMap<QString, bool> targetFileTypeMap = {
QString(".docx"), std::map<QString, bool>::value_type("doc", true),
QString(".ppt"), std::map<QString, bool>::value_type("docx", true),
// QString(".pptx"), std::map<QString, bool>::value_type("ppt", true),
QString(".xls"), // std::map<QString, bool>::value_type(".pptx", true),
// QString(".xlsx"), std::map<QString, bool>::value_type("xls", true),
QString(".txt"), // std::map<QString, bool>::value_type(".xlsx", true),
QString(".dot"), std::map<QString, bool>::value_type("txt", true),
QString(".wps"), std::map<QString, bool>::value_type("dot", true),
QString(".pps"), std::map<QString, bool>::value_type("wps", true),
QString(".dps"), std::map<QString, bool>::value_type("pps", true),
QString(".et")}; std::map<QString, bool>::value_type("dps", true),
std::map<QString, bool>::value_type("et", true)
};
//xapian will auto commit per 10,000 changes, donnot change it!!! //xapian will auto commit per 10,000 changes, donnot change it!!!
const size_t u_send_length = 8192; const size_t u_send_length = 8192;

View File

@ -19,30 +19,50 @@
*/ */
#include "inotify-index.h" #include "inotify-index.h"
#define CREATE_FILE_NAME_INDEX \
indexQueue->enqueue(QVector<QString>() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); \
IndexGenerator::getInstance()->creatAllIndex(indexQueue); \
indexQueue->clear();
#define CREATE_FILE_CONTENT_INDEX \
if ((!QString(event->name).split(".").isEmpty()) && (true == this->targetFileTypeMap[QString(event->name).split(".").last()])) { \
contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name)); \
IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue); \
contentIndexQueue->clear(); \
break; \
}
#define TRAVERSE_DIR \
QString tmp = currentPath[event->wd] + '/' + event->name; \
QFileInfo fi(tmp); \
if(!fi.isSymLink()){ \
AddWatch(tmp); \
setPath(tmp); \
Traverse(); \
}
#define CREATE_FILE \
CREATE_FILE_NAME_INDEX \
CREATE_FILE_CONTENT_INDEX
InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path) InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path)
{ {
/*-------------ukuisearchdbus Test start-----------------*/
qDebug() << "setInotifyMaxUserWatches start"; qDebug() << "setInotifyMaxUserWatches start";
UkuiSearchQDBus usQDBus; UkuiSearchQDBus usQDBus;
usQDBus.setInotifyMaxUserWatches(); usQDBus.setInotifyMaxUserWatches();
qDebug() << "setInotifyMaxUserWatches end"; qDebug() << "setInotifyMaxUserWatches end";
/*-------------ukuisearchdbus Test End-----------------*/
m_fd = inotify_init(); m_fd = inotify_init();
qDebug() << "m_fd----------->" <<m_fd; qDebug() << "m_fd----------->" <<m_fd;
this->AddWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); this->AddWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
// this->Traverse();
this->firstTraverse(); this->firstTraverse();
} }
InotifyIndex::~InotifyIndex() InotifyIndex::~InotifyIndex()
{ {
// GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2");
IndexGenerator::getInstance()->~IndexGenerator(); IndexGenerator::getInstance()->~IndexGenerator();
} }
@ -70,19 +90,13 @@ void InotifyIndex::DoSomething(const QFileInfo& fileInfo){
if(fileInfo.isDir() && (!fileInfo.isSymLink())){ if(fileInfo.isDir() && (!fileInfo.isSymLink())){
this->AddWatch(fileInfo.absoluteFilePath()); this->AddWatch(fileInfo.absoluteFilePath());
} }
QQueue<QVector<QString> >* tempFile = new QQueue<QVector<QString> >; QQueue<QVector<QString> > tempFile;
tempFile->enqueue(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString((fileInfo.isDir() && (!fileInfo.isSymLink())) ? "1" : "0")); tempFile.enqueue(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString((fileInfo.isDir() && (!fileInfo.isSymLink())) ? "1" : "0"));
IndexGenerator::getInstance()->creatAllIndex(tempFile); IndexGenerator::getInstance()->creatAllIndex(&tempFile);
if (tempFile) if ((!fileInfo.fileName().split(".").isEmpty()) && (true == targetFileTypeMap[fileInfo.fileName().split(".").last()])){
delete tempFile; QQueue<QString> tmp;
for (auto i : this->targetFileTypeVec){ tmp.enqueue(fileInfo.absoluteFilePath());
if (fileInfo.fileName().endsWith(i)){ IndexGenerator::getInstance()->creatAllIndex(&tmp);
QQueue<QString>* tempContent = new QQueue<QString>;
tempContent->enqueue(fileInfo.absoluteFilePath());
IndexGenerator::getInstance()->creatAllIndex(tempContent);
if (tempContent)
delete tempContent;
}
} }
} }
@ -120,7 +134,6 @@ bool InotifyIndex::RemoveWatch(const QString &path){
qDebug() << "remove path error"; qDebug() << "remove path error";
// return false; // return false;
} }
// Q_ASSERT(ret == 0);
// assert(ret == 0); // assert(ret == 0);
/*--------------------------------*/ /*--------------------------------*/
//在此调用删除索引 //在此调用删除索引
@ -163,33 +176,13 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){
//Create top dir first, traverse it last. //Create top dir first, traverse it last.
qDebug() << "IN_CREATE"; qDebug() << "IN_CREATE";
/*--------------------------------*/ CREATE_FILE
// IndexGenerator::getInstance()->creatAllIndex(QQueue<QVector<QString>>(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0")));
indexQueue->enqueue(QVector<QString>() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0"));
IndexGenerator::getInstance()->creatAllIndex(indexQueue);
indexQueue->clear();
for (auto i : this->targetFileTypeVec){
if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){
contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name));
IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue);
contentIndexQueue->clear();
break;
}
}
if (event->mask & IN_ISDIR){ if (event->mask & IN_ISDIR){
QString tmp = currentPath[event->wd] + '/' + event->name; TRAVERSE_DIR
QFileInfo fi(tmp);
if(!fi.isSymLink()){
AddWatch(tmp);
setPath(tmp);
Traverse();
}
} }
goto next; goto next;
} }
if ((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){ if ((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){
qDebug() << "IN_DELETE or IN_MOVED_FROM"; qDebug() << "IN_DELETE or IN_MOVED_FROM";
if (event->mask & IN_ISDIR){ if (event->mask & IN_ISDIR){
@ -200,120 +193,29 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){
goto next; goto next;
} }
if (event->mask & IN_MODIFY){ if (event->mask & IN_MODIFY){
qDebug() << "IN_MODIFY"; qDebug() << "IN_MODIFY";
if (!(event->mask & IN_ISDIR)){ if (!(event->mask & IN_ISDIR)){
// IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); // IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name));
indexQueue->enqueue(QVector<QString>() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); CREATE_FILE
IndexGenerator::getInstance()->creatAllIndex(indexQueue);
indexQueue->clear();
for (auto i : this->targetFileTypeVec){
if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){
contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name));
IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue);
contentIndexQueue->clear();
break;
}
}
} }
goto next; goto next;
} }
if (event->mask & IN_MOVED_TO){ if (event->mask & IN_MOVED_TO){
qDebug() << "IN_MOVED_TO"; qDebug() << "IN_MOVED_TO";
if (event->mask & IN_ISDIR){ if (event->mask & IN_ISDIR){
RemoveWatch(currentPath[event->wd] + '/' + event->name); RemoveWatch(currentPath[event->wd] + '/' + event->name);
// IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); // IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name));
CREATE_FILE
TRAVERSE_DIR
indexQueue->enqueue(QVector<QString>() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0"));
IndexGenerator::getInstance()->creatAllIndex(indexQueue);
indexQueue->clear();
for (auto i : this->targetFileTypeVec){
if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){
contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name));
IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue);
contentIndexQueue->clear();
break;
}
}
QString tmp = currentPath[event->wd] + '/' + event->name;
QFileInfo fi(tmp);
if(!fi.isSymLink()){
AddWatch(tmp);
setPath(tmp);
Traverse();
}
} }
else { else {
IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name));
indexQueue->enqueue(QVector<QString>() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); CREATE_FILE
IndexGenerator::getInstance()->creatAllIndex(indexQueue);
indexQueue->clear();
for (auto i : this->targetFileTypeVec){
if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){
contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name));
IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue);
contentIndexQueue->clear();
break;
}
}
} }
goto next; goto next;
} }
// }
// //传创建或移动过来的文件路径
// if((event->mask & IN_CREATE)){
// //添加监视要先序遍历先添加top节点
// if (event->mask & IN_ISDIR){
// AddWatch(currentPath[event->wd] + '/' + event->name);
// this->setPath(currentPath[event->wd] + '/' + event->name);
// Traverse();
// }
// /*--------------------------------*/
//// IndexGenerator::getInstance()->creatAllIndex(QQueue<QVector<QString>>(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0")));
// indexQueue->enqueue(QVector<QString>() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0"));
// IndexGenerator::getInstance()->creatAllIndex(indexQueue);
// indexQueue->clear();
// for (auto i : this->targetFileTypeVec){
// if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){
// contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name));
// IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue);
// contentIndexQueue->clear();
// break;
// }
// }
// /*--------------------------------*/
// }
// else if((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){
// if (event->mask & IN_ISDIR){
// RemoveWatch(currentPath[event->wd] + '/' + event->name);
// }
// IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name));
// }
// else if((event->mask & IN_MODIFY) | (event->mask & IN_MOVED_TO)){
// if (!(event->mask & IN_ISDIR)){
// IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name));
// indexQueue->enqueue(QVector<QString>() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0"));
// IndexGenerator::getInstance()->creatAllIndex(indexQueue);
// indexQueue->clear();
// for (auto i : this->targetFileTypeVec){
// if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){
// contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name));
// IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue);
// contentIndexQueue->clear();
// break;
// }
// }
// }
// }
/*--------------------------------*/
} }
next: next:
p += sizeof(struct inotify_event) + event->len; p += sizeof(struct inotify_event) + event->len;
@ -325,12 +227,6 @@ next:
contentIndexQueue = nullptr; contentIndexQueue = nullptr;
} }
/*
* Symbolic Link!!!!!!!!!!!!!!!!!!
* Sysmbolic link to database dir will make a Infinite loop !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* MouseZhangZh
*/
void InotifyIndex::run(){ void InotifyIndex::run(){
int fifo_fd; int fifo_fd;
char buffer[2]; char buffer[2];
@ -443,30 +339,6 @@ fork:
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2");
} }
} }
// QTimer* liveTime = new QTimer();
// //restart inotify-index proccess per minute
// liveTime->setInterval(60000);
// liveTime->start();
//I don't know how to use QTimer, wish someone can fix it!
//MouseZhangZh
// connect(liveTime, &QTimer::timeout, [ = ](){
//// ::_exit(0);
// *b_timeout = 1;
// });
// for (;;){
// qDebug() << "liveTime->remainingTime():" << liveTime->remainingTime();
// numRead = read(m_fd, buf, BUF_LEN);
// this->eventProcess(buf, numRead);
// if (liveTime->remainingTime() < 1){
// qDebug() << "liveTime->remainingTime():" << liveTime->remainingTime();
// ::_exit(0);
// }
// }
} }
else if (pid > 0){ else if (pid > 0){
memset(buf, 0x00, BUF_LEN); memset(buf, 0x00, BUF_LEN);

View File

@ -60,19 +60,22 @@ private:
int m_fd; int m_fd;
QMap<int, QString> currentPath; QMap<int, QString> currentPath;
const QVector<QString> targetFileTypeVec ={
QString(".doc"), const QMap<QString, bool> targetFileTypeMap = {
QString(".docx"), std::map<QString, bool>::value_type("doc", true),
QString(".ppt"), std::map<QString, bool>::value_type("docx", true),
// QString(".pptx"), std::map<QString, bool>::value_type("ppt", true),
QString(".xls"), // std::map<QString, bool>::value_type(".pptx", true),
// QString(".xlsx"), std::map<QString, bool>::value_type("xls", true),
QString(".txt"), // std::map<QString, bool>::value_type(".xlsx", true),
QString(".dot"), std::map<QString, bool>::value_type("txt", true),
QString(".wps"), std::map<QString, bool>::value_type("dot", true),
QString(".pps"), std::map<QString, bool>::value_type("wps", true),
QString(".dps"), std::map<QString, bool>::value_type("pps", true),
QString(".et")}; std::map<QString, bool>::value_type("dps", true),
std::map<QString, bool>::value_type("et", true)
};
}; };
#endif // INOTIFYINDEX_H #endif // INOTIFYINDEX_H