977
981
self.assertEqual(set([]), index.external_references(0))
979
def test__find_ancestors_one_page(self):
982
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
983
(key1, 'value', ([key2],)),
984
(key2, 'value', ([],)),
988
search_keys = index._find_ancestors([key1], 0, parent_map, missing_keys)
989
self.assertEqual({key1: (key2,), key2: ()}, parent_map)
990
self.assertEqual(set(), missing_keys)
991
self.assertEqual(set(), search_keys)
993
def test__find_ancestors_one_page_w_missing(self):
997
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
998
(key1, 'value', ([key2],)),
999
(key2, 'value', ([],)),
1002
missing_keys = set()
1003
search_keys = index._find_ancestors([key2, key3], 0, parent_map,
1005
self.assertEqual({key2: ()}, parent_map)
1006
# we know that key3 is missing because we read the page that it would
1008
self.assertEqual(set([key3]), missing_keys)
1009
self.assertEqual(set(), search_keys)
1011
def test__find_ancestors_one_parent_missing(self):
1015
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1016
(key1, 'value', ([key2],)),
1017
(key2, 'value', ([key3],)),
1020
missing_keys = set()
1021
search_keys = index._find_ancestors([key1], 0, parent_map,
1023
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1024
self.assertEqual(set(), missing_keys)
1025
# all we know is that key3 wasn't present on the page we were reading
1026
# but if you look, the last key is key2 which comes before key3, so we
1027
# don't know whether key3 would land on this page or not.
1028
self.assertEqual(set([key3]), search_keys)
1029
search_keys = index._find_ancestors(search_keys, 0, parent_map,
1031
# passing it back in, we are sure it is 'missing'
1032
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1033
self.assertEqual(set([key3]), missing_keys)
1034
self.assertEqual(set([]), search_keys)
1036
def test__find_ancestors_dont_search_known(self):
1040
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1041
(key1, 'value', ([key2],)),
1042
(key2, 'value', ([key3],)),
1043
(key3, 'value', ([],)),
1045
# We already know about key2, so we won't try to search for key3
1046
parent_map = {key2: (key3,)}
1047
missing_keys = set()
1048
search_keys = index._find_ancestors([key1], 0, parent_map,
1050
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1051
self.assertEqual(set(), missing_keys)
1052
self.assertEqual(set(), search_keys)
1054
def test__find_ancestors_multiple_pages(self):
1055
# We need to use enough keys that we actually cause a split
1056
start_time = 1249671539
1057
email = "joebob@example.com"
1061
for i in xrange(400):
1062
rev_id = '%s-%s-%s' % (email,
1063
osutils.compact_date(start_time + i),
1064
osutils.rand_chars(16))
1066
nodes.append((rev_key, 'value', ref_lists))
1067
# We have a ref 'list' of length 1, with a list of parents, with 1
1068
# parent which is a key
1069
ref_lists = ((rev_key,),)
1070
rev_keys.append(rev_key)
1071
index = self.make_index(ref_lists=1, key_elements=1, nodes=nodes)
1072
self.assertEqual(400, index.key_count())
1073
self.assertEqual(3, len(index._row_offsets))
1074
nodes = dict(index._read_nodes([1, 2]))
1077
min_l2_key = l2.min_key
1078
max_l1_key = l1.max_key
1079
self.assertTrue(max_l1_key < min_l2_key)
1080
parents_min_l2_key = l2.keys[min_l2_key][1][0]
1081
self.assertEqual((l1.max_key,), parents_min_l2_key)
1082
# Now, whatever key we select that would fall on the second page,
1083
# should give us all the parents until the page break
1084
key_idx = rev_keys.index(min_l2_key)
1085
next_key = rev_keys[key_idx+1]
1086
# So now when we get the parent map, we should get the key we are
1087
# looking for, min_l2_key, and then a reference to go look for the
1088
# parent of that key
1090
missing_keys = set()
1091
search_keys = index._find_ancestors([next_key], 0, parent_map,
1093
self.assertEqual([min_l2_key, next_key], sorted(parent_map))
1094
self.assertEqual(set(), missing_keys)
1095
self.assertEqual(set([max_l1_key]), search_keys)
1097
search_keys = index._find_ancestors([max_l1_key], 0, parent_map,
1099
self.assertEqual(sorted(l1.keys), sorted(parent_map))
1100
self.assertEqual(set(), missing_keys)
1101
self.assertEqual(set(), search_keys)
1103
def test__find_ancestors_empty_index(self):
1104
index = self.make_index(ref_lists=1, key_elements=1, nodes=[])
1106
missing_keys = set()
1107
search_keys = index._find_ancestors([('one',), ('two',)], 0, parent_map,
1109
self.assertEqual(set(), search_keys)
1110
self.assertEqual({}, parent_map)
1111
self.assertEqual(set([('one',), ('two',)]), missing_keys)
1113
def test_supports_unlimited_cache(self):
1114
builder = btree_index.BTreeBuilder(reference_lists=0, key_elements=1)
1115
# We need enough nodes to cause a page split (so we have both an
1116
# internal node and a couple leaf nodes. 500 seems to be enough.)
1117
nodes = self.make_nodes(500, 1, 0)
1119
builder.add_node(*node)
1120
stream = builder.finish()
1121
trans = get_transport(self.get_url())
1122
size = trans.put_file('index', stream)
1123
index = btree_index.BTreeGraphIndex(trans, 'index', size)
1124
self.assertEqual(500, index.key_count())
1125
# We have an internal node
1126
self.assertEqual(2, len(index._row_lengths))
1127
# We have at least 2 leaf nodes
1128
self.assertTrue(index._row_lengths[-1] >= 2)
1129
self.assertIsInstance(index._leaf_node_cache, lru_cache.LRUCache)
1130
self.assertEqual(btree_index._NODE_CACHE_SIZE,
1131
index._leaf_node_cache._max_cache)
1132
self.assertIsInstance(index._internal_node_cache, fifo_cache.FIFOCache)
1133
self.assertEqual(100, index._internal_node_cache._max_cache)
1134
# No change if unlimited_cache=False is passed
1135
index = btree_index.BTreeGraphIndex(trans, 'index', size,
1136
unlimited_cache=False)
1137
self.assertIsInstance(index._leaf_node_cache, lru_cache.LRUCache)
1138
self.assertEqual(btree_index._NODE_CACHE_SIZE,
1139
index._leaf_node_cache._max_cache)
1140
self.assertIsInstance(index._internal_node_cache, fifo_cache.FIFOCache)
1141
self.assertEqual(100, index._internal_node_cache._max_cache)
1142
index = btree_index.BTreeGraphIndex(trans, 'index', size,
1143
unlimited_cache=True)
1144
self.assertIsInstance(index._leaf_node_cache, dict)
1145
self.assertIs(type(index._internal_node_cache), dict)
1146
# Exercise the lookup code
1147
entries = set(index.iter_entries([n[0] for n in nodes]))
1148
self.assertEqual(500, len(entries))
1151
984
class TestBTreeNodes(BTreeTestCase):
986
def restore_parser(self):
987
btree_index._btree_serializer = self.saved_parser
1153
989
def setUp(self):
1154
990
BTreeTestCase.setUp(self)
1155
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
1157
995
def test_LeafNode_1_0(self):
1158
996
node_bytes = ("type=leaf\n"