~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_btree_index.py

  • Committer: Andrew Bennetts
  • Date: 2010-01-18 07:00:11 UTC
  • mto: (4973.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4975.
  • Revision ID: andrew.bennetts@canonical.com-20100118070011-zu374wvd0lcgai5a
Move news_merge plugin from contrib to bzrlib/plugins, change it to be enabled via a 'news_merge_files' config option, move more code out of the __init__ to minimise overhead, and add lots of docstrings, add NEWS entry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 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
27
27
    lru_cache,
28
28
    osutils,
29
29
    tests,
30
 
    transport,
31
30
    )
32
31
from bzrlib.tests import (
33
32
    TestCaseWithTransport,
35
34
    multiply_tests,
36
35
    split_suite_by_condition,
37
36
    )
 
37
from bzrlib.transport import get_transport
38
38
 
39
39
 
40
40
def load_tests(standard_tests, module, loader):
59
59
 
60
60
    def setUp(self):
61
61
        TestCaseWithTransport.setUp(self)
62
 
        self.overrideAttr(btree_index, '_RESERVED_HEADER_BYTES', 100)
 
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
63
67
 
64
68
    def make_nodes(self, count, key_elements, reference_lists):
65
69
        """Generate count*key_elements sample nodes."""
99
103
 
100
104
    def shrink_page_size(self):
101
105
        """Shrink the default page size so that less fits in a page."""
102
 
        self.overrideAttr(btree_index, '_PAGE_SIZE')
 
106
        old_page_size = btree_index._PAGE_SIZE
 
107
        def cleanup():
 
108
            btree_index._PAGE_SIZE = old_page_size
 
109
        self.addCleanup(cleanup)
103
110
        btree_index._PAGE_SIZE = 2048
104
111
 
105
112
 
216
223
        leaf1_bytes = zlib.decompress(leaf1)
217
224
        sorted_node_keys = sorted(node[0] for node in nodes)
218
225
        node = btree_index._LeafNode(leaf1_bytes, 1, 0)
219
 
        self.assertEqual(231, len(node))
220
 
        self.assertEqual(sorted_node_keys[:231], node.all_keys())
 
226
        self.assertEqual(231, len(node.keys))
 
227
        self.assertEqual(sorted_node_keys[:231], sorted(node.keys))
221
228
        leaf2_bytes = zlib.decompress(leaf2)
222
229
        node = btree_index._LeafNode(leaf2_bytes, 1, 0)
223
 
        self.assertEqual(400 - 231, len(node))
224
 
        self.assertEqual(sorted_node_keys[231:], node.all_keys())
 
230
        self.assertEqual(400 - 231, len(node.keys))
 
231
        self.assertEqual(sorted_node_keys[231:], sorted(node.keys))
225
232
 
226
233
    def test_last_page_rounded_1_layer(self):
227
234
        builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
241
248
        leaf2 = content[74:]
242
249
        leaf2_bytes = zlib.decompress(leaf2)
243
250
        node = btree_index._LeafNode(leaf2_bytes, 1, 0)
244
 
        self.assertEqual(10, len(node))
 
251
        self.assertEqual(10, len(node.keys))
245
252
        sorted_node_keys = sorted(node[0] for node in nodes)
246
 
        self.assertEqual(sorted_node_keys, node.all_keys())
 
253
        self.assertEqual(sorted_node_keys, sorted(node.keys))
247
254
 
248
255
    def test_last_page_not_rounded_2_layer(self):
249
256
        builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
263
270
        leaf2 = content[8192:]
264
271
        leaf2_bytes = zlib.decompress(leaf2)
265
272
        node = btree_index._LeafNode(leaf2_bytes, 1, 0)
266
 
        self.assertEqual(400 - 231, len(node))
 
273
        self.assertEqual(400 - 231, len(node.keys))
267
274
        sorted_node_keys = sorted(node[0] for node in nodes)
268
 
        self.assertEqual(sorted_node_keys[231:], node.all_keys())
 
275
        self.assertEqual(sorted_node_keys[231:], sorted(node.keys))
