954
954
self.assertEqual(set([]), index.external_references(0))
956
def test__find_ancestors(self):
959
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
960
(key1, 'value', ([key2],)),
961
(key2, 'value', ([],)),
965
search_keys = index._find_ancestors([key1], 0, parent_map, missing_keys)
966
self.assertEqual({key1: (key2,)}, parent_map)
967
self.assertEqual(set(), missing_keys)
968
self.assertEqual(set([key2]), search_keys)
969
search_keys = index._find_ancestors(search_keys, 0, parent_map,
971
self.assertEqual({key1: (key2,), key2: ()}, parent_map)
972
self.assertEqual(set(), missing_keys)
973
self.assertEqual(set(), search_keys)
975
def test__find_ancestors_w_missing(self):
979
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
980
(key1, 'value', ([key2],)),
981
(key2, 'value', ([],)),
985
search_keys = index._find_ancestors([key2, key3], 0, parent_map,
987
self.assertEqual({key2: ()}, parent_map)
988
self.assertEqual(set([key3]), missing_keys)
989
self.assertEqual(set(), search_keys)
991
def test__find_ancestors_dont_search_known(self):
995
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
996
(key1, 'value', ([key2],)),
997
(key2, 'value', ([key3],)),
998
(key3, 'value', ([],)),
1000
# We already know about key2, so we won't try to search for key3
1001
parent_map = {key2: (key3,)}
1002
missing_keys = set()
1003
search_keys = index._find_ancestors([key1], 0, parent_map,
1005
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1006
self.assertEqual(set(), missing_keys)
1007
self.assertEqual(set(), search_keys)
1009
def test_supports_unlimited_cache(self):
1010
builder = GraphIndexBuilder(0, key_elements=1)
1011
stream = builder.finish()
1012
trans = get_transport(self.get_url())
1013
size = trans.put_file('index', stream)
1014
# It doesn't matter what unlimited_cache does here, just that it can be
1016
index = GraphIndex(trans, 'index', size, unlimited_cache=True)
957
1019
class TestCombinedGraphIndex(TestCaseWithMemoryTransport):
1271
1333
['1', '2', '3'])
1272
1334
self.assertRaises(errors.NoSuchFile, index.validate)
1336
def test_find_ancestors_across_indexes(self):
1341
index1 = self.make_index('12', ref_lists=1, nodes=[
1342
(key1, 'value', ([],)),
1343
(key2, 'value', ([key1],)),
1345
index2 = self.make_index('34', ref_lists=1, nodes=[
1346
(key3, 'value', ([key2],)),
1347
(key4, 'value', ([key3],)),
1349
c_index = CombinedGraphIndex([index1, index2])
1350
parent_map, missing_keys = c_index.find_ancestry([key1], 0)
1351
self.assertEqual({key1: ()}, parent_map)
1352
self.assertEqual(set(), missing_keys)
1353
# Now look for a key from index2 which requires us to find the key in
1354
# the second index, and then continue searching for parents in the
1356
parent_map, missing_keys = c_index.find_ancestry([key3], 0)
1357
self.assertEqual({key1: (), key2: (key1,), key3: (key2,)}, parent_map)
1358
self.assertEqual(set(), missing_keys)
1360
def test_find_ancestors_missing_keys(self):
1365
index1 = self.make_index('12', ref_lists=1, nodes=[
1366
(key1, 'value', ([],)),
1367
(key2, 'value', ([key1],)),
1369
index2 = self.make_index('34', ref_lists=1, nodes=[
1370
(key3, 'value', ([key2],)),
1372
c_index = CombinedGraphIndex([index1, index2])
1373
# Searching for a key which is actually not present at all should
1374
# eventually converge
1375
parent_map, missing_keys = c_index.find_ancestry([key4], 0)
1376
self.assertEqual({}, parent_map)
1377
self.assertEqual(set([key4]), missing_keys)
1379
def test_find_ancestors_no_indexes(self):
1380
c_index = CombinedGraphIndex([])
1382
parent_map, missing_keys = c_index.find_ancestry([key1], 0)
1383
self.assertEqual({}, parent_map)
1384
self.assertEqual(set([key1]), missing_keys)
1386
def test_find_ancestors_ghost_parent(self):
1391
index1 = self.make_index('12', ref_lists=1, nodes=[
1392
(key1, 'value', ([],)),
1393
(key2, 'value', ([key1],)),
1395
index2 = self.make_index('34', ref_lists=1, nodes=[
1396
(key4, 'value', ([key2, key3],)),
1398
c_index = CombinedGraphIndex([index1, index2])
1399
# Searching for a key which is actually not present at all should
1400
# eventually converge
1401
parent_map, missing_keys = c_index.find_ancestry([key4], 0)
1402
self.assertEqual({key4: (key2, key3), key2: (key1,), key1: ()},
1404
self.assertEqual(set([key3]), missing_keys)
1406
def test__find_ancestors_empty_index(self):
1407
index = self.make_index('test', ref_lists=1, key_elements=1, nodes=[])
1409
missing_keys = set()
1410
search_keys = index._find_ancestors([('one',), ('two',)], 0, parent_map,
1412
self.assertEqual(set(), search_keys)
1413
self.assertEqual({}, parent_map)
1414
self.assertEqual(set([('one',), ('two',)]), missing_keys)
1275
1417
class TestInMemoryGraphIndex(TestCaseWithMemoryTransport):