Rev 3170: New method ``iter_inventories`` on Repository for access to many in http://people.ubuntu.com/~robertc/baz2.0/iter_inventories

Robert Collins robertc at robertcollins.net
Mon Jan 7 01:28:07 GMT 2008


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

------------------------------------------------------------
revno: 3170
revision-id:robertc at robertcollins.net-20080107012738-p74oqa65zc0z2xrr
parent: pqm at pqm.ubuntu.com-20080105015401-67wgbytv81394cl1
committer: Robert Collins <robertc at robertcollins.net>
branch nick: iter_inventories
timestamp: Mon 2008-01-07 12:27:38 +1100
message:
  New method ``iter_inventories`` on Repository for access to many
  inventories. This is primarily used by the ``revision_trees`` method, as
  direct access to inventories is discouraged. (Robert Collins)
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/remote.py               remote.py-20060720103555-yeeg2x51vn0rbtdp-1
  bzrlib/repository.py           rev_storage.py-20051111201905-119e9401e46257e3
  bzrlib/tests/repository_implementations/test_repository.py test_repository.py-20060131092128-ad07f494f5c9d26c
=== modified file 'NEWS'
--- a/NEWS	2008-01-05 01:54:01 +0000
+++ b/NEWS	2008-01-07 01:27:38 +0000
@@ -149,6 +149,10 @@
 
     * get_parent_map now always provides tuples as its output.  (Aaron Bentley)
 
+    * New method ``iter_inventories`` on Repository for access to many
+      inventories. This is primarily used by the ``revision_trees`` method, as
+      direct access to inventories is discouraged. (Robert Collins)
+
     * Parent Providers should now implement ``get_parent_map`` returning a
       dictionary instead of ``get_parents`` returning a list.
       ``get_parents`` is now considered deprecated.  (John Arbash Meinel)

=== modified file 'bzrlib/remote.py'
--- a/bzrlib/remote.py	2007-12-21 19:56:30 +0000
+++ b/bzrlib/remote.py	2008-01-07 01:27:38 +0000
@@ -644,6 +644,10 @@
         self._ensure_real()
         return self._real_repository.get_inventory(revision_id)
 
+    def iter_inventories(self, revision_ids):
+        self._ensure_real()
+        return self._real_repository.iter_inventories(revision_ids)
+
     @needs_read_lock
     def get_revision(self, revision_id):
         self._ensure_real()

=== modified file 'bzrlib/repository.py'
--- a/bzrlib/repository.py	2008-01-04 04:57:47 +0000
+++ b/bzrlib/repository.py	2008-01-07 01:27:38 +0000
@@ -502,7 +502,8 @@
         :param parents: The revision ids of the parents that revision_id
                         is known to have and are in the repository already.
 
-        returns the sha1 of the serialized inventory.
+        :returns: The validator(which is a sha1 digest, though what is sha'd is
+            repository format specific) of the serialized inventory.
         """
         assert self.is_in_write_group()
         _mod_revision.check_not_reserved_id(revision_id)
@@ -1450,9 +1451,27 @@
 
     @needs_read_lock
     def get_inventory(self, revision_id):
-        """Get Inventory object by hash."""
-        return self.deserialise_inventory(
-            revision_id, self.get_inventory_xml(revision_id))
+        """Get Inventory object by revision id."""
+        return self.iter_inventories([revision_id]).next()
+
+    def iter_inventories(self, revision_ids):
+        """Get many inventories by revision_ids.
+
+        This will buffer some or all of the texts used in constructing the
+        inventories in memory, but will only parse a single inventory at a
+        time.
+
+        :return: An iterator of inventories.
+        """
+        assert None not in revision_ids
+        assert _mod_revision.NULL_REVISION not in revision_ids
+        return self._iter_inventories(revision_ids)
+
+    def _iter_inventories(self, revision_ids):
+        """single-document based inventory iteration."""
+        texts = self.get_inventory_weave().get_texts(revision_ids)
+        for text, revision_id in zip(texts, revision_ids):
+            yield self.deserialise_inventory(revision_id, text)
 
     def deserialise_inventory(self, revision_id, xml):
         """Transform the xml into an inventory object. 
@@ -1626,12 +1645,9 @@
         """Return Tree for a revision on this branch.
 
         `revision_id` may not be None or 'null:'"""
-        assert None not in revision_ids
-        assert _mod_revision.NULL_REVISION not in revision_ids
-        texts = self.get_inventory_weave().get_texts(revision_ids)
-        for text, revision_id in zip(texts, revision_ids):
-            inv = self.deserialise_inventory(revision_id, text)
-            yield RevisionTree(self, inv, revision_id)
+        inventories = self.iter_inventories(revision_ids)
+        for inv in inventories:
+            yield RevisionTree(self, inv, inv.revision_id)
 
     @needs_read_lock
     def get_ancestry(self, revision_id, topo_sorted=True):

=== modified file 'bzrlib/tests/repository_implementations/test_repository.py'
--- a/bzrlib/tests/repository_implementations/test_repository.py	2008-01-02 15:49:06 +0000
+++ b/bzrlib/tests/repository_implementations/test_repository.py	2008-01-07 01:27:38 +0000
@@ -77,6 +77,19 @@
         tree_b.get_file_text('file1')
         rev1 = repo_b.get_revision('rev1')
 
+    def test_iter_inventories_is_ordered(self):
+        # just a smoke test
+        tree = self.make_branch_and_tree('a')
+        first_revision = tree.commit('')
+        second_revision = tree.commit('')
+        tree.lock_read()
+        self.addCleanup(tree.unlock)
+        revs = (first_revision, second_revision)
+        invs = tree.branch.repository.iter_inventories(revs)
+        for rev_id, inv in zip(revs, invs):
+            self.assertEqual(rev_id, inv.revision_id)
+            self.assertIsInstance(inv, Inventory)
+
     def test_supports_rich_root(self):
         tree = self.make_branch_and_tree('a')
         tree.commit('')



More information about the bazaar-commits mailing list