changeset 1468:b98ff95b5861

maps: disable iterating methods of RevMap Since __iter__ is disabled in SqliteRevMap, we want to avoid the pattern iterating RevMap as well. This patch disables all entries to keys, values and items and makes it possible to bypass the restriction by setting "_allowiter". Developers looking for a way to iterate the RevMap should add a new method in RevMap and SqliteRevMap instead. The rebuildmeta test needs iteration so we enable it explicitly there.
author Jun Wu <quark@fb.com>
date Tue, 31 May 2016 18:24:58 +0100
parents 53e306a6086b
children 7bb2c6ca4d24
files hgsubversion/maps.py tests/comprehensive/test_rebuildmeta.py
diffstat 2 files changed, 31 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/maps.py
+++ b/hgsubversion/maps.py
@@ -355,6 +355,9 @@ class RevMap(dict):
         self._filepath = revmap_path
         self._lastpulled_file = lastpulled_path
         self._hashes = None
+        # disable iteration to have a consistent interface with SqliteRevMap
+        # it's less about performance since RevMap needs iteration internally
+        self._allowiter = False
 
         self.firstpulled = 0
         if os.path.isfile(self._filepath):
@@ -364,16 +367,16 @@ class RevMap(dict):
 
     def hashes(self):
         if self._hashes is None:
-            self._hashes = dict((v, k) for (k, v) in self.iteritems())
+            self._hashes = dict((v, k) for (k, v) in self._origiteritems())
         return self._hashes
 
     def branchedits(self, branch, rev):
         check = lambda x: x[0][1] == branch and x[0][0] < rev.revnum
-        return sorted(filter(check, self.iteritems()), reverse=True)
+        return sorted(filter(check, self._origiteritems()), reverse=True)
 
     def branchmaxrevnum(self, branch, maxrevnum):
         result = 0
-        for num, br in self.iterkeys():
+        for num, br in self._origiterkeys():
             if br == branch and num <= maxrevnum and num > result:
                 result = num
         return result
@@ -386,7 +389,7 @@ class RevMap(dict):
         return bin(lines[-1].split(' ', 2)[1])
 
     def revhashes(self, revnum):
-        for key, value in self.iteritems():
+        for key, value in self._origiteritems():
             if key[0] == revnum:
                 yield value
 
@@ -459,6 +462,24 @@ class RevMap(dict):
         if self._hashes is not None:
             self._hashes[ha] = (revnum, branch)
 
+    @classmethod
+    def _wrapitermethods(cls):
+        def wrap(orig):
+            def wrapper(self, *args, **kwds):
+                if not self._allowiter:
+                    raise NotImplementedError(
+                        'Iteration methods on RevMap are disabled ' +
+                        'to avoid performance issues on SqliteRevMap')
+                return orig(self, *args, **kwds)
+            return wrapper
+        methodre = re.compile(r'^_*(?:iter|view)?(?:keys|items|values)?_*$')
+        for name in filter(methodre.match, dir(cls)):
+            orig = getattr(cls, name)
+            setattr(cls, '_orig%s' % name, orig)
+            setattr(cls, name, wrap(orig))
+
+RevMap._wrapitermethods()
+
 
 class SqliteRevMap(collections.MutableMapping):
     """RevMap backed by sqlite3.
--- a/tests/comprehensive/test_rebuildmeta.py
+++ b/tests/comprehensive/test_rebuildmeta.py
@@ -145,7 +145,12 @@ def _run_assertions(self, name, single, 
     srcbi = util.load(os.path.join(src.path, 'svn', 'branch_info'))
     destbi = util.load(os.path.join(dest.path, 'svn', 'branch_info'))
     self.assertEqual(sorted(srcbi.keys()), sorted(destbi.keys()))
-    revkeys = svnmeta.SVNMeta(dest).revmap.keys()
+    revmap = svnmeta.SVNMeta(dest).revmap
+    # revmap disables __iter__ intentionally to avoid possible slow code
+    # (not using database index in SqliteRevMap)
+    # we need to fetch all keys so enable it by setting _allowiter
+    revmap._allowiter = True
+    revkeys = revmap.keys()
     for branch in destbi:
         srcinfo = srcbi[branch]
         destinfo = destbi[branch]