add inotify create or delete index and fix first index makes the size of database increase unlimitedly

This commit is contained in:
zhangzihao 2021-01-10 09:01:22 +08:00
parent 4fa288b97a
commit e2fd50baa1
11 changed files with 218 additions and 14 deletions

View File

@ -103,15 +103,18 @@ QStringList GlobalSettings::getBlockDirs()
return m_block_dirs_settings->allKeys(); return m_block_dirs_settings->allKeys();
} }
//here should be override
//MouseZhangZh
void GlobalSettings::setValue(const QString &key, const QVariant &value) void GlobalSettings::setValue(const QString &key, const QVariant &value)
{ {
m_cache.insert(key, value); m_cache.insert(key, value);
QtConcurrent::run([=]() { QtConcurrent::run([=]() {
if (m_mutex.tryLock(1000)) { // if (m_mutex.tryLock(1000)) {
m_mutex.lock();
m_settings->setValue(key, value); m_settings->setValue(key, value);
m_settings->sync(); m_settings->sync();
m_mutex.unlock(); m_mutex.unlock();
} // }
}); });
} }

View File

@ -14,9 +14,15 @@ FirstIndex::FirstIndex(const QString& path) : Traverse_BFS(path)
if (indexDataBaseStatus == "" || contentIndexDataBaseStatus == ""){ if (indexDataBaseStatus == "" || contentIndexDataBaseStatus == ""){
this->bool_dataBaseExist = false; this->bool_dataBaseExist = false;
} }
else{
this->bool_dataBaseExist = true;
}
if (indexDataBaseStatus != "2" || contentIndexDataBaseStatus != "2"){ if (indexDataBaseStatus != "2" || contentIndexDataBaseStatus != "2"){
this->bool_dataBaseStatusOK = false; this->bool_dataBaseStatusOK = false;
} }
else{
this->bool_dataBaseStatusOK = true;
}
this->q_index = new QQueue<QVector<QString>>(); this->q_index = new QQueue<QVector<QString>>();
this->q_content_index = new QQueue<QString>(); this->q_content_index = new QQueue<QString>();
@ -26,10 +32,13 @@ FirstIndex::FirstIndex(const QString& path) : Traverse_BFS(path)
FirstIndex::~FirstIndex() FirstIndex::~FirstIndex()
{ {
qDebug() << "~FirstIndex";
delete this->q_index; delete this->q_index;
this->q_index = nullptr; this->q_index = nullptr;
this->q_content_index = nullptr;
delete this->q_content_index; delete this->q_content_index;
this->q_content_index = nullptr;
delete this->p_indexGenerator;
this->p_indexGenerator;
// delete this->mlm; // delete this->mlm;
// this->mlm = nullptr; // this->mlm = nullptr;
} }
@ -95,6 +104,10 @@ void FirstIndex::run(){
mutex2.unlock(); mutex2.unlock();
mutex3.unlock(); mutex3.unlock();
qDebug() << "first index end;"; qDebug() << "first index end;";
//don't use it now!!!!
//MouseZhangZh
// this->~FirstIndex();
// qDebug() << "~FirstIndex end;";
this->quit(); this->quit();
// this->wait(); // this->wait();
} }

View File

@ -123,6 +123,7 @@ IndexGenerator::~IndexGenerator()
if(m_database_content) if(m_database_content)
delete m_database_content; delete m_database_content;
GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE,"2"); GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE,"2");
GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE,"2");
GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT,"2"); GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT,"2");
} }

View File

@ -15,6 +15,7 @@ class IndexGenerator : public QObject
Q_OBJECT Q_OBJECT
public: public:
static IndexGenerator *getInstance(bool rebuild = false); static IndexGenerator *getInstance(bool rebuild = false);
~IndexGenerator();
bool setIndexdataPath(); bool setIndexdataPath();
bool isIndexdataExist(); bool isIndexdataExist();
//for search test //for search test
@ -29,7 +30,6 @@ public Q_SLOTS:
private: private:
explicit IndexGenerator(bool rebuild = false,QObject *parent = nullptr); explicit IndexGenerator(bool rebuild = false,QObject *parent = nullptr);
~IndexGenerator();
//For file name index //For file name index
void HandlePathList(QList<QVector<QString>> *messageList); void HandlePathList(QList<QVector<QString>> *messageList);
//For file content index //For file content index

