Rev 2917: (robertc) Create a reusable HeadsCache in graph.HeadsCache. (Robert Collins) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Fri Oct 19 05:28:42 BST 2007


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 2917
revision-id: pqm at pqm.ubuntu.com-20071019042839-xwvsz0loa77yokxm
parent: pqm at pqm.ubuntu.com-20071018040514-3hc1k2nj1umg3tig
parent: robertc at robertcollins.net-20071019034502-55zg0l4gvmucd1uw
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2007-10-19 05:28:39 +0100
message:
  (robertc) Create a reusable HeadsCache in graph.HeadsCache. (Robert Collins)
modified:
  bzrlib/graph.py                graph_walker.py-20070525030359-y852guab65d4wtn0-1
  bzrlib/repository.py           rev_storage.py-20051111201905-119e9401e46257e3
    ------------------------------------------------------------
    revno: 2911.4.3
    merged: robertc at robertcollins.net-20071019034502-55zg0l4gvmucd1uw
    parent: robertc at robertcollins.net-20071018013101-6zbs4utnk16q59fu
    committer: Robert Collins <robertc at robertcollins.net>
    branch nick: graph
    timestamp: Fri 2007-10-19 13:45:02 +1000
    message:
      Make the contract of HeadsCache.heads() more clear.
    ------------------------------------------------------------
    revno: 2911.4.2
    merged: robertc at robertcollins.net-20071018013101-6zbs4utnk16q59fu
    parent: robertc at robertcollins.net-20071017225921-l3ypcpn0p9aawmp5
    committer: Robert Collins <robertc at robertcollins.net>
    branch nick: graph
    timestamp: Thu 2007-10-18 11:31:01 +1000
    message:
      Make HeadsCache actually work.
    ------------------------------------------------------------
    revno: 2911.4.1
    merged: 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 file 'bzrlib/graph.py'
--- a/bzrlib/graph.py	2007-09-24 19:34:04 +0000
+++ b/bzrlib/graph.py	2007-10-19 03:45:02 +0000
@@ -371,6 +371,34 @@
         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.
+
+        This matches the API of Graph.heads(), specifically the return value is
+        a set which can be mutated, and ordering of the input is not preserved
+        in the output.
+
+        :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 = frozenset(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-17 05:59:11 +0000
+++ b/bzrlib/repository.py	2007-10-19 04:28:39 +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.
@@ -285,7 +285,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:
@@ -2486,7 +2486,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"""
@@ -2531,14 +2533,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