Rev 2607: Detect duplicate key insertion. in http://people.ubuntu.com/~robertc/baz2.0/repository

Robert Collins robertc at robertcollins.net
Fri Jul 13 07:53:54 BST 2007


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

------------------------------------------------------------
revno: 2607
revision-id: robertc at robertcollins.net-20070713065351-j7gx3hkkdwafbmvf
parent: robertc at robertcollins.net-20070713064707-c40uq3k00sk68fev
committer: Robert Collins <robertc at robertcollins.net>
branch nick: repository
timestamp: Fri 2007-07-13 16:53:51 +1000
message:
  Detect duplicate key insertion.
modified:
  bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
  bzrlib/index.py                index.py-20070712131115-lolkarso50vjr64s-1
  bzrlib/tests/test_errors.py    test_errors.py-20060210110251-41aba2deddf936a8
  bzrlib/tests/test_index.py     test_index.py-20070712131115-lolkarso50vjr64s-2
=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py	2007-07-12 16:49:54 +0000
+++ b/bzrlib/errors.py	2007-07-13 06:53:51 +0000
@@ -362,6 +362,16 @@
         self.value = value
 
 
+class BadIndexDuplicateKey(BzrError):
+
+    _fmt = "The key '%(key)s' is already in index '%(index)s'."
+
+    def __init__(self, key, index):
+        BzrError.__init__(self)
+        self.key = key
+        self.index = index
+
+
 class BadIndexKey(BzrError):
 
     _fmt = "The key '%(key)s' is not a valid key."

=== modified file 'bzrlib/index.py'
--- a/bzrlib/index.py	2007-07-13 06:47:07 +0000
+++ b/bzrlib/index.py	2007-07-13 06:53:51 +0000
@@ -39,7 +39,7 @@
             entry.
         """
         self.reference_lists = reference_lists
-        self._nodes = []
+        self._nodes = {}
 
     def add_node(self, key, references, value):
         """Add a node to the index.
@@ -60,12 +60,14 @@
             for reference in reference_list:
                 if _whitespace_re.search(reference) is not None:
                     raise errors.BadIndexKey(reference)
-        self._nodes.append((key, references, value))
+        if key in self._nodes:
+            raise errors.BadIndexDuplicateKey(key, self)
+        self._nodes[key] = (references, value)
 
     def finish(self):
         lines = [_SIGNATURE]
         lines.append(_OPTION_NODE_REFS + str(self.reference_lists) + '\n')
-        for key, references, value in self._nodes:
+        for key, (references, value) in self._nodes.items():
             flattened_references = ''
             lines.append("%s\0%s\0%s\n" % (key, flattened_references, value))
         lines.append('\n')

=== modified file 'bzrlib/tests/test_errors.py'
--- a/bzrlib/tests/test_errors.py	2007-07-12 16:49:54 +0000
+++ b/bzrlib/tests/test_errors.py	2007-07-13 06:53:51 +0000
@@ -193,6 +193,11 @@
         self.assertEqual("Error in data for index foo.",
             str(error))
 
+    def test_bad_index_duplicate_key(self):
+        error = errors.BadIndexDuplicateKey("foo", "bar")
+        self.assertEqual("The key 'foo' is already in index 'bar'.",
+            str(error))
+
     def test_bad_index_key(self):
         error = errors.BadIndexKey("foo")
         self.assertEqual("The key 'foo' is not a valid key.",

=== modified file 'bzrlib/tests/test_index.py'
--- a/bzrlib/tests/test_index.py	2007-07-13 06:47:07 +0000
+++ b/bzrlib/tests/test_index.py	2007-07-13 06:53:51 +0000
@@ -93,6 +93,12 @@
         self.assertRaises(errors.BadIndexKey, builder.add_node, 'akey',
             ([], ['a bad key']), 'data aa')
 
+    def test_add_duplicate_key(self):
+        builder = GraphIndexBuilder()
+        builder.add_node('key', (), 'data')
+        self.assertRaises(errors.BadIndexDuplicateKey, builder.add_node, 'key',
+            (), 'data')
+
 
 class TestGraphIndex(TestCaseWithMemoryTransport):
 




More information about the bazaar-commits mailing list