Rev 2637: Implement add_node/add_nodes to the GraphIndexPrefixAdapter. in http://people.ubuntu.com/~robertc/baz2.0/index

Robert Collins robertc at robertcollins.net
Mon Jul 30 05:14:25 BST 2007


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

------------------------------------------------------------
revno: 2637
revision-id: robertc at robertcollins.net-20070730041421-1jt0kd6ggvlm0jdn
parent: robertc at robertcollins.net-20070729233741-tp1d2j06b7zde9j3
committer: Robert Collins <robertc at robertcollins.net>
branch nick: index
timestamp: Mon 2007-07-30 14:14:21 +1000
message:
  Implement add_node/add_nodes to the GraphIndexPrefixAdapter.
modified:
  bzrlib/index.py                index.py-20070712131115-lolkarso50vjr64s-1
  bzrlib/tests/test_index.py     test_index.py-20070712131115-lolkarso50vjr64s-2
=== modified file 'bzrlib/index.py'
--- a/bzrlib/index.py	2007-07-29 23:37:41 +0000
+++ b/bzrlib/index.py	2007-07-30 04:14:21 +0000
@@ -692,6 +692,42 @@
         self.prefix_len = len(prefix)
         self.add_nodes_callback = add_nodes_callback
 
+    def add_nodes(self, nodes):
+        """Add nodes to the index.
+
+        :param nodes: An iterable of (key, node_refs, value) entries to add.
+        """
+        # save nodes in case its an iterator
+        nodes = tuple(nodes)
+        translated_nodes = []
+        try:
+            for (key, value, node_refs) in nodes:
+                adjusted_references = (
+                    tuple(tuple(self.prefix_key + ref_node for ref_node in ref_list)
+                        for ref_list in node_refs))
+                translated_nodes.append((self.prefix_key + key, value,
+                    adjusted_references))
+        except ValueError:
+            # XXX: TODO add an explicit interface for getting the reference list
+            # status, to handle this bit of user-friendliness in the API more 
+            # explicitly.
+            for (key, value) in nodes:
+                translated_nodes.append((self.prefix_key + key, value))
+        self.add_nodes_callback(translated_nodes)
+
+    def add_node(self, key, value, references=()):
+        """Add a node to the index.
+
+        :param key: The key. keys are non-empty tuples containing
+            as many whitespace-free utf8 bytestrings as the key length
+            defined for this index.
+        :param references: An iterable of iterables of keys. Each is a
+            reference to another key.
+        :param value: The value to associate with the key. It may be any
+            bytes as long as it does not contain \0 or \n.
+        """
+        self.add_nodes(((key, value, references), ))
+
     def _strip_prefix(self, an_iter):
         """Strip prefix data from nodes and return it."""
         for node in an_iter:

=== modified file 'bzrlib/tests/test_index.py'
--- a/bzrlib/tests/test_index.py	2007-07-29 23:37:41 +0000
+++ b/bzrlib/tests/test_index.py	2007-07-30 04:14:21 +0000
@@ -756,12 +756,35 @@
 
 class TestGraphIndexPrefixAdapter(TestCaseWithMemoryTransport):
 
-    def make_index(self, ref_lists=1, key_elements=2, nodes=[]):
+    def make_index(self, ref_lists=1, key_elements=2, nodes=[], add_callback=False):
         result = InMemoryGraphIndex(ref_lists, key_elements=key_elements)
         result.add_nodes(nodes)
-        adapter = GraphIndexPrefixAdapter(result, ('prefix', ), key_elements - 1)
+        if add_callback:
+            add_nodes_callback=result.add_nodes
+        else:
+            add_nodes_callback=None
+        adapter = GraphIndexPrefixAdapter(result, ('prefix', ), key_elements - 1,
+            add_nodes_callback=add_nodes_callback)
         return result, adapter
 
+    def test_add_node(self):
+        index, adapter = self.make_index(add_callback=True)
+        adapter.add_node(('key',), 'value', ((('ref',),),))
+        self.assertEqual(set([(('prefix', 'key'), 'value', ((('prefix', 'ref'),),))]),
+            set(index.iter_all_entries()))
+
+    def test_add_nodes(self):
+        index, adapter = self.make_index(add_callback=True)
+        adapter.add_nodes((
+            (('key',), 'value', ((('ref',),),)),
+            (('key2',), 'value2', ((),)),
+            ))
+        self.assertEqual(set([
+            (('prefix', 'key2'), 'value2', ((),)),
+            (('prefix', 'key'), 'value', ((('prefix', 'ref'),),))
+            ]),
+            set(index.iter_all_entries()))
+
     def test_construct(self):
         index = InMemoryGraphIndex()
         adapter = GraphIndexPrefixAdapter(index, ('prefix', ), 1)



More information about the bazaar-commits mailing list