~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_btree_index.py

  • Committer: John Arbash Meinel
  • Date: 2009-03-06 20:42:40 UTC
  • mto: This revision was merged to the branch mainline in revision 4088.
  • Revision ID: john@arbash-meinel.com-20090306204240-mzjavv31z3gu1x7i
Fix a small bug in setup.py when an extension fails to build

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
#
17
17
 
18
18
"""Tests for btree indices."""
23
23
from bzrlib import (
24
24
    btree_index,
25
25
    errors,
26
 
    fifo_cache,
27
 
    lru_cache,
28
 
    osutils,
29
26
    tests,
30
27
    )
31
28
from bzrlib.tests import (
32
29
    TestCaseWithTransport,
 
30
    TestScenarioApplier,
 
31
    adapt_tests,
33
32
    condition_isinstance,
34
 
    multiply_tests,
35
33
    split_suite_by_condition,
36
34
    )
37
35
from bzrlib.transport import get_transport
41
39
    # parameterise the TestBTreeNodes tests
42
40
    node_tests, others = split_suite_by_condition(standard_tests,
43
41
        condition_isinstance(TestBTreeNodes))
 
42
    applier = TestScenarioApplier()
44
43
    import bzrlib._btree_serializer_py as py_module
45
 
    scenarios = [('python', {'parse_btree': py_module})]
46
 
    if compiled_btreeparser_feature.available():
47
 
        scenarios.append(('C', {'parse_btree':
48
 
                                compiled_btreeparser_feature.module}))
49
 
    return multiply_tests(node_tests, scenarios, others)
50
 
 
51
 
 
52
 
compiled_btreeparser_feature = tests.ModuleAvailableFeature(
53
 
                                'bzrlib._btree_serializer_pyx')
 
44
    applier.scenarios = [('python', {'parse_btree': py_module})]
 
45
    if CompiledBtreeParserFeature.available():
 
46
        # Is there a way to do this that gets missing feature failures rather
 
47
        # than no indication to the user?
 
48
        import bzrlib._btree_serializer_c as c_module
 
49
        applier.scenarios.append(('C', {'parse_btree': c_module}))
 
50
    adapt_tests(node_tests, applier, others)
 
51
    return others
 
52
 
 
53
 
 
54
class _CompiledBtreeParserFeature(tests.Feature):
 
55
    def _probe(self):
 
56
        try:
 
57
            import bzrlib._btree_serializer_c
 
58
        except ImportError:
 
59
            return False
 
60
        return True
 
61
 
 
62
    def feature_name(self):
 
63
        return 'bzrlib._btree_serializer_c'
 
64
 
 
65
CompiledBtreeParserFeature = _CompiledBtreeParserFeature()
54
66
 
55
67
 
56
68
class BTreeTestCase(TestCaseWithTransport):
59
71
 
60
72
    def setUp(self):
61
73
        TestCaseWithTransport.setUp(self)
62
 
        self.overrideAttr(btree_index, '_RESERVED_HEADER_BYTES', 100)
 
74
        self._original_header = btree_index._RESERVED_HEADER_BYTES
 
75
        def restore():
 
76
            btree_index._RESERVED_HEADER_BYTES = self._original_header
 
77
        self.addCleanup(restore)
 
78
        btree_index._RESERVED_HEADER_BYTES = 100
63
79
 
64
80
    def make_nodes(self, count, key_elements, reference_lists):
65
81
        """Generate count*key_elements sample nodes."""
99
115
 
100
116
    def shrink_page_size(self):
101
117
        """Shrink the default page size so that less fits in a page."""
102
 
        self.overrideAttr(btree_index, '_PAGE_SIZE')
 
118
        old_page_size = btree_index._PAGE_SIZE
 
119
        def cleanup():
 
120
            btree_index._PAGE_SIZE = old_page_size
 
121
        self.addCleanup(cleanup)
103
122
        btree_index._PAGE_SIZE = 2048
104
123
 
105
124
 
