From 929063645b7158a753c1f60e769e5c9d9fabf903 Mon Sep 17 00:00:00 2001 From: zhangpengfei Date: Sun, 10 Jan 2021 15:59:17 +0800 Subject: [PATCH] Modified file search interface and index interface. --- libsearch/index/file-searcher.cpp | 183 +++++++++++++++++++--------- libsearch/index/file-searcher.h | 21 +++- libsearch/index/filetypefilter.cpp | 2 +- libsearch/index/index-generator.cpp | 10 +- libsearch/index/index-generator.h | 11 +- libsearch/index/inotify-index.cpp | 24 ++-- src/main.cpp | 57 ++++----- src/mainwindow.cpp | 14 ++- 8 files changed, 204 insertions(+), 118 deletions(-) diff --git a/libsearch/index/file-searcher.cpp b/libsearch/index/file-searcher.cpp index cb535e6..145ba68 100644 --- a/libsearch/index/file-searcher.cpp +++ b/libsearch/index/file-searcher.cpp @@ -1,65 +1,97 @@ #include #include #include +#include #include #include "file-searcher.h" #include "global-settings.h" - +size_t FileSearcher::uniqueSymbol1 = 0; +size_t FileSearcher::uniqueSymbol2 = 0; +size_t FileSearcher::uniqueSymbol3 = 0; +QMutex FileSearcher::m_mutex1; +QMutex FileSearcher::m_mutex2; +QMutex FileSearcher::m_mutex3; FileSearcher::FileSearcher(QObject *parent) : QObject(parent) { } -void FileSearcher::onKeywordSearch(QString keyword) +FileSearcher::~FileSearcher() { - m_search_result_file = new QQueue; - m_search_result_dir = new QQueue; - m_search_result_content = new QQueue>; - //file - QtConcurrent::run([=](){ - int begin = 0; - int num = 20; - int resultCount = 0; - while(1) - { - resultCount = keywordSearchfile(keyword,"1",1,begin,num); - if(resultCount == 0 || resultCount == -1) - break; - begin += num; - } - }); - Q_EMIT this->resultFile(m_search_result_file); - //dir - QtConcurrent::run([=](){ - int begin = 0; - int num = 20; - int resultCount = 0; - while(1) - { - resultCount = keywordSearchfile(keyword,"0",1,begin,num); - if(resultCount == 0 || resultCount == -1) - break; - begin += num; - } - }); - Q_EMIT this->resultDir(m_search_result_dir); - //content - QtConcurrent::run([=](){ - int begin = 0; - int num = 10; - int resultCount = 0; - while(1) - { - keywordSearchContent(keyword,begin,num); - if(resultCount == 0 || resultCount == -1) - break; - begin += num; - } - }); - Q_EMIT this->resultContent(m_search_result_content); } -int FileSearcher::keywordSearchfile(QString keyword, QString value, unsigned slot, int begin, int num) +void FileSearcher::onKeywordSearch(QString keyword,QQueue *searchResultFile,QQueue *searchResultDir,QQueue> *searchResultContent) +{ + m_search_result_file = searchResultFile; + m_search_result_dir = searchResultDir; + m_search_result_content = searchResultContent; + + m_mutex1.lock(); + ++uniqueSymbol1; + m_mutex1.unlock(); + m_mutex2.lock(); + ++uniqueSymbol2; + m_mutex2.unlock(); + m_mutex3.lock(); + ++uniqueSymbol3; + m_mutex3.unlock(); + //file + QtConcurrent::run([&, uniqueSymbol1](){ + if(!m_search_result_file->isEmpty()) + m_search_result_file->clear(); + int begin = 0; + int num = 5; + int resultCount = 0; + int total = 0; + while(total<20) + { + resultCount = keywordSearchfile(uniqueSymbol1,keyword,"0",1,begin,num); + if(resultCount == 0 || resultCount == -1) + break; + total += resultCount; + begin += num; + } + }); +// Q_EMIT this->resultFile(m_search_result_file); + //dir + QtConcurrent::run([&, uniqueSymbol2](){ + if(!m_search_result_dir->isEmpty()) + m_search_result_dir->clear(); + int begin = 0; + int num = 5; + int resultCount = 0; + int total = 0; + while(total<20) + { + resultCount = keywordSearchfile(uniqueSymbol2,keyword,"1",1,begin,num); + if(resultCount == 0 || resultCount == -1) + break; + total += resultCount; + begin += num; + } + }); +// Q_EMIT this->resultDir(m_search_result_dir); + //content + QtConcurrent::run([&, uniqueSymbol3](){ + if(!m_search_result_content->isEmpty()) + m_search_result_content->clear(); + int begin = 0; + int num = 5; + int resultCount = 0; + int total = 0; + while(total<20) + { + keywordSearchContent(uniqueSymbol3,keyword,begin,num); + if(resultCount == 0 || resultCount == -1) + break; + total += resultCount; + begin += num; + } + }); +// Q_EMIT this->resultContent(m_search_result_content); +} + +int FileSearcher::keywordSearchfile(size_t uniqueSymbol, QString keyword, QString value, unsigned slot, int begin, int num) { try { @@ -86,7 +118,7 @@ int FileSearcher::keywordSearchfile(QString keyword, QString value, unsigned slo Xapian::MSet result = enquire.get_mset(begin, begin+num); int resultCount = static_cast(result.get_matches_estimated()); qDebug()<< "find results count=" <(result.get_matches_estimated()); qDebug()<< "find results count=" <exists()) + if(!info.exists()) { // pathTobeDelete->append(QString::fromStdString(data)); qDebug()<enqueue(path); + m_mutex1.lock(); + if(uniqueSymbol == FileSearcher::uniqueSymbol1) + { + m_search_result_dir->enqueue(path); + m_mutex1.unlock(); + } + else + { + m_mutex1.unlock(); + exit(0); + } + break; case 0: - m_search_result_file->enqueue(path); + m_mutex2.lock(); + if(uniqueSymbol == FileSearcher::uniqueSymbol2) + { + m_search_result_file->enqueue(path); + m_mutex2.unlock(); + } + else + { + m_mutex2.unlock(); + exit(0); + } break; default: break; @@ -226,7 +279,7 @@ QStringList FileSearcher::getResult(Xapian::MSet &result, QString value) return searchResult; } -QMap FileSearcher::getContentResult(Xapian::MSet &result, std::string &keyWord) +QMap FileSearcher::getContentResult(size_t uniqueSymbol, Xapian::MSet &result, std::string &keyWord) { //QStringList *pathTobeDelete = new QStringList; //Delete those path doc which is not already exist. @@ -251,9 +304,9 @@ QMap FileSearcher::getContentResult(Xapian::MSet &result, s if(isBlocked(path)) continue; - QFileInfo *info = new QFileInfo(path); + QFileInfo info(path); - if(!info->exists()) + if(!info.exists()) { // pathTobeDelete->append(QString::fromStdString(data)); qDebug()< FileSearcher::getContentResult(Xapian::MSet &result, s snippets.append(snippet); ++count; } - m_search_result_content->enqueue(qMakePair(path,snippets)); + m_mutex3.lock(); + if(uniqueSymbol == FileSearcher::uniqueSymbol3) + { + m_search_result_content->enqueue(qMakePair(path,snippets)); + m_mutex3.unlock(); + } + else + { + m_mutex3.unlock(); + exit(0); + } searchResult.insert(path,snippets); qDebug()<< "path="<< path << ",weight=" < *searchResultFile,QQueue *searchResultDir,QQueue> *searchResultContent); Q_SIGNALS: void resultFile(QQueue *); void resultDir(QQueue *); void resultContent(QQueue> *); private: - int keywordSearchfile(QString keyword, QString value,unsigned slot = 1,int begin = 0, int num = 20); - int keywordSearchContent(QString keyword, int begin = 0, int num = 20); + int keywordSearchfile(size_t uniqueSymbol, QString keyword, QString value,unsigned slot = 1,int begin = 0, int num = 20); + int keywordSearchContent(size_t uniqueSymbol, QString keyword, int begin = 0, int num = 20); /** * @brief FileSearcher::creatQueryForFileSearch @@ -39,14 +41,21 @@ private: Xapian::Query creatQueryForFileSearch(QString keyword, Xapian::Database &db); Xapian::Query creatQueryForContentSearch(QString keyword, Xapian::Database &db); - QStringList getResult(Xapian::MSet &result, QString value); - QMap getContentResult(Xapian::MSet &result,std::string &keyWord); + QStringList getResult(size_t uniqueSymbol, Xapian::MSet &result, QString value); + QMap getContentResult(size_t uniqueSymbol, Xapian::MSet &result,std::string &keyWord); bool isBlocked(QString &path); + QQueue *m_search_result_file = nullptr; QQueue *m_search_result_dir = nullptr; QQueue> *m_search_result_content = nullptr; - + bool m_searching = false; + static size_t uniqueSymbol1; + static size_t uniqueSymbol2; + static size_t uniqueSymbol3; + static QMutex m_mutex1; + static QMutex m_mutex2; + static QMutex m_mutex3; }; #endif // FILESEARCHER_H diff --git a/libsearch/index/filetypefilter.cpp b/libsearch/index/filetypefilter.cpp index 418f454..7af45c2 100644 --- a/libsearch/index/filetypefilter.cpp +++ b/libsearch/index/filetypefilter.cpp @@ -38,7 +38,7 @@ void FileTypeFilter::Test(){ // this->result = new QList(); // this->result->append(QString("/home/zpf/桌面/DOCX 文档(1).docx")); - ig->creatAllIndex(this->result); +// ig->creatAllIndex(this->result); } diff --git a/libsearch/index/index-generator.cpp b/libsearch/index/index-generator.cpp index 982703e..c6ffd59 100644 --- a/libsearch/index/index-generator.cpp +++ b/libsearch/index/index-generator.cpp @@ -17,9 +17,11 @@ using namespace std; #define CONTENT_INDEX_PATH (QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/org.ukui/content_index_data").toStdString() static IndexGenerator *global_instance = nullptr; +QMutex IndexGenerator::m_mutex; IndexGenerator *IndexGenerator::getInstance(bool rebuild) { + QMutexLocker locker(&m_mutex); if (!global_instance) { global_instance = new IndexGenerator(rebuild); } @@ -32,7 +34,7 @@ bool IndexGenerator::setIndexdataPath() } //文件名索引 -bool IndexGenerator::creatAllIndex(QList > *messageList) +bool IndexGenerator::creatAllIndex(QQueue > *messageList) { HandlePathList(messageList); try @@ -69,7 +71,7 @@ bool IndexGenerator::creatAllIndex(QList > *messageList) return true; } //文件内容索引 -bool IndexGenerator::creatAllIndex(QList *messageList) +bool IndexGenerator::creatAllIndex(QQueue *messageList) { HandlePathList(messageList); try @@ -152,7 +154,7 @@ void IndexGenerator::insertIntoContentDatabase(Document doc) return; } -void IndexGenerator::HandlePathList(QList> *messageList) +void IndexGenerator::HandlePathList(QQueue> *messageList) { qDebug()<<"Begin HandlePathList!"; qDebug()<size(); @@ -169,7 +171,7 @@ void IndexGenerator::HandlePathList(QList> *messageList) return; } -void IndexGenerator::HandlePathList(QList *messageList) +void IndexGenerator::HandlePathList(QQueue *messageList) { qDebug()<<"Begin HandlePathList for content index!"; qDebug()<size(); diff --git a/libsearch/index/index-generator.h b/libsearch/index/index-generator.h index 20d1e58..04397f5 100644 --- a/libsearch/index/index-generator.h +++ b/libsearch/index/index-generator.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include "document.h" #include "file-reader.h" @@ -24,16 +26,17 @@ Q_SIGNALS: void transactionFinished(); void searchFinish(); public Q_SLOTS: - bool creatAllIndex(QList> *messageList); - bool creatAllIndex(QList *messageList); + bool creatAllIndex(QQueue> *messageList); + bool creatAllIndex(QQueue *messageList); bool deleteAllIndex(QStringList *pathlist); private: explicit IndexGenerator(bool rebuild = false,QObject *parent = nullptr); + static QMutex m_mutex; //For file name index - void HandlePathList(QList> *messageList); + void HandlePathList(QQueue > *messageList); //For file content index - void HandlePathList(QList *messageList); + void HandlePathList(QQueue *messageList); static Document GenerateDocument(const QVector &list); static Document GenerateContentDocument(const QString &list); //add one data in database diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index 269a7af..f31a25a 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -80,8 +80,8 @@ void InotifyIndex::run(){ ssize_t numRead; - QList>* indexList = new QList>(); - QList* contentIndexList = new QList(); + QQueue>* indexQueue = new QQueue>(); + QQueue* contentIndexQueue = new QQueue(); for (;;) { /* Read events forever */ numRead = read(m_fd, buf, BUF_LEN); @@ -110,14 +110,14 @@ void InotifyIndex::run(){ /*--------------------------------*/ // IndexGenerator::getInstance()->creatAllIndex(QQueue>(QVector() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0"))); - indexList->append(QVector() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); - IndexGenerator::getInstance()->creatAllIndex(indexList); - indexList->clear(); + 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)){ - contentIndexList->append(QString(currentPath[event->wd] + '/' + event->name)); - IndexGenerator::getInstance()->creatAllIndex(contentIndexList); - contentIndexList->clear(); + contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name)); + IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue); + contentIndexQueue->clear(); } } /*--------------------------------*/ @@ -134,8 +134,8 @@ void InotifyIndex::run(){ } } - delete indexList; - indexList = nullptr; - delete contentIndexList; - contentIndexList = nullptr; + delete indexQueue; + indexQueue = nullptr; + delete contentIndexQueue; + contentIndexQueue = nullptr; } diff --git a/src/main.cpp b/src/main.cpp index 7d5f487..e9b28d4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -92,12 +92,30 @@ int main(int argc, char *argv[]) { qInstallMessageHandler(messageOutput); + QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); - qDebug() << "main start"; - FirstIndex fi("/home"); - fi.start(); - InotifyIndex ii("/home"); - ii.start(); + QtSingleApplication app("ukui-search", argc, argv); + app.setQuitOnLastWindowClosed(false); + + if(app.isRunning()) + { + app.sendMessage(QApplication::arguments().length() > 1 ? QApplication::arguments().at(1) : app.applicationFilePath()); + qDebug() << QObject::tr("ukui-search is already running!"); + return EXIT_SUCCESS; + }/*else { + QCommandLineParser parser; + QCommandLineOption debugOption({"d", "debug"}, QObject::tr("Display debug information")); + QCommandLineOption showsearch({"s", "show"}, QObject::tr("show search widget")); + parser.addOptions({debugOption, showsearch}); + parser.process(app); + }*/ + +// qDebug() << "main start"; +// FirstIndex fi("/home"); +// fi.start(); +// InotifyIndex ii("/home"); +// ii.start(); /*-------------ukuisearchdbus Test start-----------------*/ // UkuiSearchQDBus usQDBus; // usQDBus.setInotifyMaxUserWatches(); @@ -131,27 +149,6 @@ int main(int argc, char *argv[]) // search->onKeywordSearchContent("g,e,x"); /*-------------文本搜索 Test End-----------------*/ - QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); - QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); - - QtSingleApplication app("ukui-search", argc, argv); - app.setQuitOnLastWindowClosed(false); - - qDebug() << "main start x2"; - - if(app.isRunning()) - { - app.sendMessage(QApplication::arguments().length() > 1 ? QApplication::arguments().at(1) : app.applicationFilePath()); - qDebug() << QObject::tr("ukui-search is already running!"); - return EXIT_SUCCESS; - }/*else { - QCommandLineParser parser; - QCommandLineOption debugOption({"d", "debug"}, QObject::tr("Display debug information")); - QCommandLineOption showsearch({"s", "show"}, QObject::tr("show search widget")); - parser.addOptions({debugOption, showsearch}); - parser.process(app); - }*/ - // 加载国际化文件 QTranslator translator; try { @@ -164,15 +161,15 @@ int main(int argc, char *argv[]) MainWindow *w = new MainWindow; QStringList arguments = QCoreApplication::arguments(); centerToScreen(w); - w->show(); - w->raise(); - w->activateWindow(); -// w->loadMainWindow(); + app.setActivationWindow(w); // if(arguments.size()>1) // w->searchContent(arguments.at(1)); QObject::connect(&app, SIGNAL(messageReceived(const QString&)),w, SLOT(bootOptionsFilter(const QString&))); +// qDebug() << "main start"; +// FirstIndex* fi = new FirstIndex("/home"); +// fi->start(); return app.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a801a7c..8c428b3 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -194,6 +194,8 @@ void MainWindow::bootOptionsFilter(QString opt) if (opt == "-s" || opt == "-show") { clearSearchResult(); this->show(); + this->raise(); + this->activateWindow(); } } @@ -251,7 +253,17 @@ void MainWindow::searchContent(QString searchcontent){ //文件、文件夹、内容搜索 FileSearcher *search = new FileSearcher(); connect(search, &FileSearcher::resultDir, this, [ = ](QQueue * dirQueue) { - qWarning()<<"dirFile---"; + qWarning()<<"resultDir---"; + QString firstDir; + while(1) + { + if(!dirQueue->isEmpty()){ + firstDir = dirQueue->at(0); + qWarning()<<"firstDir"< * fileQueue) { qWarning()<<"resultFile---";