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
parent 6ae9125746
commit 20a3b21f75
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 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;
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<QString, QString> 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;
}