Rev 3766: Start iter_changes between CHKMap instances. in http://people.ubuntu.com/~robertc/baz2.0/repository

Robert Collins robertc at robertcollins.net
Wed Nov 12 22:16:00 GMT 2008


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

------------------------------------------------------------
revno: 3766
revision-id: robertc at robertcollins.net-20081112221550-3z6l9ta8uvp1esa4
parent: robertc at robertcollins.net-20081112091906-dcbbp5hkfopz30je
committer: Robert Collins <robertc at robertcollins.net>
branch nick: repository
timestamp: Thu 2008-11-13 09:15:50 +1100
message:
  Start iter_changes between CHKMap instances.
modified:
  bzrlib/chk_map.py              chk_map.py-20081001014447-ue6kkuhofvdecvxa-1
  bzrlib/tests/test_chk_map.py   test_chk_map.py-20081001014447-ue6kkuhofvdecvxa-2
=== modified file 'bzrlib/chk_map.py'
--- a/bzrlib/chk_map.py	2008-11-12 09:19:06 +0000
+++ b/bzrlib/chk_map.py	2008-11-12 22:15:50 +0000
@@ -106,6 +106,28 @@
         result.apply_delta(delta)
         return result._save()
 
+    def iter_changes(self, basis):
+        """Iterate over the changes between basis and self.
+
+        :return: An iterator of tuples: (key, old_value, new_value). Old_value
+            is None for keys only in self; new_value is None for keys only in
+            basis.
+        """
+        # Cheap but let tests pass
+        new = dict(self.iteritems())
+        old = dict(basis.iteritems())
+        new_keys = set(new)
+        old_keys = set(old)
+        result = []
+        for key in new_keys - old_keys:
+            result.append((key, None, new[key]))
+        for key in old_keys - new_keys:
+            result.append((key, old[key], None))
+        for key in old_keys.intersection(new_keys):
+            if new[key] != old[key]:
+                result.append((key, old[key], new[key]))
+        return result
+
     def iteritems(self, key_filter=None):
         """Iterate over the entire CHKMap's contents."""
         self._ensure_root()

=== modified file 'bzrlib/tests/test_chk_map.py'
--- a/bzrlib/tests/test_chk_map.py	2008-11-12 03:19:34 +0000
+++ b/bzrlib/tests/test_chk_map.py	2008-11-12 22:15:50 +0000
@@ -37,8 +37,9 @@
         self.addCleanup(repo.abort_write_group)
         return repo.chk_bytes
 
-    def _get_map(self, a_dict, maximum_size=0):
-        chk_bytes = self.get_chk_bytes()
+    def _get_map(self, a_dict, maximum_size=0, chk_bytes=None):
+        if chk_bytes is None:
+            chk_bytes = self.get_chk_bytes()
         root_key = CHKMap.from_dict(chk_bytes, a_dict, maximum_size=maximum_size)
         chkmap = CHKMap(chk_bytes, root_key)
         return chkmap
@@ -94,8 +95,8 @@
         self.assertEqual(new_root, chkmap._root_node._key)
 
     def test_apply_ab_empty(self):
-        # applying a delta ("a", None, None) to an empty chkmap generates the
-        # same map as from_dict_ab.
+        # applying a delta ("a", None, None) to a map with 'a' in it generates
+        # an empty map.
         chk_bytes = self.get_chk_bytes()
         root_key = CHKMap.from_dict(chk_bytes, {("a",):"b"})
         chkmap = CHKMap(chk_bytes, root_key)
@@ -107,6 +108,16 @@
         # updated key.
         self.assertEqual(new_root, chkmap._root_node._key)
 
+    def test_iter_changes_empty_ab(self):
+        # Asking for changes between an empty dict to a dict with keys returns
+        # all the keys.
+        basis = self._get_map({})
+        target = self._get_map(
+            {('a',): 'content here', ('b',): 'more content'},
+            chk_bytes=basis._store)
+        self.assertEqual([(('a',), None, 'content here'),
+            (('b',), None, 'more content')], list(target.iter_changes(basis)))
+
     def test_iteritems_empty(self):
         chk_bytes = self.get_chk_bytes()
         root_key = CHKMap.from_dict(chk_bytes, {})




More information about the bazaar-commits mailing list