416
434
self.assertEqual(sorted(nodes), nodes)
417
435
self.assertEqual(16, len(nodes))
419
def test_spill_index_stress_1_1_no_combine(self):
420
builder = btree_index.BTreeBuilder(key_elements=1, spill_at=2)
421
builder.set_optimize(for_size=False, combine_backing_indices=False)
422
nodes = [node[0:2] for node in self.make_nodes(16, 1, 0)]
423
builder.add_node(*nodes[0])
424
# Test the parts of the index that take up memory are doing so
426
self.assertEqual(1, len(builder._nodes))
427
self.assertIs(None, builder._nodes_by_key)
428
builder.add_node(*nodes[1])
429
self.assertEqual(0, len(builder._nodes))
430
self.assertIs(None, builder._nodes_by_key)
431
self.assertEqual(1, len(builder._backing_indices))
432
self.assertEqual(2, builder._backing_indices[0].key_count())
434
builder.add_node(*nodes[2])
435
self.assertEqual(1, len(builder._nodes))
436
self.assertIs(None, builder._nodes_by_key)
437
# And spills to a second backing index but doesn't combine
438
builder.add_node(*nodes[3])
439
self.assertEqual(0, len(builder._nodes))
440
self.assertIs(None, builder._nodes_by_key)
441
self.assertEqual(2, len(builder._backing_indices))
442
for backing_index in builder._backing_indices:
443
self.assertEqual(2, backing_index.key_count())
444
# The next spills to the 3rd slot
445
builder.add_node(*nodes[4])
446
builder.add_node(*nodes[5])
447
self.assertEqual(0, len(builder._nodes))
448
self.assertIs(None, builder._nodes_by_key)
449
self.assertEqual(3, len(builder._backing_indices))
450
for backing_index in builder._backing_indices:
451
self.assertEqual(2, backing_index.key_count())
452
# Now spill a few more, and check that we don't combine
453
builder.add_node(*nodes[6])
454
builder.add_node(*nodes[7])
455
builder.add_node(*nodes[8])
456
builder.add_node(*nodes[9])
457
builder.add_node(*nodes[10])
458
builder.add_node(*nodes[11])
459
builder.add_node(*nodes[12])
460
self.assertEqual(6, len(builder._backing_indices))
461
for backing_index in builder._backing_indices:
462
self.assertEqual(2, backing_index.key_count())
463
# Test that memory and disk are both used for query methods; and that
464
# None is skipped over happily.
465
self.assertEqual([(builder,) + node for node in sorted(nodes[:13])],
466
list(builder.iter_all_entries()))
467
# Two nodes - one memory one disk
468
self.assertEqual(set([(builder,) + node for node in nodes[11:13]]),
469
set(builder.iter_entries([nodes[12][0], nodes[11][0]])))
470
self.assertEqual(13, builder.key_count())
471
self.assertEqual(set([(builder,) + node for node in nodes[11:13]]),
472
set(builder.iter_entries_prefix([nodes[12][0], nodes[11][0]])))
473
builder.add_node(*nodes[13])
474
builder.add_node(*nodes[14])
475
builder.add_node(*nodes[15])
476
self.assertEqual(8, len(builder._backing_indices))
477
for backing_index in builder._backing_indices:
478
self.assertEqual(2, backing_index.key_count())
479
# Now finish, and check we got a correctly ordered tree
480
transport = self.get_transport('')
481
size = transport.put_file('index', builder.finish())
482
index = btree_index.BTreeGraphIndex(transport, 'index', size)
483
nodes = list(index.iter_all_entries())
484
self.assertEqual(sorted(nodes), nodes)
485
self.assertEqual(16, len(nodes))
487
437
def test_set_optimize(self):
488
438
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
489
439
builder.set_optimize(for_size=True)
490
440
self.assertTrue(builder._optimize_for_size)
491
441
builder.set_optimize(for_size=False)
492
442
self.assertFalse(builder._optimize_for_size)
493
# test that we can set combine_backing_indices without effecting
496
builder._optimize_for_size = obj
497
builder.set_optimize(combine_backing_indices=False)
498
self.assertFalse(builder._combine_backing_indices)
499
self.assertIs(obj, builder._optimize_for_size)
500
builder.set_optimize(combine_backing_indices=True)
501
self.assertTrue(builder._combine_backing_indices)
502
self.assertIs(obj, builder._optimize_for_size)
504
444
def test_spill_index_stress_2_2(self):
505
445
# test that references and longer keys don't confuse things.
977
901
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
904
class TestBTreeNodes(BTreeTestCase):
906
def restore_parser(self):
907
btree_index._btree_serializer = self.saved_parser
1153
909
def setUp(self):
1154
910
BTreeTestCase.setUp(self)
1155
self.overrideAttr(btree_index, '_btree_serializer', self.parse_btree)
911
self.saved_parser = btree_index._btree_serializer
912
self.addCleanup(self.restore_parser)
913
btree_index._btree_serializer = self.parse_btree
1157
915
def test_LeafNode_1_0(self):
1158
916
node_bytes = ("type=leaf\n"