From 4e3a9d93c6bd323de02cd7afa57d2fe87d904f3c Mon Sep 17 00:00:00 2001 From: iaom Date: Tue, 15 Mar 2022 13:53:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=BF=A1=E5=8F=B7=E9=87=8F?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E8=BF=9B=E7=A8=8B=E9=A1=BA=E5=BA=8F=EF=BC=8C?= =?UTF-8?q?=E8=A7=84=E9=81=BF=E7=94=B1=E4=BA=8E=E9=A6=96=E6=AC=A1=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E6=97=B6=E9=97=B4=E8=BF=87=E9=95=BF=E5=AF=BC=E8=87=B4?= =?UTF-8?q?inotify=E9=98=9F=E5=88=97=E8=BF=87=E9=95=BF=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libsearch/common.h | 1 + libsearch/index/first-index.cpp | 55 +++++++++++------------ libsearch/index/first-index.h | 7 ++- libsearch/index/inotify-watch.cpp | 22 --------- libsearch/index/inotify-watch.h | 10 +++-- libsearch/index/pending-file-queue.cpp | 7 ++- libsearch/index/pending-file-queue.h | 2 + libsearch/index/search-method-manager.cpp | 16 +++---- libsearch/index/search-method-manager.h | 4 +- 9 files changed, 56 insertions(+), 68 deletions(-) diff --git a/libsearch/common.h b/libsearch/common.h index 2aabf21..487a0dd 100644 --- a/libsearch/common.h +++ b/libsearch/common.h @@ -8,6 +8,7 @@ #define DIR_SEARCH_VALUE "1" #define LABEL_MAX_WIDTH 300 #define HOME_PATH QDir::homePath() +#define INDEX_SEM "ukui-search-index-sem" static const QStringList allAppPath = { QDir::homePath()+"/.local/share/applications/", "/usr/share/applications/" diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index 41ff695..9e26048 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -23,14 +23,23 @@ #include "dir-watcher.h" #include -#define NEW_QUEUE(a) a = new QQueue(); qDebug("---------------------------%s %s %s new at %d..",__FILE__,__FUNCTION__,#a,__LINE__); -//#define DELETE_QUEUE(a ) using namespace UkuiSearch; -FirstIndex::FirstIndex() { +FirstIndex *FirstIndex::m_instance = nullptr; +std::once_flag g_firstIndexInstanceFlag; +FirstIndex::FirstIndex() : m_semaphore(INDEX_SEM, 1, QSystemSemaphore::AccessMode::Open) +{ m_pool.setMaxThreadCount(2); m_pool.setExpiryTimeout(100); } +FirstIndex *FirstIndex::getInstance() +{ + std::call_once(g_firstIndexInstanceFlag, [] () { + m_instance = new FirstIndex; + }); + return m_instance; +} + FirstIndex::~FirstIndex() { qDebug() << "~FirstIndex"; if(this->q_index) @@ -127,17 +136,6 @@ void FirstIndex::run() { this->q_content_index = new QQueue>(); this->m_ocr_index = new QQueue>(); - int fifo_fd; - char buffer[2]; - memset(buffer, 0, sizeof(buffer)); - buffer[0] = 0x1; - buffer[1] = '\0'; - fifo_fd = open(UKUI_SEARCH_PIPE_PATH, O_RDWR); - if(fifo_fd == -1) { - perror("open fifo error\n"); - assert(false); - } - ++FileUtils::indexStatus; pid_t pid; pid = fork(); @@ -161,23 +159,18 @@ void FirstIndex::run() { QMutex mutex1, mutex2, mutex3; mutex1.lock(); mutex2.lock(); - mutex3.lock(); - sem.acquire(4); - sem.acquire(1); - mutex1.unlock(); qInfo() << "index dir" << DirWatcher::getDirWatcher()->currentindexableDir(); qInfo() << "index block dir" << DirWatcher::getDirWatcher()->currentBlackListOfIndex(); setPath(DirWatcher::getDirWatcher()->currentindexableDir()); setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex()); - Traverse(); + this->Traverse(); FileUtils::_max_index_count = this->q_index->length(); qDebug() << "max_index_count:" << FileUtils::_max_index_count; - sem.release(5); QtConcurrent::run(&m_pool, [&]() { sem.acquire(2); - mutex2.unlock(); + mutex1.unlock(); qDebug() << "index start;"; QQueue>* tmp1 = new QQueue>(); while(!this->q_index->empty()) { @@ -193,7 +186,7 @@ void FirstIndex::run() { }); QtConcurrent::run(&m_pool,[&]() { sem.acquire(2); - mutex3.unlock(); + mutex2.unlock(); QQueue* tmp2 = new QQueue(); qDebug() << "q_content_index:" << q_content_index->size(); while(!this->q_content_index->empty()) { @@ -220,9 +213,10 @@ void FirstIndex::run() { qDebug() << "content index end;"; sem.release(2); }); - //OCR功能暂时屏蔽 +// OCR功能暂时屏蔽 // QtConcurrent::run(&m_pool,[&]() { // sem.acquire(5); +// mutex3.unlock(); // QQueue* tmpOcr = new QQueue(); // qDebug() << "m_ocr_index:" << m_ocr_index->size(); // while(!this->m_ocr_index->empty()) { @@ -250,11 +244,11 @@ void FirstIndex::run() { // }); mutex1.lock(); mutex2.lock(); - mutex3.lock(); +// mutex3.lock(); sem.acquire(5); mutex1.unlock(); mutex2.unlock(); - mutex3.unlock(); +// mutex3.unlock(); if(this->q_index) delete this->q_index; @@ -279,12 +273,13 @@ void FirstIndex::run() { --FileUtils::indexStatus; } + m_semaphore.release(1); IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "2"); - int retval1 = write(fifo_fd, buffer, strlen(buffer)); - if(retval1 == -1) { - qWarning("write error\n"); - } - qDebug("write data ok!\n"); +// int retval1 = write(fifo_fd, buffer, strlen(buffer)); +// if(retval1 == -1) { +// qWarning("write error\n"); +// } +// qDebug("write data ok!\n"); QTime t2 = QTime::currentTime(); qWarning() << t1; qWarning() << t2; diff --git a/libsearch/index/first-index.h b/libsearch/index/first-index.h index 635836d..a85a88c 100644 --- a/libsearch/index/first-index.h +++ b/libsearch/index/first-index.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -43,14 +44,16 @@ namespace UkuiSearch { class FirstIndex : public QThread, public Traverse_BFS { public: - FirstIndex(); + static FirstIndex* getInstance(); ~FirstIndex(); virtual void DoSomething(const QFileInfo &) final; protected: void run() override; private: + FirstIndex(); FirstIndex(const FirstIndex&) = delete; void operator=(const FirstIndex&) = delete; + static FirstIndex *m_instance; bool bool_dataBaseStatusOK = false; bool bool_dataBaseExist = false; IndexGenerator* p_indexGenerator = nullptr; @@ -64,6 +67,8 @@ private: QQueue>* m_ocr_index; //xapian will auto commit per 10,000 changes, donnot change it!!! const size_t u_send_length = 8192; + QSystemSemaphore m_semaphore; + }; } diff --git a/libsearch/index/inotify-watch.cpp b/libsearch/index/inotify-watch.cpp index 52b797b..b7c0834 100644 --- a/libsearch/index/inotify-watch.cpp +++ b/libsearch/index/inotify-watch.cpp @@ -175,28 +175,6 @@ void InotifyWatch::run() setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex()); firstTraverse(); - int fifo_fd; - char buffer[2]; - memset(buffer, 0, sizeof(buffer)); - fifo_fd = open(UKUI_SEARCH_PIPE_PATH, O_RDWR); - if(fifo_fd == -1) { - qWarning() << "Open fifo error\n"; - assert(false); - } - int retval = read(fifo_fd, buffer, sizeof(buffer)); - if(retval == -1) { - qWarning() << "read error\n"; - assert(false); - } - qDebug("Read fifo[%s]", buffer); - - qDebug("Read data ok"); - close(fifo_fd); - if(buffer[0] & 0x1) { - qDebug("Data confirmed\n"); - } - unlink(UKUI_SEARCH_PIPE_PATH); - while(FileUtils::SearchMethod::INDEXSEARCH == FileUtils::searchMethod) { fd_set fds; FD_ZERO(&fds); diff --git a/libsearch/index/inotify-watch.h b/libsearch/index/inotify-watch.h index 3091ceb..919a862 100644 --- a/libsearch/index/inotify-watch.h +++ b/libsearch/index/inotify-watch.h @@ -2,17 +2,20 @@ #define INOTIFYWATCH_H #include -#include -#include +#include #include #include #include +#include +#include +#include +#include + #include "traverse_bfs.h" #include "ukui-search-qdbus.h" #include "index-status-recorder.h" #include "file-utils.h" -#include "first-index.h" #include "pending-file-queue.h" #include "common.h" namespace UkuiSearch { @@ -21,7 +24,6 @@ class InotifyWatch : public QThread, public Traverse_BFS Q_OBJECT public: static InotifyWatch* getInstance(); - bool addWatch(const QString &path); bool removeWatch(const QString &path, bool removeFromDatabase = true); virtual void DoSomething(const QFileInfo &info) final; diff --git a/libsearch/index/pending-file-queue.cpp b/libsearch/index/pending-file-queue.cpp index 27d4067..0b60048 100644 --- a/libsearch/index/pending-file-queue.cpp +++ b/libsearch/index/pending-file-queue.cpp @@ -22,7 +22,7 @@ #include using namespace UkuiSearch; static PendingFileQueue *global_instance_pending_file_queue = nullptr; -PendingFileQueue::PendingFileQueue(QObject *parent) : QThread(parent) +PendingFileQueue::PendingFileQueue(QObject *parent) : QThread(parent), m_semaphore(INDEX_SEM, 0, QSystemSemaphore::AccessMode::Open) { this->start(); @@ -72,10 +72,11 @@ PendingFileQueue::~PendingFileQueue() void PendingFileQueue::forceFinish() { - QThread::msleep(600); Q_EMIT timerStop(); this->quit(); this->wait(); + processCache(); + m_semaphore.release(1); } void PendingFileQueue::enqueue(const PendingFile &file) { @@ -128,6 +129,8 @@ void PendingFileQueue::enqueue(const PendingFile &file) void PendingFileQueue::run() { + //阻塞线程直到first-index进程结束 + m_semaphore.acquire(); exec(); } diff --git a/libsearch/index/pending-file-queue.h b/libsearch/index/pending-file-queue.h index c9ce399..dea0648 100644 --- a/libsearch/index/pending-file-queue.h +++ b/libsearch/index/pending-file-queue.h @@ -25,6 +25,7 @@ #include #include #include +#include #include "pending-file.h" #include "index-generator.h" @@ -57,6 +58,7 @@ private: QVector m_pendingFiles; QMutex m_mutex; QMutex m_timeoutMutex; + QSystemSemaphore m_semaphore; QThread *m_timerThread = nullptr; bool m_timeout = false; diff --git a/libsearch/index/search-method-manager.cpp b/libsearch/index/search-method-manager.cpp index 4c458b0..a4920da 100644 --- a/libsearch/index/search-method-manager.cpp +++ b/libsearch/index/search-method-manager.cpp @@ -1,20 +1,19 @@ #include "search-method-manager.h" -#include "dir-watcher.h" using namespace UkuiSearch; static SearchMethodManager* global_instance = nullptr; - -SearchMethodManager::SearchMethodManager() +SearchMethodManager::SearchMethodManager() : m_semaphore(INDEX_SEM, 1, QSystemSemaphore::AccessMode::Create) { + qDebug() << m_semaphore.errorString(); + m_fi = FirstIndex::getInstance(); m_iw = InotifyWatch::getInstance(); - } SearchMethodManager *SearchMethodManager::getInstance() { if(!global_instance) { - global_instance = new SearchMethodManager(); - } - return global_instance; + global_instance = new SearchMethodManager(); + } + return global_instance; } void SearchMethodManager::searchMethod(FileUtils::SearchMethod sm) { @@ -41,7 +40,8 @@ void SearchMethodManager::searchMethod(FileUtils::SearchMethod sm) { } qDebug() << "create fifo success\n"; qWarning() << "start first index"; - m_fi.start(); + m_semaphore.acquire(); + m_fi->start(); qWarning() << "start inotify index"; // InotifyIndex ii("/home"); // ii.start(); diff --git a/libsearch/index/search-method-manager.h b/libsearch/index/search-method-manager.h index fbd885d..5d07698 100644 --- a/libsearch/index/search-method-manager.h +++ b/libsearch/index/search-method-manager.h @@ -1,6 +1,7 @@ #ifndef SEARCHMETHODMANAGER_H #define SEARCHMETHODMANAGER_H +#include #include "first-index.h" //#include "inotify-index.h" #include "inotify-watch.h" @@ -11,9 +12,10 @@ public: void searchMethod(FileUtils::SearchMethod sm); private: SearchMethodManager(); - FirstIndex m_fi; + FirstIndex *m_fi; // InotifyIndex* m_ii; InotifyWatch *m_iw = nullptr; + QSystemSemaphore m_semaphore; }; }