From 6e7ca5e35c6cf3260034055168750945935c779a Mon Sep 17 00:00:00 2001 From: iaom Date: Thu, 14 Apr 2022 11:16:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=B4=A2=E5=BC=95=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E7=99=BD=E5=90=8D=E5=8D=95=E5=BD=A2=E5=BC=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/mainwindow.cpp | 2 +- libsearch/common.h | 2 + libsearch/dirwatcher/dir-watcher-adaptor.h | 18 ++++---- libsearch/dirwatcher/dir-watcher.cpp | 13 ++++++ libsearch/dirwatcher/dir-watcher.h | 1 + libsearch/index/file-index-manager.cpp | 46 ++++++++++++++------- libsearch/index/file-index-manager.h | 5 +++ libsearch/index/first-index.cpp | 40 ++++++++++++------ libsearch/index/first-index.h | 4 +- libsearch/index/inotify-watch.cpp | 33 +++++++++++---- libsearch/index/inotify-watch.h | 9 ++-- libsearch/index/traverse_bfs.cpp | 3 +- libsearch/index/traverse_bfs.h | 2 +- search-ukcc-plugin/search.h | 2 +- ukui-search-service/ukui-search-service.cpp | 15 +------ 15 files changed, 130 insertions(+), 65 deletions(-) diff --git a/frontend/mainwindow.cpp b/frontend/mainwindow.cpp index 4fa5f45..1d2ca2b 100644 --- a/frontend/mainwindow.cpp +++ b/frontend/mainwindow.cpp @@ -39,7 +39,7 @@ #define MAIN_MARGINS 0, 0, 0, 0 #define TITLE_MARGINS 0,0,0,0 #define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings" -#define SEARCH_METHOD_KEY "file-index-enable" +#define SEARCH_METHOD_KEY "fileIndexEnable" #define WEB_ENGINE_KEY "webEngine" #define WINDOW_WIDTH 700 #define WINDOW_HEIGHT 610 diff --git a/libsearch/common.h b/libsearch/common.h index 56cbc96..dd245b0 100644 --- a/libsearch/common.h +++ b/libsearch/common.h @@ -17,6 +17,8 @@ static const QString FILE_SEARCH_VALUE = QStringLiteral("0"); static const QString DIR_SEARCH_VALUE = QStringLiteral("1"); static const QString INDEX_SEM = QStringLiteral("ukui-search-index-sem"); static const int OCR_MIN_SIZE = 200; +static const QByteArray UKUI_SEARCH_SCHEMAS = QByteArrayLiteral("org.ukui.search.settings"); +static const QString SEARCH_METHOD_KEY = QStringLiteral("fileIndexEnable"); static const QStringList allAppPath = { {HOME_PATH + "/.local/share/applications/"}, diff --git a/libsearch/dirwatcher/dir-watcher-adaptor.h b/libsearch/dirwatcher/dir-watcher-adaptor.h index 24c9411..3e00a26 100644 --- a/libsearch/dirwatcher/dir-watcher-adaptor.h +++ b/libsearch/dirwatcher/dir-watcher-adaptor.h @@ -29,16 +29,16 @@ QT_END_NAMESPACE class DirWatcherAdaptor: public QDBusAbstractAdaptor { Q_OBJECT - Q_CLASSINFO("D-Bus Interface", "org.ukui.search.fileindex.service") + Q_CLASSINFO("D-Bus Interface", "org.ukui.search.fileindex") Q_CLASSINFO("D-Bus Introspection", "" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" +" " +" " +" " +" " +" " +" " +" " +" " "") public: DirWatcherAdaptor(QObject *parent); diff --git a/libsearch/dirwatcher/dir-watcher.cpp b/libsearch/dirwatcher/dir-watcher.cpp index 2fce31a..8e96629 100644 --- a/libsearch/dirwatcher/dir-watcher.cpp +++ b/libsearch/dirwatcher/dir-watcher.cpp @@ -43,6 +43,19 @@ DirWatcher *DirWatcher::getDirWatcher() return global_intance; } +void DirWatcher::initDbusService() +{ + //注册服务 + QDBusConnection sessionBus = QDBusConnection::sessionBus(); + if (!sessionBus.registerService("com.ukui.search.fileindex.service")) { + qWarning() << "ukui-search-fileindex dbus register service failed reason:" << sessionBus.lastError(); + } + + if(!sessionBus.registerObject("/org/ukui/search/fileindex", this, QDBusConnection::ExportAdaptors)){ + qWarning() << "ukui-search-fileindex dbus register object failed reason:" << sessionBus.lastError(); + } +} + QStringList DirWatcher::currentindexableDir() { QMutexLocker locker(&s_mutex); diff --git a/libsearch/dirwatcher/dir-watcher.h b/libsearch/dirwatcher/dir-watcher.h index bdf79fc..c35b0aa 100644 --- a/libsearch/dirwatcher/dir-watcher.h +++ b/libsearch/dirwatcher/dir-watcher.h @@ -27,6 +27,7 @@ class DirWatcher : public QObject public: static DirWatcher *getDirWatcher(); + void initDbusService(); QStringList currentindexableDir(); QStringList currentBlackListOfIndex(); void handleIndexItemAppend(const QString &path); diff --git a/libsearch/index/file-index-manager.cpp b/libsearch/index/file-index-manager.cpp index f41121d..0a98fbf 100644 --- a/libsearch/index/file-index-manager.cpp +++ b/libsearch/index/file-index-manager.cpp @@ -1,4 +1,6 @@ #include "file-index-manager.h" +#include "dir-watcher.h" +#include "common.h" using namespace UkuiSearch; static FileIndexManager* global_instance = nullptr; FileIndexManager::FileIndexManager(QObject *parent) : QObject(parent), m_semaphore(INDEX_SEM, 1, QSystemSemaphore::AccessMode::Create) @@ -23,21 +25,6 @@ void FileIndexManager::searchMethod(FileUtils::SearchMethod sm) { qWarning("enum class error!!!\n"); } if(FileUtils::SearchMethod::INDEXSEARCH == sm && 0 == FileUtils::indexStatus) { - -// // Create a fifo at ~/.config/org.ukui/ukui-search, the fifo is used to control the order of child processes' running. -// QDir fifoDir = QDir(QDir::homePath() + "/.config/org.ukui/ukui-search"); -// if(!fifoDir.exists()) -// qDebug() << "create fifo path" << fifoDir.mkpath(fifoDir.absolutePath()); - -// unlink(UKUI_SEARCH_PIPE_PATH); -// int retval = mkfifo(UKUI_SEARCH_PIPE_PATH, 0777); -// if(retval == -1) { -// qCritical() << "creat fifo error!!"; -// syslog(LOG_ERR, "creat fifo error!!\n"); -// assert(false); -// return; -// } -// qDebug() << "create fifo success\n"; qDebug() << "start first index"; m_semaphore.acquire(); m_fi->start(); @@ -52,3 +39,32 @@ void FileIndexManager::searchMethod(FileUtils::SearchMethod sm) { } qWarning() << "searchMethod end: " << static_cast(FileUtils::searchMethod); } + +void FileIndexManager::initIndexPathSetFunction() +{ + const QByteArray id(UKUI_SEARCH_SCHEMAS); + if(QGSettings::isSchemaInstalled(id)) { + m_searchSettings = new QGSettings(id); + if(!m_searchSettings->keys().contains(SEARCH_METHOD_KEY)) { + qWarning() << "Can not find gsettings key:" << UKUI_SEARCH_SCHEMAS << SEARCH_METHOD_KEY; + return; + } + } else { + qWarning() << "Can not find gsettings:" << UKUI_SEARCH_SCHEMAS; + return; + } + + connect(DirWatcher::getDirWatcher(), &DirWatcher::appendIndexItem, this, &FileIndexManager::handleIndexPathAppend, Qt::QueuedConnection); + + DirWatcher::getDirWatcher()->initDbusService(); +} + +void FileIndexManager::handleIndexPathAppend(const QString path, const QStringList blockList) +{ + if(!m_searchSettings->get(SEARCH_METHOD_KEY).toBool()) { + m_searchSettings->set(SEARCH_METHOD_KEY, true); + } else { + m_fi->addIndexPath(path, blockList); + m_iw->addIndexPath(path, blockList); + } +} diff --git a/libsearch/index/file-index-manager.h b/libsearch/index/file-index-manager.h index fa411f1..0bab966 100644 --- a/libsearch/index/file-index-manager.h +++ b/libsearch/index/file-index-manager.h @@ -3,6 +3,7 @@ #include #include +#include #include "first-index.h" //#include "inotify-index.h" #include "inotify-watch.h" @@ -12,12 +13,16 @@ class FileIndexManager : public QObject { public: static FileIndexManager *getInstance(); void searchMethod(FileUtils::SearchMethod sm); + void initIndexPathSetFunction(); +private Q_SLOTS: + void handleIndexPathAppend(const QString path, const QStringList blockList); private: FileIndexManager(QObject *parent = nullptr); FirstIndex *m_fi; // InotifyIndex* m_ii; InotifyWatch *m_iw = nullptr; QSystemSemaphore m_semaphore; + QGSettings *m_searchSettings = nullptr; }; } diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index 44e5fb5..9186a6f 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -54,7 +54,7 @@ FirstIndex::~FirstIndex() { qDebug() << "~FirstIndex end"; } -void FirstIndex::DoSomething(const QFileInfo& fileInfo) { +void FirstIndex::work(const QFileInfo& fileInfo) { // qDebug() << "there are some shit here"<m_indexData->enqueue(QVector() << fileInfo.fileName() << fileInfo.absoluteFilePath() @@ -113,30 +113,47 @@ void FirstIndex::DoSomething(const QFileInfo& fileInfo) { } } +void FirstIndex::addIndexPath(const QString path, const QStringList blockList) +{ + m_semaphore.acquire(); + setPath(QStringList() << path); + setBlockPath(blockList); + this->wait(); + this->start(); +} + void FirstIndex::run() { QTime t1 = QTime::currentTime(); // Create a fifo at ~/.config/org.ukui/ukui-search, the fifo is used to control the order of child processes' running. QString indexDataBaseStatus = IndexStatusRecorder::getInstance()->getStatus(INDEX_DATABASE_STATE).toString(); QString contentIndexDataBaseStatus = IndexStatusRecorder::getInstance()->getStatus(CONTENT_INDEX_DATABASE_STATE).toString(); -// QString ocrIndexDatabaseStatus = IndexStatusRecorder::getInstance()->getStatus(OCR_DATABASE_STATE).toString(); + // QString ocrIndexDatabaseStatus = IndexStatusRecorder::getInstance()->getStatus(OCR_DATABASE_STATE).toString(); QString inotifyIndexStatus = IndexStatusRecorder::getInstance()->getStatus(INOTIFY_NORMAL_EXIT).toString(); qInfo() << "indexDataBaseStatus: " << indexDataBaseStatus; qInfo() << "contentIndexDataBaseStatus: " << contentIndexDataBaseStatus; -// qInfo() << "ocrIndexDatabaseStatus: " << ocrIndexDatabaseStatus; + // qInfo() << "ocrIndexDatabaseStatus: " << ocrIndexDatabaseStatus; qInfo() << "inotifyIndexStatus: " << inotifyIndexStatus; m_allDatadaseStatus = inotifyIndexStatus == "2" ? true : false; m_indexDatabaseStatus = indexDataBaseStatus == "2" ? true : false; m_contentIndexDatabaseStatus = contentIndexDataBaseStatus == "2" ? true : false; -// m_ocrIndexDatabaseStatus = ocrIndexDatabaseStatus == "2" ? true : false; + // m_ocrIndexDatabaseStatus = ocrIndexDatabaseStatus == "2" ? true : false; if(m_allDatadaseStatus && m_indexDatabaseStatus && m_contentIndexDatabaseStatus /*&& m_ocrIndexDatabaseStatus*/) { - m_semaphore.release(1); - return; + if(m_isFirstIndex) { + m_isFirstIndex = false; + m_semaphore.release(1); + return; + } + } else { + setPath(DirWatcher::getDirWatcher()->currentindexableDir()); + setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex()); } + IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "0"); + this->m_indexData = new QQueue>(); this->m_contentIndexData = new QQueue>(); // this->m_ocrIndexData = new QQueue>(); @@ -156,8 +173,6 @@ void FirstIndex::run() { qInfo() << "index dir" << DirWatcher::getDirWatcher()->currentindexableDir(); qInfo() << "index block dir" << DirWatcher::getDirWatcher()->currentBlackListOfIndex(); - setPath(DirWatcher::getDirWatcher()->currentindexableDir()); - setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex()); this->Traverse(); FileUtils::maxIndexCount = this->m_indexData->length(); @@ -165,7 +180,7 @@ void FirstIndex::run() { QtConcurrent::run(&m_pool, [&]() { sem.acquire(2); mutex1.unlock(); - if(m_allDatadaseStatus && m_indexDatabaseStatus) { + if(m_isFirstIndex && m_allDatadaseStatus && m_indexDatabaseStatus) { sem.release(2); return; } @@ -193,7 +208,7 @@ void FirstIndex::run() { QtConcurrent::run(&m_pool,[&]() { sem.acquire(2); mutex2.unlock(); - if(m_allDatadaseStatus && m_contentIndexDatabaseStatus) { + if(m_isFirstIndex && m_allDatadaseStatus && m_contentIndexDatabaseStatus) { sem.release(2); return; } @@ -236,7 +251,7 @@ void FirstIndex::run() { // mutex3.unlock(); // QQueue* tmpOcr = new QQueue(); // qDebug() << "m_ocr_index:" << m_ocr_index->size(); -// if(m_allDatadaseStatus && m_contentIndexDatabaseStatus) { +// if(m_isFirstIndex && m_allDatadaseStatus && m_contentIndexDatabaseStatus) { // sem.release(2); // return; // } @@ -296,8 +311,9 @@ void FirstIndex::run() { --FileUtils::indexStatus; } - m_semaphore.release(1); + m_isFirstIndex = false; //首次索引后置为false,后续start为添加索引目录时新建索引。 IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "2"); + m_semaphore.release(1); // int retval1 = write(fifo_fd, buffer, strlen(buffer)); // if(retval1 == -1) { // qWarning("write error\n"); diff --git a/libsearch/index/first-index.h b/libsearch/index/first-index.h index dd15fd7..c06a58b 100644 --- a/libsearch/index/first-index.h +++ b/libsearch/index/first-index.h @@ -46,7 +46,8 @@ class FirstIndex : public QThread, public Traverse_BFS { public: static FirstIndex* getInstance(); ~FirstIndex(); - virtual void DoSomething(const QFileInfo &) final; + virtual void work(const QFileInfo &) final; + void addIndexPath(const QString path, const QStringList blockList); protected: void run() override; @@ -61,6 +62,7 @@ private: bool m_contentIndexDatabaseStatus = false; bool m_ocrIndexDatabaseStatus = false; bool m_allDatadaseStatus = false; + bool m_isFirstIndex = true; QThreadPool m_pool; QQueue>* m_indexData = nullptr; diff --git a/libsearch/index/inotify-watch.cpp b/libsearch/index/inotify-watch.cpp index 4fb33c8..83ca6fd 100644 --- a/libsearch/index/inotify-watch.cpp +++ b/libsearch/index/inotify-watch.cpp @@ -14,7 +14,7 @@ UkuiSearch::InotifyWatch *UkuiSearch::InotifyWatch::getInstance() return global_instance_InotifyWatch; } -UkuiSearch::InotifyWatch::InotifyWatch(): Traverse_BFS() +UkuiSearch::InotifyWatch::InotifyWatch(): Traverse_BFS(), m_semaphore(INDEX_SEM, 0, QSystemSemaphore::AccessMode::Open) { qDebug() << "setInotifyMaxUserWatches start"; UkuiSearchQDBus usQDBus; @@ -83,7 +83,7 @@ bool InotifyWatch::removeWatch(const QString &path, bool removeFromDatabase) return true; } -void InotifyWatch::DoSomething(const QFileInfo &info) +void InotifyWatch::work(const QFileInfo &info) { qDebug() << info.fileName() << "-------" << info.absoluteFilePath(); if(info.isDir() && (!info.isSymLink())) { @@ -96,24 +96,31 @@ void InotifyWatch::DoSomething(const QFileInfo &info) PendingFileQueue::getInstance()->enqueue(f); } -void InotifyWatch::firstTraverse() +void InotifyWatch::firstTraverse(QStringList pathList, QStringList blockList) { + if(pathList.isEmpty()) { + pathList = m_pathList; + } + if(blockList.isEmpty()) { + blockList = m_blockList; + } + QQueue bfs; - for(QString blockPath : m_blockList) { - for(QString path : m_pathList) { + for(QString blockPath : blockList) { + for(QString path : pathList) { if(FileUtils::isOrUnder(path, blockPath)) { - m_pathList.removeOne(path); + pathList.removeOne(path); } } } - for(QString path : m_pathList) { + for(QString path : pathList) { addWatch(path); bfs.enqueue(path); } QFileInfoList list; QDir dir; - QStringList tmpList = m_blockList; + QStringList tmpList = blockList; dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); dir.setSorting(QDir::DirsFirst); while(!bfs.empty()) { @@ -139,6 +146,16 @@ void InotifyWatch::firstTraverse() } } +void InotifyWatch::addIndexPath(const QString path, const QStringList blockList) +{ + this->firstTraverse(QStringList() << path, blockList); +} + +void InotifyWatch::removeIndexPath(QString &path) +{ + +} + void InotifyWatch::stopWatch() { // if(this->isRunning()) { diff --git a/libsearch/index/inotify-watch.h b/libsearch/index/inotify-watch.h index ad72bbe..52af25c 100644 --- a/libsearch/index/inotify-watch.h +++ b/libsearch/index/inotify-watch.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -26,10 +27,12 @@ public: static InotifyWatch* getInstance(); bool addWatch(const QString &path); bool removeWatch(const QString &path, bool removeFromDatabase = true); - virtual void DoSomething(const QFileInfo &info) final; + virtual void work(const QFileInfo &info) final; - void firstTraverse(); + void firstTraverse(QStringList pathList = {}, QStringList blockList = {}); void stopWatch(); + void addIndexPath(const QString path, const QStringList blockList); + void removeIndexPath(QString &path); protected: void run() override; @@ -47,7 +50,7 @@ private: QSharedMemory *m_sharedMemory = nullptr; QMap m_pathMap; QMutex m_mutex; - + QSystemSemaphore m_semaphore; }; } diff --git a/libsearch/index/traverse_bfs.cpp b/libsearch/index/traverse_bfs.cpp index 1e434dc..622ba6e 100644 --- a/libsearch/index/traverse_bfs.cpp +++ b/libsearch/index/traverse_bfs.cpp @@ -35,6 +35,7 @@ void Traverse_BFS::Traverse() { } } for(QString path : m_pathList) { + work(QFileInfo(path)); bfs.enqueue(path); } @@ -62,7 +63,7 @@ void Traverse_BFS::Traverse() { if(i.isDir() && (!(i.isSymLink()))) { bfs.enqueue(i.absoluteFilePath()); } - DoSomething(i); + work(i); } } } diff --git a/libsearch/index/traverse_bfs.h b/libsearch/index/traverse_bfs.h index e52471a..080364b 100644 --- a/libsearch/index/traverse_bfs.h +++ b/libsearch/index/traverse_bfs.h @@ -30,7 +30,7 @@ public: Traverse_BFS() = default; void Traverse(); virtual ~Traverse_BFS() = default; - virtual void DoSomething(const QFileInfo&) = 0; + virtual void work(const QFileInfo&) = 0; void setPath(const QStringList&); void setBlockPath(const QStringList &pathList); protected: diff --git a/search-ukcc-plugin/search.h b/search-ukcc-plugin/search.h index 1172b16..a78ed71 100644 --- a/search-ukcc-plugin/search.h +++ b/search-ukcc-plugin/search.h @@ -22,7 +22,7 @@ #include #define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings" -#define SEARCH_METHOD_KEY "file-index-enable" +#define SEARCH_METHOD_KEY "fileIndexEnable" #define WEB_ENGINE_KEY "webEngine" //TODO #define CONFIG_FILE "/.config/org.ukui/ukui-search/ukui-search-block-dirs.conf" diff --git a/ukui-search-service/ukui-search-service.cpp b/ukui-search-service/ukui-search-service.cpp index b05452a..0d1b9f6 100644 --- a/ukui-search-service/ukui-search-service.cpp +++ b/ukui-search-service/ukui-search-service.cpp @@ -2,11 +2,9 @@ #include "ukui-search-service.h" #include "dir-watcher.h" #include "dir-watcher-adaptor.h" - +#include "common.h" #include -#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings" -#define SEARCH_METHOD_KEY "file-index-enable" using namespace UkuiSearch; UkuiSearchService::UkuiSearchService(int &argc, char *argv[], const QString &applicationName): QtSingleApplication (applicationName, argc, argv) { @@ -19,17 +17,8 @@ UkuiSearchService::UkuiSearchService(int &argc, char *argv[], const QString &app this->parseCmd(msg, true); }); - //注册服务 - QDBusConnection sessionBus = QDBusConnection::sessionBus(); - if (!sessionBus.registerService("com.ukui.search.fileindex.service")) { - qWarning() << "ukui-search-fileindex dbus register service failed reason:" << sessionBus.lastError(); - } - - if(!sessionBus.registerObject("/org/ukui/search/fileindex", DirWatcher::getDirWatcher(), QDBusConnection::ExportAdaptors)){ - qWarning() << "ukui-search-fileindex dbus register object failed reason:" << sessionBus.lastError(); - } - initGsettings(); + FileIndexManager::getInstance()->initIndexPathSetFunction(); } //parse cmd