[PATCH 04 of 10] branchmap: add the tiprev (cache key) on the branchmap object

Pierre-Yves David pierre-yves.david at ens-lyon.org
Fri Dec 21 19:48:51 CST 2012


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at logilab.fr>
# Date 1356138386 -3600
# Node ID 090ada0acddb4486e94fba9c89e25b0624442d0f
# Parent  ad194a8ab5c15e6d74fe087061ee960f5d6bbf16
branchmap: add the tiprev (cache key) on the branchmap object

The actual cache key used on disk is the (tipnode, tiprev) pair. There is no
reason not to use the revision number for the in memory version.

diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py
--- a/mercurial/branchmap.py
+++ b/mercurial/branchmap.py
@@ -13,11 +13,11 @@ def read(repo):
     try:
         f = repo.opener("cache/branchheads")
         lines = f.read().split('\n')
         f.close()
     except (IOError, OSError):
-        return branchcache(), nullrev
+        return branchcache()
 
     try:
         last, lrev = lines.pop(0).split(" ", 1)
         last, lrev = bin(last), int(lrev)
         if lrev >= len(repo) or repo[lrev].node() != last:
@@ -31,17 +31,18 @@ def read(repo):
             if not node in repo:
                 raise ValueError('invalidating branch cache because node '+
                                  '%s does not exist' % node)
             partial.setdefault(label, []).append(bin(node))
         partial.tipnode = last
+        partial.tiprev = lrev
     except KeyboardInterrupt:
         raise
     except Exception, inst:
         if repo.ui.debugflag:
             repo.ui.warn(str(inst), '\n')
-        partial, lrev = branchcache(), nullrev
-    return partial, lrev
+        partial = branchcache()
+    return partial
 
 def write(repo, branches, tip, tiprev):
     try:
         f = repo.opener("cache/branchheads", "w", atomictemp=True)
         f.write("%s %s\n" % (hex(tip), tiprev))
@@ -119,35 +120,35 @@ def updatecache(repo):
     partial = repo._branchcache
     if partial is not None and partial.tipnode == tip:
         return
 
     if partial is None or partial.tipnode not in cl.nodemap:
-        partial, lrev = read(repo)
-    else:
-        lrev = cl.rev(partial.tipnode)
+        partial = read(repo)
 
     catip = repo._cacheabletip()
-    # if lrev == catip: cache is already up to date
-    # if lrev >  catip: we have uncachable element in `partial` can't write
-    #                   on disk
-    if lrev < catip:
-        ctxgen = (repo[r] for r in cl.revs(lrev + 1, catip))
+    # if partial.tiprev == catip: cache is already up to date
+    # if partial.tiprev >  catip: we have uncachable element in `partial` can't
+    #                             write on disk
+    if partial.tiprev < catip:
+        ctxgen = (repo[r] for r in cl.revs(partial.tiprev + 1, catip))
         update(repo, partial, ctxgen)
         partial.tipnode = cl.node(catip)
-        write(repo, partial, partial.tipnode, catip)
-        lrev = catip
+        partial.tiprev = catip
+        write(repo, partial, partial.tipnode, partial.tiprev)
     # If cacheable tip were lower than actual tip, we need to update the
     # cache up to tip. This update (from cacheable to actual tip) is not
     # written to disk since it's not cacheable.
     tiprev = len(repo) - 1
-    if lrev < tiprev:
-        ctxgen = (repo[r] for r in cl.revs(lrev + 1, tiprev))
+    if partial.tiprev < tiprev:
+        ctxgen = (repo[r] for r in cl.revs(partial.tiprev + 1, tiprev))
         update(repo, partial, ctxgen)
         partial.tipnode = cl.node(tiprev)
+        partial.tiprev = tiprev
     repo._branchcache = partial
 
 class branchcache(dict):
     """A dict like object that hold branches heads cache"""
 
-    def __init__(self, entries=(), tipnode=nullid):
+    def __init__(self, entries=(), tipnode=nullid, tiprev=nullrev):
         super(branchcache, self).__init__(entries)
         self.tipnode = tipnode
+        self.tiprev = tiprev
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -1432,17 +1432,17 @@ class localrepository(object):
         '''
         # If we have info, newheadnodes, on how to update the branch cache, do
         # it, Otherwise, since nodes were destroyed, the cache is stale and this
         # will be caught the next time it is read.
         if newheadnodes:
-            tiprev = len(self) - 1
             ctxgen = (self[node] for node in newheadnodes
                       if self.changelog.hasnode(node))
-            branchmap.update(self, self._branchcache, ctxgen)
-            self._branchcache.tipnode = self.changelog.tip()
-            branchmap.write(self, self._branchcache, self._branchcache.tipnode,
-                            tiprev)
+            cache = self._branchcache
+            branchmap.update(self, cache, ctxgen)
+            cache.tipnode = self.changelog.tip()
+            cache.tiprev = self.changelog.rev(cache.tipnode)
+            branchmap.write(self, cache, cache.tipnode, cache.tiprev)
 
         # Ensure the persistent tag cache is updated.  Doing it now
         # means that the tag cache only has to worry about destroyed
         # heads immediately after a strip/rollback.  That in turn
         # guarantees that "cachetip == currenttip" (comparing both rev
@@ -2493,13 +2493,14 @@ class localrepository(object):
 
                 if rbheads:
                     rtiprev = max((int(self.changelog.rev(node))
                             for node in rbheads))
                     cache = branchmap.branchcache(rbranchmap,
-                                                  self[rtiprev].node())
+                                                  self[rtiprev].node(),
+                                                  rtiprev)
                     self._branchcache = cache
-                    branchmap.write(self, cache, cache.tipnode, rtiprev)
+                    branchmap.write(self, cache, cache.tipnode, cache.tiprev)
             self.invalidate()
             return len(self.heads()) + 1
         finally:
             lock.release()
 


More information about the Mercurial-devel mailing list