View File

@ -9,6 +9,7 @@ HEADERS += \
$$PWD/first-index.h \ $$PWD/first-index.h \
$$PWD/index-generator.h \ $$PWD/index-generator.h \
# $$PWD/inotify-manager.h \ # $$PWD/inotify-manager.h \
$$PWD/inotify-index.h \
$$PWD/inotify.h \ $$PWD/inotify.h \
$$PWD/messagelist-manager.h \ $$PWD/messagelist-manager.h \
$$PWD/traverse_bfs.h \ $$PWD/traverse_bfs.h \
@ -25,6 +26,7 @@ SOURCES += \
$$PWD/first-index.cpp \ $$PWD/first-index.cpp \
$$PWD/index-generator.cpp \ $$PWD/index-generator.cpp \
# $$PWD/inotify-manager.cpp \ # $$PWD/inotify-manager.cpp \
$$PWD/inotify-index.cpp \
$$PWD/inotify.cpp \ $$PWD/inotify.cpp \
$$PWD/messagelist-manager.cpp \ $$PWD/messagelist-manager.cpp \
$$PWD/test-Inotify-Manager.cpp \ $$PWD/test-Inotify-Manager.cpp \

View File

@ -0,0 +1,141 @@
#include "inotify-index.h"
InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path)
{
/*-------------ukuisearchdbus Test start-----------------*/
qDebug() << "setInotifyMaxUserWatches start";
UkuiSearchQDBus usQDBus;
usQDBus.setInotifyMaxUserWatches();
qDebug() << "setInotifyMaxUserWatches end";
/*-------------ukuisearchdbus Test End-----------------*/
m_fd = inotify_init();
qDebug() << "m_fd----------->" <<m_fd;
this->AddWatch("/home");
this->Traverse();
}
InotifyIndex::~InotifyIndex()
{
}
void InotifyIndex::DoSomething(const QFileInfo& fileInfo){
if(fileInfo.isDir()){
this->AddWatch(fileInfo.absoluteFilePath());
}
}
bool InotifyIndex::AddWatch(const QString &path){
int ret = inotify_add_watch(m_fd, path.toStdString().c_str(), (IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE | IN_DELETE));
if (ret == -1) {
qDebug() << "AddWatch error:" << path;
return false;
}
Q_ASSERT(ret != -1);
currentPath[ret] = path;
//qDebug() << "Watch:" << path;
return true;
}
bool InotifyIndex::RemoveWatch(const QString &path){
int ret = inotify_rm_watch(m_fd, currentPath.key(path));
if (ret){
qDebug() << "remove path error";
return false;
}
Q_ASSERT(ret != 0);
for (QMap<int, QString>::Iterator i = currentPath.begin(); i != currentPath.end();){
if (i.value().length() > path.length()){
if (i.value().mid(0, path.length()) == path){
// qDebug() << i.value();
/*--------------------------------*/
//在此调用删除索引
IndexGenerator::getInstance()->deleteAllIndex(new QStringList(path));
/*--------------------------------*/
currentPath.erase(i++);
}
else{
i++;
}
}
else{
i++;
}
}
// qDebug() << path;
//这个貌似不用删先mark一下
//currentPath.remove(currentPath.key(path));
return true;
}
void InotifyIndex::run(){
char * p;
char buf[BUF_LEN] __attribute__((aligned(8)));
ssize_t numRead;
QList<QVector<QString>>* indexList = new QList<QVector<QString>>();
QList<QString>* contentIndexList = new QList<QString>();
for (;;) { /* Read events forever */
numRead = read(m_fd, buf, BUF_LEN);
if (numRead == 0) {
qDebug() << "read() from inotify fd returned 0!";
}
if (numRead == -1) {
qDebug() << "read";
}
qDebug() << "Read " << numRead << " bytes from inotify fd";
/* Process all of the events in buffer returned by read() */
for (p = buf; p < buf + numRead;) {
struct inotify_event * event = reinterpret_cast<inotify_event *>(p);
if(event->name[0] != '.'){
//传创建或移动过来的文件路径
if((event->mask & IN_CREATE) | (event->mask & IN_MOVED_TO)){
//添加监视要先序遍历先添加top节点
if (event->mask & IN_ISDIR){
AddWatch(currentPath[event->wd] + '/' + event->name);
this->setPath(currentPath[event->wd] + '/' + event->name);
Traverse();
}
/*--------------------------------*/
// IndexGenerator::getInstance()->creatAllIndex(QQueue<QVector<QString>>(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0")));
indexList->append(QVector<QString>() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0"));
IndexGenerator::getInstance()->creatAllIndex(indexList);
indexList->clear();
for (auto i : this->targetFileTypeVec){
if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){
contentIndexList->append(QString(currentPath[event->wd] + '/' + event->name));
IndexGenerator::getInstance()->creatAllIndex(contentIndexList);
contentIndexList->clear();
}
}
/*--------------------------------*/
}
else if((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){
if (event->mask & IN_ISDIR){
RemoveWatch(currentPath[event->wd] + '/' + event->name);
}
IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name));
}
/*--------------------------------*/
}
p += sizeof(struct inotify_event) + event->len;
}
}
delete indexList;
indexList = nullptr;
delete contentIndexList;
contentIndexList = nullptr;
}

