Optimized index status record mechanism.

This commit is contained in:
iaom 2021-05-13 11:17:07 +08:00
parent 546f80af48
commit 5ff0d20b1f
16 changed files with 100 additions and 48 deletions

View File

@ -40,8 +40,10 @@ unix {
!isEmpty(target.path): INSTALLS += target !isEmpty(target.path): INSTALLS += target
header.path = /usr/include/chinese-seg/ header.path = /usr/include/chinese-seg/
header.files += *.h cppjieba/* header.files += *.h
INSTALLS += header header_cppjieba headercppjieba.path = /usr/include/chinese-seg/cppjieba/
headercppjieba.files = cppjieba/*
INSTALLS += header headercppjieba
#DISTFILES += \ #DISTFILES += \
# jiaba/jieba.pri # jiaba/jieba.pri

7
libsearch/common.h Normal file
View File

@ -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

View File

@ -56,8 +56,6 @@
//#define FINISH_CREATING_INDEX 2 //#define FINISH_CREATING_INDEX 2
#define MAX_CONTENT_LENGTH 20480000 #define MAX_CONTENT_LENGTH 20480000
#define UKUI_SEARCH_PIPE_PATH (QDir::homePath()+"/.config/org.ukui/ukui-search/ukuisearch").toLocal8Bit().constData()
namespace Zeeker { namespace Zeeker {
class LIBSEARCH_EXPORT FileUtils { class LIBSEARCH_EXPORT FileUtils {
public: public:

View File

@ -246,28 +246,16 @@ QStringList GlobalSettings::getSearchRecord() {
// return values; // return values;
//} //}
//here should be override //this method is designed for main process settings only!!
//MouseZhangZh
void GlobalSettings::setValue(const QString &key, const QVariant &value) { void GlobalSettings::setValue(const QString &key, const QVariant &value) {
// qDebug()<<"setvalue========"<<key<<":"<<value;
m_cache.insert(key, value); m_cache.insert(key, value);
// m_settings->sync(); QtConcurrent::run([ = ]() {
// QtConcurrent::run([ = ]() { if (m_mutex.tryLock(1000)) {
// qDebug()<<m_settings->status(); m_settings->setValue(key, value);
// if (m_mutex.tryLock(1000)) { m_settings->sync();
// m_mutex.lock(); m_mutex.unlock();
}
// test++; });
// qDebug()<<"QtConcurrent::run=========start!!!"<<key<<":"<<value<<test<<QTime::currentTime();
m_settings->setValue(key, value);
// qDebug()<<"QtConcurrent::run=========sync!!!";
m_settings->sync();
// qDebug()<<"QtConcurrent::run========finished!!!"<<key<<":"<<value<<test<<QTime::currentTime();
// qDebug()<<"setvalue========sync!!!"<<key<<":"<<value;
// m_mutex.unlock();
// }
// });
} }
void GlobalSettings::forceSync(const QString &key) { void GlobalSettings::forceSync(const QString &key) {

View File

@ -40,10 +40,7 @@
#define THEME_GSETTINGS_ID "org.ukui.style" #define THEME_GSETTINGS_ID "org.ukui.style"
#define STYLE_NAME_KEY "styleName" #define STYLE_NAME_KEY "styleName"
#define FONT_SIZE_KEY "systemFontSize" #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 ENABLE_CREATE_INDEX_ASK_DIALOG "enable_create_index_ask_dialog"
#define WEB_ENGINE "web_engine" #define WEB_ENGINE "web_engine"
#define PATH_EMPTY 1; #define PATH_EMPTY 1;

View File

@ -68,9 +68,9 @@ void FirstIndex::run() {
} }
qDebug() << "create fifo success\n"; qDebug() << "create fifo success\n";
QString indexDataBaseStatus = GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString(); QString indexDataBaseStatus = IndexStatusRecorder::getInstance()->getStatus(INDEX_DATABASE_STATE).toString();
QString contentIndexDataBaseStatus = GlobalSettings::getInstance()->getValue(CONTENT_INDEX_DATABASE_STATE).toString(); QString contentIndexDataBaseStatus = IndexStatusRecorder::getInstance()->getStatus(CONTENT_INDEX_DATABASE_STATE).toString();
QString inotifyIndexStatus = GlobalSettings::getInstance()->getValue(INOTIFY_NORMAL_EXIT).toString(); QString inotifyIndexStatus = IndexStatusRecorder::getInstance()->getStatus(INOTIFY_NORMAL_EXIT).toString();
qDebug() << "indexDataBaseStatus: " << indexDataBaseStatus; qDebug() << "indexDataBaseStatus: " << indexDataBaseStatus;
qDebug() << "contentIndexDataBaseStatus: " << contentIndexDataBaseStatus; qDebug() << "contentIndexDataBaseStatus: " << contentIndexDataBaseStatus;
@ -82,8 +82,7 @@ void FirstIndex::run() {
} else { } else {
this->bool_dataBaseExist = true; this->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; this->bool_dataBaseStatusOK = false;
} else { } else {
this->bool_dataBaseStatusOK = true; this->bool_dataBaseStatusOK = true;
@ -197,8 +196,8 @@ void FirstIndex::run() {
delete p_indexGenerator; delete p_indexGenerator;
p_indexGenerator = nullptr; p_indexGenerator = nullptr;
// GlobalSettings::getInstance()->forceSync(); // GlobalSettings::getInstance()->forceSync();
GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2"); IndexStatusRecorder::getInstance()->setStatus(INDEX_DATABASE_STATE, "2");
GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2"); IndexStatusRecorder::getInstance()->setStatus(CONTENT_INDEX_DATABASE_STATE, "2");
::_exit(0); ::_exit(0);
} else if(pid < 0) { } else if(pid < 0) {
qWarning() << "First Index fork error!!"; qWarning() << "First Index fork error!!";
@ -207,7 +206,7 @@ void FirstIndex::run() {
--FileUtils::_index_status; --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)); int retval1 = write(fifo_fd, buffer, strlen(buffer));
if(retval1 == -1) { if(retval1 == -1) {
qWarning("write error\n"); qWarning("write error\n");

View File

@ -37,11 +37,11 @@
#include <syslog.h> #include <syslog.h>
//#include <QtConcurrent> //#include <QtConcurrent>
#include "traverse_bfs.h" #include "traverse_bfs.h"
#include "global-settings.h" #include "index-status-recorder.h"
#include "index-generator.h" #include "index-generator.h"
#include "inotify-index.h" #include "inotify-index.h"
#include "file-utils.h" #include "file-utils.h"
#include "common.h"
namespace Zeeker { namespace Zeeker {
class FirstIndex : public QThread, public Traverse_BFS { class FirstIndex : public QThread, public Traverse_BFS {
public: public:

View File

@ -27,7 +27,6 @@
#include <QFile> #include <QFile>
#include "file-utils.h" #include "file-utils.h"
#include "index-generator.h" #include "index-generator.h"
#include "global-settings.h"
#include "chinese-segmentation.h" #include "chinese-segmentation.h"
#include "construct-document.h" #include "construct-document.h"
#include <QStandardPaths> #include <QStandardPaths>
@ -91,7 +90,7 @@ bool IndexGenerator::creatAllIndex(QQueue<QVector<QString> > *messageList) {
} catch(const Xapian::Error &e) { } catch(const Xapian::Error &e) {
qWarning() << "creatAllIndex fail!" << QString::fromStdString(e.get_description()); qWarning() << "creatAllIndex fail!" << QString::fromStdString(e.get_description());
//need a record //need a record
GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "1"); IndexStatusRecorder::getInstance()->setStatus(INDEX_DATABASE_STATE, "1");
// FileUtils::_index_status &= ~0x1; // FileUtils::_index_status &= ~0x1;
assert(false); assert(false);
} }
@ -127,7 +126,7 @@ bool IndexGenerator::creatAllIndex(QQueue<QString> *messageList) {
m_database_content->commit(); m_database_content->commit();
} catch(const Xapian::Error &e) { } catch(const Xapian::Error &e) {
qWarning() << "creat content Index fail!" << QString::fromStdString(e.get_description()); 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; // FileUtils::_index_status &= ~0x2;
assert(false); assert(false);
} }

View File

@ -29,8 +29,10 @@
#include <QMutex> #include <QMutex>
#include <QQueue> #include <QQueue>
//#include <QMetaObject> //#include <QMetaObject>
#include "index-status-recorder.h"
#include "document.h" #include "document.h"
#include "file-reader.h" #include "file-reader.h"
#include "common.h"
namespace Zeeker { namespace Zeeker {
extern QList<Document> *_doc_list_path; extern QList<Document> *_doc_list_path;

View File

@ -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);
}

View File

@ -0,0 +1,27 @@
#ifndef INDEXSTATUSRECORDER_H
#define INDEXSTATUSRECORDER_H
#include <QObject>
#include <QSettings>
#include <QDir>
#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

View File

@ -6,6 +6,7 @@ HEADERS += \
$$PWD/file-reader.h \ $$PWD/file-reader.h \
$$PWD/first-index.h \ $$PWD/first-index.h \
$$PWD/index-generator.h \ $$PWD/index-generator.h \
$$PWD/index-status-recorder.h \
$$PWD/inotify-index.h \ $$PWD/inotify-index.h \
$$PWD/search-manager.h \ $$PWD/search-manager.h \
$$PWD/searchmethodmanager.h \ $$PWD/searchmethodmanager.h \
@ -18,6 +19,7 @@ SOURCES += \
$$PWD/file-reader.cpp \ $$PWD/file-reader.cpp \
$$PWD/first-index.cpp \ $$PWD/first-index.cpp \
$$PWD/index-generator.cpp \ $$PWD/index-generator.cpp \
$$PWD/index-status-recorder.cpp \
$$PWD/inotify-index.cpp \ $$PWD/inotify-index.cpp \
$$PWD/search-manager.cpp \ $$PWD/search-manager.cpp \
$$PWD/searchmethodmanager.cpp \ $$PWD/searchmethodmanager.cpp \

View File

@ -180,7 +180,7 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp) {
ssize_t numRead = 0; ssize_t numRead = 0;
numRead = tmp; numRead = tmp;
char * p = const_cast<char*>(buf); char * p = const_cast<char*>(buf);
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "0"); IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "0");
for(; p < buf + numRead;) { for(; p < buf + numRead;) {
struct inotify_event * event = reinterpret_cast<inotify_event *>(p); struct inotify_event * event = reinterpret_cast<inotify_event *>(p);
if(event->name[0] != '.') { if(event->name[0] != '.') {
@ -237,7 +237,7 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp) {
next: next:
p += sizeof(struct inotify_event) + event->len; p += sizeof(struct inotify_event) + event->len;
} }
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "2");
delete indexQueue; delete indexQueue;
indexQueue = nullptr; indexQueue = nullptr;
delete contentIndexQueue; delete contentIndexQueue;
@ -285,11 +285,12 @@ void InotifyIndex::run() {
if(numRead == -1) { if(numRead == -1) {
printf("\033[1;31;40mread event error\033[0m\n"); 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); fflush(stdout);
assert(false); assert(false);
} }
//TODO: Merge multiple signals.
char * tmp = const_cast<char*>(buf); char * tmp = const_cast<char*>(buf);
for(; tmp < buf + numRead;) { for(; tmp < buf + numRead;) {
@ -334,7 +335,7 @@ void InotifyIndex::run() {
if(rc < 0) { if(rc < 0) {
// error // error
qWarning() << "select result < 0, error!"; qWarning() << "select result < 0, error!";
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "1"); IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "1");
assert(false); assert(false);
} else if(rc == 0) { } else if(rc == 0) {
qDebug() << "select timeout!"; qDebug() << "select timeout!";
@ -351,6 +352,7 @@ void InotifyIndex::run() {
assert(false); assert(false);
} }
//TODO: Merge multiple signals.
char * tmp = const_cast<char*>(buf); char * tmp = const_cast<char*>(buf);
for(; tmp < buf + numRead; ) { for(; tmp < buf + numRead; ) {
@ -378,7 +380,7 @@ void InotifyIndex::run() {
} }
if(FileUtils::SearchMethod::DIRECTSEARCH == FileUtils::searchMethod) { 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); RemoveWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation), false);
} }
} }

View File

@ -27,9 +27,10 @@
#include "index-generator.h" #include "index-generator.h"
#include "traverse_bfs.h" #include "traverse_bfs.h"
#include "ukui-search-qdbus.h" #include "ukui-search-qdbus.h"
#include "global-settings.h" #include "index-status-recorder.h"
#include "file-utils.h" #include "file-utils.h"
#include "first-index.h" #include "first-index.h"
#include "common.h"
#define BUF_LEN 1024000 #define BUF_LEN 1024000

View File

@ -3,7 +3,6 @@
#include "first-index.h" #include "first-index.h"
#include "inotify-index.h" #include "inotify-index.h"
namespace Zeeker { namespace Zeeker {
class SearchMethodManager { class SearchMethodManager {
public: public:

View File

@ -42,6 +42,7 @@ SOURCES += \
libsearch.cpp libsearch.cpp
HEADERS += \ HEADERS += \
common.h \
file-utils.h \ file-utils.h \
global-settings.h \ global-settings.h \
gobject-template.h \ gobject-template.h \