Rev 2645: Change KnitGraphIndex from returning data to performing a callback on insertions. in http://people.ubuntu.com/~robertc/baz2.0/repository

Robert Collins robertc at robertcollins.net
Sat Jul 14 08:02:13 BST 2007


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

------------------------------------------------------------
revno: 2645
revision-id: robertc at robertcollins.net-20070714070210-yyotg51so4afap8c
parent: robertc at robertcollins.net-20070714060745-kfw45v1elvpztojr
committer: Robert Collins <robertc at robertcollins.net>
branch nick: repository
timestamp: Sat 2007-07-14 17:02:10 +1000
message:
  Change KnitGraphIndex from returning data to performing a callback on insertions.
modified:
  bzrlib/knit.py                 knit.py-20051212171256-f056ac8f0fbe1bd9
  bzrlib/tests/test_knit.py      test_knit.py-20051212171302-95d4c00dd5f11f2b
=== modified file 'bzrlib/knit.py'
--- a/bzrlib/knit.py	2007-07-13 23:17:02 +0000
+++ b/bzrlib/knit.py	2007-07-14 07:02:10 +0000
@@ -1320,14 +1320,18 @@
 class KnitGraphIndex(object):
     """A knit index that builds on GraphIndex."""
 
-    def __init__(self, graph_index, deltas=False):
+    def __init__(self, graph_index, deltas=False, add_callback=None):
         """Construct a KnitGraphIndex on a graph_index.
 
         :param graph_index: An implementation of bzrlib.index.GraphIndex.
         :param deltas: Allow delta-compressed records.
+        :param add_callback: If not None, allow additions to the index and call
+            this callback with a list of added GraphIndex nodes:
+            [(node, node_refs, value), ...]
         """
         self._graph_index = graph_index
         self._deltas = deltas
+        self._add_callback = add_callback
 
     def _get_entries(self, version_ids):
         """Get the entries for version_ids."""
@@ -1455,7 +1459,7 @@
         present = self._present_keys(version_ids)
         missing = version_ids.difference(present)
         if missing:
-            raise RevisionNotPresent(missing.pop, self)
+            raise RevisionNotPresent(missing.pop(), self)
 
     def add_version(self, version_id, options, pos, size, parents):
         """Add a version record to the index."""
@@ -1466,13 +1470,16 @@
         
         This function does not insert data into the Immutable GraphIndex
         backing the KnitGraphIndex, instead it prepares data for insertion by
-        the caller and checks that it is safe to insert.
+        the caller and checks that it is safe to insert then calls
+        self._add_callback with the prepared GraphIndex nodes.
 
         :param versions: a list of tuples:
                          (version_id, options, pos, size, parents).
         :return: A list of (key, node_refs, value) tuples for insertion
             into a GraphIndex.
         """
+        if not self._add_callback:
+            raise errors.ReadOnlyError(self)
         # we hope there are no repositories with inconsistent parentage
         # anymore.
         # check for dups
@@ -1502,7 +1509,7 @@
         result = []
         for key, (node_refs, value) in keys.iteritems():
             result.append((key, node_refs, value))
-        return result
+        self._add_callback(result)
         
 
 class _KnitData(_KnitComponentFile):

=== modified file 'bzrlib/tests/test_knit.py'
--- a/bzrlib/tests/test_knit.py	2007-07-13 23:17:02 +0000
+++ b/bzrlib/tests/test_knit.py	2007-07-14 07:02:10 +0000
@@ -1519,7 +1519,7 @@
         trans.put_file(name, stream)
         return GraphIndex(trans, name)
 
-    def two_graph_index(self, deltas=False):
+    def two_graph_index(self, deltas=False, catch_adds=False):
         """Build a two-graph index.
 
         :param deltas: If true, use underlying indices with two node-ref
@@ -1541,7 +1541,14 @@
                 ('parent', (['tail', 'ghost'], ), ' 100 78'),
                 ('separate', ([], ), '')])
         combined_index = CombinedGraphIndex([index1, index2])
-        return KnitGraphIndex(combined_index, deltas=deltas)
+        if catch_adds:
+            self.combined_index = combined_index
+            self.caught_entries = []
+            add_callback = self.catch_add
+        else:
+            add_callback = None
+        return KnitGraphIndex(combined_index, deltas=deltas,
+            add_callback=add_callback)
 
     def two_graph_index_no_ghosts(self):
         # build a complex graph across several indices.
@@ -1671,26 +1678,36 @@
             ['tail', 'ghost'])
         index.check_versions_present(['tail', 'separate'])
 
+    def catch_add(self, entries):
+        self.caught_entries.append(entries)
+
+    def test_add_no_callback_errors(self):
+        index = self.two_graph_index()
+        self.assertRaises(errors.ReadOnlyError, index.add_version,
+            'new', 'fulltext,no-eol', 50, 60, ['separate'])
+
     def test_add_version_smoke(self):
-        index = self.two_graph_index()
-        self.assertEqual([('new', (('separate',),), 'N50 60')],
-            index.add_version('new', 'fulltext,no-eol', 50, 60, ['separate']))
+        index = self.two_graph_index(catch_adds=True)
+        index.add_version('new', 'fulltext,no-eol', 50, 60, ['separate'])
+        self.assertEqual([[('new', (('separate',),), 'N50 60')]],
+            self.caught_entries)
 
     def test_add_version_delta_not_delta_index(self):