106
125
class TestBTreeBuilder(BTreeTestCase):
107
126
 
108
 
    def test_clear_cache(self):
109
 
        builder = btree_index.BTreeBuilder(reference_lists=0, key_elements=1)
110
 
        # This is a no-op, but we need the api to be consistent with other
111
 
        # BTreeGraphIndex apis.
112
 
        builder.clear_cache()
113
 
 
114
127
    def test_empty_1_0(self):
115
128
        builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
116
129
        # NamedTemporaryFile dies on builder.finish().read(). weird.
142
155
        temp_file = builder.finish()
143
156
        content = temp_file.read()
144
157
        del temp_file
145
 
        self.assertEqual(131, len(content))
 
158
        self.assertEqual(158, len(content))
146
159
        self.assertEqual(
147
160
            "B+Tree Graph Index 2\nnode_ref_lists=0\nkey_elements=1\nlen=5\n"
148
161
            "row_lengths=1\n",
166
179
        temp_file = builder.finish()
167
180
        content = temp_file.read()
168
181
        del temp_file
169
 
        self.assertEqual(238, len(content))
 
182
        self.assertEqual(264, len(content))
170
183
        self.assertEqual(
171
184
            "B+Tree Graph Index 2\nnode_ref_lists=2\nkey_elements=2\nlen=10\n"
172
185
            "row_lengths=1\n",
232
245
        temp_file = builder.finish()
233
246
        content = temp_file.read()
234
247
        del temp_file
235
 
        self.assertEqual(155, len(content))
 
248
        self.assertEqual(181, len(content))
236
249
        self.assertEqual(
237
250
            "B+Tree Graph Index 2\nnode_ref_lists=0\nkey_elements=1\nlen=10\n"
238
251
            "row_lengths=1\n",
340
353
        # Test the parts of the index that take up memory are doing so
341
354
        # predictably.
342
355
        self.assertEqual(1, len(builder._nodes))
 
356
        self.assertEqual(1, len(builder._keys))
343
357
        self.assertIs(None, builder._nodes_by_key)
344
358
        builder.add_node(*nodes[1])
345
359
        self.assertEqual(0, len(builder._nodes))
 
360
        self.assertEqual(0, len(builder._keys))
346
361
        self.assertIs(None, builder._nodes_by_key)
347
362
        self.assertEqual(1, len(builder._backing_indices))
348
363
        self.assertEqual(2, builder._backing_indices[0].key_count())
349
364
        # now back to memory
350
365
        builder.add_node(*nodes[2])
351
366
        self.assertEqual(1, len(builder._nodes))
 
367
        self.assertEqual(1, len(builder._keys))
352
368
        self.assertIs(None, builder._nodes_by_key)
353
369
        # And spills to a second backing index combing all
354
370
        builder.add_node(*nodes[3])
355
371
        self.assertEqual(0, len(builder._nodes))
 
372
        self.assertEqual(0, len(builder._keys))
356
373
        self.assertIs(None, builder._nodes_by_key)
357
374
        self.assertEqual(2, len(builder._backing_indices))
358
375
        self.assertEqual(None, builder._backing_indices[0])
361
378
        builder.add_node(*nodes[4])
362
379
        builder.add_node(*nodes[5])
363
380
        self.assertEqual(0, len(builder._nodes))
 
381
        self.assertEqual(0, len(builder._keys))
364
382
        self.assertIs(None, builder._nodes_by_key)
365
383
        self.assertEqual(2, len(builder._backing_indices))
366
384
        self.assertEqual(2, builder._backing_indices[0].key_count())
416
434
        self.assertEqual(sorted(nodes), nodes)
417
435
        self.assertEqual(16, len(nodes))
418
436
 
419
 
    def test_spill_index_stress_1_1_no_combine(self):
420
 
        builder = btree_index.BTreeBuilder(key_elements=1, spill_at=2)
421
 
        builder.set_optimize(for_size=False, combine_backing_indices=False)
422
 
        nodes = [node[0:2] for node in self.make_nodes(16, 1, 0)]
423
 
        builder.add_node(*nodes[0])
424
 
        # Test the parts of the index that take up memory are doing so
425
 
        # predictably.
426
 
        self.assertEqual(1, len(builder._nodes))
427
 
        self.assertIs(None, builder._nodes_by_key)
428
 
        builder.add_node(*nodes[1])
429
 
        self.assertEqual(0, len(builder._nodes))
430
 
        self.assertIs(None, builder._nodes_by_key)
431
 
        self.assertEqual(1, len(builder._backing_indices))
432
 
        self.assertEqual(2, builder._backing_indices[0].key_count())
433
 
        # now back to memory
434
 
        builder.add_node(*nodes[2])
435
 
        self.assertEqual(1, len(builder._nodes))
436
 
        self.assertIs(None, builder._nodes_by_key)
437
 
        # And spills to a second backing index but doesn't combine
438
 
        builder.add_node(*nodes[3])
439
 
        self.assertEqual(0, len(builder._nodes))
440
 
        self.assertIs(None, builder._nodes_by_key)
441
 
        self.assertEqual(2, len(builder._backing_indices))
442
 
        for backing_index in builder._backing_indices:
443
 
            self.assertEqual(2, backing_index.key_count())
444
 
        # The next spills to the 3rd slot
445
 
        builder.add_node(*nodes[4])
446
 
        builder.add_node(*nodes[5])
447
 
        self.assertEqual(0, len(builder._nodes))
448
 
        self.assertIs(None, builder._nodes_by_key)
449
 
        self.assertEqual(3, len(builder._backing_indices))
450
 
        for backing_index in builder._backing_indices:
451
 
            self.assertEqual(2, backing_index.key_count())
452
 
        # Now spill a few more, and check that we don't combine
453
 
        builder.add_node(*nodes[6])
454
 
        builder.add_node(*nodes[7])
455
 
        builder.add_node(*nodes[8])
456
 
        builder.add_node(*nodes[9])
457
 
        builder.add_node(*nodes[10])
458
 
        builder.add_node(*nodes[11])
459
 
        builder.add_node(*nodes[12])
460
 
        self.assertEqual(6, len(builder._backing_indices))
461
 
        for backing_index in builder._backing_indices:
462
 
            self.assertEqual(2, backing_index.key_count())
463
 
        # Test that memory and disk are both used for query methods; and that
464
 
        # None is skipped over happily.
465
 
        self.assertEqual([(builder,) + node for node in sorted(nodes[:13])],
466
 
            list(builder.iter_all_entries()))
467
 
        # Two nodes - one memory one disk
468
 
        self.assertEqual(set([(builder,) + node for node in nodes[11:13]]),
469
 
            set(builder.iter_entries([nodes[12][0], nodes[11][0]])))
470
 
        self.assertEqual(13, builder.key_count())
471
 
        self.assertEqual(set([(builder,) + node for node in nodes[11:13]]),
472
 
            set(builder.iter_entries_prefix([nodes[12][0], nodes[11][0]])))
473
 
        builder.add_node(*nodes[13])
474
 
        builder.add_node(*nodes[14])
475
 
        builder.add_node(*nodes[15])
476
 
        self.assertEqual(8, len(builder._backing_indices))
477
 
        for backing_index in builder._backing_indices:
478
 
            self.assertEqual(2, backing_index.key_count())
479
 
        # Now finish, and check we got a correctly ordered tree
480
 
        transport = self.get_transport('')
481
 
        size = transport.put_file('index', builder.finish())
482
 
        index = btree_index.BTreeGraphIndex(transport, 'index', size)
483
 
        nodes = list(index.iter_all_entries())
484
 
        self.assertEqual(sorted(nodes), nodes)
485
 
        self.assertEqual(16, len(nodes))
486
 
 
487
437
    def test_set_optimize(self):
488
438
        builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
489
439
        builder.set_optimize(for_size=True)
490
440
        self.assertTrue(builder._optimize_for_size)
491
441
        builder.set_optimize(for_size=False)
492
442
        self.assertFalse(builder._optimize_for_size)
493
 
        # test that we can set combine_backing_indices without effecting
494
 
        # _optimize_for_size
495
 
        obj = object()
496
 
        builder._optimize_for_size = obj
497
 
        builder.set_optimize(combine_backing_indices=False)
498
 
        self.assertFalse(builder._combine_backing_indices)
499
 
        self.assertIs(obj, builder._optimize_for_size)
500
 
        builder.set_optimize(combine_backing_indices=True)
501
 
        self.assertTrue(builder._combine_backing_indices)
502
 
        self.assertIs(obj, builder._optimize_for_size)
503
443
 
504
444
    def test_spill_index_stress_2_2(self):
505
445
        # test that references and longer keys don't confuse things.
509
449
        builder.add_node(*nodes[0])
510
450
        # Test the parts of the index that take up memory are doing so
511
451
        # predictably.
 
452
        self.assertEqual(1, len(builder._keys))
512
453
        self.assertEqual(1, len(builder._nodes))
513
454
        self.assertIs(None, builder._nodes_by_key)
514
455
        builder.add_node(*nodes[1])
 
456
        self.assertEqual(0, len(builder._keys))
515
457
        self.assertEqual(0, len(builder._nodes))
516
458
        self.assertIs(None, builder._nodes_by_key)
517
459
        self.assertEqual(1, len(builder._backing_indices))
520
462
        old = dict(builder._get_nodes_by_key()) #Build up the nodes by key dict
521
463
        builder.add_node(*nodes[2])
522
464
        self.assertEqual(1, len(builder._nodes))
 
465
        self.assertEqual(1, len(builder._keys))
523
466
        self.assertIsNot(None, builder._nodes_by_key)
524
467
        self.assertNotEqual({}, builder._nodes_by_key)
525
468
        # We should have a new entry
527
470
        # And spills to a second backing index combing all
528
471
        builder.add_node(*nodes[3])
529
472
        self.assertEqual(0, len(builder._nodes))
 
473
        self.assertEqual(0, len(builder._keys))
530
474
        self.assertIs(None, builder._nodes_by_key)
531
475
        self.assertEqual(2, len(builder._backing_indices))
532
476
        self.assertEqual(None, builder._backing_indices[0])
535
479
        builder.add_node(*nodes[4])
536
480
        builder.add_node(*nodes[5])
537
481
        self.assertEqual(0, len(builder._nodes))
 
482
        self.assertEqual(0, len(builder._keys))
538
483
        self.assertIs(None, builder._nodes_by_key)
539
484
        self.assertEqual(2, len(builder._backing_indices))
540
485
        self.assertEqual(2, builder._backing_indices[0].key_count())
611
556
        size = trans.put_file('index', stream)
612
557
        return btree_index.BTreeGraphIndex(trans, 'index', size)
613
558
 
614
 
    def test_clear_cache(self):
615
 
        nodes = self.make_nodes(160, 2, 2)
616
 
        index = self.make_index(ref_lists=2, key_elements=2, nodes=nodes)
617
 
        self.assertEqual(1, len(list(index.iter_entries([nodes[30][0]]))))
618
 
        self.assertEqual([1, 4], index._row_lengths)
619
 
        self.assertIsNot(None, index._root_node)
620
 
        internal_node_pre_clear = index._internal_node_cache.keys()
621
 
        self.assertTrue(len(index._leaf_node_cache) > 0)
622
 
        index.clear_cache()
623
 
        # We don't touch _root_node or _internal_node_cache, both should be
624
 
        # small, and can save a round trip or two
625
 
        self.assertIsNot(None, index._root_node)
626
 
        # NOTE: We don't want to affect the _internal_node_cache, as we expect
627
 
        #       it will be small, and if we ever do touch this index again, it
628
 
        #       will save round-trips.  This assertion isn't very strong,
629
 
        #       becuase without a 3-level index, we don't have any internal
630
 
        #       nodes cached.
631
 
        self.assertEqual(internal_node_pre_clear,
632
 
                         index._internal_node_cache.keys())
633
 
        self.assertEqual(0, len(index._leaf_node_cache))
634
 
 
635
559
    def test_trivial_constructor(self):
636
560
        transport = get_transport('trace+' + self.get_url(''))
637
561
        index = btree_index.BTreeGraphIndex(transport, 'index', None)
684
608
        # The entire index should have been read, as it is one page long.
685
609
        self.assertEqual([('readv', 'index', [(0, size)], False, None)],
686
610
            transport._activity)
687
 
        self.assertEqual(1173, size)
 
611
        self.assertEqual(1199, size)
688
612
 
689
613
    def test__read_nodes_no_size_one_page_reads_once(self):
690
614
        self.make_index(nodes=[(('key',), 'value', ())])
738
662
        # The entire index should have been read linearly.
739
663
        self.assertEqual([('readv', 'index', [(0, size)], False, None)],
740
664
            transport._activity)
741
 
        self.assertEqual(1488, size)
 
665
        self.assertEqual(1514, size)
742
666
 
743
667
    def test_validate_two_pages(self):
744
668
        builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
976
900
            ])
977
901
        self.assertEqual(set([]), index.external_references(0))
978
902
 
979
 
    def test__find_ancestors_one_page(self):
980
 
        key1 = ('key-1',)
981
 
        key2 = ('key-2',)
982
 
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
983
 
            (key1, 'value', ([key2],)),
984
 
            (key2, 'value', ([],)),
985
 
            ])
