From f77e1c180b46d24c69b21954c76dd302939d6935 Mon Sep 17 00:00:00 2001 From: zhangpengfei Date: Fri, 8 Jan 2021 19:08:24 +0800 Subject: [PATCH] Add blacklist function; Using a queue to return file search results. --- libsearch/global-settings.cpp | 23 +++- libsearch/global-settings.h | 11 +- libsearch/index/file-searcher.cpp | 202 +++++++++++++++++++--------- libsearch/index/file-searcher.h | 31 ++++- libsearch/index/index-generator.cpp | 2 +- src/mainwindow.cpp | 60 ++++----- 6 files changed, 230 insertions(+), 99 deletions(-) diff --git a/libsearch/global-settings.cpp b/libsearch/global-settings.cpp index 50b5765..591231c 100644 --- a/libsearch/global-settings.cpp +++ b/libsearch/global-settings.cpp @@ -16,6 +16,7 @@ GlobalSettings *GlobalSettings::getInstance() GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) { m_settings = new QSettings("org.ukui", "ukui-search", this); + m_block_dirs_settings = new QSettings("org.ukui","ukui-search-block-dirs",this); //the default number of transparency in mainwindow is 0.7 //if someone changes the num in mainwindow, here should be modified too m_cache.insert(TRANSPARENCY_KEY, 0.7); @@ -77,9 +78,27 @@ void GlobalSettings::resetAll() }); } -QList GlobalSettings::getBlockDirs() +bool GlobalSettings::setBlockDirs(const QString &path, QString &returnMessage) { - return m_cache.keys(); + QStringList blockDirs = m_block_dirs_settings->allKeys(); + for(QString i:blockDirs) + { + if(path.startsWith(i)) + { + returnMessage = QString(tr("Parent folder has been blocked!")); + return false; + } + + if(i.startsWith(path)) + m_block_dirs_settings->remove(i); + } + m_block_dirs_settings->setValue(path,"0"); + return true; +} + +QStringList GlobalSettings::getBlockDirs() +{ + return m_block_dirs_settings->allKeys(); } void GlobalSettings::setValue(const QString &key, const QVariant &value) diff --git a/libsearch/global-settings.h b/libsearch/global-settings.h index 4a3e3d6..6685db2 100644 --- a/libsearch/global-settings.h +++ b/libsearch/global-settings.h @@ -34,7 +34,15 @@ public Q_SLOTS: void setValue(const QString&, const QVariant&); void reset(const QString&); void resetAll(); - QList getBlockDirs(); + /** + * @brief setBlockDirs + * set path for blacklist,return true if success,otherwise return false. + * @param path path to be blocked + * @param returnMessage this message will be set when return false. + * @return + */ + bool setBlockDirs(const QString& path, QString &returnMessage); + QStringList getBlockDirs(); void forceSync(const QString& = nullptr); @@ -44,6 +52,7 @@ private: QSettings* m_settings; QGSettings* m_gsettings; + QSettings *m_block_dirs_settings; QMap m_cache; QMutex m_mutex; diff --git a/libsearch/index/file-searcher.cpp b/libsearch/index/file-searcher.cpp index e36c4fc..9379fbd 100644 --- a/libsearch/index/file-searcher.cpp +++ b/libsearch/index/file-searcher.cpp @@ -1,82 +1,103 @@ -#include "file-searcher.h" #include #include +#include #include +#include "file-searcher.h" +#include "global-settings.h" + FileSearcher::FileSearcher(QObject *parent) : QObject(parent) { - } -void FileSearcher::onKeywordSearch(QString keyword, int begin, int num) +void FileSearcher::onKeywordSearch(QString keyword) +{ + //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) { - QVector searchResult; try { qDebug()<<"--search start--"; - Xapian::Database db(INDEX_PATH); + Xapian::Query query = creatQueryForFileSearch(keyword,db); Xapian::Enquire enquire(db); - Xapian::QueryParser qp; - qp.set_default_op(Xapian::Query::OP_PHRASE); - qp.set_database(db); - auto userInput = keyword; - std::string queryStr = keyword.replace(""," ").toStdString(); -// std::string s =db.get_spelling_suggestion(queryStr,10); -// qDebug()<<"spelling_suggestion!"< v; - for(int i=0;iresult(searchResult); - return; - } -void FileSearcher::onKeywordSearchContent(QString keyword, int begin, int num) +int FileSearcher::keywordSearchContent(QString keyword, int begin, int num) { - QMap searchResult = QMap(); try { qDebug()<<"--content search start--"; @@ -108,24 +129,52 @@ void FileSearcher::onKeywordSearchContent(QString keyword, int begin, int num) enquire.set_query(query); //dir result Xapian::MSet result = enquire.get_mset(begin, begin+num); - qDebug()<< "find results count=" <(result.get_matches_estimated()); + int resultCount = static_cast(result.get_matches_estimated()); + qDebug()<< "find results count=" <contentResult(searchResult); -// qDebug()< v; + for(int i=0;iexists()) { // pathTobeDelete->append(QString::fromStdString(data)); - qDebug()<enqueue(path); + break; + case 0: + m_search_result_file->enqueue(path); + break; + default: + break; + } + searchResult.append(path); } - qDebug()<< "doc="<< QString::fromStdString(data) << ",weight=" < resultP); - void contentResult(QMap resultC); + void resultFile(QQueue *); + void resultDir(QQueue *); + void resultContent(QQueue> *); private: - QStringList getResult(Xapian::MSet &result); + 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); + + /** + * @brief FileSearcher::creatQueryForFileSearch + * This part shall be optimized frequently to provide a more stable search function. + * @param keyword + * @param db + * @return Xapian::Query + */ + 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); + + bool isBlocked(QString &path); + QQueue *m_search_result_file; + QQueue *m_search_result_dir; + QQueue> *m_search_result_content; + }; #endif // FILESEARCHER_H diff --git a/libsearch/index/index-generator.cpp b/libsearch/index/index-generator.cpp index 26d7c4a..7b18824 100644 --- a/libsearch/index/index-generator.cpp +++ b/libsearch/index/index-generator.cpp @@ -79,7 +79,7 @@ bool IndexGenerator::creatAllIndex(QList *messageList) { insertIntoContentDatabase(m_doc_list_content->at(i)); - if(++count == 9999) + if(++count == 1000) { count = 0; m_database_content->commit(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 65a3af9..50e5250 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -255,25 +255,25 @@ void MainWindow::searchContent(QString searchcontent){ //内容搜索测试用数据,每个文件(路径)对应一段文本内容 FileSearcher *search = new FileSearcher(); - - connect(search, &FileSearcher::contentResult, this, [ = ](QMap map) { - m_types.append(SearchItem::SearchType::Contents); - QStringList pathlist, contentList; - qDebug() << map; - for (auto i : map.keys()){ - QString temp; - pathlist << i; - for (auto s : map[i]){ - temp.append(s); - } - contentList.append(temp); - } - m_lists.append(pathlist); - m_contentFrame->setContentList(contentList); - }); +//iaom--------this part shall be rewrite +// connect(search, &FileSearcher::contentResult, this, [ = ](QMap map) { +// m_types.append(SearchItem::SearchType::Contents); +// QStringList pathlist, contentList; +// qDebug() << map; +// for (auto i : map.keys()){ +// QString temp; +// pathlist << i; +// for (auto s : map[i]){ +// temp.append(s); +// } +// contentList.append(temp); +// } +// m_lists.append(pathlist); +// m_contentFrame->setContentList(contentList); +// }); QTime t1 = QTime::currentTime(); - search->onKeywordSearchContent(searchcontent); +// search->onKeywordSearch(searchcontent); QTime t2 = QTime::currentTime(); qDebug() << t1; qDebug() << t2; @@ -289,21 +289,21 @@ void MainWindow::searchContent(QString searchcontent){ //文件搜索 FileSearcher *searcher = new FileSearcher(); +//iaom--------this part shall be rewrite +// connect(searcher,&FileSearcher::result,[=](QVector resultV){ - connect(searcher,&FileSearcher::result,[=](QVector resultV){ +// QStringList list1 = resultV.at(0); +// QStringList list2 = resultV.at(1); - QStringList list1 = resultV.at(0); - QStringList list2 = resultV.at(1); - - // QVector lists; - m_lists.append(list1); - m_lists.append(list2); - // QVector types; - m_types.append(SearchItem::SearchType::Dirs); - m_types.append(SearchItem::SearchType::Files); - m_contentFrame->refreshSearchList(m_types, m_lists, searchcontent); - }); - searcher->onKeywordSearch(searchcontent,0,10); +// // QVector lists; +// m_lists.append(list1); +// m_lists.append(list2); +// // QVector types; +// m_types.append(SearchItem::SearchType::Dirs); +// m_types.append(SearchItem::SearchType::Files); +// m_contentFrame->refreshSearchList(m_types, m_lists, searchcontent); +// }); +// searcher->onKeywordSearch(searchcontent,0,10); // QStringList res = IndexGenerator::IndexSearch(searchcontent); // types.append(SearchItem::SearchType::Files); // lists.append(res);