From 5692fe6505232e5845691e36914aba3fd901b765 Mon Sep 17 00:00:00 2001 From: iaom <18504285112@163.com> Date: Fri, 4 Jun 2021 16:38:02 +0800 Subject: [PATCH] Use shared memory to synchronous the path map between inotify-index subprocess and main process. --- libsearch/index/inotify-index.cpp | 36 +++++++++++++++++++++++++++++-- libsearch/index/inotify-index.h | 2 ++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index 0396f9e..04351f1 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -18,6 +18,7 @@ * */ #include "inotify-index.h" +#include #define CREATE_FILE_NAME_INDEX \ indexQueue->enqueue(QVector() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); \ @@ -51,7 +52,7 @@ InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path) { UkuiSearchQDBus usQDBus; usQDBus.setInotifyMaxUserWatches(); qDebug() << "setInotifyMaxUserWatches end"; - + m_sharedMemory = new QSharedMemory("ukui-search-shared-map", this); } InotifyIndex::~InotifyIndex() { @@ -341,7 +342,24 @@ void InotifyIndex::run() { qDebug() << "select timeout!"; ::free(read_timeout); IndexGenerator::getInstance()->~IndexGenerator(); -// GlobalSettings::getInstance()->forceSync(); + QBuffer buffer; + QDataStream out(&buffer); + if (m_sharedMemory->isAttached()) { + m_sharedMemory->detach(); + } + buffer.open(QBuffer::ReadWrite); + out << currentPath; + int size = buffer.size(); + if (!m_sharedMemory->create(size)) { + qDebug() << "Create sharedMemory Error: " << m_sharedMemory->errorString(); + } else { + m_sharedMemory->lock(); + char *to = static_cast(m_sharedMemory->data()); + const char *from = buffer.data().constData(); + memcpy(to, from, qMin(size, m_sharedMemory->size())); + m_sharedMemory->unlock(); + } + // GlobalSettings::getInstance()->forceSync(); ::_exit(0); } else { memset(buf, 0x00, BUF_LEN); @@ -373,6 +391,20 @@ void InotifyIndex::run() { } else if(pid > 0) { memset(buf, 0x00, BUF_LEN); waitpid(pid, NULL, 0); + if (!m_sharedMemory->attach()) { + qDebug() << "SharedMemory attach Error: " << m_sharedMemory->errorString(); + } else { + QBuffer buffer; + QDataStream in(&buffer); + QMap pathMap; + m_sharedMemory->lock(); + buffer.setData(static_cast(m_sharedMemory->constData()), m_sharedMemory->size()); + buffer.open(QBuffer::ReadWrite); + in >> pathMap; + m_sharedMemory->unlock(); + m_sharedMemory->detach(); + currentPath = pathMap; + } --FileUtils::_index_status; } else { assert(false); diff --git a/libsearch/index/inotify-index.h b/libsearch/index/inotify-index.h index fe3dd7b..1b31f2a 100644 --- a/libsearch/index/inotify-index.h +++ b/libsearch/index/inotify-index.h @@ -22,6 +22,7 @@ #include #include +#include #include #include #include "index-generator.h" @@ -77,6 +78,7 @@ private: std::map::value_type("et", true), std::map::value_type("pdf", true) }; + QSharedMemory *m_sharedMemory = nullptr; }; }