-        index = self.two_graph_index()
+        index = self.two_graph_index(catch_adds=True)
         self.assertRaises(errors.KnitCorrupt, index.add_version,
             'new', 'no-eol,line-delta', 0, 100, ['parent'])
+        self.assertEqual([], self.caught_entries)
 
     def test_add_version_same_dup(self):
-        index = self.two_graph_index()
+        index = self.two_graph_index(catch_adds=True)
         # options can be spelt two different ways
-        self.assertEqual([], list(index.add_version(
-            'tip', 'fulltext,no-eol', 0, 100, ['parent'])))
-        self.assertEqual([], list(index.add_version(
-            'tip', 'no-eol,fulltext', 0, 100, ['parent'])))
+        index.add_version('tip', 'fulltext,no-eol', 0, 100, ['parent'])
+        index.add_version('tip', 'no-eol,fulltext', 0, 100, ['parent'])
+        # but neither should have added data.
+        self.assertEqual([[], []], self.caught_entries)
         
     def test_add_version_different_dup(self):
-        index = self.two_graph_index(deltas=True)
+        index = self.two_graph_index(deltas=True, catch_adds=True)
         # change options
         self.assertRaises(errors.KnitCorrupt, index.add_version,
             'tip', 'no-eol,line-delta', 0, 100, ['parent'])
@@ -1706,40 +1723,46 @@
         # parents
         self.assertRaises(errors.KnitCorrupt, index.add_version,
             'tip', 'fulltext,no-eol', 0, 100, [])
+        self.assertEqual([], self.caught_entries)
         
     def test_add_versions_nodeltas(self):
-        index = self.two_graph_index()
+        index = self.two_graph_index(catch_adds=True)
+        index.add_versions([
+                ('new', 'fulltext,no-eol', 50, 60, ['separate']),
+                ('new2', 'fulltext', 0, 6, ['new']),
+                ])
         self.assertEqual([('new', (('separate',),), 'N50 60'),
             ('new2', (('new',),), ' 0 6')],
-            sorted(index.add_versions([
-                ('new', 'fulltext,no-eol', 50, 60, ['separate']),
-                ('new2', 'fulltext', 0, 6, ['new']),
-                ])))
+            sorted(self.caught_entries[0]))
+        self.assertEqual(1, len(self.caught_entries))
 
     def test_add_versions_deltas(self):
-        index = self.two_graph_index(deltas=True)
+        index = self.two_graph_index(deltas=True, catch_adds=True)
+        index.add_versions([
+                ('new', 'fulltext,no-eol', 50, 60, ['separate']),
+                ('new2', 'line-delta', 0, 6, ['new']),
+                ])
         self.assertEqual([('new', (('separate',), ()), 'N50 60'),
             ('new2', (('new',), ('new',), ), ' 0 6')],
-            sorted(index.add_versions([
-                ('new', 'fulltext,no-eol', 50, 60, ['separate']),
-                ('new2', 'line-delta', 0, 6, ['new']),
-                ])))
+            sorted(self.caught_entries[0]))
+        self.assertEqual(1, len(self.caught_entries))
 
     def test_add_versions_delta_not_delta_index(self):
-        index = self.two_graph_index()
+        index = self.two_graph_index(catch_adds=True)
         self.assertRaises(errors.KnitCorrupt, index.add_versions,
             [('new', 'no-eol,line-delta', 0, 100, ['parent'])])
+        self.assertEqual([], self.caught_entries)
 
     def test_add_versions_same_dup(self):
-        index = self.two_graph_index()
+        index = self.two_graph_index(catch_adds=True)
         # options can be spelt two different ways
-        self.assertEqual([], list(index.add_versions([(
-            'tip', 'fulltext,no-eol', 0, 100, ['parent'])])))
-        self.assertEqual([], list(index.add_versions([(
-            'tip', 'no-eol,fulltext', 0, 100, ['parent'])])))
+        index.add_versions([('tip', 'fulltext,no-eol', 0, 100, ['parent'])])
+        index.add_versions([('tip', 'no-eol,fulltext', 0, 100, ['parent'])])
+        # but neither should have added data.
+        self.assertEqual([[], []], self.caught_entries)
         
     def test_add_versions_different_dup(self):
-        index = self.two_graph_index(deltas=True)
+        index = self.two_graph_index(deltas=True, catch_adds=True)
         # change options
         self.assertRaises(errors.KnitCorrupt, index.add_versions,
             [('tip', 'no-eol,line-delta', 0, 100, ['parent'])])
@@ -1759,10 +1782,4 @@
         self.assertRaises(errors.KnitCorrupt, index.add_versions,
             [('tip', 'fulltext,no-eol', 0, 100, ['parent']),
              ('tip', 'no-eol,line-delta', 0, 100, ['parent'])])
-        
-
-## --- mutating tests for later ---
-#
-#  def test_add_version
-#  def test_add_versions
-#  
+        self.assertEqual([], self.caught_entries)




More information about the bazaar-commits mailing list