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 04:58:34 BST 2007
At http://people.ubuntu.com/~robertc/baz2.0/index
------------------------------------------------------------
revno: 2637
revision-id: robertc at robertcollins.net-20070730035831-mi0q02ry7zznzulr
parent: robertc at robertcollins.net-20070729233741-tp1d2j06b7zde9j3
committer: Robert Collins <robertc at robertcollins.net>
branch nick: index
timestamp: Mon 2007-07-30 13:58:31 +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 03:58:31 +0000
@@ -692,6 +692,39 @@
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)
+ try:
+ for (key, value, node_refs) in nodes:
+ self.add_node(key, value, node_refs)
+ 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:
+ self.add_node(key, value)
+
+ 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.
+ """
+ adjusted_references = (
+ tuple(tuple(self.prefix_key + ref_node for ref_node in ref_list)
+ for ref_list in references))
+ self.adapted.add_node(self.prefix_key + key, value, adjusted_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 03:58:31 +0000
@@ -762,6 +762,24 @@
adapter = GraphIndexPrefixAdapter(result, ('prefix', ), key_elements - 1)
return result, adapter
+ def test_add_node(self):
+ index, adapter = self.make_index()
+ 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()
+ 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