269
276
 
270
277
    def test_three_level_tree_details(self):
271
278
        # The left most pointer in the second internal node in a row should
280
287
 
281
288
        for node in nodes:
282
289
            builder.add_node(*node)
283
 
        t = transport.get_transport('trace+' + self.get_url(''))
284
 
        size = t.put_file('index', self.time(builder.finish))
 
290
        transport = get_transport('trace+' + self.get_url(''))
 
291
        size = transport.put_file('index', self.time(builder.finish))
285
292
        del builder
286
 
        index = btree_index.BTreeGraphIndex(t, 'index', size)
 
293
        index = btree_index.BTreeGraphIndex(transport, 'index', size)
287
294
        # Seed the metadata, we're using internal calls now.
288
295
        index.key_count()
289
296
        self.assertEqual(3, len(index._row_lengths),
302
309
        # in the second node it points at
303
310
        pos = index._row_offsets[2] + internal_node2.offset + 1
304
311
        leaf = index._get_leaf_nodes([pos])[pos]
305
 
        self.assertTrue(internal_node2.keys[0] in leaf)
 
312
        self.assertTrue(internal_node2.keys[0] in leaf.keys)
306
313
 
307
314
    def test_2_leaves_2_2(self):
308
315
        builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
409
416
        self.assertEqual(None, builder._backing_indices[2])
410
417
        self.assertEqual(16, builder._backing_indices[3].key_count())
411
418
        # Now finish, and check we got a correctly ordered tree
412
 
        t = self.get_transport('')
413
 
        size = t.put_file('index', builder.finish())
414
 
        index = btree_index.BTreeGraphIndex(t, 'index', size)
 
419
        transport = self.get_transport('')
 
420
        size = transport.put_file('index', builder.finish())
 
421
        index = btree_index.BTreeGraphIndex(transport, 'index', size)
415
422
        nodes = list(index.iter_all_entries())
416
423
        self.assertEqual(sorted(nodes), nodes)
417
424
        self.assertEqual(16, len(nodes))
607
614
        for key, value, references in nodes:
608
615
            builder.add_node(key, value, references)
609
616
        stream = builder.finish()
610
 
        trans = transport.get_transport('trace+' + self.get_url())
 
617
        trans = get_transport('trace+' + self.get_url())
611
618
        size = trans.put_file('index', stream)
612
619
        return btree_index.BTreeGraphIndex(trans, 'index', size)
613
620
 
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
 
 
629
621
    def test_clear_cache(self):
630
622
        nodes = self.make_nodes(160, 2, 2)
631
623
        index = self.make_index(ref_lists=2, key_elements=2, nodes=nodes)
648
640
        self.assertEqual(0, len(index._leaf_node_cache))
649
641
 
650
642
    def test_trivial_constructor(self):
651
 
        t = transport.get_transport('trace+' + self.get_url(''))
652
 
        index = btree_index.BTreeGraphIndex(t, 'index', None)
 
643
        transport = get_transport('trace+' + self.get_url(''))
 
644
        index = btree_index.BTreeGraphIndex(transport, 'index', None)
653
645
        # Checks the page size at load, but that isn't logged yet.
654
 
        self.assertEqual([], t._activity)
 
646
        self.assertEqual([], transport._activity)
655
647
 
656
648
    def test_with_size_constructor(self):
657
 
        t = transport.get_transport('trace+' + self.get_url(''))
658
 
        index = btree_index.BTreeGraphIndex(t, 'index', 1)
 
649
        transport = get_transport('trace+' + self.get_url(''))
 
650
        index = btree_index.BTreeGraphIndex(transport, 'index', 1)
659
651
        # Checks the page size at load, but that isn't logged yet.
660
 
        self.assertEqual([], t._activity)
 
652
        self.assertEqual([], transport._activity)
661
653
 
662
654
    def test_empty_key_count_no_size(self):
663
655
        builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
664
 
        t = transport.get_transport('trace+' + self.get_url(''))
665
 
        t.put_file('index', builder.finish())
666
 
        index = btree_index.BTreeGraphIndex(t, 'index', None)
667
 
        del t._activity[:]
668
 
        self.assertEqual([], t._activity)
 
656
        transport = get_transport('trace+' + self.get_url(''))
 
657
        transport.put_file('index', builder.finish())
 
658
        index = btree_index.BTreeGraphIndex(transport, 'index', None)
 
659
        del transport._activity[:]
 
660
        self.assertEqual([], transport._activity)
669
661
        self.assertEqual(0, index.key_count())
670
662
        # The entire index should have been requested (as we generally have the
671
663
        # size available, and doing many small readvs is inappropriate).
672
664
        # We can't tell how much was actually read here, but - check the code.
673
 
        self.assertEqual([('get', 'index')], t._activity)
 
665
        self.assertEqual([('get', 'index')], transport._activity)
674
666
 
675
667
    def test_empty_key_count(self):
676
668
        builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
677
 
        t = transport.get_transport('trace+' + self.get_url(''))
678
 
        size = t.put_file('index', builder.finish())
 
669
        transport = get_transport('trace+' + self.get_url(''))
 
670
        size = transport.put_file('index', builder.finish())
679
671
        self.assertEqual(72, size)
680
 
        index = btree_index.BTreeGraphIndex(t, 'index', size)
681
 
        del t._activity[:]
682
 
        self.assertEqual([], t._activity)
 
672
        index = btree_index.BTreeGraphIndex(transport, 'index', size)
 
673
        del transport._activity[:]
 
674
        self.assertEqual([], transport._activity)
683
675
        self.assertEqual(0, index.key_count())
684
676
        # The entire index should have been read, as 4K > size
685
677
        self.assertEqual([('readv', 'index', [(0, 72)], False, None)],
686
 
                         t._activity)
 
678
            transport._activity)
