Skip to content

Commit

Permalink
fix upgrade performance issue
Browse files Browse the repository at this point in the history
  • Loading branch information
bapt committed May 27, 2022
1 parent 1ff6638 commit 7c47dfd
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion libpkg/pkg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,7 @@ pkg_try_installed(struct pkgdb *db, const char *name,
struct pkgdb_it *it = NULL;
int ret = EPKG_FATAL;

if ((it = pkgdb_query(db, name, MATCH_EXACT)) == NULL)
if ((it = pkgdb_query(db, name, MATCH_INTERNAL)) == NULL)
return (EPKG_FATAL);

ret = pkgdb_it_next(it, pkg, flags);
Expand Down
1 change: 1 addition & 0 deletions libpkg/pkg.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ typedef enum {
* case insensitive according to pkgdb_case_sensitive()
*/
MATCH_REGEX,
MATCH_INTERNAL
} match_t;

/**
Expand Down
17 changes: 9 additions & 8 deletions libpkg/pkg_jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ new_pkg_version(struct pkg_jobs *j)
}

/* Use maximum priority for pkg */
if (pkg_jobs_find_upgrade(j, uid, MATCH_EXACT) == EPKG_OK) {
if (pkg_jobs_find_upgrade(j, uid, MATCH_INTERNAL) == EPKG_OK) {
/*
* Now we can have *potential* upgrades, but we can have a situation,
* when our upgrade candidate comes from another repo
Expand Down Expand Up @@ -880,7 +880,7 @@ pkg_jobs_guess_upgrade_candidate(struct pkg_jobs *j, const char *pattern)
/* First of all, try to search a package with the same name */
pos = strchr(pattern, '/');
if (pos != NULL && pos[1] != '\0') {
if (pkg_jobs_try_remote_candidate(j, pos + 1, NULL, opattern, MATCH_EXACT)
if (pkg_jobs_try_remote_candidate(j, pos + 1, NULL, opattern, MATCH_INTERNAL)
== EPKG_OK)
return (EPKG_OK);

Expand All @@ -903,7 +903,7 @@ pkg_jobs_guess_upgrade_candidate(struct pkg_jobs *j, const char *pattern)
/* Try exact pattern without numbers */
cpy = xmalloc(len + 1);
strlcpy(cpy, pos, len + 1);
if (pkg_jobs_try_remote_candidate(j, cpy, NULL, opattern, MATCH_EXACT) != EPKG_OK) {
if (pkg_jobs_try_remote_candidate(j, cpy, NULL, opattern, MATCH_INTERNAL) != EPKG_OK) {
free(cpy);
cpy = sqlite3_mprintf(" WHERE name REGEXP ('^' || %.*Q || '[0-9.]*$')",
len, pos);
Expand Down Expand Up @@ -1028,7 +1028,7 @@ pkg_jobs_installed_local_pkg(struct pkg_jobs *j, struct pkg *pkg)
{
struct job_pattern jfp;

jfp.match = MATCH_EXACT;
jfp.match = MATCH_INTERNAL;
jfp.pattern = pkg->name;
return (pkg_jobs_check_local_pkg(j, &jfp));
}
Expand All @@ -1042,7 +1042,7 @@ pkg_jobs_find_remote_pattern(struct pkg_jobs *j, struct job_pattern *jp)
struct pkg_job_request *req;

if (!(jp->flags & PKG_PATTERN_FLAG_FILE)) {
if (j->type == PKG_JOBS_UPGRADE && jp->match == MATCH_EXACT) {
if (j->type == PKG_JOBS_UPGRADE && jp->match == MATCH_INTERNAL) {
/*
* For upgrade patterns we must ensure that a local package is
* installed as well. This only works if we're operating on an
Expand Down Expand Up @@ -1510,7 +1510,7 @@ pkg_jobs_check_remote_candidate(struct pkg_jobs *j, struct pkg *pkg)
if (pkg->digest == NULL)
return (true);

it = pkgdb_repo_query(j->db, pkg->uid, MATCH_EXACT, j->reponame);
it = pkgdb_repo_query(j->db, pkg->uid, MATCH_INTERNAL, j->reponame);
if (it != NULL) {
/*
* If we have the same package in a remote repo, it is not an
Expand Down Expand Up @@ -1550,6 +1550,7 @@ pkg_jobs_find_install_candidates(struct pkg_jobs *j)
return (NULL);

while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) == EPKG_OK) {

if ((j->flags & PKG_FLAG_FORCE) ||
pkg_jobs_check_remote_candidate(j, pkg)) {
tll_push_front(*candidates, pkg->id);
Expand Down Expand Up @@ -1592,7 +1593,7 @@ jobs_solve_full_upgrade(struct pkg_jobs *j)
pkg = NULL;
while (pkgdb_it_next(it, &pkg, flags) == EPKG_OK) {
/* Do not test we ignore what doesn't exists remotely */
pkg_jobs_find_upgrade(j, pkg->uid, MATCH_EXACT);
pkg_jobs_find_upgrade(j, pkg->uid, MATCH_INTERNAL);
}
pkg_free(pkg);
pkgdb_it_free(it);
Expand Down Expand Up @@ -1751,7 +1752,7 @@ jobs_solve_fetch(struct pkg_jobs *j)
}
else {
/* Do not test we ignore what doesn't exists remotely */
pkg_jobs_find_upgrade(j, pkg->uid, MATCH_EXACT);
pkg_jobs_find_upgrade(j, pkg->uid, MATCH_INTERNAL);
}
pkg = NULL;
}
Expand Down
6 changes: 3 additions & 3 deletions libpkg/pkg_jobs_universe.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pkg_jobs_universe_get_local(struct pkg_jobs_universe *universe,
}
}

if ((it = pkgdb_query(universe->j->db, uid, MATCH_EXACT)) == NULL)
if ((it = pkgdb_query(universe->j->db, uid, MATCH_INTERNAL)) == NULL)
return (NULL);

if (pkgdb_it_next(it, &pkg, flag) != EPKG_OK)
Expand Down Expand Up @@ -132,7 +132,7 @@ pkg_jobs_universe_get_remote(struct pkg_jobs_universe *universe,
}
}

if ((it = pkgdb_repo_query(universe->j->db, uid, MATCH_EXACT,
if ((it = pkgdb_repo_query(universe->j->db, uid, MATCH_INTERNAL,
universe->j->reponame)) == NULL)
return (NULL);

Expand Down Expand Up @@ -1192,7 +1192,7 @@ pkg_jobs_universe_get_upgrade_candidates(struct pkg_jobs_universe *universe,
}
}

if ((it = pkgdb_repo_query(universe->j->db, uid, MATCH_EXACT,
if ((it = pkgdb_repo_query(universe->j->db, uid, MATCH_INTERNAL,
universe->j->reponame)) == NULL)
return (NULL);

Expand Down
18 changes: 16 additions & 2 deletions libpkg/pkgdb_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ pkgdb_get_pattern_query(const char *pattern, match_t match)
case MATCH_ALL:
comp = "";
break;
case MATCH_INTERNAL:
comp = " WHERE p.name = ?1";
break;
case MATCH_EXACT:
if (pkgdb_case_sensitive()) {
if (checkuid == NULL) {
Expand Down Expand Up @@ -149,7 +152,7 @@ pkgdb_query_cond(struct pkgdb *db, const char *cond, const char *pattern, match_

comp = pkgdb_get_pattern_query(pattern, match);

if (cond)
if (cond) {
sqlite3_snprintf(sizeof(sql), sql,
"WITH flavors AS "
" (SELECT package_id, value.annotation AS flavor FROM pkg_annotation "
Expand All @@ -167,7 +170,17 @@ pkgdb_query_cond(struct pkgdb *db, const char *cond, const char *pattern, match_
" LEFT JOIN flavors ON flavors.package_id = p.id "
" %s %s (%s) ORDER BY p.name;",
comp, pattern == NULL ? "WHERE" : "AND", cond + 7);
else
} else if (match == MATCH_INTERNAL) {
sqlite3_snprintf(sizeof(sql), sql,
"SELECT DISTINCT p.id, origin, p.name, p.name as uniqueid, "
"version, comment, desc, "
"message, arch, maintainer, www, "
"prefix, flatsize, licenselogic, automatic, "
"locked, time, manifestdigest, vital "
"FROM packages AS p "
"%s"
" ORDER BY p.name", comp);
} else {
sqlite3_snprintf(sizeof(sql), sql,
"WITH flavors AS "
" (SELECT package_id, value.annotation AS flavor FROM pkg_annotation "
Expand All @@ -185,6 +198,7 @@ pkgdb_query_cond(struct pkgdb *db, const char *cond, const char *pattern, match_
"LEFT JOIN flavors ON flavors.package_id = p.id "
"%s"
" ORDER BY p.name", comp);
}

if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) {
ERROR_SQLITE(db->sqlite, sql);
Expand Down
20 changes: 18 additions & 2 deletions libpkg/repo/binary/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ pkg_repo_binary_query(struct pkg_repo *repo, const char *cond, const char *patte
sqlite3_stmt *stmt = NULL;
char *sql = NULL;
const char *comp = NULL;
char basesql_quick[] = ""
"SELECT DISTINCT p.id, origin, p.name, p.name as uniqueid, version, comment, "
"prefix, desc, arch, maintainer, www, "
"licenselogic, flatsize, pkgsize, "
"cksum, manifestdigest, path AS repopath, '%s' AS dbname "
"FROM packages as p "
" %s "
"%s%s%s "
"ORDER BY p.name;";
char basesql[] = ""
"WITH flavors AS "
" (SELECT package_id, value.annotation AS flavor FROM pkg_annotation "
Expand All @@ -120,6 +129,10 @@ pkg_repo_binary_query(struct pkg_repo *repo, const char *cond, const char *patte
" %s "
"%s%s%s "
"ORDER BY p.name;";
char *bsql = basesql;

if (match == MATCH_INTERNAL)
bsql = basesql_quick;

if (match != MATCH_ALL && (pattern == NULL || pattern[0] == '\0'))
return (NULL);
Expand All @@ -128,9 +141,9 @@ pkg_repo_binary_query(struct pkg_repo *repo, const char *cond, const char *patte
if (comp == NULL)
comp = "";
if (cond == NULL)
xasprintf(&sql, basesql, repo->name, comp, "", "", "");
xasprintf(&sql, bsql, repo->name, comp, "", "", "");
else
xasprintf(&sql, basesql, repo->name, comp,
xasprintf(&sql, bsql, repo->name, comp,
comp[0] != '\0' ? "AND (" : "WHERE ( ", cond + 7, " )");

stmt = prepare_sql(sqlite, sql);
Expand Down Expand Up @@ -272,6 +285,9 @@ pkg_repo_binary_search_how(match_t match)
case MATCH_ALL:
how = NULL;
break;
case MATCH_INTERNAL:
how = "%s = ?1";
break;
case MATCH_EXACT:
if (pkgdb_case_sensitive())
how = "%s = ?1";
Expand Down

0 comments on commit 7c47dfd

Please sign in to comment.