diff --git a/libsearch/index/index.pri b/libsearch/index/index.pri index 7de6c00..8d96cc7 100644 --- a/libsearch/index/index.pri +++ b/libsearch/index/index.pri @@ -7,8 +7,8 @@ HEADERS += \ $$PWD/first-index.h \ $$PWD/index-generator.h \ $$PWD/inotify-index.h \ + $$PWD/search-manager.h \ $$PWD/traverse_bfs.h \ - $$PWD/file-searcher.h \ $$PWD/ukui-search-qdbus.h SOURCES += \ @@ -18,7 +18,7 @@ SOURCES += \ $$PWD/first-index.cpp \ $$PWD/index-generator.cpp \ $$PWD/inotify-index.cpp \ + $$PWD/search-manager.cpp \ $$PWD/traverse_bfs.cpp \ - $$PWD/file-searcher.cpp \ $$PWD/ukui-search-qdbus.cpp diff --git a/libsearch/index/search-manager.cpp b/libsearch/index/search-manager.cpp new file mode 100644 index 0000000..c59a184 --- /dev/null +++ b/libsearch/index/search-manager.cpp @@ -0,0 +1,442 @@ +/* + * 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 . + * + * Authors: zhangpengfei + * + */ +#include +#include +#include +#include +#include +#include "search-manager.h" +#include "global-settings.h" +#include "chinese-segmentation.h" + +size_t SearchManager::uniqueSymbol1 = 0; +size_t SearchManager::uniqueSymbol2 = 0; +size_t SearchManager::uniqueSymbol3 = 0; +QMutex SearchManager::m_mutex1; +QMutex SearchManager::m_mutex2; +QMutex SearchManager::m_mutex3; +SearchManager::SearchManager(QObject *parent) : QObject(parent) +{ + m_pool.setMaxThreadCount(2); + m_pool.setExpiryTimeout(1000); +} + +SearchManager::~SearchManager() +{ +} + +int SearchManager::getCurrentIndexCount() +{ + try + { + Xapian::Database db(INDEX_PATH); + return db.get_doccount(); + } + catch(const Xapian::Error &e) + { + qWarning() < *searchResultFile,QQueue *searchResultDir, + QQueue> *searchResultContent) +{ + m_mutex1.lock(); + ++uniqueSymbol1; + m_mutex1.unlock(); + m_mutex2.lock(); + ++uniqueSymbol2; + m_mutex2.unlock(); + m_mutex3.lock(); + ++uniqueSymbol3; + m_mutex3.unlock(); + + FileSearch *filesearch; + filesearch = new FileSearch(searchResultFile,uniqueSymbol1,keyword,"0",1,0,5); + m_pool.start(filesearch); + + FileSearch *dirsearch; + dirsearch = new FileSearch(searchResultDir,uniqueSymbol2,keyword,"1",1,0,5); + m_pool.start(dirsearch); + + FileContentSearch *contentSearch; + contentSearch = new FileContentSearch(searchResultContent,uniqueSymbol3,keyword,0,5); + m_pool.start(contentSearch); + return; +} + +bool SearchManager::isBlocked(QString &path) +{ + QStringList blockList = GlobalSettings::getInstance()->getBlockDirs(); + for(QString i : blockList) + { + if(path.startsWith(i.prepend("/"))) + return true; + } + return false; + +} + +FileSearch::FileSearch(QQueue *searchResult,size_t uniqueSymbol, QString keyword, QString value, unsigned slot, int begin, int num) +{ + this->setAutoDelete(true); + m_search_result = searchResult; + m_uniqueSymbol = uniqueSymbol; + m_keyword = keyword; + m_value = value; + m_slot = slot; + m_begin = begin; + m_num = num; +} + +FileSearch::~FileSearch() +{ + m_search_result = nullptr; +} + +void FileSearch::run() +{ + if(!m_search_result->isEmpty()) + m_search_result->clear(); + int resultCount = 0; + int total = 0; + while(total < 100) + { + resultCount = keywordSearchfile(); + if(resultCount == 0 || resultCount == -1) + break; + total += resultCount; + m_begin += m_num; + } + return; +} + +int FileSearch::keywordSearchfile() +{ + try + { + qDebug() << "--keywordSearchfile start--"; + Xapian::Database db(INDEX_PATH); + Xapian::Query query = creatQueryForFileSearch(db); + Xapian::Enquire enquire(db); + + Xapian::Query queryFile; + if(!m_value.isEmpty()) + { + std::string slotValue = m_value.toStdString(); + Xapian::Query queryValue = Xapian::Query(Xapian::Query::OP_VALUE_RANGE,m_slot,slotValue,slotValue); + queryFile = Xapian::Query(Xapian::Query::OP_AND,query,queryValue); + } + else + { + queryFile = query; + } + + qDebug() << "keywordSearchfile:"< v; + for(int i=0;iappend(QString::fromStdString(data)); + qDebug()<enqueue(path); + SearchManager::m_mutex1.unlock(); + } + else + { + SearchManager::m_mutex1.unlock(); + return -1; + } + + break; + case 0: + SearchManager::m_mutex2.lock(); + if(m_uniqueSymbol == SearchManager::uniqueSymbol1) + { + m_search_result->enqueue(path); + SearchManager::m_mutex2.unlock(); + } + else + { + SearchManager::m_mutex2.unlock(); + return -1; + } + break; + default: + break; + } + // searchResult.append(path); + } + qDebug()<< "doc="<< path << ",weight=" <callSegement(ret, keyword.toLocal8Bit().data()); + for (::friso::ResultMap::iterator it_map = ret.begin(); it_map != ret.end(); ++it_map){ + target_str += it_map->first; + target_str += " "; + it_map->second.first.clear(); + ::std::vector().swap(it_map->second.first); + } + + ret.clear(); + ret.erase(ret.begin(), ret.end()); + ::friso::ResultMap().swap(ret); +*/ + QVector sKeyWord = ChineseSegmentation::getInstance()->callSegement(m_keyword); + //Creat a query + std::string words; + for(int i=0;i sKeyWord = ChineseSegmentation::getInstance()->callSegement(keyword); +// //Creat a query +// std::string words; +// for(int i=0;i v; + // for(int i=0;iappend(QString::fromStdString(data)); + qDebug()<enqueue(qMakePair(path,snippets)); + SearchManager::m_mutex3.unlock(); + snippets.clear(); + QStringList().swap(snippets); + } + else + { + SearchManager::m_mutex3.unlock(); + return -1; + } + // 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(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 SearchManager::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); + +// int getResult(size_t uniqueSymbol, Xapian::MSet &result, QString value); +// int getContentResult(size_t uniqueSymbol, Xapian::MSet &result,std::string &keyWord); + + static bool isBlocked(QString &path); + +// QQueue *m_search_result_file = nullptr; +// QQueue *m_search_result_dir = nullptr; +// QQueue> *m_search_result_content = nullptr; + QThreadPool m_pool; +}; + +class FileSearch : public QRunnable +{ +public: + explicit FileSearch(QQueue *searchResult,size_t uniqueSymbol, QString keyword, QString value,unsigned slot = 1,int begin = 0, int num = 20); + ~FileSearch(); +protected: + void run(); +private: + int keywordSearchfile(); + Xapian::Query creatQueryForFileSearch(Xapian::Database &db); + int getResult(Xapian::MSet &result); + + QQueue *m_search_result = nullptr; + QString m_value; + unsigned m_slot = 1; + size_t m_uniqueSymbol; + QString m_keyword; + int m_begin = 0; + int m_num = 20; +}; +class FileContentSearch : public QRunnable +{ +public: + explicit FileContentSearch(QQueue> *searchResult,size_t uniqueSymbol, QString keyword, int begin = 0, int num = 20); + ~FileContentSearch(); +protected: + void run(); +private: + int keywordSearchContent(); + int getResult(Xapian::MSet &result,std::string &keyWord); + + QQueue> *m_search_result = nullptr; + size_t m_uniqueSymbol; + QString m_keyword; + int m_begin = 0; + int m_num = 20; +}; +#endif // SEARCHMANAGER_H diff --git a/libsearch/libsearch.h b/libsearch/libsearch.h index 2767756..83febb4 100644 --- a/libsearch/libsearch.h +++ b/libsearch/libsearch.h @@ -21,7 +21,7 @@ #define LIBSEARCH_H #include "libsearch_global.h" -#include "index/file-searcher.h" +#include "index/search-manager.h" #include "appsearch/app-match.h" #include "settingsearch/setting-match.h" #include "file-utils.h" diff --git a/libsearch/libsearch.pro b/libsearch/libsearch.pro index 19af936..2c584c5 100644 --- a/libsearch/libsearch.pro +++ b/libsearch/libsearch.pro @@ -69,8 +69,5 @@ unix { INCLUDEPATH += $$PWD/../libchinese-segmentation DEPENDPATH += $$PWD/../libchinese-segmentation -#INCLUDEPATH += $$PWD/../libfriso -#DEPENDPATH += $$PWD/../libfriso - #DISTFILES += \ # ../translations/libsearch/libukui-search_zh_CN.ts diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index ef1ecb3..737a58b 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -53,7 +53,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { - m_searcher = new FileSearcher(); + m_searcher = new SearchManager(); // FileUtils::findMultiToneWords("仇仇仇仇仇仇仇仇仇仇仇翟康宁test"); /*-------------Inotify Test Start---------------*/ // QTime t1 = QTime::currentTime(); diff --git a/src/mainwindow.h b/src/mainwindow.h index ce0d67e..19d0a9a 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -96,7 +96,7 @@ private: SearchResult * m_search_result_thread = nullptr; SearchAppThread * m_seach_app_thread = nullptr; - FileSearcher* m_searcher = nullptr; + SearchManager* m_searcher = nullptr; QSystemTrayIcon *m_sys_tray_icon; protected: diff --git a/src/settings-widget.cpp b/src/settings-widget.cpp index ca00704..e477d72 100644 --- a/src/settings-widget.cpp +++ b/src/settings-widget.cpp @@ -28,7 +28,6 @@ #include "folder-list-item.h" #include "global-settings.h" #include "file-utils.h" -#include "index/file-searcher.h" extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed); SettingsWidget::SettingsWidget(QWidget *parent) : QDialog(parent) @@ -265,7 +264,7 @@ void SettingsWidget::refreshIndexState() } else { this->setIndexState(false); } - m_indexNumLabel->setText(QString("%1/%2").arg(QString::number(FileSearcher::getCurrentIndexCount())).arg(QString::number(FileUtils::_max_index_count))); + m_indexNumLabel->setText(QString("%1/%2").arg(QString::number(SearchManager::getCurrentIndexCount())).arg(QString::number(FileUtils::_max_index_count))); m_timer = new QTimer; connect(m_timer, &QTimer::timeout, this, [ = ]() { qDebug()<<"FileUtils::_index_status: "<setIndexState(false); } - m_indexNumLabel->setText(QString("%1/%2").arg(QString::number(FileSearcher::getCurrentIndexCount())).arg(QString::number(FileUtils::_max_index_count))); + m_indexNumLabel->setText(QString("%1/%2").arg(QString::number(SearchManager::getCurrentIndexCount())).arg(QString::number(FileUtils::_max_index_count))); }); m_timer->start(0.5 * 1000); } diff --git a/src/settings-widget.h b/src/settings-widget.h index 8d13ad1..d896808 100644 --- a/src/settings-widget.h +++ b/src/settings-widget.h @@ -32,6 +32,7 @@ #include #include #include +#include class SettingsWidget : public QDialog { diff --git a/src/src.pro b/src/src.pro index 5e44f23..bb24369 100644 --- a/src/src.pro +++ b/src/src.pro @@ -70,14 +70,10 @@ INSTALLS += qm_files LIBS += -L$$OUT_PWD/../libsearch -lukui-search \ -L$$OUT_PWD/../libchinese-segmentation -lchinese-segmentation -# -L$$OUT_PWD/../libfriso -lfriso INCLUDEPATH += $$PWD/../libsearch DEPENDPATH += $$PWD/../libsearch -#INCLUDEPATH += $$PWD/../libfriso -#DEPENDPATH += $$PWD/../libfriso - #DISTFILES += \ # ../data/ukui-search-menu.desktop \ # $$OUT_PWD/.qm/bo.qm \ diff --git a/ukui-search.pro b/ukui-search.pro index a45b59b..47447cd 100644 --- a/ukui-search.pro +++ b/ukui-search.pro @@ -3,7 +3,6 @@ SUBDIRS += $$PWD/libchinese-segmentation \ $$PWD/libsearch \ $$PWD/src \ $$PWD/ukuisearch-systemdbus -# $$PWD/libfriso # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the @@ -16,7 +15,6 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 libsearch.depends += libchinese-segmentation -# libfriso src.depends = libsearch CONFIG += ordered