687
679
 
688
680
    def test_non_empty_key_count_2_2(self):
689
681
        builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
690
682
        nodes = self.make_nodes(35, 2, 2)
691
683
        for node in nodes:
692
684
            builder.add_node(*node)
693
 
        t = transport.get_transport('trace+' + self.get_url(''))
694
 
        size = t.put_file('index', builder.finish())
695
 
        index = btree_index.BTreeGraphIndex(t, 'index', size)
696
 
        del t._activity[:]
697
 
        self.assertEqual([], t._activity)
 
685
        transport = get_transport('trace+' + self.get_url(''))
 
686
        size = transport.put_file('index', builder.finish())
 
687
        index = btree_index.BTreeGraphIndex(transport, 'index', size)
 
688
        del transport._activity[:]
 
689
        self.assertEqual([], transport._activity)
698
690
        self.assertEqual(70, index.key_count())
699
691
        # The entire index should have been read, as it is one page long.
700
692
        self.assertEqual([('readv', 'index', [(0, size)], False, None)],
701
 
            t._activity)
 
693
            transport._activity)
702
694
        self.assertEqual(1173, size)
703
695
 
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
 
 
723
696
    def test__read_nodes_no_size_one_page_reads_once(self):
724
697
        self.make_index(nodes=[(('key',), 'value', ())])
725
 
        trans = transport.get_transport('trace+' + self.get_url())
 
698
        trans = get_transport('trace+' + self.get_url())
726
699
        index = btree_index.BTreeGraphIndex(trans, 'index', None)
727
700
        del trans._activity[:]
728
701
        nodes = dict(index._read_nodes([0]))
729
702
        self.assertEqual([0], nodes.keys())
730
703
        node = nodes[0]
731
 
        self.assertEqual([('key',)], node.all_keys())
 
704
        self.assertEqual([('key',)], node.keys.keys())
732
705
        self.assertEqual([('get', 'index')], trans._activity)
733
706
 
734
707
    def test__read_nodes_no_size_multiple_pages(self):
736
709
        index.key_count()
737
710
        num_pages = index._row_offsets[-1]
738
711
        # Reopen with a traced transport and no size