View File

@ -0,0 +1,40 @@
#ifndef INOTIFYINDEX_H
#define INOTIFYINDEX_H
#include <QThread>
#include <unistd.h>
#include <sys/inotify.h>
#include "index-generator.h"
#include "traverse_bfs.h"
#include "ukui-search-qdbus.h"
#define BUF_LEN 1024
class InotifyIndex : public QThread, public Traverse_BFS
{
Q_OBJECT
public:
InotifyIndex(const QString&);
~InotifyIndex();
bool AddWatch(const QString&);
bool RemoveWatch(const QString&);
virtual void DoSomething(const QFileInfo &) final;
protected:
void run() override;
private:
QString* m_watch_path;
int m_fd;
QMap<int, QString> currentPath;
const QVector<QString> targetFileTypeVec ={
// QString(".doc"),
QString(".docx"),
// QString(".ppt"),
// QString(".pptx"),
// QString(".xls"),
// QString(".xlsx"),
QString(".txt")};
};
#endif // INOTIFYINDEX_H

View File

@ -33,7 +33,7 @@ InotifyManagerRefact::InotifyManagerRefact(const QString& path) : Traverse_BFS(p
num2string.insert(IN_UNMOUNT, "IN_UNMOUNT"); num2string.insert(IN_UNMOUNT, "IN_UNMOUNT");
num2string.insert(IN_Q_OVERFLOW, "IN_Q_OVERFLOW"); num2string.insert(IN_Q_OVERFLOW, "IN_Q_OVERFLOW");
num2string.insert(IN_IGNORED, "IN_IGNORED"); num2string.insert(IN_IGNORED, "IN_IGNORED");
this->mlm = new MessageListManager(); // this->mlm = new MessageListManager();
this->AddWatch("/home"); this->AddWatch("/home");
this->Traverse(); this->Traverse();
@ -44,14 +44,14 @@ InotifyManagerRefact::InotifyManagerRefact(const QString& path) : Traverse_BFS(p
} }
InotifyManagerRefact::~InotifyManagerRefact(){ InotifyManagerRefact::~InotifyManagerRefact(){
delete this->mlm; // delete this->mlm;
this->mlm = nullptr; // this->mlm = nullptr;
// delete dirPath; // delete dirPath;
// dirPath = nullptr; // dirPath = nullptr;
} }
void InotifyManagerRefact::DoSomething(const QFileInfo& fileInfo){ void InotifyManagerRefact::DoSomething(const QFileInfo& fileInfo){
this->mlm->AddMessage(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir()?"1":"0")); // this->mlm->AddMessage(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir()?"1":"0"));
// if(QString(bool((fileInfo.isDir()))) == QString("1")) // if(QString(bool((fileInfo.isDir()))) == QString("1"))
// qDebug()<<"bool((fileInfo.isDir())"<<QString(fileInfo.isDir()); // qDebug()<<"bool((fileInfo.isDir())"<<QString(fileInfo.isDir());
// this->mlm->AddMessage(QVector<QString>() << "PLog" << "/home/zpf/baidunetdisk/PLog" << "1"); // this->mlm->AddMessage(QVector<QString>() << "PLog" << "/home/zpf/baidunetdisk/PLog" << "1");
@ -65,7 +65,7 @@ void InotifyManagerRefact::DoSomething(const QFileInfo& fileInfo){
void InotifyManagerRefact::SendRestMessage() void InotifyManagerRefact::SendRestMessage()
{ {
this->mlm->SendMessage(); // this->mlm->SendMessage();
} }
bool InotifyManagerRefact::AddWatch(const QString &path){ bool InotifyManagerRefact::AddWatch(const QString &path){
@ -73,6 +73,7 @@ bool InotifyManagerRefact::AddWatch(const QString &path){
// qDebug() << "m_fd: " <<m_fd; // qDebug() << "m_fd: " <<m_fd;
//int ret = inotify_add_watch(m_fd, path.toStdString().c_str(), IN_ALL_EVENTS); //int ret = inotify_add_watch(m_fd, path.toStdString().c_str(), IN_ALL_EVENTS);
int ret = inotify_add_watch(m_fd, path.toStdString().c_str(), (IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE | IN_DELETE)); int ret = inotify_add_watch(m_fd, path.toStdString().c_str(), (IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE | IN_DELETE));
Q_ASSERT(ret == 0);
if (ret == -1) { if (ret == -1) {
qDebug() << "AddWatch error:" << path; qDebug() << "AddWatch error:" << path;
return false; return false;
@ -155,8 +156,8 @@ void InotifyManagerRefact::run(){
// else { // else {
//IndexGenerator::getInstance()->creatAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); //IndexGenerator::getInstance()->creatAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name));
this->mlm->AddMessage(QVector<QString>() << event->name << (currentPath[event->wd] + '/' + event->name) << QString(bool((event->mask & IN_ISDIR)))); // this->mlm->AddMessage(QVector<QString>() << event->name << (currentPath[event->wd] + '/' + event->name) << QString(bool((event->mask & IN_ISDIR))));
this->mlm->SendMessage(); // this->mlm->SendMessage();
// } // }
} }
else if((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){ else if((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){

View File

@ -29,7 +29,7 @@ private:
int m_fd; int m_fd;
QMap<int, QString> currentPath; QMap<int, QString> currentPath;
QMap<int, QString> num2string; QMap<int, QString> num2string;
MessageListManager* mlm; // MessageListManager* mlm;
QMap<QString, QStringList>* dirPath; QMap<QString, QStringList>* dirPath;
}; };

View File

@ -13,6 +13,7 @@
#include "index/filetypefilter.h" #include "index/filetypefilter.h"
#include "index/ukui-search-qdbus.h" #include "index/ukui-search-qdbus.h"
#include "index/inotify-index.h"
class LIBSEARCH_EXPORT GlobalSearch class LIBSEARCH_EXPORT GlobalSearch
{ {

View File

@ -94,8 +94,10 @@ int main(int argc, char *argv[])
qInstallMessageHandler(messageOutput); qInstallMessageHandler(messageOutput);
qDebug() << "main start"; qDebug() << "main start";
FirstIndex* fi = new FirstIndex("/home"); FirstIndex fi("/home");
fi->start(); fi.start();
InotifyIndex ii("/home");
ii.start();
/*-------------ukuisearchdbus Test start-----------------*/ /*-------------ukuisearchdbus Test start-----------------*/
// UkuiSearchQDBus usQDBus; // UkuiSearchQDBus usQDBus;
// usQDBus.setInotifyMaxUserWatches(); // usQDBus.setInotifyMaxUserWatches();