Sort all dbapi.cp_list() results in ascending order and
authorZac Medico <zmedico@gentoo.org>
Sun, 28 Oct 2007 07:26:24 +0000 (07:26 -0000)
committerZac Medico <zmedico@gentoo.org>
Sun, 28 Oct 2007 07:26:24 +0000 (07:26 -0000)
cache the results when possible. The order is preserved
in dbapi.match() so those results are also sorted.

svn path=/main/trunk/; revision=8329

pym/_emerge/__init__.py
pym/portage/dbapi/__init__.py
pym/portage/dbapi/porttree.py
pym/portage/dbapi/vartree.py
pym/portage/dbapi/virtual.py

index e5ac45cd5b189ce81ed44ba0b09356e309d75142..a5d6f88fc550a46624fe27c241793032a49d70ab 100644 (file)
@@ -827,23 +827,6 @@ def perform_global_updates(mycpv, mydb, mycommands):
        if updates:
                mydb.aux_update(mycpv, updates)
 
-def cpv_sort_descending(cpv_list):
-       """Sort in place, returns None."""
-       if len(cpv_list) <= 1:
-               return
-       first_split = portage.catpkgsplit(cpv_list[0])
-       cat = first_split[0]
-       cpv_list[0] = first_split[1:]
-       for i in xrange(1, len(cpv_list)):
-               cpv_list[i] = portage.catpkgsplit(cpv_list[i])[1:]
-       cpv_list.sort(portage.pkgcmp, reverse=True)
-       for i, (pn, ver, rev) in enumerate(cpv_list):
-               if rev == "r0":
-                       cpv = cat + "/" + pn + "-" + ver
-               else:
-                       cpv = cat + "/" + pn + "-" + ver + "-" + rev
-               cpv_list[i] = cpv
-
 def visible(pkgsettings, cpv, metadata, built=False, installed=False):
        """
        Check if a package is visible. This can raise an InvalidDependString
@@ -1873,7 +1856,8 @@ class depgraph(object):
                                                # we have to try all of them to prevent the old-style
                                                # virtuals from overriding available new-styles.
                                        continue
-                               cpv_sort_descending(cpv_list)
+                               # descending order
+                               cpv_list.reverse()
                                for cpv in cpv_list:
                                        if filtered_db.cpv_exists(cpv):
                                                continue
@@ -1984,7 +1968,8 @@ class depgraph(object):
                                cpv_list = db.xmatch("match-all", atom)
                        else:
                                cpv_list = db.match(atom)
-                       cpv_sort_descending(cpv_list)
+                       # descending order
+                       cpv_list.reverse()
                        for cpv in cpv_list:
                                try:
                                        metadata = dict(izip(db_keys,
@@ -2098,7 +2083,8 @@ class depgraph(object):
                                        cpv_list = db.xmatch("match-all", atom)
                                else:
                                        cpv_list = db.match(atom)
-                               cpv_sort_descending(cpv_list)
+                               # descending order
+                               cpv_list.reverse()
                                for cpv in cpv_list:
                                        reinstall_for_flags = None
                                        try:
index 329271ee68663e6d1fd3f68193830e997027cc00..0bec4cce5a49162b4e5053440095b1494b8c9f27 100644 (file)
@@ -9,7 +9,7 @@ from portage.locks import unlockfile
 from portage.output import red
 from portage.util import writemsg
 from portage import dep_expand
-from portage.versions import catsplit
+from portage.versions import catpkgsplit, catsplit, pkgcmp
 
 
 class dbapi(object):
@@ -22,6 +22,25 @@ class dbapi(object):
        def cp_list(self, cp, use_cache=1):
                return
 
+       def _cpv_sort_ascending(self, cpv_list):
+               """
+               Use this to sort self.cp_list() results in ascending
+               order. It sorts in place and returns None.
+               """
+               if len(cpv_list) > 1:
+                       first_split = catpkgsplit(cpv_list[0])
+                       cat = first_split[0]
+                       cpv_list[0] = first_split[1:]
+                       for i in xrange(1, len(cpv_list)):
+                               cpv_list[i] = catpkgsplit(cpv_list[i])[1:]
+                       cpv_list.sort(pkgcmp)
+                       for i, (pn, ver, rev) in enumerate(cpv_list):
+                               if rev == "r0":
+                                       cpv = cat + "/" + pn + "-" + ver
+                               else:
+                                       cpv = cat + "/" + pn + "-" + ver + "-" + rev
+                               cpv_list[i] = cpv
+
        def cpv_all(self):
                """Return all CPVs in the db
                Args:
index 4f9b1cbbe3a9dd030503db310d8657465a6e89dc..84f8c196dd36116eaf3c7a0b5874feacd4fa9e6b 100644 (file)
@@ -528,17 +528,7 @@ class portdbapi(dbapi):
                        mylist = d.keys()
                # Always sort in ascending order here since it's handy
                # and the result can be easily cached and reused.
-               if len(mylist) > 1:
-                       for i in xrange(len(mylist)):
-                               mylist[i] = catpkgsplit(mylist[i])[1:]
-                       mylist.sort(pkgcmp)
-                       cat = mysplit[0]
-                       for i, (pn, ver, rev) in enumerate(mylist):
-                               if rev == "r0":
-                                       cpv = cat + "/" + pn + "-" + ver
-                               else:
-                                       cpv = cat + "/" + pn + "-" + ver + "-" + rev
-                               mylist[i] = cpv
+               self._cpv_sort_ascending(mylist)
                if self.frozen and mytree is None:
                        cachelist = mylist[:]
                        self.xcache["cp-list"][mycp] = cachelist
index 9126146156c457f001945b0dcafe2ba4b4a99612..abc557583bf5b771b470d156a7a4d7742a2e9f1c 100644 (file)
@@ -343,7 +343,7 @@ class vardbapi(dbapi):
                if use_cache and self.cpcache.has_key(mycp):
                        cpc = self.cpcache[mycp]
                        if cpc[0] == mystat:
-                               return cpc[1]
+                               return cpc[1][:]
                cat_dir = self.getpath(mysplit[0])
                try:
                        dir_list = os.listdir(cat_dir)
@@ -369,7 +369,8 @@ class vardbapi(dbapi):
                                if ps[0] == mysplit[1]:
                                        returnme.append(mysplit[0]+"/"+x)
                if use_cache:
-                       self.cpcache[mycp] = [mystat,returnme]
+                       self._cpv_sort_ascending(returnme)
+                       self.cpcache[mycp] = [mystat, returnme[:]]
                elif self.cpcache.has_key(mycp):
                        del self.cpcache[mycp]
                return returnme
index 65ac0a8d03405b4074ea5e2c86f26dd2351cd3f8..52aca6880edc3a4c82459ee27977dfe1020b5bca 100644 (file)
@@ -41,10 +41,17 @@ class fakedbapi(dbapi):
                return self.cpvdict.has_key(mycpv)
 
        def cp_list(self, mycp, use_cache=1):
-               if not self.cpdict.has_key(mycp):
-                       return []
-               else:
-                       return self.cpdict[mycp]
+               cachelist = self._match_cache.get(mycp)
+               # cp_list() doesn't expand old-style virtuals
+               if cachelist and cachelist[0].startswith(mycp):
+                       return cachelist[:]
+               cpv_list = self.cpdict.get(mycp)
+               if cpv_list is None:
+                       cpv_list = []
+               self._cpv_sort_ascending(cpv_list)
+               if not (not cpv_list and mycp.startswith("virtual/")):
+                       self._match_cache[mycp] = cpv_list[:]
+               return cpv_list
 
        def cp_all(self):
                return list(self.cpdict)