739
 
        trans = transport.get_transport('trace+' + self.get_url())
 
712
        trans = get_transport('trace+' + self.get_url())
740
713
        index = btree_index.BTreeGraphIndex(trans, 'index', None)
741
714
        del trans._activity[:]
742
715
        nodes = dict(index._read_nodes([0]))
747
720
        nodes = self.make_nodes(160, 2, 2)
748
721
        for node in nodes:
749
722
            builder.add_node(*node)
750
 
        t = transport.get_transport('trace+' + self.get_url(''))
751
 
        size = t.put_file('index', builder.finish())
 
723
        transport = get_transport('trace+' + self.get_url(''))
 
724
        size = transport.put_file('index', builder.finish())
752
725
        self.assertEqual(17692, size)
753
 
        index = btree_index.BTreeGraphIndex(t, 'index', size)
754
 
        del t._activity[:]
755
 
        self.assertEqual([], t._activity)
 
726
        index = btree_index.BTreeGraphIndex(transport, 'index', size)
 
727
        del transport._activity[:]
 
728
        self.assertEqual([], transport._activity)
756
729
        self.assertEqual(320, index.key_count())
757
730
        # The entire index should not have been read.
758
731
        self.assertEqual([('readv', 'index', [(0, 4096)], False, None)],
759
 
                         t._activity)
 
732
            transport._activity)
760
733
 
761
734
    def test_validate_one_page(self):
762
735
        builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
763
736
        nodes = self.make_nodes(45, 2, 2)
764
737
        for node in nodes:
765
738
            builder.add_node(*node)
766
 
        t = transport.get_transport('trace+' + self.get_url(''))
767
 
        size = t.put_file('index', builder.finish())
768
 
        index = btree_index.BTreeGraphIndex(t, 'index', size)
769
 
        del t._activity[:]
770
 
        self.assertEqual([], t._activity)
 
739
        transport = get_transport('trace+' + self.get_url(''))
 
740
        size = transport.put_file('index', builder.finish())
 
741
        index = btree_index.BTreeGraphIndex(transport, 'index', size)
 
742
        del transport._activity[:]
 
743
        self.assertEqual([], transport._activity)
771
744
        index.validate()
772
745
        # The entire index should have been read linearly.
773
746
        self.assertEqual([('readv', 'index', [(0, size)], False, None)],
774
 
                         t._activity)
 
747
            transport._activity)
775
748
        self.assertEqual(1488, size)
776
749
 
777
750
    def test_validate_two_pages(self):
779
752
        nodes = self.make_nodes(80, 2, 2)
780
753
        for node in nodes:
781
754
            builder.add_node(*node)
782
 
        t = transport.get_transport('trace+' + self.get_url(''))
783
 
        size = t.put_file('index', builder.finish())
 
755
        transport = get_transport('trace+' + self.get_url(''))
 
756
        size = transport.put_file('index', builder.finish())
784
757
        # Root page, 2 leaf pages
785
758
        self.assertEqual(9339, size)
786
 
        index = btree_index.BTreeGraphIndex(t, 'index', size)
787
 
        del t._activity[:]
788
 
        self.assertEqual([], t._activity)
 
759
        index = btree_index.BTreeGraphIndex(transport, 'index', size)
 
760
        del transport._activity[:]
 
761
        self.assertEqual([], transport._activity)
789
762
        index.validate()
790
763
        # The entire index should have been read linearly.
791
 
        self.assertEqual(
792
 
            [('readv', 'index', [(0, 4096)], False, None),
793
 
             ('readv', 'index', [(4096, 4096), (8192, 1147)], False, None)],
794
 
            t._activity)
 
764
        self.assertEqual([('readv', 'index', [(0, 4096)], False, None),
 
765
            ('readv', 'index', [(4096, 4096), (8192, 1147)], False, None)],
 
766
            transport._activity)
795
767
        # XXX: TODO: write some badly-ordered nodes, and some pointers-to-wrong
796
768
        # node and make validate find them.
797
769
 
798
770
    def test_eq_ne(self):
