32
31
from bzrlib.tests import (
33
32
TestCaseWithTransport,
36
from bzrlib.tests import (
41
load_tests = scenarios.load_tests_apply_scenarios
44
def btreeparser_scenarios():
35
split_suite_by_condition,
37
from bzrlib.transport import get_transport
40
def load_tests(standard_tests, module, loader):
41
# parameterise the TestBTreeNodes tests
42
node_tests, others = split_suite_by_condition(standard_tests,
43
condition_isinstance(TestBTreeNodes))
45
44
import bzrlib._btree_serializer_py as py_module
46
45
scenarios = [('python', {'parse_btree': py_module})]
47
46
if compiled_btreeparser_feature.available():
48
scenarios.append(('C',
49
{'parse_btree': compiled_btreeparser_feature.module}))
53
compiled_btreeparser_feature = features.ModuleAvailableFeature(
54
'bzrlib._btree_serializer_pyx')
47
scenarios.append(('C', {'parse_btree':
48
compiled_btreeparser_feature.module}))
49
return multiply_tests(node_tests, scenarios, others)
52
compiled_btreeparser_feature = tests.ModuleAvailableFeature(
53
'bzrlib._btree_serializer_pyx')
57
56
class BTreeTestCase(TestCaseWithTransport):
649
648
self.assertEqual(0, len(index._leaf_node_cache))
651
650
def test_trivial_constructor(self):
652
t = transport.get_transport_from_url('trace+' + self.get_url(''))
653
index = btree_index.BTreeGraphIndex(t, 'index', None)
651
transport = get_transport('trace+' + self.get_url(''))
652
index = btree_index.BTreeGraphIndex(transport, 'index', None)
654
653
# Checks the page size at load, but that isn't logged yet.
655
self.assertEqual([], t._activity)
654
self.assertEqual([], transport._activity)
657
656
def test_with_size_constructor(self):
658
t = transport.get_transport_from_url('trace+' + self.get_url(''))
659
index = btree_index.BTreeGraphIndex(t, 'index', 1)
657
transport = get_transport('trace+' + self.get_url(''))
658
index = btree_index.BTreeGraphIndex(transport, 'index', 1)
660
659
# Checks the page size at load, but that isn't logged yet.
661
self.assertEqual([], t._activity)
660
self.assertEqual([], transport._activity)
663
662
def test_empty_key_count_no_size(self):
664
663
builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
665
t = transport.get_transport_from_url('trace+' + self.get_url(''))
666
t.put_file('index', builder.finish())
667
index = btree_index.BTreeGraphIndex(t, 'index', None)
669
self.assertEqual([], t._activity)
664
transport = get_transport('trace+' + self.get_url(''))
665
transport.put_file('index', builder.finish())
666
index = btree_index.BTreeGraphIndex(transport, 'index', None)
667
del transport._activity[:]
668
self.assertEqual([], transport._activity)
670
669
self.assertEqual(0, index.key_count())
671
670
# The entire index should have been requested (as we generally have the
672
671
# size available, and doing many small readvs is inappropriate).
673
672
# We can't tell how much was actually read here, but - check the code.
674
self.assertEqual([('get', 'index')], t._activity)
673
self.assertEqual([('get', 'index')], transport._activity)
676
675
def test_empty_key_count(self):
677
676
builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
678
t = transport.get_transport_from_url('trace+' + self.get_url(''))
679
size = t.put_file('index', builder.finish())
677
transport = get_transport('trace+' + self.get_url(''))
678
size = transport.put_file('index', builder.finish())
680
679
self.assertEqual(72, size)
681
index = btree_index.BTreeGraphIndex(t, 'index', size)
683
self.assertEqual([], t._activity)
680
index = btree_index.BTreeGraphIndex(transport, 'index', size)
681
del transport._activity[:]
682
self.assertEqual([], transport._activity)
684
683
self.assertEqual(0, index.key_count())
685
684
# The entire index should have been read, as 4K > size
686
685
self.assertEqual([('readv', 'index', [(0, 72)], False, None)],
689
688
def test_non_empty_key_count_2_2(self):
690
689
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
691
690
nodes = self.make_nodes(35, 2, 2)
692
691
for node in nodes:
693
692
builder.add_node(*node)
694
t = transport.get_transport_from_url('trace+' + self.get_url(''))
695
size = t.put_file('index', builder.finish())
696
index = btree_index.BTreeGraphIndex(t, 'index', size)
698
self.assertEqual([], t._activity)
693
transport = get_transport('trace+' + self.get_url(''))
694
size = transport.put_file('index', builder.finish())
695
index = btree_index.BTreeGraphIndex(transport, 'index', size)
696
del transport._activity[:]
697
self.assertEqual([], transport._activity)
699
698
self.assertEqual(70, index.key_count())
700
699
# The entire index should have been read, as it is one page long.
701
700
self.assertEqual([('readv', 'index', [(0, size)], False, None)],
703
702
self.assertEqual(1173, size)
705
704
def test_with_offset_no_size(self):
748
747
nodes = self.make_nodes(160, 2, 2)
749
748
for node in nodes:
750
749
builder.add_node(*node)
751
t = transport.get_transport_from_url('trace+' + self.get_url(''))
752
size = t.put_file('index', builder.finish())
750
transport = get_transport('trace+' + self.get_url(''))
751
size = transport.put_file('index', builder.finish())
753
752
self.assertEqual(17692, size)
754
index = btree_index.BTreeGraphIndex(t, 'index', size)
756
self.assertEqual([], t._activity)
753
index = btree_index.BTreeGraphIndex(transport, 'index', size)
754
del transport._activity[:]
755
self.assertEqual([], transport._activity)
757
756
self.assertEqual(320, index.key_count())
758
757
# The entire index should not have been read.
759
758
self.assertEqual([('readv', 'index', [(0, 4096)], False, None)],
762
761
def test_validate_one_page(self):
763
762
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
764
763
nodes = self.make_nodes(45, 2, 2)
765
764
for node in nodes:
766
765
builder.add_node(*node)
767
t = transport.get_transport_from_url('trace+' + self.get_url(''))
768
size = t.put_file('index', builder.finish())
769
index = btree_index.BTreeGraphIndex(t, 'index', size)
771
self.assertEqual([], t._activity)
766
transport = get_transport('trace+' + self.get_url(''))
767
size = transport.put_file('index', builder.finish())
768
index = btree_index.BTreeGraphIndex(transport, 'index', size)
769
del transport._activity[:]
770
self.assertEqual([], transport._activity)
773
772
# The entire index should have been read linearly.
774
773
self.assertEqual([('readv', 'index', [(0, size)], False, None)],
776
775
self.assertEqual(1488, size)
778
777
def test_validate_two_pages(self):
780
779
nodes = self.make_nodes(80, 2, 2)
781
780
for node in nodes:
782
781
builder.add_node(*node)
783
t = transport.get_transport_from_url('trace+' + self.get_url(''))
784
size = t.put_file('index', builder.finish())
782
transport = get_transport('trace+' + self.get_url(''))
783
size = transport.put_file('index', builder.finish())
785
784
# Root page, 2 leaf pages
786
785
self.assertEqual(9339, size)
787
index = btree_index.BTreeGraphIndex(t, 'index', size)
789
self.assertEqual([], t._activity)
786
index = btree_index.BTreeGraphIndex(transport, 'index', size)
787
del transport._activity[:]
788
self.assertEqual([], transport._activity)
791
790
# The entire index should have been read linearly.
793
[('readv', 'index', [(0, 4096)], False, None),
794
('readv', 'index', [(4096, 4096), (8192, 1147)], False, None)],
791
self.assertEqual([('readv', 'index', [(0, 4096)], False, None),
792
('readv', 'index', [(4096, 4096), (8192, 1147)], False, None)],
796
794
# XXX: TODO: write some badly-ordered nodes, and some pointers-to-wrong
797
795
# node and make validate find them.
799
797
def test_eq_ne(self):
800
798
# two indices are equal when constructed with the same parameters:
801
t1 = transport.get_transport_from_url('trace+' + self.get_url(''))
802
t2 = self.get_transport()
804
btree_index.BTreeGraphIndex(t1, 'index', None) ==
805
btree_index.BTreeGraphIndex(t1, 'index', None))
807
btree_index.BTreeGraphIndex(t1, 'index', 20) ==
808
btree_index.BTreeGraphIndex(t1, 'index', 20))
810
btree_index.BTreeGraphIndex(t1, 'index', 20) ==
811
btree_index.BTreeGraphIndex(t2, 'index', 20))
813
btree_index.BTreeGraphIndex(t1, 'inde1', 20) ==
814
btree_index.BTreeGraphIndex(t1, 'inde2', 20))
816
btree_index.BTreeGraphIndex(t1, 'index', 10) ==
817
btree_index.BTreeGraphIndex(t1, 'index', 20))
819
btree_index.BTreeGraphIndex(t1, 'index', None) !=
820
btree_index.BTreeGraphIndex(t1, 'index', None))
822
btree_index.BTreeGraphIndex(t1, 'index', 20) !=
823
btree_index.BTreeGraphIndex(t1, 'index', 20))
825
btree_index.BTreeGraphIndex(t1, 'index', 20) !=
826
btree_index.BTreeGraphIndex(t2, 'index', 20))
828
btree_index.BTreeGraphIndex(t1, 'inde1', 20) !=
829
btree_index.BTreeGraphIndex(t1, 'inde2', 20))
831
btree_index.BTreeGraphIndex(t1, 'index', 10) !=
832
btree_index.BTreeGraphIndex(t1, 'index', 20))
799
transport1 = get_transport('trace+' + self.get_url(''))
800
transport2 = get_transport(self.get_url(''))
802
btree_index.BTreeGraphIndex(transport1, 'index', None) ==
803
btree_index.BTreeGraphIndex(transport1, 'index', None))
805
btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
806
btree_index.BTreeGraphIndex(transport1, 'index', 20))
808
btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
809
btree_index.BTreeGraphIndex(transport2, 'index', 20))
811
btree_index.BTreeGraphIndex(transport1, 'inde1', 20) ==
812
btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
814
btree_index.BTreeGraphIndex(transport1, 'index', 10) ==
815
btree_index.BTreeGraphIndex(transport1, 'index', 20))
817
btree_index.BTreeGraphIndex(transport1, 'index', None) !=
818
btree_index.BTreeGraphIndex(transport1, 'index', None))
820
btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
821
btree_index.BTreeGraphIndex(transport1, 'index', 20))
823
btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
824
btree_index.BTreeGraphIndex(transport2, 'index', 20))
826
btree_index.BTreeGraphIndex(transport1, 'inde1', 20) !=
827
btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
829
btree_index.BTreeGraphIndex(transport1, 'index', 10) !=
830
btree_index.BTreeGraphIndex(transport1, 'index', 20))
834
832
def test_iter_all_only_root_no_size(self):
835
833
self.make_index(nodes=[(('key',), 'value', ())])
836
t = transport.get_transport_from_url('trace+' + self.get_url(''))
837
index = btree_index.BTreeGraphIndex(t, 'index', None)
834
trans = get_transport('trace+' + self.get_url(''))
835
index = btree_index.BTreeGraphIndex(trans, 'index', None)
836
del trans._activity[:]
839
837
self.assertEqual([(('key',), 'value')],
840
838
[x[1:] for x in index.iter_all_entries()])
841
self.assertEqual([('get', 'index')], t._activity)
839
self.assertEqual([('get', 'index')], trans._activity)
843
841
def test_iter_all_entries_reads(self):
844
842
# iterating all entries reads the header, then does a linear