From 20a3b21f75b185f411efdd717211226d3f8f5ca8 Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Tue, 8 Aug 2023 16:54:07 +0800 Subject: [PATCH] perf(app-database-service):optimize the logic of the tranPidToDesktopFp method. --- .../app-db-manager.cpp | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/ukui-search-app-data-service/app-db-manager.cpp b/ukui-search-app-data-service/app-db-manager.cpp index 152e8a7..1f30d94 100644 --- a/ukui-search-app-data-service/app-db-manager.cpp +++ b/ukui-search-app-data-service/app-db-manager.cpp @@ -1415,19 +1415,32 @@ bool AppDBManager::handleValueSet(const ApplicationInfoMap appInfoMap) QString AppDBManager::tranPidToDesktopFp(uint pid) { - QString exePath = QFile::symLinkTarget("/proc/" + QString::number(pid) + "/exe"); + QFile file("/proc/" + QString::number(pid) + "/cmdline"); + file.open(QFile::ReadOnly); + QList cmdlist = file.readAll().split('\0'); + file.close(); + + cmdlist.removeAll(""); + QString exePath; + for (int i = cmdlist.size() - 1; i >= 0; i--) { + if (cmdlist.at(i).startsWith("-") || cmdlist.at(i).contains("%")) { + continue; + } + exePath = cmdlist.at(i); + break; + } QString desktopFilePath; - QSqlQuery sql(m_database); - sql.setForwardOnly(true); - sql.prepare("SELECT DESKTOP_FILE_PATH, EXEC FROM APPINFO WHERE EXEC LIKE :exePath"); - sql.bindValue(":exePath", "%" + exePath.section('/', -1) + "%"); + QSqlQuery query(m_database); + query.setForwardOnly(true); + query.prepare("SELECT DESKTOP_FILE_PATH, EXEC FROM APPINFO WHERE EXEC LIKE :exePath"); + query.bindValue(":exePath", "%" + exePath.section('/', -1) + "%"); - if (sql.exec()) { + if (query.exec()) { QMap execInfos; - while (sql.next()) { - execInfos[sql.value("DESKTOP_FILE_PATH").toString()] = sql.value("EXEC").toString(); - desktopFilePath = sql.value("DESKTOP_FILE_PATH").toString(); + while (query.next()) { + execInfos[query.value("DESKTOP_FILE_PATH").toString()] = query.value("EXEC").toString(); + desktopFilePath = query.value("DESKTOP_FILE_PATH").toString(); } //筛选后有多个结果时进一步过滤 if (execInfos.size() > 1) { @@ -1447,9 +1460,16 @@ QString AppDBManager::tranPidToDesktopFp(uint pid) partOfExec.remove("\""); } //compare the binary path - if ((partOfExec.contains("/") && partOfExec == exePath) || (partOfExec == exePath.section("/", -1))) { - desktopFilePath = it.key(); - break; + if (partOfExec.contains("/") && exePath.contains("/")) { + if (partOfExec == exePath) { + desktopFilePath = it.key(); + break; + } + } else { + if ((partOfExec == exePath.section("/", -1)) || (partOfExec.section("/", -1) == exePath)) { + desktopFilePath = it.key(); + break; + } } } if (!desktopFilePath.isEmpty()) { @@ -1464,7 +1484,7 @@ QString AppDBManager::tranPidToDesktopFp(uint pid) qWarning() << "Can not find the desktop file of" << exePath << "by pid:" << pid; } } else { - qWarning() << "Fail to exec cmd" << sql.lastQuery() << m_database.lastError(); + qWarning() << "Fail to exec cmd" << query.lastQuery() << m_database.lastError(); } return desktopFilePath; }