799
771
        # two indices are equal when constructed with the same parameters:
800
 
        t1 = transport.get_transport('trace+' + self.get_url(''))
801
 
        t2 = transport.get_transport(self.get_url(''))
802
 
        self.assertTrue(
803
 
            btree_index.BTreeGraphIndex(t1, 'index', None) ==
804
 
            btree_index.BTreeGraphIndex(t1, 'index', None))
805
 
        self.assertTrue(
806
 
            btree_index.BTreeGraphIndex(t1, 'index', 20) ==
807
 
            btree_index.BTreeGraphIndex(t1, 'index', 20))
808
 
        self.assertFalse(
809
 
            btree_index.BTreeGraphIndex(t1, 'index', 20) ==
810
 
            btree_index.BTreeGraphIndex(t2, 'index', 20))
811
 
        self.assertFalse(
812
 
            btree_index.BTreeGraphIndex(t1, 'inde1', 20) ==
813
 
            btree_index.BTreeGraphIndex(t1, 'inde2', 20))
814
 
        self.assertFalse(
815
 
            btree_index.BTreeGraphIndex(t1, 'index', 10) ==
816
 
            btree_index.BTreeGraphIndex(t1, 'index', 20))
817
 
        self.assertFalse(
818
 
            btree_index.BTreeGraphIndex(t1, 'index', None) !=
819
 
            btree_index.BTreeGraphIndex(t1, 'index', None))
820
 
        self.assertFalse(
821
 
            btree_index.BTreeGraphIndex(t1, 'index', 20) !=
822
 
            btree_index.BTreeGraphIndex(t1, 'index', 20))
823
 
        self.assertTrue(
824
 
            btree_index.BTreeGraphIndex(t1, 'index', 20) !=
825
 
            btree_index.BTreeGraphIndex(t2, 'index', 20))
826
 
        self.assertTrue(
827
 
            btree_index.BTreeGraphIndex(t1, 'inde1', 20) !=
828
 
            btree_index.BTreeGraphIndex(t1, 'inde2', 20))
829
 
        self.assertTrue(
830
 
            btree_index.BTreeGraphIndex(t1, 'index', 10) !=
831
 
            btree_index.BTreeGraphIndex(t1, 'index', 20))
 
772
        transport1 = get_transport('trace+' + self.get_url(''))
 
773
        transport2 = get_transport(self.get_url(''))
 
774
        self.assertTrue(
 
775
            btree_index.BTreeGraphIndex(transport1, 'index', None) ==
 
776
            btree_index.BTreeGraphIndex(transport1, 'index', None))
 
777
        self.assertTrue(
 
778
            btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
 
779
            btree_index.BTreeGraphIndex(transport1, 'index', 20))
 
780
        self.assertFalse(
 
781
            btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
 
782
            btree_index.BTreeGraphIndex(transport2, 'index', 20))
 
