调整索引建立和增加的逻辑,可以实现单独重建某个数据库。

This commit is contained in:
iaom 2022-05-11 18:05:42 +08:00
parent 4294019e28
commit 1a440bf78f
4 changed files with 58 additions and 19 deletions

View File

@ -26,8 +26,7 @@ void FileIndexManager::searchMethod(FileUtils::SearchMethod sm) {
} }
if(FileUtils::SearchMethod::INDEXSEARCH == sm && 0 == FileUtils::indexStatus) { if(FileUtils::SearchMethod::INDEXSEARCH == sm && 0 == FileUtils::indexStatus) {
qDebug() << "start first index"; qDebug() << "start first index";
m_semaphore.acquire(); m_fi->rebuildDatebase();
m_fi->start();
qDebug() << "start inotify index"; qDebug() << "start inotify index";
if(!this->m_iw->isRunning()) { if(!this->m_iw->isRunning()) {
this->m_iw->start(); this->m_iw->start();

View File

@ -35,6 +35,7 @@ FirstIndex::FirstIndex() : m_semaphore(INDEX_SEM, 1, QSystemSemaphore::AccessMod
{ {
m_pool.setMaxThreadCount(2); m_pool.setMaxThreadCount(2);
m_pool.setExpiryTimeout(100); m_pool.setExpiryTimeout(100);
connect(this, &FirstIndex::needRebuild, this, &FirstIndex::rebuildDatebase, Qt::QueuedConnection);
} }
FirstIndex *FirstIndex::getInstance() FirstIndex *FirstIndex::getInstance()
@ -118,9 +119,18 @@ void FirstIndex::work(const QFileInfo& fileInfo) {
} }
} }
void FirstIndex::rebuildDatebase()
{
m_semaphore.acquire();
m_isRebuildProcess = true;
this->wait();
this->start();
}
void FirstIndex::addIndexPath(const QString path, const QStringList blockList) void FirstIndex::addIndexPath(const QString path, const QStringList blockList)
{ {
m_semaphore.acquire(); m_semaphore.acquire();
m_isRebuildProcess = false;
setPath(QStringList() << path); setPath(QStringList() << path);
setBlockPath(blockList); setBlockPath(blockList);
this->wait(); this->wait();
@ -139,20 +149,34 @@ void FirstIndex::run() {
// qInfo() << "ocrIndexDatabaseStatus: " << ocrIndexDatabaseStatus; // qInfo() << "ocrIndexDatabaseStatus: " << ocrIndexDatabaseStatus;
qInfo() << "inotifyIndexStatus: " << inotifyIndexStatus; qInfo() << "inotifyIndexStatus: " << inotifyIndexStatus;
m_allDatadaseStatus = inotifyIndexStatus == "2" ? true : false; m_inotifyIndexStatus = inotifyIndexStatus == "2" ? true : false;
m_indexDatabaseStatus = indexDataBaseStatus == "2" ? true : false; m_indexDatabaseStatus = indexDataBaseStatus == "2" ? true : false;
m_contentIndexDatabaseStatus = contentIndexDataBaseStatus == "2" ? true : false; m_contentIndexDatabaseStatus = contentIndexDataBaseStatus == "2" ? true : false;
// m_ocrIndexDatabaseStatus = ocrIndexDatabaseStatus == "2" ? true : false; // m_ocrIndexDatabaseStatus = ocrIndexDatabaseStatus == "2" ? true : false;
if(m_allDatadaseStatus && m_indexDatabaseStatus && m_contentIndexDatabaseStatus /*&& m_ocrIndexDatabaseStatus*/) { if(m_inotifyIndexStatus && m_indexDatabaseStatus && m_contentIndexDatabaseStatus /*&& m_ocrIndexDatabaseStatus*/) {
if(m_isFirstIndex) { m_needRebuild = false;
m_isFirstIndex = false; if(m_isRebuildProcess) {
m_isRebuildProcess = false;
m_semaphore.release(1); m_semaphore.release(1);
return; return;
} }
} else { } else {
setPath(DirWatcher::getDirWatcher()->currentIndexableDir()); if(m_isRebuildProcess) {
setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex()); setPath(DirWatcher::getDirWatcher()->currentIndexableDir());
setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex());
} else {
if(m_inotifyIndexStatus && (!m_indexDatabaseStatus || !m_contentIndexDatabaseStatus)) {
m_needRebuild = true;
}
if(!m_inotifyIndexStatus || (!m_indexDatabaseStatus && !m_contentIndexDatabaseStatus)) {
m_needRebuild = false;
qInfo() << "Entering rebuild procedure";
Q_EMIT needRebuild();
m_semaphore.release(1);
return;
}
}
} }
@ -187,12 +211,15 @@ void FirstIndex::run() {
QtConcurrent::run(&m_pool, [&]() { QtConcurrent::run(&m_pool, [&]() {
sem.acquire(2); sem.acquire(2);
mutex1.unlock(); mutex1.unlock();
if(m_isFirstIndex && m_allDatadaseStatus && m_indexDatabaseStatus) { if(m_isRebuildProcess && m_inotifyIndexStatus && m_indexDatabaseStatus) { //重建索引且无异常
sem.release(2); sem.release(2);
return; return;
} } else if(m_isRebuildProcess) { //重建索引且有异常
if (!m_allDatadaseStatus || !m_indexDatabaseStatus || !m_contentIndexDatabaseStatus) {
IndexGenerator::getInstance()->rebuildIndexDatabase(); IndexGenerator::getInstance()->rebuildIndexDatabase();
} else if(!m_inotifyIndexStatus || !m_indexDatabaseStatus) { //添加目录且有异常
qWarning() << "Index database need rebuild!";
sem.release(2);
return;
} }
qDebug() << "index start;" << m_indexData->size(); qDebug() << "index start;" << m_indexData->size();
@ -218,12 +245,15 @@ void FirstIndex::run() {
QtConcurrent::run(&m_pool,[&]() { QtConcurrent::run(&m_pool,[&]() {
sem.acquire(2); sem.acquire(2);
mutex2.unlock(); mutex2.unlock();
if(m_isFirstIndex && m_allDatadaseStatus && m_contentIndexDatabaseStatus) { if(m_isRebuildProcess && m_inotifyIndexStatus && m_contentIndexDatabaseStatus) {
sem.release(2); sem.release(2);
return; return;
} } else if(m_isRebuildProcess) { //重建索引且有异常
if (!m_allDatadaseStatus || !m_contentIndexDatabaseStatus || !m_indexDatabaseStatus) {
IndexGenerator::getInstance()->rebuildContentIndexDatabase(); IndexGenerator::getInstance()->rebuildContentIndexDatabase();
} else if(!m_inotifyIndexStatus || !m_contentIndexDatabaseStatus) { //添加目录且有异常
qWarning() << "Content index database need rebuild!";
sem.release(2);
return;
} }
qDebug() << "content index start:" << m_contentIndexData->size(); qDebug() << "content index start:" << m_contentIndexData->size();
QQueue<QString>* tmp2 = new QQueue<QString>(); QQueue<QString>* tmp2 = new QQueue<QString>();
@ -323,8 +353,12 @@ void FirstIndex::run() {
--FileUtils::indexStatus; --FileUtils::indexStatus;
} }
m_isFirstIndex = false; //首次索引后置为false,后续start为添加索引目录时新建索引。
IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "2"); IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "2");
if(m_needRebuild) {
m_needRebuild = false;
qInfo() << "Entering rebuild procedure";
Q_EMIT needRebuild();
}
m_semaphore.release(1); m_semaphore.release(1);
// int retval1 = write(fifo_fd, buffer, strlen(buffer)); // int retval1 = write(fifo_fd, buffer, strlen(buffer));
// if(retval1 == -1) { // if(retval1 == -1) {

View File

@ -42,13 +42,17 @@
#include "file-utils.h" #include "file-utils.h"
#include "common.h" #include "common.h"
namespace UkuiSearch { namespace UkuiSearch {
class FirstIndex : public QThread, public Traverse_BFS { class FirstIndex : public QThread, public Traverse_BFS
{
Q_OBJECT
public: public:
static FirstIndex* getInstance(); static FirstIndex* getInstance();
~FirstIndex(); ~FirstIndex();
virtual void work(const QFileInfo &) final; virtual void work(const QFileInfo &) final;
void rebuildDatebase();
void addIndexPath(const QString path, const QStringList blockList); void addIndexPath(const QString path, const QStringList blockList);
Q_SIGNALS:
void needRebuild();
protected: protected:
void run() override; void run() override;
private: private:
@ -61,8 +65,9 @@ private:
bool m_indexDatabaseStatus = false; bool m_indexDatabaseStatus = false;
bool m_contentIndexDatabaseStatus = false; bool m_contentIndexDatabaseStatus = false;
bool m_ocrIndexDatabaseStatus = false; bool m_ocrIndexDatabaseStatus = false;
bool m_allDatadaseStatus = false; bool m_inotifyIndexStatus = false;
bool m_isFirstIndex = true; bool m_isRebuildProcess = true;
bool m_needRebuild = false;
QThreadPool m_pool; QThreadPool m_pool;
QQueue<QVector<QString>>* m_indexData = nullptr; QQueue<QVector<QString>>* m_indexData = nullptr;

View File

@ -22,6 +22,7 @@ void IndexStatusRecorder::setStatus(const QString &key, const QVariant &value)
const QVariant IndexStatusRecorder::getStatus(const QString &key) const QVariant IndexStatusRecorder::getStatus(const QString &key)
{ {
m_status->sync();
return m_status->value(key); return m_status->value(key);
} }