981
982
self.assertEqual(set([]), index.external_references(0))
984
def test__find_ancestors_one_page(self):
987
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
988
(key1, 'value', ([key2],)),
989
(key2, 'value', ([],)),
993
search_keys = index._find_ancestors([key1], 0, parent_map, missing_keys)
994
self.assertEqual({key1: (key2,), key2: ()}, parent_map)
995
self.assertEqual(set(), missing_keys)
996
self.assertEqual(set(), search_keys)
998
def test__find_ancestors_one_page_w_missing(self):
1002
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1003
(key1, 'value', ([key2],)),
1004
(key2, 'value', ([],)),
1007
missing_keys = set()
1008
search_keys = index._find_ancestors([key2, key3], 0, parent_map,
1010
self.assertEqual({key2: ()}, parent_map)
1011
# we know that key3 is missing because we read the page that it would
1013
self.assertEqual(set([key3]), missing_keys)
1014
self.assertEqual(set(), search_keys)
1016
def test__find_ancestors_one_parent_missing(self):
1020
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1021
(key1, 'value', ([key2],)),
1022
(key2, 'value', ([key3],)),
1025
missing_keys = set()
1026
search_keys = index._find_ancestors([key1], 0, parent_map,
1028
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1029
self.assertEqual(set(), missing_keys)
1030
# all we know is that key3 wasn't present on the page we were reading
1031
# but if you look, the last key is key2 which comes before key3, so we
1032
# don't know whether key3 would land on this page or not.
1033
self.assertEqual(set([key3]), search_keys)
1034
search_keys = index._find_ancestors(search_keys, 0, parent_map,
1036
# passing it back in, we are sure it is 'missing'
1037
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1038
self.assertEqual(set([key3]), missing_keys)
1039
self.assertEqual(set([]), search_keys)
1041
def test__find_ancestors_dont_search_known(self):
1045
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1046
(key1, 'value', ([key2],)),
1047
(key2, 'value', ([key3],)),
1048
(key3, 'value', ([],)),
1050
# We already know about key2, so we won't try to search for key3
1051
parent_map = {key2: (key3,)}
1052
missing_keys = set()
1053
search_keys = index._find_ancestors([key1], 0, parent_map,
1055
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1056
self.assertEqual(set(), missing_keys)
1057
self.assertEqual(set(), search_keys)
1059
def test__find_ancestors_multiple_pages(self):
1060
# We need to use enough keys that we actually cause a split
1061
start_time = 1249671539
1062
email = "joebob@example.com"
1066
for i in xrange(400):
1067
rev_id = '%s-%s-%s' % (email,
1068
osutils.compact_date(start_time + i),
1069
osutils.rand_chars(16))
1071
nodes.append((rev_key, 'value', ref_lists))
1072
# We have a ref 'list' of length 1, with a list of parents, with 1
1073
# parent which is a key
1074
ref_lists = ((rev_key,),)
1075
rev_keys.append(rev_key)
1076
index = self.make_index(ref_lists=1, key_elements=1, nodes=nodes)
1077
self.assertEqual(400, index.key_count())
1078
self.assertEqual(3, len(index._row_offsets))
1079
nodes = dict(index._read_nodes([1, 2]))
1082
min_l2_key = l2.min_key
1083
max_l1_key = l1.max_key
1084
self.assertTrue(max_l1_key < min_l2_key)
1085
parents_min_l2_key = l2.keys[min_l2_key][1][0]
1086
self.assertEqual((l1.max_key,), parents_min_l2_key)
1087
# Now, whatever key we select that would fall on the second page,
1088
# should give us all the parents until the page break
1089
key_idx = rev_keys.index(min_l2_key)
1090
next_key = rev_keys[key_idx+1]
1091
# So now when we get the parent map, we should get the key we are
1092
# looking for, min_l2_key, and then a reference to go look for the
1093
# parent of that key
1095
missing_keys = set()
1096
search_keys = index._find_ancestors([next_key], 0, parent_map,
1098
self.assertEqual([min_l2_key, next_key], sorted(parent_map))
1099
self.assertEqual(set(), missing_keys)
1100
self.assertEqual(set([max_l1_key]), search_keys)
1102
search_keys = index._find_ancestors([max_l1_key], 0, parent_map,
1104
self.assertEqual(sorted(l1.keys), sorted(parent_map))
1105
self.assertEqual(set(), missing_keys)
1106
self.assertEqual(set(), search_keys)
1108
def test__find_ancestors_empty_index(self):
1109
index = self.make_index(ref_lists=1, key_elements=1, nodes=[])
1111
missing_keys = set()
1112
search_keys = index._find_ancestors([('one',), ('two',)], 0, parent_map,
1114
self.assertEqual(set(), search_keys)
1115
self.assertEqual({}, parent_map)
1116
self.assertEqual(set([('one',), ('two',)]), missing_keys)
984
1119
class TestBTreeNodes(BTreeTestCase):