986
 
        parent_map = {}
987
 
        missing_keys = set()
988
 
        search_keys = index._find_ancestors([key1], 0, parent_map, missing_keys)
989
 
        self.assertEqual({key1: (key2,), key2: ()}, parent_map)
990
 
        self.assertEqual(set(), missing_keys)
991
 
        self.assertEqual(set(), search_keys)
992
 
 
993
 
    def test__find_ancestors_one_page_w_missing(self):
994
 
        key1 = ('key-1',)
995
 
        key2 = ('key-2',)
996
 
        key3 = ('key-3',)
997
 
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
998
 
            (key1, 'value', ([key2],)),
999
 
            (key2, 'value', ([],)),
1000
 
            ])
1001
 
        parent_map = {}
1002
 
        missing_keys = set()
1003
 
        search_keys = index._find_ancestors([key2, key3], 0, parent_map,
1004
 
                                            missing_keys)
1005
 
        self.assertEqual({key2: ()}, parent_map)
1006
 
        # we know that key3 is missing because we read the page that it would
1007
 
        # otherwise be on
1008
 
        self.assertEqual(set([key3]), missing_keys)
1009
 
        self.assertEqual(set(), search_keys)
1010
 
 
1011
 
    def test__find_ancestors_one_parent_missing(self):
1012
 
        key1 = ('key-1',)