783
        self.assertFalse(
 
784
            btree_index.BTreeGraphIndex(transport1, 'inde1', 20) ==
 
785
            btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
 
786
        self.assertFalse(
 
787
            btree_index.BTreeGraphIndex(transport1, 'index', 10) ==
 
788
            btree_index.BTreeGraphIndex(transport1, 'index', 20))
 
789
        self.assertFalse(
 
790
            btree_index.BTreeGraphIndex(transport1, 'index', None) !=
 
791
            btree_index.BTreeGraphIndex(transport1, 'index', None))
 
792
        self.assertFalse(
 
793
            btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
 
794
            btree_index.BTreeGraphIndex(transport1, 'index', 20))
 
795
        self.assertTrue(
 
796
            btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
 
797
            btree_index.BTreeGraphIndex(transport2, 'index', 20))
 
798
        self.assertTrue(
 
799
            btree_index.BTreeGraphIndex(transport1, 'inde1', 20) !=
 
800
            btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
 
801
        self.assertTrue(
 
802
            btree_index.BTreeGraphIndex(transport1, 'index', 10) !=
 
803
            btree_index.BTreeGraphIndex(transport1, 'index', 20))
832
804
 
833
805
    def test_iter_all_only_root_no_size(self):
834
806
        self.make_index(nodes=[(('key',), 'value', ())])
835
 
        t = transport.get_transport('trace+' + self.get_url(''))
836
 
        index = btree_index.BTreeGraphIndex(t, 'index', None)
837
 
        del t._activity[:]
 
807
        trans = get_transport('trace+' + self.get_url(''))
 
808
        index = btree_index.BTreeGraphIndex(trans, 'index', None)
 
809
        del trans._activity[:]
838
810
        self.assertEqual([(('key',), 'value')],
839
811
                         [x[1:] for x in index.iter_all_entries()])
840
 
        self.assertEqual([('get', 'index')], t._activity)
 
812
        self.assertEqual([('get', 'index')], trans._activity)
841
813
 
842
814
    def test_iter_all_entries_reads(self):
843
815
        # iterating all entries reads the header, then does a linear
849
821
        nodes = self.make_nodes(10000, 2, 2)
850
822
        for node in nodes:
851
823
            builder.add_node(*node)
852
 
        t = transport.get_transport('trace+' + self.get_url(''))
853
 
        size = t.put_file('index', builder.finish())
 
824
        transport = get_transport('trace+' + self.get_url(''))
 
825
        size = transport.put_file('index', builder.finish())
854
826
        self.assertEqual(1303220, size, 'number of expected bytes in the'
855
827
                                        ' output changed')
856
828
        page_size = btree_index._PAGE_SIZE
857
829
        del builder
858
 
        index = btree_index.BTreeGraphIndex(t, 'index', size)
859
 
        del t._activity[:]
860
 
        self.assertEqual([], t._activity)
 
830
        index = btree_index.BTreeGraphIndex(transport, 'index', size)
 
831
        del transport._activity[:]
 
832
        self.assertEqual([], transport._activity)
861
833
        found_nodes = self.time(list, index.iter_all_entries())
862
834
        bare_nodes = []
863
835
        for node in found_nodes:
884
856
        readv_request[-1] = (readv_request[-1][0], 1303220 % page_size)
885
857
        expected = [('readv', 'index', [(0, page_size)], False, None),
886
858
             ('readv',  'index', readv_request, False, None)]
887
 
        if expected != t._activity:
 
859
        if expected != transport._activity:
888
860
            self.assertEqualDiff(pprint.pformat(expected),
889
861
                                 pprint.pformat(transport._activity))
890
862
 
904
876
        nodes = self.make_nodes(160, 2, 2)
905
877
        for node in nodes:
906
878
            builder.add_node(*node)
907
 
        t = transport.get_transport('trace+' + self.get_url(''))
908
 
        size = t.put_file('index', builder.finish())
 
879
        transport = get_transport('trace+' + self.get_url(''))
 
880
        size = transport.put_file('index', builder.finish())
909
881
        del builder
910
 
        index = btree_index.BTreeGraphIndex(t, 'index', size)
911
 
        del t._activity[:]
912
 
        self.assertEqual([], t._activity)
 
882
        index = btree_index.BTreeGraphIndex(transport, 'index', size)
 
883
        del transport._activity[:]
 
884
        self.assertEqual([], transport._activity)
913
885
        # search for one key
914
886
        found_nodes = list(index.iter_entries([nodes[30][0]]))
915
887
        bare_nodes = []
923
895
        # Should have read the root node, then one leaf page:
924
896
        self.assertEqual([('readv', 'index', [(0, 4096)], False, None),
925
897
             ('readv',  'index', [(8192, 4096), ], False, None)],
926
 
            t._activity)
 
898
            transport._activity)
927
899
 
928
900
    def test_iter_key_prefix_1_element_key_None(self):
929
901
        index = self.make_index()
1112
1084
        min_l2_key = l2.min_key
1113
1085
        max_l1_key = l1.max_key
1114
1086
        self.assertTrue(max_l1_key < min_l2_key)
1115
 
        parents_min_l2_key = l2[min_l2_key][1][0]
 
1087
        parents_min_l2_key = l2.keys[min_l2_key][1][0]
1116
1088
        self.assertEqual((l1.max_key,), parents_min_l2_key)
1117
1089
        # Now, whatever key we select that would fall on the second page,
1118
1090
        # should give us all the parents until the page break
1131
1103
        parent_map = {}
1132
1104
        search_keys = index._find_ancestors([max_l1_key], 0, parent_map,
1133
1105
                                            missing_keys)
1134
 
        self.assertEqual(l1.all_keys(), sorted(parent_map))
 
1106
        self.assertEqual(sorted(l1.keys), sorted(parent_map))
1135
1107
        self.assertEqual(set(), missing_keys)
1136
1108
        self.assertEqual(set(), search_keys)
1137
1109
 
1153
1125
        for node in nodes:
1154
1126
            builder.add_node(*node)
1155
1127
        stream = builder.finish()
1156
 
        trans = transport.get_transport(self.get_url())
 
1128
        trans = get_transport(self.get_url())
1157
1129
        size = trans.put_file('index', stream)
1158
1130
        index = btree_index.BTreeGraphIndex(trans, 'index', size)
1159
1131
        self.assertEqual(500, index.key_count())
1185
1157
 
1186
1158
class TestBTreeNodes(BTreeTestCase):
1187
1159
 
 
1160
    def restore_parser(self):
 
1161
        btree_index._btree_serializer = self.saved_parser
 
1162
 
1188
1163
    def setUp(self):
1189
1164
        BTreeTestCase.setUp(self)
1190
 
        self.overrideAttr(btree_index, '_btree_serializer', self.parse_btree)
 
1165
        self.saved_parser = btree_index._btree_serializer
 
1166
        self.addCleanup(self.restore_parser)
 
1167
        btree_index._btree_serializer = self.parse_btree
1191
1168
 
1192
1169
    def test_LeafNode_1_0(self):
1193
1170
        node_bytes = ("type=leaf\n"
1205
1182
            ("2222222222222222222222222222222222222222",): ("value:2", ()),
1206
1183
            ("3333333333333333333333333333333333333333",): ("value:3", ()),
1207
1184
            ("4444444444444444444444444444444444444444",): ("value:4", ()),
1208
 
            }, dict(node.all_items()))
 
