From 546f80af48b0c48734fff7b7aad2ffafb3c3569c Mon Sep 17 00:00:00 2001 From: rookie-J Date: Wed, 12 May 2021 15:01:20 +0800 Subject: [PATCH] Optimize the process of Qsettings.setValue(). --- libsearch/global-settings.cpp | 15 ++++++++----- libsearch/global-settings.h | 2 ++ libsearch/index/first-index.cpp | 6 +++-- libsearch/index/index-generator.cpp | 8 +++---- libsearch/index/inotify-index.cpp | 35 ++++++++++++++++++----------- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/libsearch/global-settings.cpp b/libsearch/global-settings.cpp index da596c2..aa3e2f7 100644 --- a/libsearch/global-settings.cpp +++ b/libsearch/global-settings.cpp @@ -252,17 +252,22 @@ void GlobalSettings::setValue(const QString &key, const QVariant &value) { // qDebug()<<"setvalue========"<sync(); - QtConcurrent::run([ = ]() { +// QtConcurrent::run([ = ]() { // qDebug()<status(); // if (m_mutex.tryLock(1000)) { // m_mutex.lock(); - m_settings->setValue(key, value); - // qDebug()<<"setvalue========finish!!!"<sync(); + +// test++; +// qDebug()<<"QtConcurrent::run=========start!!!"<setValue(key, value); +// qDebug()<<"QtConcurrent::run=========sync!!!"; + m_settings->sync(); +// qDebug()<<"QtConcurrent::run========finished!!!"<bool_dataBaseExist = true; } - if(indexDataBaseStatus != "2" || contentIndexDataBaseStatus != "2" || inotifyIndexStatus != "2") { +// if(indexDataBaseStatus != "2" || contentIndexDataBaseStatus != "2" || inotifyIndexStatus != "2") { + if(indexDataBaseStatus == "1" or contentIndexDataBaseStatus == "1" or inotifyIndexStatus != "2"){ this->bool_dataBaseStatusOK = false; } else { this->bool_dataBaseStatusOK = true; @@ -196,6 +197,8 @@ void FirstIndex::run() { delete p_indexGenerator; p_indexGenerator = nullptr; // GlobalSettings::getInstance()->forceSync(); + GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2"); + GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2"); ::_exit(0); } else if(pid < 0) { qWarning() << "First Index fork error!!"; @@ -204,7 +207,6 @@ void FirstIndex::run() { --FileUtils::_index_status; } - GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); int retval1 = write(fifo_fd, buffer, strlen(buffer)); if(retval1 == -1) { diff --git a/libsearch/index/index-generator.cpp b/libsearch/index/index-generator.cpp index bbaeef4..6affca9 100644 --- a/libsearch/index/index-generator.cpp +++ b/libsearch/index/index-generator.cpp @@ -69,7 +69,7 @@ bool IndexGenerator::creatAllIndex(QQueue > *messageList) { return false; } qDebug() << "begin creatAllIndex"; - GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "0"); +// GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "0"); try { // m_indexer = new Xapian::TermGenerator(); // m_indexer.set_database(*m_database_path); @@ -95,7 +95,7 @@ bool IndexGenerator::creatAllIndex(QQueue > *messageList) { // FileUtils::_index_status &= ~0x1; assert(false); } - GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2"); +// GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2"); qDebug() << "finish creatAllIndex"; // FileUtils::_index_status &= ~0x1; _doc_list_path->clear(); @@ -114,7 +114,7 @@ bool IndexGenerator::creatAllIndex(QQueue *messageList) { int size = _doc_list_content->size(); qDebug() << "begin creatAllIndex for content" << size; if(!size == 0) { - GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "0"); +// GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "0"); try { int count = 0; for(auto i : *_doc_list_content) { @@ -131,7 +131,7 @@ bool IndexGenerator::creatAllIndex(QQueue *messageList) { // FileUtils::_index_status &= ~0x2; assert(false); } - GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2"); +// GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2"); // FileUtils::_index_status &= ~0x2; qDebug() << "finish creatAllIndex for content"; _doc_list_content->clear(); diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index 49ad5df..5cfe399 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -180,12 +180,14 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp) { ssize_t numRead = 0; numRead = tmp; char * p = const_cast(buf); - + GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "0"); for(; p < buf + numRead;) { struct inotify_event * event = reinterpret_cast(p); - qDebug() << "Read Event event->wd: " << event->wd; - qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask; if(event->name[0] != '.') { + + qDebug() << "Read Event event->wd: " << event->wd; + qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask; + qDebug() << QString(currentPath[event->wd] + '/' + event->name); // switch (event->mask) { if(event->mask & IN_CREATE) { @@ -235,7 +237,7 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp) { next: p += sizeof(struct inotify_event) + event->len; } - + GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); delete indexQueue; indexQueue = nullptr; delete contentIndexQueue; @@ -288,14 +290,13 @@ void InotifyIndex::run() { assert(false); } - char * tmp = const_cast(buf); for(; tmp < buf + numRead;) { struct inotify_event * event = reinterpret_cast(tmp); - // qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask; if(event->name[0] != '.') { - GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "0"); +// qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask; +// qDebug("mask:0x%x,",event->mask); break; } tmp += sizeof(struct inotify_event) + event->len; @@ -318,7 +319,6 @@ void InotifyIndex::run() { qDebug() << "read"; } eventProcess(buf, numRead); - GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); fd_set read_fds; int rc; @@ -343,7 +343,6 @@ void InotifyIndex::run() { // GlobalSettings::getInstance()->forceSync(); ::_exit(0); } else { - GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "0"); memset(buf, 0x00, BUF_LEN); numRead = read(m_fd, buf, BUF_LEN); if(numRead == -1) { @@ -351,9 +350,22 @@ void InotifyIndex::run() { fflush(stdout); assert(false); } + + char * tmp = const_cast(buf); + + for(; tmp < buf + numRead; ) { + struct inotify_event * event = reinterpret_cast(tmp); + if(event->name[0] != '.') { + break; + } + tmp += sizeof(struct inotify_event) + event->len; + } + if(tmp >= buf + numRead) { + continue; + } + qDebug() << "Read " << numRead << " bytes from inotify fd"; this->eventProcess(buf, numRead); - GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); } } } else if(pid > 0) { @@ -369,7 +381,4 @@ void InotifyIndex::run() { GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "3"); RemoveWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation), false); } - - - }