420
427
self.assertEqual(None, builder._backing_indices[2])
421
428
self.assertEqual(16, builder._backing_indices[3].key_count())
422
429
# Now finish, and check we got a correctly ordered tree
423
t = self.get_transport('')
424
size = t.put_file('index', builder.finish())
425
index = btree_index.BTreeGraphIndex(t, 'index', size)
426
nodes = list(index.iter_all_entries())
427
self.assertEqual(sorted(nodes), nodes)
428
self.assertEqual(16, len(nodes))
430
def test_spill_index_stress_1_1_no_combine(self):
431
builder = btree_index.BTreeBuilder(key_elements=1, spill_at=2)
432
builder.set_optimize(for_size=False, combine_backing_indices=False)
433
nodes = [node[0:2] for node in self.make_nodes(16, 1, 0)]
434
builder.add_node(*nodes[0])
435
# Test the parts of the index that take up memory are doing so
437
self.assertEqual(1, len(builder._nodes))
438
self.assertIs(None, builder._nodes_by_key)
439
builder.add_node(*nodes[1])
440
self.assertEqual(0, len(builder._nodes))
441
self.assertIs(None, builder._nodes_by_key)
442
self.assertEqual(1, len(builder._backing_indices))
443
self.assertEqual(2, builder._backing_indices[0].key_count())
445
builder.add_node(*nodes[2])
446
self.assertEqual(1, len(builder._nodes))
447
self.assertIs(None, builder._nodes_by_key)
448
# And spills to a second backing index but doesn't combine
449
builder.add_node(*nodes[3])
450
self.assertEqual(0, len(builder._nodes))
451
self.assertIs(None, builder._nodes_by_key)
452
self.assertEqual(2, len(builder._backing_indices))
453
for backing_index in builder._backing_indices:
454
self.assertEqual(2, backing_index.key_count())
455
# The next spills to the 3rd slot
456
builder.add_node(*nodes[4])
457
builder.add_node(*nodes[5])
458
self.assertEqual(0, len(builder._nodes))
459
self.assertIs(None, builder._nodes_by_key)
460
self.assertEqual(3, len(builder._backing_indices))
461
for backing_index in builder._backing_indices:
462
self.assertEqual(2, backing_index.key_count())
463
# Now spill a few more, and check that we don't combine
464
builder.add_node(*nodes[6])
465
builder.add_node(*nodes[7])
466
builder.add_node(*nodes[8])
467
builder.add_node(*nodes[9])
468
builder.add_node(*nodes[10])
469
builder.add_node(*nodes[11])
470
builder.add_node(*nodes[12])
471
self.assertEqual(6, len(builder._backing_indices))
472
for backing_index in builder._backing_indices:
473
self.assertEqual(2, backing_index.key_count())
474
# Test that memory and disk are both used for query methods; and that
475
# None is skipped over happily.
476
self.assertEqual([(builder,) + node for node in sorted(nodes[:13])],
477
list(builder.iter_all_entries()))
478
# Two nodes - one memory one disk
479
self.assertEqual(set([(builder,) + node for node in nodes[11:13]]),
480
set(builder.iter_entries([nodes[12][0], nodes[11][0]])))
481
self.assertEqual(13, builder.key_count())
482
self.assertEqual(set([(builder,) + node for node in nodes[11:13]]),
483
set(builder.iter_entries_prefix([nodes[12][0], nodes[11][0]])))
484
builder.add_node(*nodes[13])
485
builder.add_node(*nodes[14])
486
builder.add_node(*nodes[15])
487
self.assertEqual(8, len(builder._backing_indices))
488
for backing_index in builder._backing_indices:
489
self.assertEqual(2, backing_index.key_count())
490
# Now finish, and check we got a correctly ordered tree
491
430
transport = self.get_transport('')
492
431
size = transport.put_file('index', builder.finish())
493
432
index = btree_index.BTreeGraphIndex(transport, 'index', size)
618
545
for key, value, references in nodes:
619
546
builder.add_node(key, value, references)
620
547
stream = builder.finish()
621
trans = transport.get_transport_from_url('trace+' + self.get_url())
548
trans = get_transport('trace+' + self.get_url())
622
549
size = trans.put_file('index', stream)
623
550
return btree_index.BTreeGraphIndex(trans, 'index', size)
625
def make_index_with_offset(self, ref_lists=1, key_elements=1, nodes=[],
627
builder = btree_index.BTreeBuilder(key_elements=key_elements,
628
reference_lists=ref_lists)
629
builder.add_nodes(nodes)
630
transport = self.get_transport('')
631
# NamedTemporaryFile dies on builder.finish().read(). weird.
632
temp_file = builder.finish()
633
content = temp_file.read()
636
transport.put_bytes('index', (' '*offset)+content)
637
return btree_index.BTreeGraphIndex(transport, 'index', size=size,
640
def test_clear_cache(self):
641
nodes = self.make_nodes(160, 2, 2)
642
index = self.make_index(ref_lists=2, key_elements=2, nodes=nodes)
643
self.assertEqual(1, len(list(index.iter_entries([nodes[30][0]]))))
644
self.assertEqual([1, 4], index._row_lengths)
645
self.assertIsNot(None, index._root_node)
646
internal_node_pre_clear = index._internal_node_cache.keys()
647
self.assertTrue(len(index._leaf_node_cache) > 0)
649
# We don't touch _root_node or _internal_node_cache, both should be
650
# small, and can save a round trip or two
651
self.assertIsNot(None, index._root_node)
652
# NOTE: We don't want to affect the _internal_node_cache, as we expect
653
# it will be small, and if we ever do touch this index again, it
654
# will save round-trips. This assertion isn't very strong,
655
# becuase without a 3-level index, we don't have any internal
657
self.assertEqual(internal_node_pre_clear,
658
index._internal_node_cache.keys())
659
self.assertEqual(0, len(index._leaf_node_cache))
661
552
def test_trivial_constructor(self):
662
t = transport.get_transport_from_url('trace+' + self.get_url(''))
663
index = btree_index.BTreeGraphIndex(t, 'index', None)
553
transport = get_transport('trace+' + self.get_url(''))
554
index = btree_index.BTreeGraphIndex(transport, 'index', None)
664
555
# Checks the page size at load, but that isn't logged yet.
665
self.assertEqual([], t._activity)
556
self.assertEqual([], transport._activity)
667
558
def test_with_size_constructor(self):
668
t = transport.get_transport_from_url('trace+' + self.get_url(''))
669
index = btree_index.BTreeGraphIndex(t, 'index', 1)
559
transport = get_transport('trace+' + self.get_url(''))
560
index = btree_index.BTreeGraphIndex(transport, 'index', 1)
670
561
# Checks the page size at load, but that isn't logged yet.
671
self.assertEqual([], t._activity)
562
self.assertEqual([], transport._activity)
673
564
def test_empty_key_count_no_size(self):
674
565
builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
675
t = transport.get_transport_from_url('trace+' + self.get_url(''))
676
t.put_file('index', builder.finish())
677
index = btree_index.BTreeGraphIndex(t, 'index', None)
679
self.assertEqual([], t._activity)
566
transport = get_transport('trace+' + self.get_url(''))
567
transport.put_file('index', builder.finish())
568
index = btree_index.BTreeGraphIndex(transport, 'index', None)
569
del transport._activity[:]
570
self.assertEqual([], transport._activity)
680
571
self.assertEqual(0, index.key_count())
681
572
# The entire index should have been requested (as we generally have the
682
573
# size available, and doing many small readvs is inappropriate).
683
574
# We can't tell how much was actually read here, but - check the code.
684
self.assertEqual([('get', 'index')], t._activity)
575
self.assertEqual([('get', 'index'),
576
('readv', 'index', [(0, 72)], False, None)],
686
579
def test_empty_key_count(self):
687
580
builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
688
t = transport.get_transport_from_url('trace+' + self.get_url(''))
689
size = t.put_file('index', builder.finish())
581
transport = get_transport('trace+' + self.get_url(''))
582
size = transport.put_file('index', builder.finish())
690
583
self.assertEqual(72, size)
691
index = btree_index.BTreeGraphIndex(t, 'index', size)
693
self.assertEqual([], t._activity)
584
index = btree_index.BTreeGraphIndex(transport, 'index', size)
585
del transport._activity[:]
586
self.assertEqual([], transport._activity)
694
587
self.assertEqual(0, index.key_count())
695
588
# The entire index should have been read, as 4K > size
696
589
self.assertEqual([('readv', 'index', [(0, 72)], False, None)],
699
592
def test_non_empty_key_count_2_2(self):
700
593
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
701
594
nodes = self.make_nodes(35, 2, 2)
702
595
for node in nodes:
703
596
builder.add_node(*node)
704
t = transport.get_transport_from_url('trace+' + self.get_url(''))
705
size = t.put_file('index', builder.finish())
706
index = btree_index.BTreeGraphIndex(t, 'index', size)
708
self.assertEqual([], t._activity)
597
transport = get_transport('trace+' + self.get_url(''))
598
size = transport.put_file('index', builder.finish())
599
index = btree_index.BTreeGraphIndex(transport, 'index', size)
600
del transport._activity[:]
601
self.assertEqual([], transport._activity)
709
602
self.assertEqual(70, index.key_count())
710
603
# The entire index should have been read, as it is one page long.
711
604
self.assertEqual([('readv', 'index', [(0, size)], False, None)],
713
self.assertEqualApproxCompressed(1173, size)
715
def test_with_offset_no_size(self):
716
index = self.make_index_with_offset(key_elements=1, ref_lists=1,
718
nodes=self.make_nodes(200, 1, 1))
719
index._size = None # throw away the size info
720
self.assertEqual(200, index.key_count())
722
def test_with_small_offset(self):
723
index = self.make_index_with_offset(key_elements=1, ref_lists=1,
725
nodes=self.make_nodes(200, 1, 1))
726
self.assertEqual(200, index.key_count())
728
def test_with_large_offset(self):
729
index = self.make_index_with_offset(key_elements=1, ref_lists=1,
731
nodes=self.make_nodes(200, 1, 1))
732
self.assertEqual(200, index.key_count())
734
def test__read_nodes_no_size_one_page_reads_once(self):
735
self.make_index(nodes=[(('key',), 'value', ())])
736
trans = transport.get_transport_from_url('trace+' + self.get_url())
737
index = btree_index.BTreeGraphIndex(trans, 'index', None)
738
del trans._activity[:]
739
nodes = dict(index._read_nodes([0]))
740
self.assertEqual([0], nodes.keys())
742
self.assertEqual([('key',)], node.all_keys())
743
self.assertEqual([('get', 'index')], trans._activity)
745
def test__read_nodes_no_size_multiple_pages(self):
746
index = self.make_index(2, 2, nodes=self.make_nodes(160, 2, 2))
748
num_pages = index._row_offsets[-1]
749
# Reopen with a traced transport and no size
750
trans = transport.get_transport_from_url('trace+' + self.get_url())
751
index = btree_index.BTreeGraphIndex(trans, 'index', None)
752
del trans._activity[:]
753
nodes = dict(index._read_nodes([0]))
754
self.assertEqual(range(num_pages), nodes.keys())
606
self.assertEqual(1199, size)
756
608
def test_2_levels_key_count_2_2(self):
757
609
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
758
610
nodes = self.make_nodes(160, 2, 2)
759
611
for node in nodes:
760
612
builder.add_node(*node)
761
t = transport.get_transport_from_url('trace+' + self.get_url(''))
762
size = t.put_file('index', builder.finish())
763
self.assertEqualApproxCompressed(17692, size)
764
index = btree_index.BTreeGraphIndex(t, 'index', size)
766
self.assertEqual([], t._activity)
613
transport = get_transport('trace+' + self.get_url(''))
614
size = transport.put_file('index', builder.finish())
615
self.assertEqual(17692, size)
616
index = btree_index.BTreeGraphIndex(transport, 'index', size)
617
del transport._activity[:]
618
self.assertEqual([], transport._activity)
767
619
self.assertEqual(320, index.key_count())
768
620
# The entire index should not have been read.
769
621
self.assertEqual([('readv', 'index', [(0, 4096)], False, None)],
772
624
def test_validate_one_page(self):
773
625
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
774
626
nodes = self.make_nodes(45, 2, 2)
775
627
for node in nodes:
776
628
builder.add_node(*node)
777
t = transport.get_transport_from_url('trace+' + self.get_url(''))
778
size = t.put_file('index', builder.finish())
779
index = btree_index.BTreeGraphIndex(t, 'index', size)
781
self.assertEqual([], t._activity)
629
transport = get_transport('trace+' + self.get_url(''))
630
size = transport.put_file('index', builder.finish())
631
index = btree_index.BTreeGraphIndex(transport, 'index', size)
632
del transport._activity[:]
633
self.assertEqual([], transport._activity)
783
635
# The entire index should have been read linearly.
784
636
self.assertEqual([('readv', 'index', [(0, size)], False, None)],
786
self.assertEqualApproxCompressed(1488, size)
638
self.assertEqual(1514, size)
788
640
def test_validate_two_pages(self):
789
641
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
790
642
nodes = self.make_nodes(80, 2, 2)
791
643
for node in nodes:
792
644
builder.add_node(*node)
793
t = transport.get_transport_from_url('trace+' + self.get_url(''))
794
size = t.put_file('index', builder.finish())
645
transport = get_transport('trace+' + self.get_url(''))
646
size = transport.put_file('index', builder.finish())
795
647
# Root page, 2 leaf pages
796
self.assertEqualApproxCompressed(9339, size)
797
index = btree_index.BTreeGraphIndex(t, 'index', size)
799
self.assertEqual([], t._activity)
648
self.assertEqual(9339, size)
649
index = btree_index.BTreeGraphIndex(transport, 'index', size)
650
del transport._activity[:]
651
self.assertEqual([], transport._activity)
801
rem = size - 8192 # Number of remaining bytes after second block
802
653
# The entire index should have been read linearly.
804
[('readv', 'index', [(0, 4096)], False, None),
805
('readv', 'index', [(4096, 4096), (8192, rem)], False, None)],
654
self.assertEqual([('readv', 'index', [(0, 4096)], False, None),
655
('readv', 'index', [(4096, 4096), (8192, 1147)], False, None)],
807
657
# XXX: TODO: write some badly-ordered nodes, and some pointers-to-wrong
808
658
# node and make validate find them.
810
660
def test_eq_ne(self):
811
661
# two indices are equal when constructed with the same parameters:
812
t1 = transport.get_transport_from_url('trace+' + self.get_url(''))
813
t2 = self.get_transport()
815
btree_index.BTreeGraphIndex(t1, 'index', None) ==
816
btree_index.BTreeGraphIndex(t1, 'index', None))
818
btree_index.BTreeGraphIndex(t1, 'index', 20) ==
819
btree_index.BTreeGraphIndex(t1, 'index', 20))
821
btree_index.BTreeGraphIndex(t1, 'index', 20) ==
822
btree_index.BTreeGraphIndex(t2, 'index', 20))
824
btree_index.BTreeGraphIndex(t1, 'inde1', 20) ==
825
btree_index.BTreeGraphIndex(t1, 'inde2', 20))
827
btree_index.BTreeGraphIndex(t1, 'index', 10) ==
828
btree_index.BTreeGraphIndex(t1, 'index', 20))
830
btree_index.BTreeGraphIndex(t1, 'index', None) !=
831
btree_index.BTreeGraphIndex(t1, 'index', None))
833
btree_index.BTreeGraphIndex(t1, 'index', 20) !=
834
btree_index.BTreeGraphIndex(t1, 'index', 20))
836
btree_index.BTreeGraphIndex(t1, 'index', 20) !=
837
btree_index.BTreeGraphIndex(t2, 'index', 20))
839
btree_index.BTreeGraphIndex(t1, 'inde1', 20) !=
840
btree_index.BTreeGraphIndex(t1, 'inde2', 20))
842
btree_index.BTreeGraphIndex(t1, 'index', 10) !=
843
btree_index.BTreeGraphIndex(t1, 'index', 20))
845
def test_key_too_big(self):
846
# the size that matters here is the _compressed_ size of the key, so we can't
847
# do a simple character repeat.
848
bigKey = ''.join(map(repr, xrange(btree_index._PAGE_SIZE)))
849
self.assertRaises(errors.BadIndexKey,
851
nodes=[((bigKey,), 'value', ())])
853
def test_iter_all_only_root_no_size(self):
854
self.make_index(nodes=[(('key',), 'value', ())])
855
t = transport.get_transport_from_url('trace+' + self.get_url(''))
856
index = btree_index.BTreeGraphIndex(t, 'index', None)
858
self.assertEqual([(('key',), 'value')],
859
[x[1:] for x in index.iter_all_entries()])
860
self.assertEqual([('get', 'index')], t._activity)
662
transport1 = get_transport('trace+' + self.get_url(''))
663
transport2 = get_transport(self.get_url(''))
665
btree_index.BTreeGraphIndex(transport1, 'index', None) ==
666
btree_index.BTreeGraphIndex(transport1, 'index', None))
668
btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
669
btree_index.BTreeGraphIndex(transport1, 'index', 20))
671
btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
672
btree_index.BTreeGraphIndex(transport2, 'index', 20))
674
btree_index.BTreeGraphIndex(transport1, 'inde1', 20) ==
675
btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
677
btree_index.BTreeGraphIndex(transport1, 'index', 10) ==
678
btree_index.BTreeGraphIndex(transport1, 'index', 20))
680
btree_index.BTreeGraphIndex(transport1, 'index', None) !=
681
btree_index.BTreeGraphIndex(transport1, 'index', None))
683
btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
684
btree_index.BTreeGraphIndex(transport1, 'index', 20))
686
btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
687
btree_index.BTreeGraphIndex(transport2, 'index', 20))
689
btree_index.BTreeGraphIndex(transport1, 'inde1', 20) !=
690
btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
692
btree_index.BTreeGraphIndex(transport1, 'index', 10) !=
693
btree_index.BTreeGraphIndex(transport1, 'index', 20))
862
695
def test_iter_all_entries_reads(self):
863
696
# iterating all entries reads the header, then does a linear
998
833
(index, ('name', 'fin2'), 'beta', ((), ))]),
999
834
set(index.iter_entries_prefix([('name', None)])))
1001
# XXX: external_references tests are duplicated in test_index. We
1002
# probably should have per_graph_index tests...
1003
def test_external_references_no_refs(self):
1004
index = self.make_index(ref_lists=0, nodes=[])
1005
self.assertRaises(ValueError, index.external_references, 0)
1007
def test_external_references_no_results(self):
1008
index = self.make_index(ref_lists=1, nodes=[
1009
(('key',), 'value', ([],))])
1010
self.assertEqual(set(), index.external_references(0))
1012
def test_external_references_missing_ref(self):
1013
missing_key = ('missing',)
1014
index = self.make_index(ref_lists=1, nodes=[
1015
(('key',), 'value', ([missing_key],))])
1016
self.assertEqual(set([missing_key]), index.external_references(0))
1018
def test_external_references_multiple_ref_lists(self):
1019
missing_key = ('missing',)
1020
index = self.make_index(ref_lists=2, nodes=[
1021
(('key',), 'value', ([], [missing_key]))])
1022
self.assertEqual(set([]), index.external_references(0))
1023
self.assertEqual(set([missing_key]), index.external_references(1))
1025
def test_external_references_two_records(self):
1026
index = self.make_index(ref_lists=1, nodes=[
1027
(('key-1',), 'value', ([('key-2',)],)),
1028
(('key-2',), 'value', ([],)),
1030
self.assertEqual(set([]), index.external_references(0))
1032
def test__find_ancestors_one_page(self):
1035
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1036
(key1, 'value', ([key2],)),
1037
(key2, 'value', ([],)),
1040
missing_keys = set()
1041
search_keys = index._find_ancestors([key1], 0, parent_map, missing_keys)
1042
self.assertEqual({key1: (key2,), key2: ()}, parent_map)
1043
self.assertEqual(set(), missing_keys)
1044
self.assertEqual(set(), search_keys)
1046
def test__find_ancestors_one_page_w_missing(self):
1050
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1051
(key1, 'value', ([key2],)),
1052
(key2, 'value', ([],)),
1055
missing_keys = set()
1056
search_keys = index._find_ancestors([key2, key3], 0, parent_map,
1058
self.assertEqual({key2: ()}, parent_map)
1059
# we know that key3 is missing because we read the page that it would
1061
self.assertEqual(set([key3]), missing_keys)
1062
self.assertEqual(set(), search_keys)
1064
def test__find_ancestors_one_parent_missing(self):
1068
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1069
(key1, 'value', ([key2],)),
1070
(key2, 'value', ([key3],)),
1073
missing_keys = set()
1074
search_keys = index._find_ancestors([key1], 0, parent_map,
1076
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1077
self.assertEqual(set(), missing_keys)
1078
# all we know is that key3 wasn't present on the page we were reading
1079
# but if you look, the last key is key2 which comes before key3, so we
1080
# don't know whether key3 would land on this page or not.
1081
self.assertEqual(set([key3]), search_keys)
1082
search_keys = index._find_ancestors(search_keys, 0, parent_map,
1084
# passing it back in, we are sure it is 'missing'
1085
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1086
self.assertEqual(set([key3]), missing_keys)
1087
self.assertEqual(set([]), search_keys)
1089
def test__find_ancestors_dont_search_known(self):
1093
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1094
(key1, 'value', ([key2],)),
1095
(key2, 'value', ([key3],)),
1096
(key3, 'value', ([],)),
1098
# We already know about key2, so we won't try to search for key3
1099
parent_map = {key2: (key3,)}
1100
missing_keys = set()
1101
search_keys = index._find_ancestors([key1], 0, parent_map,
1103
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1104
self.assertEqual(set(), missing_keys)
1105
self.assertEqual(set(), search_keys)
1107
def test__find_ancestors_multiple_pages(self):
1108
# We need to use enough keys that we actually cause a split
1109
start_time = 1249671539
1110
email = "joebob@example.com"
1114
for i in xrange(400):
1115
rev_id = '%s-%s-%s' % (email,
1116
osutils.compact_date(start_time + i),
1117
osutils.rand_chars(16))
1119
nodes.append((rev_key, 'value', ref_lists))
1120
# We have a ref 'list' of length 1, with a list of parents, with 1
1121
# parent which is a key
1122
ref_lists = ((rev_key,),)
1123
rev_keys.append(rev_key)
1124
index = self.make_index(ref_lists=1, key_elements=1, nodes=nodes)
1125
self.assertEqual(400, index.key_count())
1126
self.assertEqual(3, len(index._row_offsets))
1127
nodes = dict(index._read_nodes([1, 2]))
1130
min_l2_key = l2.min_key
1131
max_l1_key = l1.max_key
1132
self.assertTrue(max_l1_key < min_l2_key)
1133
parents_min_l2_key = l2[min_l2_key][1][0]
1134
self.assertEqual((l1.max_key,), parents_min_l2_key)
1135
# Now, whatever key we select that would fall on the second page,
1136
# should give us all the parents until the page break
1137
key_idx = rev_keys.index(min_l2_key)
1138
next_key = rev_keys[key_idx+1]
1139
# So now when we get the parent map, we should get the key we are
1140
# looking for, min_l2_key, and then a reference to go look for the
1141
# parent of that key
1143
missing_keys = set()
1144
search_keys = index._find_ancestors([next_key], 0, parent_map,
1146
self.assertEqual([min_l2_key, next_key], sorted(parent_map))
1147
self.assertEqual(set(), missing_keys)
1148
self.assertEqual(set([max_l1_key]), search_keys)
1150
search_keys = index._find_ancestors([max_l1_key], 0, parent_map,
1152
self.assertEqual(l1.all_keys(), sorted(parent_map))
1153
self.assertEqual(set(), missing_keys)
1154
self.assertEqual(set(), search_keys)
1156
def test__find_ancestors_empty_index(self):
1157
index = self.make_index(ref_lists=1, key_elements=1, nodes=[])
1159
missing_keys = set()
1160
search_keys = index._find_ancestors([('one',), ('two',)], 0, parent_map,
1162
self.assertEqual(set(), search_keys)
1163
self.assertEqual({}, parent_map)
1164
self.assertEqual(set([('one',), ('two',)]), missing_keys)
1166
def test_supports_unlimited_cache(self):
1167
builder = btree_index.BTreeBuilder(reference_lists=0, key_elements=1)
1168
# We need enough nodes to cause a page split (so we have both an
1169
# internal node and a couple leaf nodes. 500 seems to be enough.)
1170
nodes = self.make_nodes(500, 1, 0)
1172
builder.add_node(*node)
1173
stream = builder.finish()
1174
trans = self.get_transport()
1175
size = trans.put_file('index', stream)
1176
index = btree_index.BTreeGraphIndex(trans, 'index', size)
1177
self.assertEqual(500, index.key_count())
1178
# We have an internal node
1179
self.assertEqual(2, len(index._row_lengths))
1180
# We have at least 2 leaf nodes
1181
self.assertTrue(index._row_lengths[-1] >= 2)
1182
self.assertIsInstance(index._leaf_node_cache, lru_cache.LRUCache)
1183
self.assertEqual(btree_index._NODE_CACHE_SIZE,
1184
index._leaf_node_cache._max_cache)
1185
self.assertIsInstance(index._internal_node_cache, fifo_cache.FIFOCache)
1186
self.assertEqual(100, index._internal_node_cache._max_cache)
1187
# No change if unlimited_cache=False is passed
1188
index = btree_index.BTreeGraphIndex(trans, 'index', size,
1189
unlimited_cache=False)
1190
self.assertIsInstance(index._leaf_node_cache, lru_cache.LRUCache)
1191
self.assertEqual(btree_index._NODE_CACHE_SIZE,
1192
index._leaf_node_cache._max_cache)
1193
self.assertIsInstance(index._internal_node_cache, fifo_cache.FIFOCache)
1194
self.assertEqual(100, index._internal_node_cache._max_cache)
1195
index = btree_index.BTreeGraphIndex(trans, 'index', size,
1196
unlimited_cache=True)
1197
self.assertIsInstance(index._leaf_node_cache, dict)
1198
self.assertIs(type(index._internal_node_cache), dict)
1199
# Exercise the lookup code
1200
entries = set(index.iter_entries([n[0] for n in nodes]))
1201
self.assertEqual(500, len(entries))
1204
837
class TestBTreeNodes(BTreeTestCase):
1206
scenarios = btreeparser_scenarios()
839
def restore_parser(self):
840
btree_index._btree_serializer = self.saved_parser
1208
842
def setUp(self):
1209
super(TestBTreeNodes, self).setUp()
1210
self.overrideAttr(btree_index, '_btree_serializer', self.parse_btree)
843
BTreeTestCase.setUp(self)
844
self.saved_parser = btree_index._btree_serializer
845
self.addCleanup(self.restore_parser)
846
btree_index._btree_serializer = self.parse_btree
1212
848
def test_LeafNode_1_0(self):
1213
849
node_bytes = ("type=leaf\n"
1360
996
(4, ['g', 'h'])],
1361
997
['a', 'b', 'd', 'e', 'g', 'h'],
1362
998
['c', 'd', 'f', 'g'])
1365
class TestExpandOffsets(tests.TestCase):
1367
def make_index(self, size, recommended_pages=None):
1368
"""Make an index with a generic size.
1370
This doesn't actually create anything on disk, it just primes a
1371
BTreeGraphIndex with the recommended information.
1373
index = btree_index.BTreeGraphIndex(
1374
transport.get_transport_from_url('memory:///'),
1375
'test-index', size=size)
1376
if recommended_pages is not None:
1377
index._recommended_pages = recommended_pages
1380
def set_cached_offsets(self, index, cached_offsets):
1381
"""Monkeypatch to give a canned answer for _get_offsets_for...()."""
1382
def _get_offsets_to_cached_pages():
1383
cached = set(cached_offsets)
1385
index._get_offsets_to_cached_pages = _get_offsets_to_cached_pages
1387
def prepare_index(self, index, node_ref_lists, key_length, key_count,
1388
row_lengths, cached_offsets):
1389
"""Setup the BTreeGraphIndex with some pre-canned information."""
1390
index.node_ref_lists = node_ref_lists
1391
index._key_length = key_length
1392
index._key_count = key_count
1393
index._row_lengths = row_lengths
1394
index._compute_row_offsets()
1395
index._root_node = btree_index._InternalNode('internal\noffset=0\n')
1396
self.set_cached_offsets(index, cached_offsets)
1398
def make_100_node_index(self):
1399
index = self.make_index(4096*100, 6)
1400
# Consider we've already made a single request at the middle
1401
self.prepare_index(index, node_ref_lists=0, key_length=1,
1402
key_count=1000, row_lengths=[1, 99],
1403
cached_offsets=[0, 50])
1406
def make_1000_node_index(self):
1407
index = self.make_index(4096*1000, 6)
1408
# Pretend we've already made a single request in the middle
1409
self.prepare_index(index, node_ref_lists=0, key_length=1,
1410
key_count=90000, row_lengths=[1, 9, 990],
1411
cached_offsets=[0, 5, 500])
1414
def assertNumPages(self, expected_pages, index, size):
1416
self.assertEqual(expected_pages, index._compute_total_pages_in_index())
1418
def assertExpandOffsets(self, expected, index, offsets):
1419
self.assertEqual(expected, index._expand_offsets(offsets),
1420
'We did not get the expected value after expanding'
1423
def test_default_recommended_pages(self):
1424
index = self.make_index(None)
1425
# local transport recommends 4096 byte reads, which is 1 page
1426
self.assertEqual(1, index._recommended_pages)
1428
def test__compute_total_pages_in_index(self):
1429
index = self.make_index(None)
1430
self.assertNumPages(1, index, 1024)
1431
self.assertNumPages(1, index, 4095)
1432
self.assertNumPages(1, index, 4096)
1433
self.assertNumPages(2, index, 4097)
1434
self.assertNumPages(2, index, 8192)
1435
self.assertNumPages(76, index, 4096*75 + 10)
1437
def test__find_layer_start_and_stop(self):
1438
index = self.make_1000_node_index()
1439
self.assertEqual((0, 1), index._find_layer_first_and_end(0))
1440
self.assertEqual((1, 10), index._find_layer_first_and_end(1))
1441
self.assertEqual((1, 10), index._find_layer_first_and_end(9))
1442
self.assertEqual((10, 1000), index._find_layer_first_and_end(10))
1443
self.assertEqual((10, 1000), index._find_layer_first_and_end(99))
1444
self.assertEqual((10, 1000), index._find_layer_first_and_end(999))
1446
def test_unknown_size(self):
1447
# We should not expand if we don't know the file size
1448
index = self.make_index(None, 10)
1449
self.assertExpandOffsets([0], index, [0])
1450
self.assertExpandOffsets([1, 4, 9], index, [1, 4, 9])
1452
def test_more_than_recommended(self):
1453
index = self.make_index(4096*100, 2)
1454
self.assertExpandOffsets([1, 10], index, [1, 10])
1455
self.assertExpandOffsets([1, 10, 20], index, [1, 10, 20])
1457
def test_read_all_from_root(self):
1458
index = self.make_index(4096*10, 20)
1459
self.assertExpandOffsets(range(10), index, [0])
1461
def test_read_all_when_cached(self):
1462
# We've read enough that we can grab all the rest in a single request
1463
index = self.make_index(4096*10, 5)
1464
self.prepare_index(index, node_ref_lists=0, key_length=1,
1465
key_count=1000, row_lengths=[1, 9],
1466
cached_offsets=[0, 1, 2, 5, 6])
1467
# It should fill the remaining nodes, regardless of the one requested
1468
self.assertExpandOffsets([3, 4, 7, 8, 9], index, [3])
1469
self.assertExpandOffsets([3, 4, 7, 8, 9], index, [8])
1470
self.assertExpandOffsets([3, 4, 7, 8, 9], index, [9])
1472
def test_no_root_node(self):
1473
index = self.make_index(4096*10, 5)
1474
self.assertExpandOffsets([0], index, [0])
1476
def test_include_neighbors(self):
1477
index = self.make_100_node_index()
1478
# We expand in both directions, until we have at least 'recommended'
1480
self.assertExpandOffsets([9, 10, 11, 12, 13, 14, 15], index, [12])
1481
self.assertExpandOffsets([88, 89, 90, 91, 92, 93, 94], index, [91])
1482
# If we hit an 'edge' we continue in the other direction
1483
self.assertExpandOffsets([1, 2, 3, 4, 5, 6], index, [2])
1484
self.assertExpandOffsets([94, 95, 96, 97, 98, 99], index, [98])
1486
# Requesting many nodes will expand all locations equally
1487
self.assertExpandOffsets([1, 2, 3, 80, 81, 82], index, [2, 81])
1488
self.assertExpandOffsets([1, 2, 3, 9, 10, 11, 80, 81, 82], index,
1491
def test_stop_at_cached(self):
1492
index = self.make_100_node_index()
1493
self.set_cached_offsets(index, [0, 10, 19])
1494
self.assertExpandOffsets([11, 12, 13, 14, 15, 16], index, [11])
1495
self.assertExpandOffsets([11, 12, 13, 14, 15, 16], index, [12])
1496
self.assertExpandOffsets([12, 13, 14, 15, 16, 17, 18], index, [15])
1497
self.assertExpandOffsets([13, 14, 15, 16, 17, 18], index, [16])
1498
self.assertExpandOffsets([13, 14, 15, 16, 17, 18], index, [17])
1499
self.assertExpandOffsets([13, 14, 15, 16, 17, 18], index, [18])
1501
def test_cannot_fully_expand(self):
1502
index = self.make_100_node_index()
1503
self.set_cached_offsets(index, [0, 10, 12])
1504
# We don't go into an endless loop if we are bound by cached nodes
1505
self.assertExpandOffsets([11], index, [11])
1507
def test_overlap(self):
1508
index = self.make_100_node_index()
1509
self.assertExpandOffsets([10, 11, 12, 13, 14, 15], index, [12, 13])
1510
self.assertExpandOffsets([10, 11, 12, 13, 14, 15], index, [11, 14])
1512
def test_stay_within_layer(self):
1513
index = self.make_1000_node_index()
1514
# When expanding a request, we won't read nodes from the next layer
1515
self.assertExpandOffsets([1, 2, 3, 4], index, [2])
1516
self.assertExpandOffsets([6, 7, 8, 9], index, [6])
1517
self.assertExpandOffsets([6, 7, 8, 9], index, [9])
1518
self.assertExpandOffsets([10, 11, 12, 13, 14, 15], index, [10])
1519
self.assertExpandOffsets([10, 11, 12, 13, 14, 15, 16], index, [13])
1521
self.set_cached_offsets(index, [0, 4, 12])
1522
self.assertExpandOffsets([5, 6, 7, 8, 9], index, [7])
1523
self.assertExpandOffsets([10, 11], index, [11])
1525
def test_small_requests_unexpanded(self):
1526
index = self.make_100_node_index()
1527
self.set_cached_offsets(index, [0])
1528
self.assertExpandOffsets([1], index, [1])
1529
self.assertExpandOffsets([50], index, [50])
1530
# If we request more than one node, then we'll expand
1531
self.assertExpandOffsets([49, 50, 51, 59, 60, 61], index, [50, 60])
1533
# The first pass does not expand
1534
index = self.make_1000_node_index()
1535
self.set_cached_offsets(index, [0])
1536
self.assertExpandOffsets([1], index, [1])
1537
self.set_cached_offsets(index, [0, 1])
1538
self.assertExpandOffsets([100], index, [100])
1539
self.set_cached_offsets(index, [0, 1, 100])
1540
# But after the first depth, we will expand
1541
self.assertExpandOffsets([2, 3, 4, 5, 6, 7], index, [2])
1542
self.assertExpandOffsets([2, 3, 4, 5, 6, 7], index, [4])
1543
self.set_cached_offsets(index, [0, 1, 2, 3, 4, 5, 6, 7, 100])
1544
self.assertExpandOffsets([102, 103, 104, 105, 106, 107, 108], index,