1185
            }, node.keys)
1209
1186
 
1210
1187
    def test_LeafNode_2_2(self):
1211
1188
        node_bytes = ("type=leaf\n"
1225
1202
            ('11', '33'): ('value:3',
1226
1203
                ((('11', 'ref22'),), (('11', 'ref22'), ('11', 'ref22')))),
1227
1204
            ('11', '44'): ('value:4', ((), (('11', 'ref00'),)))
1228
 
            }, dict(node.all_items()))
 
1205
            }, node.keys)
1229
1206
 
1230
1207
    def test_InternalNode_1(self):
1231
1208
        node_bytes = ("type=internal\n"
1266
1243
            ('11', '33'): ('value:3',
1267
1244
                ((('11', 'ref22'),), (('11', 'ref22'), ('11', 'ref22')))),
1268
1245
            ('11', '44'): ('value:4', ((), (('11', 'ref00'),)))
1269
 
            }, dict(node.all_items()))
 
1246
            }, node.keys)
1270
1247
 
1271
1248
    def assertFlattened(self, expected, key, value, refs):
1272
1249
        flat_key, flat_line = self.parse_btree._flatten_node(
1350
1327
        This doesn't actually create anything on disk, it just primes a
1351
1328
        BTreeGraphIndex with the recommended information.
1352
1329
        """
1353
 
        index = btree_index.BTreeGraphIndex(
1354
 
            transport.get_transport('memory:///'), 'test-index', size=size)
 
1330
        index = btree_index.BTreeGraphIndex(get_transport('memory:///'),
 
1331
                                            'test-index', size=size)
1355
1332
        if recommended_pages is not None:
1356
1333
            index._recommended_pages = recommended_pages
1357
1334
        return index