From 7e3bb085a8851319f4d2836c11a1fe445f615bdd Mon Sep 17 00:00:00 2001 From: zhangpengfei Date: Tue, 26 Jan 2021 11:32:02 +0800 Subject: [PATCH] Optimized index sub-process code. --- libsearch/index/first-index.cpp | 67 ++++++++--------------------- libsearch/index/index-generator.cpp | 9 +++- libsearch/index/inotify-index.cpp | 3 ++ 3 files changed, 29 insertions(+), 50 deletions(-) diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index 03a450b..da232c8 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -63,6 +63,7 @@ void FirstIndex::DoSomething(const QFileInfo& fileInfo){ } void FirstIndex::run(){ + int fifo_fd; char buffer[2]; memset(buffer, 0, sizeof(buffer)); @@ -75,32 +76,6 @@ void FirstIndex::run(){ assert(false); } - if (this->bool_dataBaseExist){ - if (this->bool_dataBaseStatusOK){ - int retval = write(fifo_fd, buffer, strlen(buffer)); - if(retval == -1) - { - perror("write error\n"); - } - printf("write data ok!\n"); - - //why??????????????????????????????????????????????????????????????? - //why not quit? -// this->quit(); -// exit(0); - return; -// this->wait(); - } - else{ - //if the parameter is false, index won't be rebuild - //if it is true, index will be rebuild - p_indexGenerator = IndexGenerator::getInstance(true,this); - } - } - else{ - p_indexGenerator = IndexGenerator::getInstance(false,this); - } - // this->q_content_index->enqueue(QString("/home/zhangzihao/Desktop/qwerty/四库全书.txt")); // this->p_indexGenerator->creatAllIndex(this->q_content_index); @@ -114,6 +89,19 @@ void FirstIndex::run(){ { prctl(PR_SET_PDEATHSIG, SIGTERM); prctl(PR_SET_NAME,"first-index"); + if (this->bool_dataBaseExist){ + if (this->bool_dataBaseStatusOK){ + ::exit(0); + } + else{ + //if the parameter is false, index won't be rebuild + //if it is true, index will be rebuild + p_indexGenerator = IndexGenerator::getInstance(true,this); + } + } + else{ + p_indexGenerator = IndexGenerator::getInstance(false,this); + } QSemaphore sem(5); QMutex mutex1, mutex2, mutex3; mutex1.lock(); @@ -151,17 +139,6 @@ void FirstIndex::run(){ mutex1.unlock(); mutex2.unlock(); mutex3.unlock(); - ::exit(0); - - - - - // qDebug() << "first index end;"; - //don't use it now!!!! - //MouseZhangZh - // this->~FirstIndex(); - // qDebug() << "~FirstIndex end;" - if (this->q_index) delete this->q_index; @@ -172,9 +149,7 @@ void FirstIndex::run(){ if (p_indexGenerator) delete p_indexGenerator; p_indexGenerator = nullptr; - - QThreadPool::globalInstance()->releaseThread(); - QThreadPool::globalInstance()->waitForDone(); + ::exit(0); } else if(pid < 0) { @@ -186,17 +161,13 @@ void FirstIndex::run(){ --FileUtils::_index_status; } + int retval = write(fifo_fd, buffer, strlen(buffer)); if(retval == -1) { - perror("write error\n"); + qWarning("write error\n"); } - printf("write data ok!\n"); + qDebug("write data ok!\n"); + return; - - //quit() is shit!!! -// return; -// exit(0); - this->quit(); -// this->wait(); } diff --git a/libsearch/index/index-generator.cpp b/libsearch/index/index-generator.cpp index 265507c..bfca0af 100644 --- a/libsearch/index/index-generator.cpp +++ b/libsearch/index/index-generator.cpp @@ -82,6 +82,7 @@ bool IndexGenerator::creatAllIndex(QQueue *messageList) { // FileUtils::_index_status |= 0x2; HandlePathList(messageList); + qDebug()<<"begin creatAllIndex"; int size = _doc_list_content->size(); if(!size == 0) { @@ -105,6 +106,7 @@ bool IndexGenerator::creatAllIndex(QQueue *messageList) assert(false); } // FileUtils::_index_status &= ~0x2; + qDebug()<<"finish creatAllIndex"; _doc_list_content->clear(); delete _doc_list_content; _doc_list_content = nullptr; @@ -142,10 +144,13 @@ IndexGenerator::~IndexGenerator() QMutexLocker locker(&m_mutex); qDebug() << "~IndexGenerator"; if(m_database_path) - delete m_database_path; + m_database_path->~WritableDatabase(); +// delete m_database_path; m_database_path = nullptr; if(m_database_content) - delete m_database_content; + m_database_content->~WritableDatabase(); +// delete m_database_content; + m_database_path = nullptr; m_database_content = nullptr; global_instance = nullptr; // if(m_index_map) diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index 232e185..eb566b7 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -353,6 +353,8 @@ fork: } else if ( rc == 0 ) { qDebug() << "select timeout!"; + ::free(read_timeout); + IndexGenerator::getInstance()->~IndexGenerator(); ::exit(0); }else{ numRead = read(m_fd, buf, BUF_LEN); @@ -387,6 +389,7 @@ fork: // ::exit(0); // } // } + } else if (pid > 0){ memset(buf, 0x00, BUF_LEN);