Rev 5078: Split things out a bit, so you have the 'builder' and a separate runtime class. in http://bzr.arbash-meinel.com/branches/bzr/lp/2.2.0b2-contained-pack

John Arbash Meinel john at arbash-meinel.com
Thu Mar 4 22:09:11 GMT 2010


At http://bzr.arbash-meinel.com/branches/bzr/lp/2.2.0b2-contained-pack

------------------------------------------------------------
revno: 5078
revision-id: john at arbash-meinel.com-20100304220843-say9gc02mpn9ty6w
parent: john at arbash-meinel.com-20100304214850-4a0r305ahuocgh11
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.2.0b2-contained-pack
timestamp: Thu 2010-03-04 16:08:43 -0600
message:
  Split things out a bit, so you have the 'builder' and a separate runtime class.
-------------- next part --------------
=== modified file 'bzrlib/sack.py'
--- a/bzrlib/sack.py	2010-03-04 21:48:50 +0000
+++ b/bzrlib/sack.py	2010-03-04 22:08:43 +0000
@@ -45,16 +45,19 @@
     # Note that the header intentionally starts with '\n' so that it separates
     # from the rest of the data if you open it in a text editor
     _HEADER_BASE = '\nBazaar Sack v'
+    _VERSION = 1
 
-    def __init__(self, version, start_offset):
+    def __init__(self, start_offset):
         self.start_offset = start_offset
-        self.version = version
+        self.version = self.__class__._VERSION
         self._index_builder = btree_index.BTreeBuilder(reference_lists=0,
                                                        key_elements=1)
 
     def add_index_info(self, index_type, start, length):
-        return
-        self._index_builder.add_node(index_type, '%d %d' % (start, length))
+        # Note: bzr-search uses a ContainerWriter to write out the bytes, and
+        # then adjusts the offsets so that it skips the 'Pack' overhead bytes.
+        # I guess I don't really see the benefit versus the crufty overhead...
+        self._index_builder.add_node((index_type,), '%d %d' % (start, length))
 
     def finish(self):
         chunks = []
@@ -64,11 +67,22 @@
         chunks.append(struct.pack('!QI', self.start_offset, self.version))
         return ''.join(chunks)
 
-    @classmethod
-    def from_tail(cls, bytes):
+
+
+class TrailingIndex(object):
+    """Track the root-structure for the Sack.
+
+    See TrailingIndexBuilder for the structure of this tail.
+    """
+    def __init__(self, version, start_offset):
+        self.start_offset = start_offset
+        self.version = version
+
+    @staticmethod
+    def from_tail(bytes):
         """Get the meta-info out of the last 12 bytes of content."""
         offset, version = struct.unpack('!QI', bytes[-12:])
-        tie = cls(version, offset)
+        tie = TrailingIndex(version, offset)
         return tie
 
     @classmethod
@@ -76,13 +90,6 @@
         pass
 
 
-class TrailingIndex(object):
-    """Track the root-structure for the Sack.
-
-    See TrailingIndexBuilder for the structure of this tail.
-    """
-
-
 class Sack(object):
     """A self-contained pack file.
 

=== modified file 'bzrlib/tests/test_sack.py'
--- a/bzrlib/tests/test_sack.py	2010-03-04 21:48:50 +0000
+++ b/bzrlib/tests/test_sack.py	2010-03-04 22:08:43 +0000
@@ -59,18 +59,34 @@
         index_bytes = content[len(header):-12]
         assert_btree_matches(self, index_content, index_bytes)
 
+    def test_tail_info(self):
+        self.assertAsBytes({}, sack.TrailingIndexBuilder(0))
+        self.assertAsBytes({}, sack.TrailingIndexBuilder(12345))
+        self.assertAsBytes({}, sack.TrailingIndexBuilder(2**48-1))
+        self.assertAsBytes({}, sack.TrailingIndexBuilder(2**64-1))
+        ti = sack.TrailingIndexBuilder(2**64-1)
+        ti.version = 3
+        self.assertAsBytes({}, ti)
+
+    def test_with_content(self):
+        builder = sack.TrailingIndexBuilder(start_offset=500)
+        builder.add_index_info('revisions', 0, 100)
+        builder.add_index_info('inventories', 100, 50)
+        builder.add_index_info('texts', 150, 350)
+        self.assertAsBytes({('revisions',): ('0 100',),
+                            ('inventories',): ('100 50',),
+                            ('texts',): ('150 350',),
+                           }, builder)
+
+
+class TestTrailingIndex(tests.TestCase):
+
     def assertFromTail(self, start_offset, version, bytes):
-        ti = sack.TrailingIndexBuilder.from_tail(bytes)
-        self.assertIsInstance(ti, sack.TrailingIndexBuilder)
+        ti = sack.TrailingIndex.from_tail(bytes)
+        self.assertIsInstance(ti, sack.TrailingIndex)
         self.assertEqual(start_offset, ti.start_offset)
         self.assertEqual(version, ti.version)
 
-    def test_as_bytes(self):
-        self.assertAsBytes({}, sack.TrailingIndexBuilder(1, 0))
-        self.assertAsBytes({}, sack.TrailingIndexBuilder(1, 12345))
-        self.assertAsBytes({}, sack.TrailingIndexBuilder(3, 2**48-1))
-        self.assertAsBytes({}, sack.TrailingIndexBuilder(3, 2**64-1))
-
     def test_from_tail(self):
         self.assertFromTail(12345, 1,
                             '\x00\x00\x00\x00\x00\x00\x30\x39'



More information about the bazaar-commits mailing list