From e046b609f15dc76c3149ce70bf0e3970f6168fa0 Mon Sep 17 00:00:00 2001 From: zhangzihao Date: Mon, 11 Jan 2021 16:59:50 +0800 Subject: [PATCH] add the feature of database won't be rebuild after restart and fix some bugs --- libsearch/global-settings.cpp | 8 +++--- libsearch/index/first-index.cpp | 39 ++++++++++++++++++++++++++--- libsearch/index/first-index.h | 4 ++- libsearch/index/index-generator.cpp | 5 ++++ libsearch/index/inotify-index.cpp | 11 +++++++- libsearch/index/inotify-index.h | 9 ++++++- src/main.cpp | 13 ++++++---- src/mainwindow.cpp | 2 +- 8 files changed, 74 insertions(+), 17 deletions(-) diff --git a/libsearch/global-settings.cpp b/libsearch/global-settings.cpp index a0eee27..4660381 100644 --- a/libsearch/global-settings.cpp +++ b/libsearch/global-settings.cpp @@ -3,14 +3,14 @@ #include #include "global-settings.h" -static GlobalSettings *global_instance = nullptr; +static GlobalSettings *global_instance_of_global_settings = nullptr; GlobalSettings *GlobalSettings::getInstance() { - if (!global_instance) { - global_instance = new GlobalSettings; + if (!global_instance_of_global_settings) { + global_instance_of_global_settings = new GlobalSettings; } - return global_instance; + return global_instance_of_global_settings; } GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index 2f0384f..0eaab8f 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -2,6 +2,25 @@ #include "first-index.h" #include + +void handler(int){ + qDebug() << "Recieved SIGTERM!"; + GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE,"2"); + GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE,"2"); + GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT,"2"); + + + qDebug() << "indexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString(); + qDebug() << "contentIndexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(CONTENT_INDEX_DATABASE_STATE).toString(); + +// InotifyIndex::getInstance("/home")->~InotifyIndex(); + qDebug() << "~IndexGenerator() end!" << endl; + + //wait linux kill this thread forcedly +// while (true); +} + + FirstIndex::FirstIndex(const QString& path) : Traverse_BFS(path) { QString indexDataBaseStatus = GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString(); @@ -37,8 +56,8 @@ FirstIndex::~FirstIndex() this->q_index = nullptr; delete this->q_content_index; this->q_content_index = nullptr; - delete this->p_indexGenerator; - this->p_indexGenerator; +// delete this->p_indexGenerator; +// this->p_indexGenerator; // delete this->mlm; // this->mlm = nullptr; } @@ -56,7 +75,12 @@ void FirstIndex::DoSomething(const QFileInfo& fileInfo){ void FirstIndex::run(){ if (this->bool_dataBaseExist){ if (this->bool_dataBaseStatusOK){ - this->quit(); + + //why??????????????????????????????????????????????????????????????? + //why not quit? +// this->quit(); + exit(0); +// return; // this->wait(); } else{ @@ -107,7 +131,14 @@ void FirstIndex::run(){ //don't use it now!!!! //MouseZhangZh // this->~FirstIndex(); -// qDebug() << "~FirstIndex end;"; + qDebug() << "~FirstIndex end;"; + + + + qDebug() << "sigset start!"; + sigset( SIGTERM, handler); + qDebug() << "sigset end!"; + this->quit(); // this->wait(); } diff --git a/libsearch/index/first-index.h b/libsearch/index/first-index.h index 9665bdc..5bd989b 100644 --- a/libsearch/index/first-index.h +++ b/libsearch/index/first-index.h @@ -3,10 +3,12 @@ #include #include +#include //#include #include "traverse_bfs.h" #include "global-settings.h" #include "index-generator.h" +#include "inotify-index.h" class FirstIndex : public QThread, public Traverse_BFS { @@ -21,7 +23,7 @@ private: void operator=(const FirstIndex&) = delete; bool bool_dataBaseStatusOK = false; bool bool_dataBaseExist = false; - IndexGenerator* p_indexGenerator; + IndexGenerator* p_indexGenerator = nullptr; //here should be refact // MessageListManager* mlm; diff --git a/libsearch/index/index-generator.cpp b/libsearch/index/index-generator.cpp index de5ce64..466f253 100644 --- a/libsearch/index/index-generator.cpp +++ b/libsearch/index/index-generator.cpp @@ -24,6 +24,8 @@ IndexGenerator *IndexGenerator::getInstance(bool rebuild) if (!global_instance) { global_instance = new IndexGenerator(rebuild); } + qDebug() << "global_instance" << global_instance; + qDebug() << "QThread::currentThreadId()" << QThread::currentThreadId(); return global_instance; } @@ -126,6 +128,9 @@ IndexGenerator::~IndexGenerator() GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE,"2"); GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE,"2"); GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT,"2"); + + qDebug() << "QThread::currentThreadId()" << QThread::currentThreadId(); + qDebug() << "~IndexGenerator 22222222222222222222"; } void IndexGenerator::insertIntoDatabase(Document doc) diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index f31a25a..0448bc3 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -10,6 +10,9 @@ InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path) qDebug() << "setInotifyMaxUserWatches end"; /*-------------ukuisearchdbus Test End-----------------*/ + + + m_fd = inotify_init(); qDebug() << "m_fd----------->" <~IndexGenerator(); } void InotifyIndex::DoSomething(const QFileInfo& fileInfo){ @@ -72,6 +75,11 @@ bool InotifyIndex::RemoveWatch(const QString &path){ return true; } +/* + * Symbolic Link!!!!!!!!!!!!!!!!!! + * Sysmbolic link to database dir will make a Infinite loop !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + * MouseZhangZh +*/ void InotifyIndex::run(){ @@ -110,6 +118,7 @@ void InotifyIndex::run(){ /*--------------------------------*/ // IndexGenerator::getInstance()->creatAllIndex(QQueue>(QVector() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0"))); + qDebug() << QString(currentPath[event->wd] + '/' + event->name); indexQueue->enqueue(QVector() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); IndexGenerator::getInstance()->creatAllIndex(indexQueue); indexQueue->clear(); diff --git a/libsearch/index/inotify-index.h b/libsearch/index/inotify-index.h index c9ae1ec..695ce76 100644 --- a/libsearch/index/inotify-index.h +++ b/libsearch/index/inotify-index.h @@ -9,11 +9,18 @@ #include "ukui-search-qdbus.h" #define BUF_LEN 1024 - +class InotifyIndex; +static InotifyIndex* global_instance_of_index = nullptr; class InotifyIndex : public QThread, public Traverse_BFS { Q_OBJECT public: + static InotifyIndex* getInstance(const QString& path){ + if (!global_instance_of_index) { + global_instance_of_index = new InotifyIndex(path); + } + return global_instance_of_index; + } InotifyIndex(const QString&); ~InotifyIndex(); diff --git a/src/main.cpp b/src/main.cpp index e9b28d4..901b029 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -92,6 +92,7 @@ int main(int argc, char *argv[]) { qInstallMessageHandler(messageOutput); + QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); @@ -111,11 +112,7 @@ int main(int argc, char *argv[]) parser.process(app); }*/ -// qDebug() << "main start"; -// FirstIndex fi("/home"); -// fi.start(); -// InotifyIndex ii("/home"); -// ii.start(); + /*-------------ukuisearchdbus Test start-----------------*/ // UkuiSearchQDBus usQDBus; // usQDBus.setInotifyMaxUserWatches(); @@ -170,6 +167,12 @@ int main(int argc, char *argv[]) // qDebug() << "main start"; // FirstIndex* fi = new FirstIndex("/home"); // fi->start(); + qDebug() << "main start"; + FirstIndex fi("/home"); + fi.start(); + InotifyIndex* ii = InotifyIndex::getInstance("/home"); +// InotifyIndex ii("/home"); + ii->start(); return app.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 8c428b3..192599f 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -271,7 +271,7 @@ void MainWindow::searchContent(QString searchcontent){ connect(search, &FileSearcher::resultContent, this, [ = ](QQueue> * contentQueue) { qWarning()<<"resultContent---"; }); - search->onKeywordSearch(searchcontent); +// search->onKeywordSearch(searchcontent); //将搜索结果加入列表 m_contentFrame->refreshSearchList(m_types, m_lists, searchcontent);