From 5ff0d20b1f7cdf9724f417a97f2ef0ac6296e7fe Mon Sep 17 00:00:00 2001 From: iaom <18504285112@163.com> Date: Thu, 13 May 2021 11:17:07 +0800 Subject: [PATCH] Optimized index status record mechanism. --- .../libchinese-segmentation.pro | 6 ++-- libsearch/common.h | 7 +++++ libsearch/file-utils.h | 2 -- libsearch/global-settings.cpp | 28 ++++++------------- libsearch/global-settings.h | 5 +--- libsearch/index/first-index.cpp | 15 +++++----- libsearch/index/first-index.h | 4 +-- libsearch/index/index-generator.cpp | 5 ++-- libsearch/index/index-generator.h | 2 ++ libsearch/index/index-status-recorder.cpp | 28 +++++++++++++++++++ libsearch/index/index-status-recorder.h | 27 ++++++++++++++++++ libsearch/index/index.pri | 2 ++ libsearch/index/inotify-index.cpp | 12 ++++---- libsearch/index/inotify-index.h | 3 +- libsearch/index/searchmethodmanager.h | 1 - libsearch/libsearch.pro | 1 + 16 files changed, 100 insertions(+), 48 deletions(-) create mode 100644 libsearch/common.h create mode 100644 libsearch/index/index-status-recorder.cpp create mode 100644 libsearch/index/index-status-recorder.h diff --git a/libchinese-segmentation/libchinese-segmentation.pro b/libchinese-segmentation/libchinese-segmentation.pro index a082395..583f794 100644 --- a/libchinese-segmentation/libchinese-segmentation.pro +++ b/libchinese-segmentation/libchinese-segmentation.pro @@ -40,8 +40,10 @@ unix { !isEmpty(target.path): INSTALLS += target header.path = /usr/include/chinese-seg/ - header.files += *.h cppjieba/* - INSTALLS += header header_cppjieba + header.files += *.h + headercppjieba.path = /usr/include/chinese-seg/cppjieba/ + headercppjieba.files = cppjieba/* + INSTALLS += header headercppjieba #DISTFILES += \ # jiaba/jieba.pri diff --git a/libsearch/common.h b/libsearch/common.h new file mode 100644 index 0000000..d3c238c --- /dev/null +++ b/libsearch/common.h @@ -0,0 +1,7 @@ +#ifndef COMMON_H +#define COMMON_H + +#define UKUI_SEARCH_PIPE_PATH (QDir::homePath()+"/.config/org.ukui/ukui-search/ukuisearch").toLocal8Bit().constData() + +//TODO Put things that needed to be put here here. +#endif // COMMON_H diff --git a/libsearch/file-utils.h b/libsearch/file-utils.h index 0e036bd..a352d63 100644 --- a/libsearch/file-utils.h +++ b/libsearch/file-utils.h @@ -56,8 +56,6 @@ //#define FINISH_CREATING_INDEX 2 #define MAX_CONTENT_LENGTH 20480000 -#define UKUI_SEARCH_PIPE_PATH (QDir::homePath()+"/.config/org.ukui/ukui-search/ukuisearch").toLocal8Bit().constData() - namespace Zeeker { class LIBSEARCH_EXPORT FileUtils { public: diff --git a/libsearch/global-settings.cpp b/libsearch/global-settings.cpp index aa3e2f7..36d3ba6 100644 --- a/libsearch/global-settings.cpp +++ b/libsearch/global-settings.cpp @@ -246,28 +246,16 @@ QStringList GlobalSettings::getSearchRecord() { // return values; //} -//here should be override -//MouseZhangZh +//this method is designed for main process settings only!! void GlobalSettings::setValue(const QString &key, const QVariant &value) { - // qDebug()<<"setvalue========"<sync(); -// QtConcurrent::run([ = ]() { - // qDebug()<status(); -// if (m_mutex.tryLock(1000)) { - // m_mutex.lock(); - -// test++; -// qDebug()<<"QtConcurrent::run=========start!!!"<setValue(key, value); -// qDebug()<<"QtConcurrent::run=========sync!!!"; - m_settings->sync(); -// qDebug()<<"QtConcurrent::run========finished!!!"<setValue(key, value); + m_settings->sync(); + m_mutex.unlock(); + } + }); } void GlobalSettings::forceSync(const QString &key) { diff --git a/libsearch/global-settings.h b/libsearch/global-settings.h index e65db6e..7815eb2 100644 --- a/libsearch/global-settings.h +++ b/libsearch/global-settings.h @@ -40,10 +40,7 @@ #define THEME_GSETTINGS_ID "org.ukui.style" #define STYLE_NAME_KEY "styleName" #define FONT_SIZE_KEY "systemFontSize" -#define INDEX_DATABASE_STATE "index_database_state" -#define CONTENT_INDEX_DATABASE_STATE "content_index_database_state" -#define INDEX_GENERATOR_NORMAL_EXIT "index_generator_normal_exit" -#define INOTIFY_NORMAL_EXIT "inotify_normal_exit" + #define ENABLE_CREATE_INDEX_ASK_DIALOG "enable_create_index_ask_dialog" #define WEB_ENGINE "web_engine" #define PATH_EMPTY 1; diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index 2e86104..b8fafb9 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -68,9 +68,9 @@ void FirstIndex::run() { } qDebug() << "create fifo success\n"; - QString indexDataBaseStatus = GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString(); - QString contentIndexDataBaseStatus = GlobalSettings::getInstance()->getValue(CONTENT_INDEX_DATABASE_STATE).toString(); - QString inotifyIndexStatus = GlobalSettings::getInstance()->getValue(INOTIFY_NORMAL_EXIT).toString(); + QString indexDataBaseStatus = IndexStatusRecorder::getInstance()->getStatus(INDEX_DATABASE_STATE).toString(); + QString contentIndexDataBaseStatus = IndexStatusRecorder::getInstance()->getStatus(CONTENT_INDEX_DATABASE_STATE).toString(); + QString inotifyIndexStatus = IndexStatusRecorder::getInstance()->getStatus(INOTIFY_NORMAL_EXIT).toString(); qDebug() << "indexDataBaseStatus: " << indexDataBaseStatus; qDebug() << "contentIndexDataBaseStatus: " << contentIndexDataBaseStatus; @@ -82,8 +82,7 @@ void FirstIndex::run() { } else { this->bool_dataBaseExist = true; } -// if(indexDataBaseStatus != "2" || contentIndexDataBaseStatus != "2" || inotifyIndexStatus != "2") { - if(indexDataBaseStatus == "1" or contentIndexDataBaseStatus == "1" or inotifyIndexStatus != "2"){ + if(indexDataBaseStatus != "2" || contentIndexDataBaseStatus != "2" || inotifyIndexStatus != "2") { this->bool_dataBaseStatusOK = false; } else { this->bool_dataBaseStatusOK = true; @@ -197,8 +196,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"); + IndexStatusRecorder::getInstance()->setStatus(INDEX_DATABASE_STATE, "2"); + IndexStatusRecorder::getInstance()->setStatus(CONTENT_INDEX_DATABASE_STATE, "2"); ::_exit(0); } else if(pid < 0) { qWarning() << "First Index fork error!!"; @@ -207,7 +206,7 @@ void FirstIndex::run() { --FileUtils::_index_status; } - GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); + IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "2"); 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 9424c3e..7455992 100644 --- a/libsearch/index/first-index.h +++ b/libsearch/index/first-index.h @@ -37,11 +37,11 @@ #include //#include #include "traverse_bfs.h" -#include "global-settings.h" +#include "index-status-recorder.h" #include "index-generator.h" #include "inotify-index.h" #include "file-utils.h" - +#include "common.h" namespace Zeeker { class FirstIndex : public QThread, public Traverse_BFS { public: diff --git a/libsearch/index/index-generator.cpp b/libsearch/index/index-generator.cpp index 6affca9..e650271 100644 --- a/libsearch/index/index-generator.cpp +++ b/libsearch/index/index-generator.cpp @@ -27,7 +27,6 @@ #include #include "file-utils.h" #include "index-generator.h" -#include "global-settings.h" #include "chinese-segmentation.h" #include "construct-document.h" #include @@ -91,7 +90,7 @@ bool IndexGenerator::creatAllIndex(QQueue > *messageList) { } catch(const Xapian::Error &e) { qWarning() << "creatAllIndex fail!" << QString::fromStdString(e.get_description()); //need a record - GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "1"); + IndexStatusRecorder::getInstance()->setStatus(INDEX_DATABASE_STATE, "1"); // FileUtils::_index_status &= ~0x1; assert(false); } @@ -127,7 +126,7 @@ bool IndexGenerator::creatAllIndex(QQueue *messageList) { m_database_content->commit(); } catch(const Xapian::Error &e) { qWarning() << "creat content Index fail!" << QString::fromStdString(e.get_description()); - GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "1"); + IndexStatusRecorder::getInstance()->setStatus(CONTENT_INDEX_DATABASE_STATE, "1"); // FileUtils::_index_status &= ~0x2; assert(false); } diff --git a/libsearch/index/index-generator.h b/libsearch/index/index-generator.h index 80b2355..bc614b2 100644 --- a/libsearch/index/index-generator.h +++ b/libsearch/index/index-generator.h @@ -29,8 +29,10 @@ #include #include //#include +#include "index-status-recorder.h" #include "document.h" #include "file-reader.h" +#include "common.h" namespace Zeeker { extern QList *_doc_list_path; diff --git a/libsearch/index/index-status-recorder.cpp b/libsearch/index/index-status-recorder.cpp new file mode 100644 index 0000000..63cba32 --- /dev/null +++ b/libsearch/index/index-status-recorder.cpp @@ -0,0 +1,28 @@ +#include "index-status-recorder.h" + +using namespace Zeeker; +static IndexStatusRecorder *global_instance_indexStatusRecorder = nullptr; + +IndexStatusRecorder *IndexStatusRecorder::getInstance() +{ + if(!global_instance_indexStatusRecorder) { + global_instance_indexStatusRecorder = new IndexStatusRecorder; + } + return global_instance_indexStatusRecorder; +} + +void IndexStatusRecorder::setStatus(const QString &key, const QVariant &value) +{ + m_status->setValue(key, value); + m_status->sync(); +} + +const QVariant IndexStatusRecorder::getStatus(const QString &key) +{ + return m_status->value(key); +} + +IndexStatusRecorder::IndexStatusRecorder(QObject *parent) : QObject(parent) +{ + m_status = new QSettings(INDEX_STATUS, QSettings::IniFormat, this); +} diff --git a/libsearch/index/index-status-recorder.h b/libsearch/index/index-status-recorder.h new file mode 100644 index 0000000..25f8009 --- /dev/null +++ b/libsearch/index/index-status-recorder.h @@ -0,0 +1,27 @@ +#ifndef INDEXSTATUSRECORDER_H +#define INDEXSTATUSRECORDER_H + +#include +#include +#include +#define CONTENT_INDEX_DATABASE_STATE "content_index_database_state" +#define INDEX_DATABASE_STATE "index_database_state" +#define INOTIFY_NORMAL_EXIT "inotify_normal_exit" +#define INDEX_STATUS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-index-status.conf" +namespace Zeeker { +//fixme: we need a better way to record index status. +class IndexStatusRecorder : public QObject +{ + Q_OBJECT +public: + static IndexStatusRecorder *getInstance(); + void setStatus(const QString& key, const QVariant& value); + const QVariant getStatus(const QString& key); + +private: + explicit IndexStatusRecorder(QObject *parent = nullptr); + QSettings *m_status; +}; +} + +#endif // INDEXSTATUSRECORDER_H diff --git a/libsearch/index/index.pri b/libsearch/index/index.pri index 9ae146d..6ceadc6 100644 --- a/libsearch/index/index.pri +++ b/libsearch/index/index.pri @@ -6,6 +6,7 @@ HEADERS += \ $$PWD/file-reader.h \ $$PWD/first-index.h \ $$PWD/index-generator.h \ + $$PWD/index-status-recorder.h \ $$PWD/inotify-index.h \ $$PWD/search-manager.h \ $$PWD/searchmethodmanager.h \ @@ -18,6 +19,7 @@ SOURCES += \ $$PWD/file-reader.cpp \ $$PWD/first-index.cpp \ $$PWD/index-generator.cpp \ + $$PWD/index-status-recorder.cpp \ $$PWD/inotify-index.cpp \ $$PWD/search-manager.cpp \ $$PWD/searchmethodmanager.cpp \ diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index 5cfe399..0396f9e 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -180,7 +180,7 @@ 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"); + IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "0"); for(; p < buf + numRead;) { struct inotify_event * event = reinterpret_cast(p); if(event->name[0] != '.') { @@ -237,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"); + IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "2"); delete indexQueue; indexQueue = nullptr; delete contentIndexQueue; @@ -285,11 +285,12 @@ void InotifyIndex::run() { if(numRead == -1) { printf("\033[1;31;40mread event error\033[0m\n"); - GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "1"); + IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "1"); fflush(stdout); assert(false); } + //TODO: Merge multiple signals. char * tmp = const_cast(buf); for(; tmp < buf + numRead;) { @@ -334,7 +335,7 @@ void InotifyIndex::run() { if(rc < 0) { // error qWarning() << "select result < 0, error!"; - GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "1"); + IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "1"); assert(false); } else if(rc == 0) { qDebug() << "select timeout!"; @@ -351,6 +352,7 @@ void InotifyIndex::run() { assert(false); } + //TODO: Merge multiple signals. char * tmp = const_cast(buf); for(; tmp < buf + numRead; ) { @@ -378,7 +380,7 @@ void InotifyIndex::run() { } if(FileUtils::SearchMethod::DIRECTSEARCH == FileUtils::searchMethod) { - GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "3"); + IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "3"); RemoveWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation), false); } } diff --git a/libsearch/index/inotify-index.h b/libsearch/index/inotify-index.h index b99cef2..fe3dd7b 100644 --- a/libsearch/index/inotify-index.h +++ b/libsearch/index/inotify-index.h @@ -27,9 +27,10 @@ #include "index-generator.h" #include "traverse_bfs.h" #include "ukui-search-qdbus.h" -#include "global-settings.h" +#include "index-status-recorder.h" #include "file-utils.h" #include "first-index.h" +#include "common.h" #define BUF_LEN 1024000 diff --git a/libsearch/index/searchmethodmanager.h b/libsearch/index/searchmethodmanager.h index 62886ba..1c842ca 100644 --- a/libsearch/index/searchmethodmanager.h +++ b/libsearch/index/searchmethodmanager.h @@ -3,7 +3,6 @@ #include "first-index.h" #include "inotify-index.h" - namespace Zeeker { class SearchMethodManager { public: diff --git a/libsearch/libsearch.pro b/libsearch/libsearch.pro index d5b509c..4b85b0d 100644 --- a/libsearch/libsearch.pro +++ b/libsearch/libsearch.pro @@ -42,6 +42,7 @@ SOURCES += \ libsearch.cpp HEADERS += \ + common.h \ file-utils.h \ global-settings.h \ gobject-template.h \