Rev 2912: Factor out the Graph.heads() cache from _RevisionTextVersionCache for reuse, and use it in commit. in http://people.ubuntu.com/~robertc/baz2.0/graph

Robert Collins robertc at robertcollins.net
Wed Oct 17 23:59:34 BST 2007


At http://people.ubuntu.com/~robertc/baz2.0/graph

------------------------------------------------------------
revno: 2912
revision-id: robertc at robertcollins.net-20071017225921-l3ypcpn0p9aawmp5
parent: pqm at pqm.ubuntu.com-20071016112750-1q8brfaq6metpfn8
committer: Robert Collins <robertc at robertcollins.net>
branch nick: graph
timestamp: Thu 2007-10-18 08:59:21 +1000
message:
  Factor out the Graph.heads() cache from _RevisionTextVersionCache for reuse, and use it in commit.
modified:
  bzrlib/graph.py                graph_walker.py-20070525030359-y852guab65d4wtn0-1
  bzrlib/repository.py           rev_storage.py-20051111201905-119e9401e46257e3
=== modified file 'bzrlib/graph.py'
--- a/bzrlib/graph.py	2007-09-24 19:34:04 +0000
+++ b/bzrlib/graph.py	2007-10-17 22:59:21 +0000
@@ -371,6 +371,30 @@
         return False
 
 
+class HeadsCache(object):
+    """A cache of results for graph heads calls."""
+
+    def __init__(self, graph):
+        self.graph = graph
+        self._heads = {}
+
+    def heads(self, keys):
+        """Return the heads of keys.
+
+        :see also: Graph.heads.
+        :param keys: The keys to calculate heads for.
+        :return: A set containing the heads, which may be mutated without
+            affecting future lookups.
+        """
+        keys = set(keys)
+        try:
+            return set(self._heads[keys])
+        except KeyError:
+            heads = self.graph.heads(keys)
+            self._heads[keys] = heads
+            return set(heads)
+
+
 class _BreadthFirstSearcher(object):
     """Parallel search the breadth-first the ancestry of revisions.
 

=== modified file 'bzrlib/repository.py'
--- a/bzrlib/repository.py	2007-10-16 02:42:33 +0000
+++ b/bzrlib/repository.py	2007-10-17 22:59:21 +0000
@@ -116,7 +116,7 @@
             self._timezone = int(timezone)
 
         self._generate_revision_if_needed()
-        self._repo_graph = repository.get_graph()
+        self._heads = graph.HeadsCache(repository.get_graph()).heads
 
     def commit(self, message):
         """Make the actual commit.
@@ -294,7 +294,7 @@
         # XXX: Friction: parent_candidates should return a list not a dict
         #      so that we don't have to walk the inventories again.
         parent_candiate_entries = ie.parent_candidates(parent_invs)
-        head_set = self._repo_graph.heads(parent_candiate_entries.keys())
+        head_set = self._heads(parent_candiate_entries.keys())
         heads = []
         for inv in parent_invs:
             if ie.file_id in inv:
@@ -2495,7 +2495,9 @@
         self.revision_versions = {}
         self.revision_parents = {}
         self.repo_graph = self.repository.get_graph()
-        self.rev_heads = {}
+        # XXX: RBC: I haven't tracked down what uses this, but it would be
+        # better to use the headscache directly I think.
+        self.heads = graph.HeadsCache(self.repo_graph).heads
 
     def add_revision_text_versions(self, tree):
         """Cache text version data from the supplied revision tree"""
@@ -2540,14 +2542,6 @@
             self.revision_parents[revision_id] = parents
             return parents
 
-    def heads(self, revision_ids):
-        revision_ids = tuple(revision_ids)
-        try:
-            return self.rev_heads[revision_ids]
-        except KeyError:
-            heads = self.repo_graph.heads(revision_ids)
-            self.rev_heads[revision_ids] = heads
-            return heads
 
 class VersionedFileChecker(object):
 



More information about the bazaar-commits mailing list