~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_btree_index.py

  • Committer: Robert Collins
  • Date: 2010-04-08 04:34:03 UTC
  • mfrom: (5138 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5139.
  • Revision ID: robertc@robertcollins.net-20100408043403-56z0d07vdqrx7f3t
Update bugfix for 528114 to trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
 
60
60
    def setUp(self):
61
61
        TestCaseWithTransport.setUp(self)
62
 
        self._original_header = btree_index._RESERVED_HEADER_BYTES
63
 
        def restore():
64
 
            btree_index._RESERVED_HEADER_BYTES = self._original_header
65
 
        self.addCleanup(restore)
66
 
        btree_index._RESERVED_HEADER_BYTES = 100
 
62
        self.overrideAttr(btree_index, '_RESERVED_HEADER_BYTES', 100)
67
63
 
68
64
    def make_nodes(self, count, key_elements, reference_lists):
69
65
        """Generate count*key_elements sample nodes."""
103
99
 
104
100
    def shrink_page_size(self):
105
101
        """Shrink the default page size so that less fits in a page."""
106
 
        old_page_size = btree_index._PAGE_SIZE
107
 
        def cleanup():
108
 
            btree_index._PAGE_SIZE = old_page_size
109
 
        self.addCleanup(cleanup)
 
102
        self.overrideAttr(btree_index, '_PAGE_SIZE')
110
103
        btree_index._PAGE_SIZE = 2048
111
104
 
112
105
 
618
611
        size = trans.put_file('index', stream)
619
612
        return btree_index.BTreeGraphIndex(trans, 'index', size)
620
613
 
 
614
    def make_index_with_offset(self, ref_lists=1, key_elements=1, nodes=[],
 
615
                               offset=0):
 
616
        builder = btree_index.BTreeBuilder(key_elements=key_elements,
 
617
                                           reference_lists=ref_lists)
 
618
        builder.add_nodes(nodes)
 
619
        transport = self.get_transport('')
 
620
        # NamedTemporaryFile dies on builder.finish().read(). weird.
 
621
        temp_file = builder.finish()
 
622
        content = temp_file.read()
 
623
        del temp_file
 
624
        size = len(content)
 
625
        transport.put_bytes('index', (' '*offset)+content)
 
626
        return btree_index.BTreeGraphIndex(transport, 'index', size=size,
 
627
                                           offset=offset)
 
628
 
621
629
    def test_clear_cache(self):
622
630
        nodes = self.make_nodes(160, 2, 2)
623
631
        index = self.make_index(ref_lists=2, key_elements=2, nodes=nodes)
693
701
            transport._activity)
694
702
        self.assertEqual(1173, size)
695
703
 
 
704
    def test_with_offset_no_size(self):
 
705
        index = self.make_index_with_offset(key_elements=1, ref_lists=1,
 
706
                                            offset=1234,
 
707
                                            nodes=self.make_nodes(200, 1, 1))
 
708
        index._size = None # throw away the size info
 
709
        self.assertEqual(200, index.key_count())
 
710
 
 
711
    def test_with_small_offset(self):
 
712
        index = self.make_index_with_offset(key_elements=1, ref_lists=1,
 
713
                                            offset=1234,
 
714
                                            nodes=self.make_nodes(200, 1, 1))
 
715
        self.assertEqual(200, index.key_count())
 
716
 
 
717
    def test_with_large_offset(self):
 
718
        index = self.make_index_with_offset(key_elements=1, ref_lists=1,
 
719
                                            offset=123456,
 
720
                                            nodes=self.make_nodes(200, 1, 1))
 
721
        self.assertEqual(200, index.key_count())
 
722
 
696
723
    def test__read_nodes_no_size_one_page_reads_once(self):
697
724
        self.make_index(nodes=[(('key',), 'value', ())])
698
725
        trans = get_transport('trace+' + self.get_url())
1157
1184
 
1158
1185
class TestBTreeNodes(BTreeTestCase):
1159
1186
 
1160
 
    def restore_parser(self):
1161
 
        btree_index._btree_serializer = self.saved_parser
1162
 
 
1163
1187
    def setUp(self):
1164
1188
        BTreeTestCase.setUp(self)
1165
 
        self.saved_parser = btree_index._btree_serializer
1166
 
        self.addCleanup(self.restore_parser)
1167
 
        btree_index._btree_serializer = self.parse_btree
 
1189
        self.overrideAttr(btree_index, '_btree_serializer', self.parse_btree)
1168
1190
 
1169
1191
    def test_LeafNode_1_0(self):
1170
1192
        node_bytes = ("type=leaf\n"