1013
 
        key2 = ('key-2',)
1014
 
        key3 = ('key-3',)
1015
 
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1016
 
            (key1, 'value', ([key2],)),
1017
 
            (key2, 'value', ([key3],)),
1018
 
            ])
1019
 
        parent_map = {}
1020
 
        missing_keys = set()
1021
 
        search_keys = index._find_ancestors([key1], 0, parent_map,
1022
 
                                            missing_keys)
1023
 
        self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1024
 
        self.assertEqual(set(), missing_keys)
1025
 
        # all we know is that key3 wasn't present on the page we were reading
1026
 
        # but if you look, the last key is key2 which comes before key3, so we
1027
 
        # don't know whether key3 would land on this page or not.
1028
 
        self.assertEqual(set([key3]), search_keys)
1029
 
        search_keys = index._find_ancestors(search_keys, 0, parent_map,
1030
 
                                            missing_keys)
1031
 
        # passing it back in, we are sure it is 'missing'
1032
 
        self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1033
 
        self.assertEqual(set([key3]), missing_keys)
1034
 
        self.assertEqual(set([]), search_keys)
1035
 
 
1036
 
    def test__find_ancestors_dont_search_known(self):
1037
 
        key1 = ('key-1',)
1038
 
        key2 = ('key-2',)
