Avoid double iteration on 'rpm -e' now that iterator count works
authorPanu Matilainen <pmatilai@redhat.com>
Thu, 6 Sep 2012 07:48:51 +0000 (10:48 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Thu, 6 Sep 2012 07:48:51 +0000 (10:48 +0300)
- While harmless, having to count on one and act on another iteration
  gets expensive when there are lots of labels specified. Especially
  as the iterator initialization can already load the same headers
  multiple times, sigh...

lib/rpminstall.c

index 41e796a..a3623fd 100644 (file)
@@ -646,17 +646,10 @@ int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
 
     qfmt = rpmExpand("%{?_query_all_fmt}\n", NULL);
     for (arg = argv; *arg; arg++) {
-       rpmdbMatchIterator mi;
-       int matches = 0;
+       rpmdbMatchIterator mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
+       int matches = rpmdbGetIteratorCount(mi);
        int erasing = 1;
 
-       /* Iterator count isn't reliable with labels, count manually... */
-       mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
-       while (rpmdbNextIterator(mi) != NULL) {
-           matches++;
-       }
-       rpmdbFreeIterator(mi);
-
        if (! matches) {
            rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
            numFailed++;
@@ -671,7 +664,6 @@ int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
                erasing = 0;
            }
 
-           mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
            while ((h = rpmdbNextIterator(mi)) != NULL) {
                if (erasing) {
                    (void) rpmtsAddEraseElement(ts, h, -1);
@@ -682,8 +674,8 @@ int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
                    free(nevra);
                }
            }
-           rpmdbFreeIterator(mi);
        }
+       rpmdbFreeIterator(mi);
     }
     free(qfmt);