Rev 5373: Add tests that we handle sizes close to and >2GB properly. in http://bazaar.launchpad.net/~jameinel/bzr/2.3-btree-chk-leaf

John Arbash Meinel john at arbash-meinel.com
Wed Aug 4 02:31:20 BST 2010


At http://bazaar.launchpad.net/~jameinel/bzr/2.3-btree-chk-leaf

------------------------------------------------------------
revno: 5373
revision-id: john at arbash-meinel.com-20100804013103-mz61vkhuj1w3uozh
parent: john at arbash-meinel.com-20100803234937-h7s1qn5mif9k7su6
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.3-btree-chk-leaf
timestamp: Tue 2010-08-03 20:31:03 -0500
message:
  Add tests that we handle sizes close to and >2GB properly.
  
  We can't use %llu because that wasn't added until python 2.7, but
  we can use str(PyLong).
-------------- next part --------------
=== modified file 'bzrlib/_btree_serializer_pyx.pyx'
--- a/bzrlib/_btree_serializer_pyx.pyx	2010-08-03 23:49:37 +0000
+++ b/bzrlib/_btree_serializer_pyx.pyx	2010-08-04 01:31:03 +0000
@@ -30,6 +30,8 @@
         pass
     int PyList_Append(object lst, object item) except -1
 
+    int HAVE_LONG_LONG
+
     char *PyString_AsString(object p) except NULL
     object PyString_FromStringAndSize(char *, Py_ssize_t)
     PyObject *PyString_FromStringAndSize_ptr "PyString_FromStringAndSize" (char *, Py_ssize_t)
@@ -330,7 +332,7 @@
 #       One slightly ugly option would be to cache block offsets in a global.
 #       However, that leads to thread-safety issues, etc.
 ctypedef struct gc_chk_sha1_record:
-    unsigned long long block_offset
+    long long block_offset
     unsigned int block_length
     unsigned int record_start
     unsigned int record_end
@@ -520,10 +522,19 @@
         value_and_refs = StaticTuple_New(2)
         # This is really inefficient to go from a logical state back to a
         # string, but it makes things work a bit better internally for now.
-        value = PyString_FromFormat('%lu %lu %lu %lu',
-                                    <unsigned long>record.block_offset,
+        if record.block_offset >= 0xFFFFFFFFull:
+            # %llu is what we really want, but unfortunately it was only added
+            # in python 2.7... :(
+            block_offset_str = str(record.block_offset)
+            value = PyString_FromFormat('%s %lu %lu %lu',
+                                    PyString_AS_STRING(block_offset_str),
                                     record.block_length,
                                     record.record_start, record.record_end)
+        else:
+            value = PyString_FromFormat('%lu %lu %lu %lu',
+                                        <unsigned long>record.block_offset,
+                                        record.block_length,
+                                        record.record_start, record.record_end)
         Py_INCREF(value)
         StaticTuple_SET_ITEM(value_and_refs, 0, value)
         # Always empty refs

=== modified file 'bzrlib/tests/test__btree_serializer.py'
--- a/bzrlib/tests/test__btree_serializer.py	2010-08-03 23:41:01 +0000
+++ b/bzrlib/tests/test__btree_serializer.py	2010-08-04 01:31:03 +0000
@@ -131,6 +131,12 @@
 sha1:123456789012345678901234567890abcdefabcd\x00\x001 2 3 4
 """
 
+_large_offsets = """type=leaf
+sha1:123456789012345678901234567890abcdefabcd\x00\x0012345678901 1234567890 0 1
+sha1:abcd123456789012345678901234567890abcdef\x00\x002147483648 2147483647 0 1
+sha1:abcdefabcd123456789012345678901234567890\x00\x004294967296 4294967295 4294967294 1
+"""
+
 _multi_key_content = """type=leaf
 sha1:123456789012345678901234567890abcdefabcd\x00\x001 2 3 4
 sha1:abcd123456789012345678901234567890abcdef\x00\x005678 2345 3456 4567
@@ -163,15 +169,22 @@
     def test_one_key_leaf(self):
         leaf = self.module._parse_into_chk(_one_key_content, 1, 0)
         self.assertEqual(1, len(leaf))
-        sha_key = ('sha1:123456789012345678901234567890abcdefabcd',)
+        sha_key = ('sha1:' + _hex_form,)
         self.assertEqual([sha_key], leaf.all_keys())
         self.assertEqual([(sha_key, ('1 2 3 4', ()))], leaf.all_items())
         self.assertTrue(sha_key in leaf)
 
+    def test_large_offsets(self):
+        leaf = self.module._parse_into_chk(_large_offsets, 1, 0)
+        self.assertEqual(['12345678901 1234567890 0 1',
+                          '2147483648 2147483647 0 1',
+                          '4294967296 4294967295 4294967294 1',
+                         ], [x[1][0] for x in leaf.all_items()])
+
     def test_many_key_leaf(self):
         leaf = self.module._parse_into_chk(_multi_key_content, 1, 0)
         self.assertEqual(2, len(leaf))
-        sha_key1 = ('sha1:123456789012345678901234567890abcdefabcd',)
+        sha_key1 = ('sha1:' + _hex_form,)
         sha_key2 = ('sha1:abcd123456789012345678901234567890abcdef',)
         self.assertEqual([sha_key1, sha_key2], leaf.all_keys())
         self.assertEqual([(sha_key1, ('1 2 3 4', ())),



More information about the bazaar-commits mailing list