Optimize the process of Qsettings.setValue().

This commit is contained in:
rookie-J 2021-05-12 15:01:20 +08:00
parent 550681b38f
commit 546f80af48
5 changed files with 42 additions and 24 deletions

View File

@ -252,17 +252,22 @@ void GlobalSettings::setValue(const QString &key, const QVariant &value) {
// qDebug()<<"setvalue========"<<key<<":"<<value; // qDebug()<<"setvalue========"<<key<<":"<<value;
m_cache.insert(key, value); m_cache.insert(key, value);
// m_settings->sync(); // m_settings->sync();
QtConcurrent::run([ = ]() { // QtConcurrent::run([ = ]() {
// qDebug()<<m_settings->status(); // qDebug()<<m_settings->status();
// if (m_mutex.tryLock(1000)) { // if (m_mutex.tryLock(1000)) {
// m_mutex.lock(); // m_mutex.lock();
// test++;
// qDebug()<<"QtConcurrent::run=========start!!!"<<key<<":"<<value<<test<<QTime::currentTime();
m_settings->setValue(key, value); m_settings->setValue(key, value);
// qDebug()<<"setvalue========finish!!!"<<key<<":"<<value; // qDebug()<<"QtConcurrent::run=========sync!!!";
m_settings->sync(); m_settings->sync();
// qDebug()<<"QtConcurrent::run========finished!!!"<<key<<":"<<value<<test<<QTime::currentTime();
// qDebug()<<"setvalue========sync!!!"<<key<<":"<<value; // qDebug()<<"setvalue========sync!!!"<<key<<":"<<value;
// m_mutex.unlock(); // m_mutex.unlock();
// } // }
}); // });
} }
void GlobalSettings::forceSync(const QString &key) { void GlobalSettings::forceSync(const QString &key) {

View File

@ -105,6 +105,8 @@ private:
QStringList m_history; QStringList m_history;
QMutex m_mutex; QMutex m_mutex;
// size_t test = 0;
}; };

View File

@ -82,7 +82,8 @@ 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;
@ -196,6 +197,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");
GlobalSettings::getInstance()->setValue(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!!";
@ -204,7 +207,6 @@ void FirstIndex::run() {
--FileUtils::_index_status; --FileUtils::_index_status;
} }
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); GlobalSettings::getInstance()->setValue(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) {

View File

@ -69,7 +69,7 @@ bool IndexGenerator::creatAllIndex(QQueue<QVector<QString> > *messageList) {
return false; return false;
} }
qDebug() << "begin creatAllIndex"; qDebug() << "begin creatAllIndex";
GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "0"); // GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "0");
try { try {
// m_indexer = new Xapian::TermGenerator(); // m_indexer = new Xapian::TermGenerator();
// m_indexer.set_database(*m_database_path); // m_indexer.set_database(*m_database_path);
@ -95,7 +95,7 @@ bool IndexGenerator::creatAllIndex(QQueue<QVector<QString> > *messageList) {
// FileUtils::_index_status &= ~0x1; // FileUtils::_index_status &= ~0x1;
assert(false); assert(false);
} }
GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2"); // GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2");
qDebug() << "finish creatAllIndex"; qDebug() << "finish creatAllIndex";
// FileUtils::_index_status &= ~0x1; // FileUtils::_index_status &= ~0x1;
_doc_list_path->clear(); _doc_list_path->clear();
@ -114,7 +114,7 @@ bool IndexGenerator::creatAllIndex(QQueue<QString> *messageList) {
int size = _doc_list_content->size(); int size = _doc_list_content->size();
qDebug() << "begin creatAllIndex for content" << size; qDebug() << "begin creatAllIndex for content" << size;
if(!size == 0) { if(!size == 0) {
GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "0"); // GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "0");
try { try {
int count = 0; int count = 0;
for(auto i : *_doc_list_content) { for(auto i : *_doc_list_content) {
@ -131,7 +131,7 @@ bool IndexGenerator::creatAllIndex(QQueue<QString> *messageList) {
// FileUtils::_index_status &= ~0x2; // FileUtils::_index_status &= ~0x2;
assert(false); assert(false);
} }
GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2"); // GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2");
// FileUtils::_index_status &= ~0x2; // FileUtils::_index_status &= ~0x2;
qDebug() << "finish creatAllIndex for content"; qDebug() << "finish creatAllIndex for content";
_doc_list_content->clear(); _doc_list_content->clear();

View File

@ -180,12 +180,14 @@ 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");
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] != '.') {
qDebug() << "Read Event event->wd: " << event->wd; qDebug() << "Read Event event->wd: " << event->wd;
qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask; qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask;
if(event->name[0] != '.') {
qDebug() << QString(currentPath[event->wd] + '/' + event->name); qDebug() << QString(currentPath[event->wd] + '/' + event->name);
// switch (event->mask) { // switch (event->mask) {
if(event->mask & IN_CREATE) { if(event->mask & IN_CREATE) {
@ -235,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");
delete indexQueue; delete indexQueue;
indexQueue = nullptr; indexQueue = nullptr;
delete contentIndexQueue; delete contentIndexQueue;
@ -288,14 +290,13 @@ void InotifyIndex::run() {
assert(false); assert(false);
} }
char * tmp = const_cast<char*>(buf); char * tmp = const_cast<char*>(buf);
for(; tmp < buf + numRead;) { for(; tmp < buf + numRead;) {
struct inotify_event * event = reinterpret_cast<inotify_event *>(tmp); struct inotify_event * event = reinterpret_cast<inotify_event *>(tmp);
// qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask;
if(event->name[0] != '.') { if(event->name[0] != '.') {
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "0"); // qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask;
// qDebug("mask:0x%x,",event->mask);
break; break;
} }
tmp += sizeof(struct inotify_event) + event->len; tmp += sizeof(struct inotify_event) + event->len;
@ -318,7 +319,6 @@ void InotifyIndex::run() {
qDebug() << "read"; qDebug() << "read";
} }
eventProcess(buf, numRead); eventProcess(buf, numRead);
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2");
fd_set read_fds; fd_set read_fds;
int rc; int rc;
@ -343,7 +343,6 @@ void InotifyIndex::run() {
// GlobalSettings::getInstance()->forceSync(); // GlobalSettings::getInstance()->forceSync();
::_exit(0); ::_exit(0);
} else { } else {
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "0");
memset(buf, 0x00, BUF_LEN); memset(buf, 0x00, BUF_LEN);
numRead = read(m_fd, buf, BUF_LEN); numRead = read(m_fd, buf, BUF_LEN);
if(numRead == -1) { if(numRead == -1) {
@ -351,9 +350,22 @@ void InotifyIndex::run() {
fflush(stdout); fflush(stdout);
assert(false); assert(false);
} }
char * tmp = const_cast<char*>(buf);
for(; tmp < buf + numRead; ) {
struct inotify_event * event = reinterpret_cast<inotify_event *>(tmp);
if(event->name[0] != '.') {
break;
}
tmp += sizeof(struct inotify_event) + event->len;
}
if(tmp >= buf + numRead) {
continue;
}
qDebug() << "Read " << numRead << " bytes from inotify fd"; qDebug() << "Read " << numRead << " bytes from inotify fd";
this->eventProcess(buf, numRead); this->eventProcess(buf, numRead);
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2");
} }
} }
} else if(pid > 0) { } else if(pid > 0) {
@ -369,7 +381,4 @@ void InotifyIndex::run() {
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "3"); GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "3");
RemoveWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation), false); RemoveWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation), false);
} }
} }