~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_btree_index.py

  • Committer: Vincent Ladeuil
  • Date: 2010-02-10 15:46:03 UTC
  • mfrom: (4985.3.21 update)
  • mto: This revision was merged to the branch mainline in revision 5021.
  • Revision ID: v.ladeuil+lp@free.fr-20100210154603-k4no1gvfuqpzrw7p
Update performs two merges in a more logical order but stop on conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008, 2009 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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,
26
29
    tests,
27
30
    )
28
31
from bzrlib.tests import (
29
32
    TestCaseWithTransport,
30
 
    TestScenarioApplier,
31
 
    adapt_tests,
32
33
    condition_isinstance,
 
34
    multiply_tests,
33
35
    split_suite_by_condition,
34
36
    )
35
37
from bzrlib.transport import get_transport
39
41
    # parameterise the TestBTreeNodes tests
40
42
    node_tests, others = split_suite_by_condition(standard_tests,
41
43
        condition_isinstance(TestBTreeNodes))
42
 
    applier = TestScenarioApplier()
43
44
    import bzrlib._btree_serializer_py as py_module
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()
 
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')
66
54
 
67
55
 
68
56
class BTreeTestCase(TestCaseWithTransport):
71
59
 
72
60
    def setUp(self):
73
61
        TestCaseWithTransport.setUp(self)
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
 
62
        self.overrideAttr(btree_index, '_RESERVED_HEADER_BYTES', 100)
79
63
 
80
64
    def make_nodes(self, count, key_elements, reference_lists):
81
65
        """Generate count*key_elements sample nodes."""
115
99
 
116
100
    def shrink_page_size(self):
117
101
        """Shrink the default page size so that less fits in a page."""
118
 
        old_page_size = btree_index._PAGE_SIZE
119
 
        def cleanup():
120
 
            btree_index._PAGE_SIZE = old_page_size
121
 
        self.addCleanup(cleanup)
 
102
        self.overrideAttr(btree_index, '_PAGE_SIZE')
122
103
        btree_index._PAGE_SIZE = 2048
123
104
 
124
105
 
125
106
class TestBTreeBuilder(BTreeTestCase):
126
107
 
 
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
 
127
114
    def test_empty_1_0(self):
128
115
        builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
129
116
        # NamedTemporaryFile dies on builder.finish().read(). weird.
155
142
        temp_file = builder.finish()
156
143
        content = temp_file.read()
157
144
        del temp_file
158
 
        self.assertEqual(158, len(content))
 
145
        self.assertEqual(131, len(content))