1039
 
        key3 = ('key-3',)
1040
 
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1041
 
            (key1, 'value', ([key2],)),
1042
 
            (key2, 'value', ([key3],)),
1043
 
            (key3, 'value', ([],)),
1044
 
            ])
1045
 
        # We already know about key2, so we won't try to search for key3
1046
 
        parent_map = {key2: (key3,)}
1047
 
        missing_keys = set()
1048
 
        search_keys = index._find_ancestors([key1], 0, parent_map,
1049
 
                                            missing_keys)
1050
 
        self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1051
 
        self.assertEqual(set(), missing_keys)
1052
 
        self.assertEqual(set(), search_keys)
1053
 
 
1054
 
    def test__find_ancestors_multiple_pages(self):
1055
 
        # We need to use enough keys that we actually cause a split
1056
 
        start_time = 1249671539
1057
 
        email = "joebob@example.com"
1058
 
        nodes = []
1059
 
        ref_lists = ((),)
1060
 
        rev_keys = []
1061
 
        for i in xrange(400):
1062
 
            rev_id = '%s-%s-%s' % (email,
1063
 
                                   osutils.compact_date(start_time + i),
1064
 
                                   osutils.rand_chars(16))
1065
 
            rev_key = (rev_id,)
1066
 
            nodes.append((rev_key, 'value', ref_lists))
