607
632
for key, value, references in nodes:
608
633
builder.add_node(key, value, references)
609
634
stream = builder.finish()
610
trans = transport.get_transport('trace+' + self.get_url())
635
trans = get_transport('trace+' + self.get_url())
611
636
size = trans.put_file('index', stream)
612
637
return btree_index.BTreeGraphIndex(trans, 'index', size)
614
def make_index_with_offset(self, ref_lists=1, key_elements=1, nodes=[],
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()
625
transport.put_bytes('index', (' '*offset)+content)
626
return btree_index.BTreeGraphIndex(transport, 'index', size=size,
629
def test_clear_cache(self):
630
nodes = self.make_nodes(160, 2, 2)
631
index = self.make_index(ref_lists=2, key_elements=2, nodes=nodes)
632
self.assertEqual(1, len(list(index.iter_entries([nodes[30][0]]))))
633
self.assertEqual([1, 4], index._row_lengths)
634
self.assertIsNot(None, index._root_node)
635
internal_node_pre_clear = index._internal_node_cache.keys()
636
self.assertTrue(len(index._leaf_node_cache) > 0)
638
# We don't touch _root_node or _internal_node_cache, both should be
639
# small, and can save a round trip or two
640
self.assertIsNot(None, index._root_node)
641
# NOTE: We don't want to affect the _internal_node_cache, as we expect
642
# it will be small, and if we ever do touch this index again, it
643
# will save round-trips. This assertion isn't very strong,
644
# becuase without a 3-level index, we don't have any internal
646
self.assertEqual(internal_node_pre_clear,
647
index._internal_node_cache.keys())
648
self.assertEqual(0, len(index._leaf_node_cache))
650
639
def test_trivial_constructor(self):
651
t = transport.get_transport('trace+' + self.get_url(''))
652
index = btree_index.BTreeGraphIndex(t, 'index', None)
640
transport = get_transport('trace+' + self.get_url(''))
641
index = btree_index.BTreeGraphIndex(transport, 'index', None)
653
642
# Checks the page size at load, but that isn't logged yet.
654
self.assertEqual([], t._activity)
643
self.assertEqual([], transport._activity)
656
645
def test_with_size_constructor(self):
657
t = transport.get_transport('trace+' + self.get_url(''))
658
index = btree_index.BTreeGraphIndex(t, 'index', 1)
646
transport = get_transport('trace+' + self.get_url(''))
647
index = btree_index.BTreeGraphIndex(transport, 'index', 1)
659
648
# Checks the page size at load, but that isn't logged yet.
660
self.assertEqual([], t._activity)
649
self.assertEqual([], transport._activity)
662
651
def test_empty_key_count_no_size(self):
663
652
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)
668
self.assertEqual([], t._activity)
653
transport = get_transport('trace+' + self.get_url(''))
654
transport.put_file('index', builder.finish())
655
index = btree_index.BTreeGraphIndex(transport, 'index', None)
656
del transport._activity[:]
657
self.assertEqual([], transport._activity)
669
658
self.assertEqual(0, index.key_count())
670
659
# The entire index should have been requested (as we generally have the
671
660
# size available, and doing many small readvs is inappropriate).
672
661
# We can't tell how much was actually read here, but - check the code.
673
self.assertEqual([('get', 'index')], t._activity)
662
self.assertEqual([('get', 'index')], transport._activity)
675
664
def test_empty_key_count(self):
676
665
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())
666
transport = get_transport('trace+' + self.get_url(''))
667
size = transport.put_file('index', builder.finish())
679
668
self.assertEqual(72, size)
680
index = btree_index.BTreeGraphIndex(t, 'index', size)
682
self.assertEqual([], t._activity)
669
index = btree_index.BTreeGraphIndex(transport, 'index', size)
670
del transport._activity[:]
671
self.assertEqual([], transport._activity)
683
672
self.assertEqual(0, index.key_count())
684
673
# The entire index should have been read, as 4K > size
685
674
self.assertEqual([('readv', 'index', [(0, 72)], False, None)],
688
677
def test_non_empty_key_count_2_2(self):
689
678
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
690
679
nodes = self.make_nodes(35, 2, 2)
691
680
for node in nodes:
692
681
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)
697
self.assertEqual([], t._activity)
682
transport = get_transport('trace+' + self.get_url(''))
683
size = transport.put_file('index', builder.finish())
684
index = btree_index.BTreeGraphIndex(transport, 'index', size)
685
del transport._activity[:]
686
self.assertEqual([], transport._activity)
698
687
self.assertEqual(70, index.key_count())
699
688
# The entire index should have been read, as it is one page long.
700
689
self.assertEqual([('readv', 'index', [(0, size)], False, None)],
702
self.assertEqual(1173, size)
704
def test_with_offset_no_size(self):
705
index = self.make_index_with_offset(key_elements=1, ref_lists=1,
707
nodes=self.make_nodes(200, 1, 1))
708
index._size = None # throw away the size info
709
self.assertEqual(200, index.key_count())
711
def test_with_small_offset(self):
712
index = self.make_index_with_offset(key_elements=1, ref_lists=1,
714
nodes=self.make_nodes(200, 1, 1))
715
self.assertEqual(200, index.key_count())
717
def test_with_large_offset(self):
718
index = self.make_index_with_offset(key_elements=1, ref_lists=1,
720
nodes=self.make_nodes(200, 1, 1))
721
self.assertEqual(200, index.key_count())
691
self.assertEqual(1199, size)
723
693
def test__read_nodes_no_size_one_page_reads_once(self):
724
694
self.make_index(nodes=[(('key',), 'value', ())])
725
trans = transport.get_transport('trace+' + self.get_url())
695
trans = get_transport('trace+' + self.get_url())
726
696
index = btree_index.BTreeGraphIndex(trans, 'index', None)
727
697
del trans._activity[:]
728
698
nodes = dict(index._read_nodes([0]))
729
699
self.assertEqual([0], nodes.keys())
731
self.assertEqual([('key',)], node.all_keys())
701
self.assertEqual([('key',)], node.keys.keys())
732
702
self.assertEqual([('get', 'index')], trans._activity)
734
704
def test__read_nodes_no_size_multiple_pages(self):
747
717
nodes = self.make_nodes(160, 2, 2)
748
718
for node in nodes:
749
719
builder.add_node(*node)
750
t = transport.get_transport('trace+' + self.get_url(''))
751
size = t.put_file('index', builder.finish())
720
transport = get_transport('trace+' + self.get_url(''))
721
size = transport.put_file('index', builder.finish())
752
722
self.assertEqual(17692, size)
753
index = btree_index.BTreeGraphIndex(t, 'index', size)
755
self.assertEqual([], t._activity)
723
index = btree_index.BTreeGraphIndex(transport, 'index', size)
724
del transport._activity[:]
725
self.assertEqual([], transport._activity)
756
726
self.assertEqual(320, index.key_count())
757
727
# The entire index should not have been read.
758
728
self.assertEqual([('readv', 'index', [(0, 4096)], False, None)],
761
731
def test_validate_one_page(self):
762
732
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
763
733
nodes = self.make_nodes(45, 2, 2)
764
734
for node in nodes:
765
735
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)
770
self.assertEqual([], t._activity)
736
transport = get_transport('trace+' + self.get_url(''))
737
size = transport.put_file('index', builder.finish())
738
index = btree_index.BTreeGraphIndex(transport, 'index', size)
739
del transport._activity[:]
740
self.assertEqual([], transport._activity)
772
742
# The entire index should have been read linearly.
773
743
self.assertEqual([('readv', 'index', [(0, size)], False, None)],
775
self.assertEqual(1488, size)
745
self.assertEqual(1514, size)
777
747
def test_validate_two_pages(self):
778
748
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
779
749
nodes = self.make_nodes(80, 2, 2)
780
750
for node in nodes:
781
751
builder.add_node(*node)
782
t = transport.get_transport('trace+' + self.get_url(''))
783
size = t.put_file('index', builder.finish())
752
transport = get_transport('trace+' + self.get_url(''))
753
size = transport.put_file('index', builder.finish())
784
754
# Root page, 2 leaf pages
785
755
self.assertEqual(9339, size)
786
index = btree_index.BTreeGraphIndex(t, 'index', size)
788
self.assertEqual([], t._activity)
756
index = btree_index.BTreeGraphIndex(transport, 'index', size)
757
del transport._activity[:]
758
self.assertEqual([], transport._activity)
790
760
# The entire index should have been read linearly.
792
[('readv', 'index', [(0, 4096)], False, None),
793
('readv', 'index', [(4096, 4096), (8192, 1147)], False, None)],
761
self.assertEqual([('readv', 'index', [(0, 4096)], False, None),
762
('readv', 'index', [(4096, 4096), (8192, 1147)], False, None)],
795
764
# XXX: TODO: write some badly-ordered nodes, and some pointers-to-wrong
796
765
# node and make validate find them.
798
767
def test_eq_ne(self):
799
768
# 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(''))
803
btree_index.BTreeGraphIndex(t1, 'index', None) ==
804
btree_index.BTreeGraphIndex(t1, 'index', None))
806
btree_index.BTreeGraphIndex(t1, 'index', 20) ==
807
btree_index.BTreeGraphIndex(t1, 'index', 20))
809
btree_index.BTreeGraphIndex(t1, 'index', 20) ==
810
btree_index.BTreeGraphIndex(t2, 'index', 20))
812
btree_index.BTreeGraphIndex(t1, 'inde1', 20) ==
813
btree_index.BTreeGraphIndex(t1, 'inde2', 20))
815
btree_index.BTreeGraphIndex(t1, 'index', 10) ==
816
btree_index.BTreeGraphIndex(t1, 'index', 20))
818
btree_index.BTreeGraphIndex(t1, 'index', None) !=
819
btree_index.BTreeGraphIndex(t1, 'index', None))
821
btree_index.BTreeGraphIndex(t1, 'index', 20) !=
822
btree_index.BTreeGraphIndex(t1, 'index', 20))
824
btree_index.BTreeGraphIndex(t1, 'index', 20) !=
825
btree_index.BTreeGraphIndex(t2, 'index', 20))
827
btree_index.BTreeGraphIndex(t1, 'inde1', 20) !=
828
btree_index.BTreeGraphIndex(t1, 'inde2', 20))
830
btree_index.BTreeGraphIndex(t1, 'index', 10) !=
831
btree_index.BTreeGraphIndex(t1, 'index', 20))
769
transport1 = get_transport('trace+' + self.get_url(''))
770
transport2 = get_transport(self.get_url(''))
772
btree_index.BTreeGraphIndex(transport1, 'index', None) ==
773
btree_index.BTreeGraphIndex(transport1, 'index', None))
775
btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
776
btree_index.BTreeGraphIndex(transport1, 'index', 20))
778
btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
779
btree_index.BTreeGraphIndex(transport2, 'index', 20))
781
btree_index.BTreeGraphIndex(transport1, 'inde1', 20) ==
782
btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
784
btree_index.BTreeGraphIndex(transport1, 'index', 10) ==
785
btree_index.BTreeGraphIndex(transport1, 'index', 20))
787
btree_index.BTreeGraphIndex(transport1, 'index', None) !=
788
btree_index.BTreeGraphIndex(transport1, 'index', None))
790
btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
791
btree_index.BTreeGraphIndex(transport1, 'index', 20))
793
btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
794
btree_index.BTreeGraphIndex(transport2, 'index', 20))
796
btree_index.BTreeGraphIndex(transport1, 'inde1', 20) !=
797
btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
799
btree_index.BTreeGraphIndex(transport1, 'index', 10) !=
800
btree_index.BTreeGraphIndex(transport1, 'index', 20))
833
802
def test_iter_all_only_root_no_size(self):
834
803
self.make_index(nodes=[(('key',), 'value', ())])
835
t = transport.get_transport('trace+' + self.get_url(''))
836
index = btree_index.BTreeGraphIndex(t, 'index', None)
804
trans = get_transport('trace+' + self.get_url(''))
805
index = btree_index.BTreeGraphIndex(trans, 'index', None)
806
del trans._activity[:]
838
807
self.assertEqual([(('key',), 'value')],
839
808
[x[1:] for x in index.iter_all_entries()])
840
self.assertEqual([('get', 'index')], t._activity)
809
self.assertEqual([('get', 'index')], trans._activity)
842
811
def test_iter_all_entries_reads(self):
843
812
# iterating all entries reads the header, then does a linear
1012
981
self.assertEqual(set([]), index.external_references(0))
1014
def test__find_ancestors_one_page(self):
1017
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1018
(key1, 'value', ([key2],)),
1019
(key2, 'value', ([],)),
1022
missing_keys = set()
1023
search_keys = index._find_ancestors([key1], 0, parent_map, missing_keys)
1024
self.assertEqual({key1: (key2,), key2: ()}, parent_map)
1025
self.assertEqual(set(), missing_keys)
1026
self.assertEqual(set(), search_keys)
1028
def test__find_ancestors_one_page_w_missing(self):
1032
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1033
(key1, 'value', ([key2],)),
1034
(key2, 'value', ([],)),
1037
missing_keys = set()
1038
search_keys = index._find_ancestors([key2, key3], 0, parent_map,
1040
self.assertEqual({key2: ()}, parent_map)
1041
# we know that key3 is missing because we read the page that it would
1043
self.assertEqual(set([key3]), missing_keys)
1044
self.assertEqual(set(), search_keys)
1046
def test__find_ancestors_one_parent_missing(self):
1050
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1051
(key1, 'value', ([key2],)),
1052
(key2, 'value', ([key3],)),
1055
missing_keys = set()
1056
search_keys = index._find_ancestors([key1], 0, parent_map,
1058
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1059
self.assertEqual(set(), missing_keys)
1060
# all we know is that key3 wasn't present on the page we were reading
1061
# but if you look, the last key is key2 which comes before key3, so we
1062
# don't know whether key3 would land on this page or not.
1063
self.assertEqual(set([key3]), search_keys)
1064
search_keys = index._find_ancestors(search_keys, 0, parent_map,
1066
# passing it back in, we are sure it is 'missing'
1067
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1068
self.assertEqual(set([key3]), missing_keys)
1069
self.assertEqual(set([]), search_keys)
1071
def test__find_ancestors_dont_search_known(self):
1075
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1076
(key1, 'value', ([key2],)),
1077
(key2, 'value', ([key3],)),
1078
(key3, 'value', ([],)),
1080
# We already know about key2, so we won't try to search for key3
1081
parent_map = {key2: (key3,)}
1082
missing_keys = set()
1083
search_keys = index._find_ancestors([key1], 0, parent_map,
1085
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1086
self.assertEqual(set(), missing_keys)
1087
self.assertEqual(set(), search_keys)
1089
def test__find_ancestors_multiple_pages(self):
1090
# We need to use enough keys that we actually cause a split
1091
start_time = 1249671539
1092
email = "joebob@example.com"
1096
for i in xrange(400):
1097
rev_id = '%s-%s-%s' % (email,
1098
osutils.compact_date(start_time + i),
1099
osutils.rand_chars(16))
1101
nodes.append((rev_key, 'value', ref_lists))
1102
# We have a ref 'list' of length 1, with a list of parents, with 1
1103
# parent which is a key
1104
ref_lists = ((rev_key,),)
1105
rev_keys.append(rev_key)
1106
index = self.make_index(ref_lists=1, key_elements=1, nodes=nodes)
1107
self.assertEqual(400, index.key_count())
1108
self.assertEqual(3, len(index._row_offsets))
1109
nodes = dict(index._read_nodes([1, 2]))
1112
min_l2_key = l2.min_key
1113
max_l1_key = l1.max_key
1114
self.assertTrue(max_l1_key < min_l2_key)
1115
parents_min_l2_key = l2[min_l2_key][1][0]
1116
self.assertEqual((l1.max_key,), parents_min_l2_key)
1117
# Now, whatever key we select that would fall on the second page,
1118
# should give us all the parents until the page break
1119
key_idx = rev_keys.index(min_l2_key)
1120
next_key = rev_keys[key_idx+1]
1121
# So now when we get the parent map, we should get the key we are
1122
# looking for, min_l2_key, and then a reference to go look for the
1123
# parent of that key
1125
missing_keys = set()
1126
search_keys = index._find_ancestors([next_key], 0, parent_map,
1128
self.assertEqual([min_l2_key, next_key], sorted(parent_map))
1129
self.assertEqual(set(), missing_keys)
1130
self.assertEqual(set([max_l1_key]), search_keys)
1132
search_keys = index._find_ancestors([max_l1_key], 0, parent_map,
1134
self.assertEqual(l1.all_keys(), sorted(parent_map))
1135
self.assertEqual(set(), missing_keys)
1136
self.assertEqual(set(), search_keys)
1138
def test__find_ancestors_empty_index(self):
1139
index = self.make_index(ref_lists=1, key_elements=1, nodes=[])
1141
missing_keys = set()
1142
search_keys = index._find_ancestors([('one',), ('two',)], 0, parent_map,
1144
self.assertEqual(set(), search_keys)
1145
self.assertEqual({}, parent_map)
1146
self.assertEqual(set([('one',), ('two',)]), missing_keys)
1148
def test_supports_unlimited_cache(self):
1149
builder = btree_index.BTreeBuilder(reference_lists=0, key_elements=1)
1150
# We need enough nodes to cause a page split (so we have both an
1151
# internal node and a couple leaf nodes. 500 seems to be enough.)
1152
nodes = self.make_nodes(500, 1, 0)
1154
builder.add_node(*node)
1155
stream = builder.finish()
1156
trans = transport.get_transport(self.get_url())
1157
size = trans.put_file('index', stream)
1158
index = btree_index.BTreeGraphIndex(trans, 'index', size)
1159
self.assertEqual(500, index.key_count())
1160
# We have an internal node
1161
self.assertEqual(2, len(index._row_lengths))
1162
# We have at least 2 leaf nodes
1163
self.assertTrue(index._row_lengths[-1] >= 2)
1164
self.assertIsInstance(index._leaf_node_cache, lru_cache.LRUCache)
1165
self.assertEqual(btree_index._NODE_CACHE_SIZE,
1166
index._leaf_node_cache._max_cache)
1167
self.assertIsInstance(index._internal_node_cache, fifo_cache.FIFOCache)
1168
self.assertEqual(100, index._internal_node_cache._max_cache)
1169
# No change if unlimited_cache=False is passed
1170
index = btree_index.BTreeGraphIndex(trans, 'index', size,
1171
unlimited_cache=False)
1172
self.assertIsInstance(index._leaf_node_cache, lru_cache.LRUCache)
1173
self.assertEqual(btree_index._NODE_CACHE_SIZE,
1174
index._leaf_node_cache._max_cache)
1175
self.assertIsInstance(index._internal_node_cache, fifo_cache.FIFOCache)
1176
self.assertEqual(100, index._internal_node_cache._max_cache)
1177
index = btree_index.BTreeGraphIndex(trans, 'index', size,
1178
unlimited_cache=True)
1179
self.assertIsInstance(index._leaf_node_cache, dict)
1180
self.assertIs(type(index._internal_node_cache), dict)
1181
# Exercise the lookup code
1182
entries = set(index.iter_entries([n[0] for n in nodes]))
1183
self.assertEqual(500, len(entries))
1186
984
class TestBTreeNodes(BTreeTestCase):
986
def restore_parser(self):
987
btree_index._btree_serializer = self.saved_parser
1188
989
def setUp(self):
1189
990
BTreeTestCase.setUp(self)
1190
self.overrideAttr(btree_index, '_btree_serializer', self.parse_btree)
991
self.saved_parser = btree_index._btree_serializer
992
self.addCleanup(self.restore_parser)
993
btree_index._btree_serializer = self.parse_btree
1192
995
def test_LeafNode_1_0(self):
1193
996
node_bytes = ("type=leaf\n"