perf(app-database-service):optimize the logic of the tranPidToDesktopFp method.

This commit is contained in:
JunjieBai 2023-08-08 16:54:07 +08:00 committed by iaom
parent d6b622a7a2
commit 5a62faa16d
1 changed files with 33 additions and 13 deletions

View File

@ -1415,19 +1415,32 @@ bool AppDBManager::handleValueSet(const ApplicationInfoMap appInfoMap)
QString AppDBManager::tranPidToDesktopFp(uint pid) 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<QByteArray> 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; QString desktopFilePath;
QSqlQuery sql(m_database); QSqlQuery query(m_database);
sql.setForwardOnly(true); query.setForwardOnly(true);
sql.prepare("SELECT DESKTOP_FILE_PATH, EXEC FROM APPINFO WHERE EXEC LIKE :exePath"); query.prepare("SELECT DESKTOP_FILE_PATH, EXEC FROM APPINFO WHERE EXEC LIKE :exePath");
sql.bindValue(":exePath", "%" + exePath.section('/', -1) + "%"); query.bindValue(":exePath", "%" + exePath.section('/', -1) + "%");
if (sql.exec()) { if (query.exec()) {
QMap<QString, QString> execInfos; QMap<QString, QString> execInfos;
while (sql.next()) { while (query.next()) {
execInfos[sql.value("DESKTOP_FILE_PATH").toString()] = sql.value("EXEC").toString(); execInfos[query.value("DESKTOP_FILE_PATH").toString()] = query.value("EXEC").toString();
desktopFilePath = sql.value("DESKTOP_FILE_PATH").toString(); desktopFilePath = query.value("DESKTOP_FILE_PATH").toString();
} }
//筛选后有多个结果时进一步过滤 //筛选后有多个结果时进一步过滤
if (execInfos.size() > 1) { if (execInfos.size() > 1) {
@ -1447,10 +1460,17 @@ QString AppDBManager::tranPidToDesktopFp(uint pid)
partOfExec.remove("\""); partOfExec.remove("\"");
} }
//compare the binary path //compare the binary path
if ((partOfExec.contains("/") && partOfExec == exePath) || (partOfExec == exePath.section("/", -1))) { if (partOfExec.contains("/") && exePath.contains("/")) {
if (partOfExec == exePath) {
desktopFilePath = it.key(); desktopFilePath = it.key();
break; break;
} }
} else {
if ((partOfExec == exePath.section("/", -1)) || (partOfExec.section("/", -1) == exePath)) {
desktopFilePath = it.key();
break;
}
}
} }
if (!desktopFilePath.isEmpty()) { if (!desktopFilePath.isEmpty()) {
break; break;
@ -1464,7 +1484,7 @@ QString AppDBManager::tranPidToDesktopFp(uint pid)
qWarning() << "Can not find the desktop file of" << exePath << "by pid:" << pid; qWarning() << "Can not find the desktop file of" << exePath << "by pid:" << pid;
} }
} else { } else {
qWarning() << "Fail to exec cmd" << sql.lastQuery() << m_database.lastError(); qWarning() << "Fail to exec cmd" << query.lastQuery() << m_database.lastError();
} }
return desktopFilePath; return desktopFilePath;
} }