1067
 
            # We have a ref 'list' of length 1, with a list of parents, with 1
1068
 
            # parent which is a key
1069
 
            ref_lists = ((rev_key,),)
1070
 
            rev_keys.append(rev_key)
1071
 
        index = self.make_index(ref_lists=1, key_elements=1, nodes=nodes)
1072
 
        self.assertEqual(400, index.key_count())
1073
 
        self.assertEqual(3, len(index._row_offsets))
1074
 
        nodes = dict(index._read_nodes([1, 2]))
1075
 
        l1 = nodes[1]
1076
 
        l2 = nodes[2]
1077
 
        min_l2_key = l2.min_key
1078
 
        max_l1_key = l1.max_key
1079
 
        self.assertTrue(max_l1_key < min_l2_key)
1080
 
        parents_min_l2_key = l2.keys[min_l2_key][1][0]
1081
 
        self.assertEqual((l1.max_key,), parents_min_l2_key)
1082
 
        # Now, whatever key we select that would fall on the second page,
1083
 
        # should give us all the parents until the page break
1084
 
        key_idx = rev_keys.index(min_l2_key)
1085
 
        next_key = rev_keys[key_idx+1]
1086
 
        # So now when we get the parent map, we should get the key we are
1087
 
        # looking for, min_l2_key, and then a reference to go look for the
1088
 
        # parent of that key
1089
 
        parent_map = {}
1090
 
        missing_keys = set()
1091
 
        search_keys = index._find_ancestors([next_key], 0, parent_map,
1092
 
                                            missing_keys)
1093
 
        self.assertEqual([min_l2_key, next_key], sorted(parent_map))
1094
 
        self.assertEqual(set(), missing_keys)
1095
 
        self.assertEqual(set([max_l1_key]), search_keys)
1096
 
        parent_map = {}
1097
 
        search_keys = index._find_ancestors([max_l1_key], 0, parent_map,
1098
 
                                            missing_keys)
1099
 
        self.assertEqual(sorted(l1.keys), sorted(parent_map))
1100
 
        self.assertEqual(set(), missing_keys)
1101
 
        self.assertEqual(set(), search_keys)
1102
 
 
1103
 
    def test__find_ancestors_empty_index(self):
1104
 
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[])
1105
 
        parent_map = {}
1106
 
        missing_keys = set()