159
146
        self.assertEqual(
160
147
            "B+Tree Graph Index 2\nnode_ref_lists=0\nkey_elements=1\nlen=5\n"
161
148
            "row_lengths=1\n",
179
166
        temp_file = builder.finish()
180
167
        content = temp_file.read()
181
168
        del temp_file
182
 
        self.assertEqual(264, len(content))
 
169
        self.assertEqual(238, len(content))
183
170
        self.assertEqual(
184
171
            "B+Tree Graph Index 2\nnode_ref_lists=2\nkey_elements=2\nlen=10\n"
185
172
            "row_lengths=1\n",
245
232
        temp_file = builder.finish()
246
233
        content = temp_file.read()
247
234
        del temp_file
248
 
        self.assertEqual(181, len(content))
 
235
        self.assertEqual(155, len(content))
249
236
        self.assertEqual(
250
237
            "B+Tree Graph Index 2\nnode_ref_lists=0\nkey_elements=1\nlen=10\n"
251
238
            "row_lengths=1\n",
353
340
        # Test the parts of the index that take up memory are doing so
354
341
        # predictably.
355
342
        self.assertEqual(1, len(builder._nodes))
356
 
        self.assertEqual(1, len(builder._keys))
357
343
        self.assertIs(None, builder._nodes_by_key)
358
344
        builder.add_node(*nodes[1])
359
345
        self.assertEqual(0, len(builder._nodes))
360
 
        self.assertEqual(0, len(builder._keys))
361
346
        self.assertIs(None, builder._nodes_by_key)
362
347
        self.assertEqual(1, len(builder._backing_indices))
363
348
        self.assertEqual(2, builder._backing_indices[0].key_count())
364
349
        # now back to memory
365
350
        builder.add_node(*nodes[2])
366
351
        self.assertEqual(1, len(builder._nodes))
367
 
        self.assertEqual(1, len(builder._keys))
368
352
        self.assertIs(None, builder._nodes_by_key)
369
353
        # And spills to a second backing index combing all
370
354
        builder.add_node(*nodes[3])
371
355
        self.assertEqual(0, len(builder._nodes))
372
 
        self.assertEqual(0, len(builder._keys))
373
356
        self.assertIs(None, builder._nodes_by_key)
374
357
        self.assertEqual(2, len(builder._backing_indices))
375
358
        self.assertEqual(None, builder._backing_indices[0])
378
361
        builder.add_node(*nodes[4])
379
362
        builder.add_node(*nodes[5])
380
363
        self.assertEqual(0, len(builder._nodes))
381
 
        self.assertEqual(0, len(builder._keys))
382
364
        self.assertIs(None, builder._nodes_by_key)
383
365
        self.assertEqual(2, len(builder._backing_indices))
384
366
        self.assertEqual(2, builder._backing_indices[0].key_count())
434
416
        self.assertEqual(sorted(nodes), nodes)
435
417
        self.assertEqual(16, len(nodes))
436
418
 
 
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
 
437
487
    def test_set_optimize(self):
438
488
        builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
439
489
        builder.set_optimize(for_size=True)
440
490
        self.assertTrue(builder._optimize_for_size)
441
491
        builder.set_optimize(for_size=False)
442
492
        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)
443
503
 
444
504
    def test_spill_index_stress_2_2(self):
445
505
        # test that references and longer keys don't confuse things.
449
509
        builder.add_node(*nodes[0])
450
510
        # Test the parts of the index that take up memory are doing so
451
511
        # predictably.
452
 
        self.assertEqual(1, len(builder._keys))
453
512
        self.assertEqual(1, len(builder._nodes))
454
513
        self.assertIs(None, builder._nodes_by_key)
455
514
        builder.add_node(*nodes[1])
456
 
        self.assertEqual(0, len(builder._keys))
457
515
        self.assertEqual(0, len(builder._nodes))
458
516
        self.assertIs(None, builder._nodes_by_key)
459
517
        self.assertEqual(1, len(builder._backing_indices))
462
520
        old = dict(builder._get_nodes_by_key()) #Build up the nodes by key dict
463
521
        builder.add_node(*nodes[2])
464
522
        self.assertEqual(1, len(builder._nodes))
465
 
        self.assertEqual(1, len(builder._keys))
466
523
        self.assertIsNot(None, builder._nodes_by_key)
467
524
        self.assertNotEqual({}, builder._nodes_by_key)
468
525
        # We should have a new entry
470
527
        # And spills to a second backing index combing all
471
528
        builder.add_node(*nodes[3])
472
529
        self.assertEqual(0, len(builder._nodes))
473
 
        self.assertEqual(0, len(builder._keys))
474
530
        self.assertIs(None, builder._nodes_by_key)
475
531
        self.assertEqual(2, len(builder._backing_indices))
476
532
        self.assertEqual(None, builder._backing_indices[0])
479
535
        builder.add_node(*nodes[4])
480
536
        builder.add_node(*nodes[5])
481
537
        self.assertEqual(0, len(builder._nodes))
482
 
        self.assertEqual(0, len(builder._keys))
483
538
        self.assertIs(None, builder._nodes_by_key)
484
539
        self.assertEqual(2, len(builder._backing_indices))
485
540
        self.assertEqual(2, builder._backing_indices[0].key_count())
556
611
        size = trans.put_file('index', stream)
557
612
        return btree_index.BTreeGraphIndex(trans, 'index', size)
558
613
 
 
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
 
559
635
    def test_trivial_constructor(self):
560
636
        transport = get_transport('trace+' + self.get_url(''))
561
637
        index = btree_index.BTreeGraphIndex(transport, 'index', None)
608
684
        # The entire index should have been read, as it is one page long.
609
685
        self.assertEqual([('readv', 'index', [(0, size)], False, None)],
610
686
            transport._activity)
611
 
        self.assertEqual(1199, size)
 
687
        self.assertEqual(1173, size)
612
688
 
613
689
    def test__read_nodes_no_size_one_page_reads_once(self):
614
690
        self.make_index(nodes=[(('key',), 'value', ())])
662
738
        # The entire index should have been read linearly.
663
739
        self.assertEqual([('readv', 'index', [(0, size)], False, None)],
664
740
            transport._activity)
665
 
        self.assertEqual(1514, size)
 
741
        self.assertEqual(1488, size)
666
742
 
667
743
    def test_validate_two_pages(self):
668
744
        builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
869
945
            (index, ('name', 'fin2'), 'beta', ((), ))]),
