From 3dc0eb4af2ee2def56f95b1fbd3a90f55db91d95 Mon Sep 17 00:00:00 2001 From: Mouse Zhang Date: Wed, 7 Apr 2021 22:28:49 +0800 Subject: [PATCH] Optimize code logic. --- libsearch/index/first-index.cpp | 6 +- libsearch/index/first-index.h | 28 ++-- libsearch/index/inotify-index.cpp | 208 ++++++------------------------ libsearch/index/inotify-index.h | 29 +++-- 4 files changed, 73 insertions(+), 198 deletions(-) diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index 9b60726..3463439 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -75,10 +75,8 @@ FirstIndex::~FirstIndex() void FirstIndex::DoSomething(const QFileInfo& fileInfo){ // qDebug() << "there are some shit here"<q_index->enqueue(QVector() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString((fileInfo.isDir() && (!fileInfo.isSymLink())) ? "1" : "0")); - for (auto i : this->targetFileTypeVec) { - if (fileInfo.fileName().endsWith(i)) { - this->q_content_index->enqueue(fileInfo.absoluteFilePath()); - } + if ((!fileInfo.fileName().split(".").isEmpty()) && (true == targetFileTypeMap[fileInfo.fileName().split(".").last()])){ + this->q_content_index->enqueue(fileInfo.absoluteFilePath()); } } diff --git a/libsearch/index/first-index.h b/libsearch/index/first-index.h index 4c40fdc..d3368fb 100644 --- a/libsearch/index/first-index.h +++ b/libsearch/index/first-index.h @@ -62,19 +62,21 @@ private: //test QQueue>* q_index; QQueue* q_content_index; - const QVector targetFileTypeVec ={ - QString(".doc"), - QString(".docx"), - QString(".ppt"), - // QString(".pptx"), - QString(".xls"), - // QString(".xlsx"), - QString(".txt"), - QString(".dot"), - QString(".wps"), - QString(".pps"), - QString(".dps"), - QString(".et")}; + + const QMap targetFileTypeMap = { + std::map::value_type("doc", true), + std::map::value_type("docx", true), + std::map::value_type("ppt", true), +// std::map::value_type(".pptx", true), + std::map::value_type("xls", true), +// std::map::value_type(".xlsx", true), + std::map::value_type("txt", true), + std::map::value_type("dot", true), + std::map::value_type("wps", true), + std::map::value_type("pps", true), + std::map::value_type("dps", true), + std::map::value_type("et", true) + }; //xapian will auto commit per 10,000 changes, donnot change it!!! const size_t u_send_length = 8192; diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index 24bf91a..b0367f3 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -19,30 +19,50 @@ */ #include "inotify-index.h" +#define CREATE_FILE_NAME_INDEX \ + indexQueue->enqueue(QVector() << 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) { - /*-------------ukuisearchdbus Test start-----------------*/ qDebug() << "setInotifyMaxUserWatches start"; UkuiSearchQDBus usQDBus; usQDBus.setInotifyMaxUserWatches(); qDebug() << "setInotifyMaxUserWatches end"; - /*-------------ukuisearchdbus Test End-----------------*/ - - - m_fd = inotify_init(); qDebug() << "m_fd----------->" <AddWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); -// this->Traverse(); this->firstTraverse(); } InotifyIndex::~InotifyIndex() { -// GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); IndexGenerator::getInstance()->~IndexGenerator(); } @@ -70,19 +90,13 @@ void InotifyIndex::DoSomething(const QFileInfo& fileInfo){ if(fileInfo.isDir() && (!fileInfo.isSymLink())){ this->AddWatch(fileInfo.absoluteFilePath()); } - QQueue >* tempFile = new QQueue >; - tempFile->enqueue(QVector() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString((fileInfo.isDir() && (!fileInfo.isSymLink())) ? "1" : "0")); - IndexGenerator::getInstance()->creatAllIndex(tempFile); - if (tempFile) - delete tempFile; - for (auto i : this->targetFileTypeVec){ - if (fileInfo.fileName().endsWith(i)){ - QQueue* tempContent = new QQueue; - tempContent->enqueue(fileInfo.absoluteFilePath()); - IndexGenerator::getInstance()->creatAllIndex(tempContent); - if (tempContent) - delete tempContent; - } + QQueue > tempFile; + tempFile.enqueue(QVector() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString((fileInfo.isDir() && (!fileInfo.isSymLink())) ? "1" : "0")); + IndexGenerator::getInstance()->creatAllIndex(&tempFile); + if ((!fileInfo.fileName().split(".").isEmpty()) && (true == targetFileTypeMap[fileInfo.fileName().split(".").last()])){ + QQueue tmp; + tmp.enqueue(fileInfo.absoluteFilePath()); + IndexGenerator::getInstance()->creatAllIndex(&tmp); } } @@ -120,7 +134,6 @@ bool InotifyIndex::RemoveWatch(const QString &path){ qDebug() << "remove path error"; // return false; } -// Q_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. qDebug() << "IN_CREATE"; - /*--------------------------------*/ - // IndexGenerator::getInstance()->creatAllIndex(QQueue>(QVector() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0"))); - indexQueue->enqueue(QVector() << 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; - } - } - + CREATE_FILE if (event->mask & IN_ISDIR){ - QString tmp = currentPath[event->wd] + '/' + event->name; - QFileInfo fi(tmp); - if(!fi.isSymLink()){ - AddWatch(tmp); - setPath(tmp); - Traverse(); - } + TRAVERSE_DIR } goto next; } - if ((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){ qDebug() << "IN_DELETE or IN_MOVED_FROM"; if (event->mask & IN_ISDIR){ @@ -200,120 +193,29 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){ goto next; } - if (event->mask & IN_MODIFY){ qDebug() << "IN_MODIFY"; if (!(event->mask & IN_ISDIR)){ // IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); - indexQueue->enqueue(QVector() << 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; - } - } + CREATE_FILE } goto next; } - if (event->mask & IN_MOVED_TO){ qDebug() << "IN_MOVED_TO"; if (event->mask & IN_ISDIR){ RemoveWatch(currentPath[event->wd] + '/' + event->name); // IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); - - - indexQueue->enqueue(QVector() << 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(); - } + CREATE_FILE + TRAVERSE_DIR } else { IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); - indexQueue->enqueue(QVector() << 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; - } - } + CREATE_FILE } 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() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0"))); - // indexQueue->enqueue(QVector() << 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(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: p += sizeof(struct inotify_event) + event->len; @@ -325,12 +227,6 @@ next: contentIndexQueue = nullptr; } -/* - * Symbolic Link!!!!!!!!!!!!!!!!!! - * Sysmbolic link to database dir will make a Infinite loop !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - * MouseZhangZh -*/ - void InotifyIndex::run(){ int fifo_fd; char buffer[2]; @@ -443,30 +339,6 @@ fork: 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){ memset(buf, 0x00, BUF_LEN); diff --git a/libsearch/index/inotify-index.h b/libsearch/index/inotify-index.h index 38d131c..fb2c4ad 100644 --- a/libsearch/index/inotify-index.h +++ b/libsearch/index/inotify-index.h @@ -60,19 +60,22 @@ private: int m_fd; QMap currentPath; - const QVector targetFileTypeVec ={ - QString(".doc"), - QString(".docx"), - QString(".ppt"), -// QString(".pptx"), - QString(".xls"), -// QString(".xlsx"), - QString(".txt"), - QString(".dot"), - QString(".wps"), - QString(".pps"), - QString(".dps"), - QString(".et")}; + + const QMap targetFileTypeMap = { + std::map::value_type("doc", true), + std::map::value_type("docx", true), + std::map::value_type("ppt", true), +// std::map::value_type(".pptx", true), + std::map::value_type("xls", true), +// std::map::value_type(".xlsx", true), + std::map::value_type("txt", true), + std::map::value_type("dot", true), + std::map::value_type("wps", true), + std::map::value_type("pps", true), + std::map::value_type("dps", true), + std::map::value_type("et", true) + }; + }; #endif // INOTIFYINDEX_H