1107
 
        search_keys = index._find_ancestors([('one',), ('two',)], 0, parent_map,
1108
 
                                            missing_keys)
1109
 
        self.assertEqual(set(), search_keys)
1110
 
        self.assertEqual({}, parent_map)
1111
 
        self.assertEqual(set([('one',), ('two',)]), missing_keys)
1112
 
 
1113
 
    def test_supports_unlimited_cache(self):
1114
 
        builder = btree_index.BTreeBuilder(reference_lists=0, key_elements=1)
1115
 
        # We need enough nodes to cause a page split (so we have both an
1116
 
        # internal node and a couple leaf nodes. 500 seems to be enough.)
1117
 
        nodes = self.make_nodes(500, 1, 0)
1118
 
        for node in nodes:
1119
 
            builder.add_node(*node)
1120
 
        stream = builder.finish()
1121
 
        trans = get_transport(self.get_url())
1122
 
        size = trans.put_file('index', stream)
1123
 
        index = btree_index.BTreeGraphIndex(trans, 'index', size)
1124
 
        self.assertEqual(500, index.key_count())
1125
 
        # We have an internal node
1126
 
        self.assertEqual(2, len(index._row_lengths))
1127
 
        # We have at least 2 leaf nodes
1128
 
        self.assertTrue(index._row_lengths[-1] >= 2)
1129
 
        self.assertIsInstance(index._leaf_node_cache, lru_cache.LRUCache)
1130
 
        self.assertEqual(btree_index._NODE_CACHE_SIZE,
1131
 
                         index._leaf_node_cache._max_cache)
1132
 
        self.assertIsInstance(index._internal_node_cache, fifo_cache.FIFOCache)
1133
 
        self.assertEqual(100, index._internal_node_cache._max_cache)
1134
 
        # No change if unlimited_cache=False is passed
1135
 
        index = btree_index.BTreeGraphIndex(trans, 'index', size,
1136
 
                                            unlimited_cache=False)
1137
 
        self.assertIsInstance(index._leaf_node_cache, lru_cache.LRUCache)
1138
 
        self.assertEqual(btree_index._NODE_CACHE_SIZE,
1139
 
                         index._leaf_node_cache._max_cache)
1140
 
        self.assertIsInstance(index._internal_node_cache, fifo_cache.FIFOCache)
1141
 
        self.assertEqual(100, index._internal_node_cache._max_cache)
1142
 
        index = btree_index.BTreeGraphIndex(trans, 'index', size,
1143
 
                                            unlimited_cache=True)
1144
 
        self.assertIsInstance(index._leaf_node_cache, dict)
1145
 
        self.assertIs(type(index._internal_node_cache), dict)
1146
 
        # Exercise the lookup code
1147
 
        entries = set(index.iter_entries([n[0] for n in nodes]))
1148
 
        self.assertEqual(500, len(entries))
1149
 
 
1150
903
 
1151
904
class TestBTreeNodes(BTreeTestCase):
1152
905
 
 
906
    def restore_parser(self):
 
907
        btree_index._btree_serializer = self.saved_parser
 
908
 
1153
909
    def setUp(self):
1154
910
        BTreeTestCase.setUp(self)
1155
 
        self.overrideAttr(btree_index, '_btree_serializer', self.parse_btree)
 
911
        self.saved_parser = btree_index._btree_serializer
 
912
        self.addCleanup(self.restore_parser)
 
913
        btree_index._btree_serializer = self.parse_btree
1156
914
 
1157
915
    def test_LeafNode_1_0(self):
1158
916
        node_bytes = ("type=leaf\n"
1269
1027
    def test_exists(self):
1270
1028
        # This is just to let the user know if they don't have the feature
1271
1029
        # available
1272
 
        self.requireFeature(compiled_btreeparser_feature)
 
1030
        self.requireFeature(CompiledBtreeParserFeature)
1273
1031
 
1274
1032
 
1275
1033
class TestMultiBisectRight(tests.TestCase):