870
946
            set(index.iter_entries_prefix([('name', None)])))
871
947
 
 
948
    # XXX: external_references tests are duplicated in test_index.  We
 
949
    # probably should have per_graph_index tests...
 
950
    def test_external_references_no_refs(self):
 
951
        index = self.make_index(ref_lists=0, nodes=[])
 
952
        self.assertRaises(ValueError, index.external_references, 0)
 
953
 
 
954
    def test_external_references_no_results(self):
 
955
        index = self.make_index(ref_lists=1, nodes=[
 
956
            (('key',), 'value', ([],))])
 
957
        self.assertEqual(set(), index.external_references(0))
 
958
 
 
959
    def test_external_references_missing_ref(self):
 
960
        missing_key = ('missing',)
 
961
        index = self.make_index(ref_lists=1, nodes=[
 
962
            (('key',), 'value', ([missing_key],))])
 
963
        self.assertEqual(set([missing_key]), index.external_references(0))
 
964
 
 
965
    def test_external_references_multiple_ref_lists(self):
 
966
        missing_key = ('missing',)
 
967
        index = self.make_index(ref_lists=2, nodes=[
 
968
            (('key',), 'value', ([], [missing_key]))])
 
969
        self.assertEqual(set([]), index.external_references(0))
 
970
        self.assertEqual(set([missing_key]), index.external_references(1))
 
971
 
 
972
    def test_external_references_two_records(self):
 
973
        index = self.make_index(ref_lists=1, nodes=[
 
974
            (('key-1',), 'value', ([('key-2',)],)),
 
975
            (('key-2',), 'value', ([],)),
 
976
            ])
 
977
        self.assertEqual(set([]), index.external_references(0))
 
978
 
 
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
 
872
1150
 
873
1151
class TestBTreeNodes(BTreeTestCase):
874
1152
 
875
 
    def restore_parser(self):
876
 
        btree_index._btree_serializer = self.saved_parser
877
 
 
878
1153
    def setUp(self):
879
1154
        BTreeTestCase.setUp(self)
880
 
        self.saved_parser = btree_index._btree_serializer
881
 
        self.addCleanup(self.restore_parser)
882
 
        btree_index._btree_serializer = self.parse_btree
 
1155
        self.overrideAttr(btree_index, '_btree_serializer', self.parse_btree)
883
1156
 
884
1157
    def test_LeafNode_1_0(self):
885
1158
        node_bytes = ("type=leaf\n"
996
1269
    def test_exists(self):
997
1270
        # This is just to let the user know if they don't have the feature
998
1271
        # available
999
 
        self.requireFeature(CompiledBtreeParserFeature)
 
1272
        self.requireFeature(compiled_btreeparser_feature)
1000
1273
 
1001
1274
 
1002
1275
class TestMultiBisectRight(tests.TestCase):