969
915
index = self.make_index(nodes=[(('key', ), 'value', ())])
972
# XXX: external_references tests are duplicated in test_btree_index. We
973
# probably should have per_graph_index tests...
974
def test_external_references_no_refs(self):
975
index = self.make_index(ref_lists=0, nodes=[])
976
self.assertRaises(ValueError, index.external_references, 0)
978
def test_external_references_no_results(self):
979
index = self.make_index(ref_lists=1, nodes=[
980
(('key',), 'value', ([],))])
981
self.assertEqual(set(), index.external_references(0))
983
def test_external_references_missing_ref(self):
984
missing_key = ('missing',)
985
index = self.make_index(ref_lists=1, nodes=[
986
(('key',), 'value', ([missing_key],))])
987
self.assertEqual(set([missing_key]), index.external_references(0))
989
def test_external_references_multiple_ref_lists(self):
990
missing_key = ('missing',)
991
index = self.make_index(ref_lists=2, nodes=[
992
(('key',), 'value', ([], [missing_key]))])
993
self.assertEqual(set([]), index.external_references(0))
994
self.assertEqual(set([missing_key]), index.external_references(1))
996
def test_external_references_two_records(self):
997
index = self.make_index(ref_lists=1, nodes=[
998
(('key-1',), 'value', ([('key-2',)],)),
999
(('key-2',), 'value', ([],)),
1001
self.assertEqual(set([]), index.external_references(0))
1003
def test__find_ancestors(self):
1006
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1007
(key1, 'value', ([key2],)),
1008
(key2, 'value', ([],)),
1011
missing_keys = set()
1012
search_keys = index._find_ancestors([key1], 0, parent_map, missing_keys)
1013
self.assertEqual({key1: (key2,)}, parent_map)
1014
self.assertEqual(set(), missing_keys)
1015
self.assertEqual(set([key2]), search_keys)
1016
search_keys = index._find_ancestors(search_keys, 0, parent_map,
1018
self.assertEqual({key1: (key2,), key2: ()}, parent_map)
1019
self.assertEqual(set(), missing_keys)
1020
self.assertEqual(set(), search_keys)
1022
def test__find_ancestors_w_missing(self):
1026
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1027
(key1, 'value', ([key2],)),
1028
(key2, 'value', ([],)),
1031
missing_keys = set()
1032
search_keys = index._find_ancestors([key2, key3], 0, parent_map,
1034
self.assertEqual({key2: ()}, parent_map)
1035
self.assertEqual(set([key3]), missing_keys)
1036
self.assertEqual(set(), search_keys)
1038
def test__find_ancestors_dont_search_known(self):
1042
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1043
(key1, 'value', ([key2],)),
1044
(key2, 'value', ([key3],)),
1045
(key3, 'value', ([],)),
1047
# We already know about key2, so we won't try to search for key3
1048
parent_map = {key2: (key3,)}
1049
missing_keys = set()
1050
search_keys = index._find_ancestors([key1], 0, parent_map,
1052
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1053
self.assertEqual(set(), missing_keys)
1054
self.assertEqual(set(), search_keys)
1056
def test_supports_unlimited_cache(self):
1057
builder = index.GraphIndexBuilder(0, key_elements=1)
1058
stream = builder.finish()
1059
trans = self.get_transport()
1060
size = trans.put_file('index', stream)
1061
# It doesn't matter what unlimited_cache does here, just that it can be
1063
idx = index.GraphIndex(trans, 'index', size, unlimited_cache=True)
1066
class TestCombinedGraphIndex(tests.TestCaseWithMemoryTransport):
919
class TestCombinedGraphIndex(TestCaseWithMemoryTransport):
1068
921
def make_index(self, name, ref_lists=0, key_elements=1, nodes=[]):
1069
builder = index.GraphIndexBuilder(ref_lists, key_elements=key_elements)
922
builder = GraphIndexBuilder(ref_lists, key_elements=key_elements)
1070
923
for key, value, references in nodes:
1071
924
builder.add_node(key, value, references)
1072
925
stream = builder.finish()
1073
926
trans = self.get_transport()
1074
927
size = trans.put_file(name, stream)
1075
return index.GraphIndex(trans, name, size)
1077
def make_combined_index_with_missing(self, missing=['1', '2']):
1078
"""Create a CombinedGraphIndex which will have missing indexes.
1080
This creates a CGI which thinks it has 2 indexes, however they have
1081
been deleted. If CGI._reload_func() is called, then it will repopulate
1084
:param missing: The underlying indexes to delete
1085
:return: (CombinedGraphIndex, reload_counter)
1087
idx1 = self.make_index('1', nodes=[(('1',), '', ())])
1088
idx2 = self.make_index('2', nodes=[(('2',), '', ())])
1089
idx3 = self.make_index('3', nodes=[
1093
# total_reloads, num_changed, num_unchanged
1094
reload_counter = [0, 0, 0]
1096
reload_counter[0] += 1
1097
new_indices = [idx3]
1098
if idx._indices == new_indices:
1099
reload_counter[2] += 1
1101
reload_counter[1] += 1
1102
idx._indices[:] = new_indices
1104
idx = index.CombinedGraphIndex([idx1, idx2], reload_func=reload)
1105
trans = self.get_transport()
1106
for fname in missing:
1108
return idx, reload_counter
928
return GraphIndex(trans, name, size)
1110
930
def test_open_missing_index_no_error(self):
1111
931
trans = self.get_transport()
1112
idx1 = index.GraphIndex(trans, 'missing', 100)
1113
idx = index.CombinedGraphIndex([idx1])
932
index1 = GraphIndex(trans, 'missing', 100)
933
index = CombinedGraphIndex([index1])
1115
935
def test_add_index(self):
1116
idx = index.CombinedGraphIndex([])
1117
idx1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1118
idx.insert_index(0, idx1)
1119
self.assertEqual([(idx1, ('key', ), '')],
1120
list(idx.iter_all_entries()))
1122
def test_clear_cache(self):
1125
class ClearCacheProxy(object):
1127
def __init__(self, index):
1130
def __getattr__(self, name):
1131
return getattr(self._index)
1133
def clear_cache(self):
1134
log.append(self._index)
1135
return self._index.clear_cache()
1137
idx = index.CombinedGraphIndex([])
1138
idx1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1139
idx.insert_index(0, ClearCacheProxy(idx1))
1140
idx2 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1141
idx.insert_index(1, ClearCacheProxy(idx2))
1142
# CombinedGraphIndex should call 'clear_cache()' on all children
1144
self.assertEqual(sorted([idx1, idx2]), sorted(log))
936
index = CombinedGraphIndex([])
937
index1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
938
index.insert_index(0, index1)
939
self.assertEqual([(index1, ('key', ), '')], list(index.iter_all_entries()))
1146
941
def test_iter_all_entries_empty(self):
1147
idx = index.CombinedGraphIndex([])
1148
self.assertEqual([], list(idx.iter_all_entries()))
942
index = CombinedGraphIndex([])
943
self.assertEqual([], list(index.iter_all_entries()))
1150
945
def test_iter_all_entries_children_empty(self):
1151
idx1 = self.make_index('name')
1152
idx = index.CombinedGraphIndex([idx1])
1153
self.assertEqual([], list(idx.iter_all_entries()))
946
index1 = self.make_index('name')
947
index = CombinedGraphIndex([index1])
948
self.assertEqual([], list(index.iter_all_entries()))
1155
950
def test_iter_all_entries_simple(self):
1156
idx1 = self.make_index('name', nodes=[(('name', ), 'data', ())])
1157
idx = index.CombinedGraphIndex([idx1])
1158
self.assertEqual([(idx1, ('name', ), 'data')],
1159
list(idx.iter_all_entries()))
951
index1 = self.make_index('name', nodes=[(('name', ), 'data', ())])
952
index = CombinedGraphIndex([index1])
953
self.assertEqual([(index1, ('name', ), 'data')],
954
list(index.iter_all_entries()))
1161
956
def test_iter_all_entries_two_indices(self):
1162
idx1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1163
idx2 = self.make_index('name2', nodes=[(('2', ), '', ())])
1164
idx = index.CombinedGraphIndex([idx1, idx2])
1165
self.assertEqual([(idx1, ('name', ), 'data'),
1166
(idx2, ('2', ), '')],
1167
list(idx.iter_all_entries()))
957
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
958
index2 = self.make_index('name2', nodes=[(('2', ), '', ())])
959
index = CombinedGraphIndex([index1, index2])
960
self.assertEqual([(index1, ('name', ), 'data'),
961
(index2, ('2', ), '')],
962
list(index.iter_all_entries()))
1169
964
def test_iter_entries_two_indices_dup_key(self):
1170
idx1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1171
idx2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1172
idx = index.CombinedGraphIndex([idx1, idx2])
1173
self.assertEqual([(idx1, ('name', ), 'data')],
1174
list(idx.iter_entries([('name', )])))
965
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
966
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
967
index = CombinedGraphIndex([index1, index2])
968
self.assertEqual([(index1, ('name', ), 'data')],
969
list(index.iter_entries([('name', )])))
1176
971
def test_iter_all_entries_two_indices_dup_key(self):
1177
idx1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1178
idx2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1179
idx = index.CombinedGraphIndex([idx1, idx2])
1180
self.assertEqual([(idx1, ('name', ), 'data')],
1181
list(idx.iter_all_entries()))
972
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
973
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
974
index = CombinedGraphIndex([index1, index2])
975
self.assertEqual([(index1, ('name', ), 'data')],
976
list(index.iter_all_entries()))
1183
978
def test_iter_key_prefix_2_key_element_refs(self):
1184
idx1 = self.make_index('1', 1, key_elements=2, nodes=[
1185
(('name', 'fin1'), 'data', ([('ref', 'erence')], ))])
1186
idx2 = self.make_index('2', 1, key_elements=2, nodes=[
1187
(('name', 'fin2'), 'beta', ([], )),
1188
(('ref', 'erence'), 'refdata', ([], ))])
1189
idx = index.CombinedGraphIndex([idx1, idx2])
1190
self.assertEqual(set([(idx1, ('name', 'fin1'), 'data',
1191
((('ref', 'erence'),),)),
1192
(idx2, ('ref', 'erence'), 'refdata', ((), ))]),
1193
set(idx.iter_entries_prefix([('name', 'fin1'),
1194
('ref', 'erence')])))
1195
self.assertEqual(set([(idx1, ('name', 'fin1'), 'data',
1196
((('ref', 'erence'),),)),
1197
(idx2, ('name', 'fin2'), 'beta', ((), ))]),
1198
set(idx.iter_entries_prefix([('name', None)])))
979
index1 = self.make_index('1', 1, key_elements=2, nodes=[
980
(('name', 'fin1'), 'data', ([('ref', 'erence')], ))])
981
index2 = self.make_index('2', 1, key_elements=2, nodes=[
982
(('name', 'fin2'), 'beta', ([], )),
983
(('ref', 'erence'), 'refdata', ([], ))])
984
index = CombinedGraphIndex([index1, index2])
985
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
986
(index2, ('ref', 'erence'), 'refdata', ((), ))]),
987
set(index.iter_entries_prefix([('name', 'fin1'), ('ref', 'erence')])))
988
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
989
(index2, ('name', 'fin2'), 'beta', ((), ))]),
990
set(index.iter_entries_prefix([('name', None)])))
1200
992
def test_iter_nothing_empty(self):
1201
idx = index.CombinedGraphIndex([])
1202
self.assertEqual([], list(idx.iter_entries([])))
993
index = CombinedGraphIndex([])
994
self.assertEqual([], list(index.iter_entries([])))
1204
996
def test_iter_nothing_children_empty(self):
1205
idx1 = self.make_index('name')
1206
idx = index.CombinedGraphIndex([idx1])
1207
self.assertEqual([], list(idx.iter_entries([])))
997
index1 = self.make_index('name')
998
index = CombinedGraphIndex([index1])
999
self.assertEqual([], list(index.iter_entries([])))
1209
1001
def test_iter_all_keys(self):
1210
idx1 = self.make_index('1', 1, nodes=[(('name', ), 'data',
1212
idx2 = self.make_index('2', 1, nodes=[(('ref', ), 'refdata', ((), ))])
1213
idx = index.CombinedGraphIndex([idx1, idx2])
1214
self.assertEqual(set([(idx1, ('name', ), 'data', ((('ref', ), ), )),
1215
(idx2, ('ref', ), 'refdata', ((), ))]),
1216
set(idx.iter_entries([('name', ), ('ref', )])))
1002
index1 = self.make_index('1', 1, nodes=[
1003
(('name', ), 'data', ([('ref', )], ))])
1004
index2 = self.make_index('2', 1, nodes=[
1005
(('ref', ), 'refdata', ((), ))])
1006
index = CombinedGraphIndex([index1, index2])
1007
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref', ), ), )),
1008
(index2, ('ref', ), 'refdata', ((), ))]),
1009
set(index.iter_entries([('name', ), ('ref', )])))
1218
1011
def test_iter_all_keys_dup_entry(self):
1219
idx1 = self.make_index('1', 1, nodes=[(('name', ), 'data',
1221
(('ref', ), 'refdata', ([], ))])
1222
idx2 = self.make_index('2', 1, nodes=[(('ref', ), 'refdata', ([], ))])
1223
idx = index.CombinedGraphIndex([idx1, idx2])
1224
self.assertEqual(set([(idx1, ('name', ), 'data', ((('ref',),),)),
1225
(idx1, ('ref', ), 'refdata', ((), ))]),
1226
set(idx.iter_entries([('name', ), ('ref', )])))
1012
index1 = self.make_index('1', 1, nodes=[
1013
(('name', ), 'data', ([('ref', )], )),
1014
(('ref', ), 'refdata', ([], ))])
1015
index2 = self.make_index('2', 1, nodes=[
1016
(('ref', ), 'refdata', ([], ))])
1017
index = CombinedGraphIndex([index1, index2])
1018
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref',),),)),
1019
(index1, ('ref', ), 'refdata', ((), ))]),
1020
set(index.iter_entries([('name', ), ('ref', )])))
1228
1022
def test_iter_missing_entry_empty(self):
1229
idx = index.CombinedGraphIndex([])
1230
self.assertEqual([], list(idx.iter_entries([('a', )])))
1023
index = CombinedGraphIndex([])
1024
self.assertEqual([], list(index.iter_entries([('a', )])))
1232
1026
def test_iter_missing_entry_one_index(self):
1233
idx1 = self.make_index('1')
1234
idx = index.CombinedGraphIndex([idx1])
1235
self.assertEqual([], list(idx.iter_entries([('a', )])))
1027
index1 = self.make_index('1')
1028
index = CombinedGraphIndex([index1])
1029
self.assertEqual([], list(index.iter_entries([('a', )])))
1237
1031
def test_iter_missing_entry_two_index(self):
1238
idx1 = self.make_index('1')
1239
idx2 = self.make_index('2')
1240
idx = index.CombinedGraphIndex([idx1, idx2])
1241
self.assertEqual([], list(idx.iter_entries([('a', )])))
1032
index1 = self.make_index('1')
1033
index2 = self.make_index('2')
1034
index = CombinedGraphIndex([index1, index2])
1035
self.assertEqual([], list(index.iter_entries([('a', )])))
1243
1037
def test_iter_entry_present_one_index_only(self):
1244
idx1 = self.make_index('1', nodes=[(('key', ), '', ())])
1245
idx2 = self.make_index('2', nodes=[])
1246
idx = index.CombinedGraphIndex([idx1, idx2])
1247
self.assertEqual([(idx1, ('key', ), '')],
1248
list(idx.iter_entries([('key', )])))
1038
index1 = self.make_index('1', nodes=[(('key', ), '', ())])
1039
index2 = self.make_index('2', nodes=[])
1040
index = CombinedGraphIndex([index1, index2])
1041
self.assertEqual([(index1, ('key', ), '')],
1042
list(index.iter_entries([('key', )])))
1249
1043
# and in the other direction
1250
idx = index.CombinedGraphIndex([idx2, idx1])
1251
self.assertEqual([(idx1, ('key', ), '')],
1252
list(idx.iter_entries([('key', )])))
1044
index = CombinedGraphIndex([index2, index1])
1045
self.assertEqual([(index1, ('key', ), '')],
1046
list(index.iter_entries([('key', )])))
1254
1048
def test_key_count_empty(self):
1255
idx1 = self.make_index('1', nodes=[])
1256
idx2 = self.make_index('2', nodes=[])
1257
idx = index.CombinedGraphIndex([idx1, idx2])
1258
self.assertEqual(0, idx.key_count())
1049
index1 = self.make_index('1', nodes=[])
1050
index2 = self.make_index('2', nodes=[])
1051
index = CombinedGraphIndex([index1, index2])
1052
self.assertEqual(0, index.key_count())
1260
1054
def test_key_count_sums_index_keys(self):
1261
idx1 = self.make_index('1', nodes=[
1055
index1 = self.make_index('1', nodes=[
1262
1056
(('1',), '', ()),
1263
1057
(('2',), '', ())])
1264
idx2 = self.make_index('2', nodes=[(('1',), '', ())])
1265
idx = index.CombinedGraphIndex([idx1, idx2])
1266
self.assertEqual(3, idx.key_count())
1058
index2 = self.make_index('2', nodes=[(('1',), '', ())])
1059
index = CombinedGraphIndex([index1, index2])
1060
self.assertEqual(3, index.key_count())
1268
1062
def test_validate_bad_child_index_errors(self):
1269
1063
trans = self.get_transport()
1270
1064
trans.put_bytes('name', "not an index\n")
1271
idx1 = index.GraphIndex(trans, 'name', 13)
1272
idx = index.CombinedGraphIndex([idx1])
1273
self.assertRaises(errors.BadIndexFormatSignature, idx.validate)
1065
index1 = GraphIndex(trans, 'name', 13)
1066
index = CombinedGraphIndex([index1])
1067
self.assertRaises(errors.BadIndexFormatSignature, index.validate)
1275
1069
def test_validate_empty(self):
1276
idx = index.CombinedGraphIndex([])
1279
def test_key_count_reloads(self):
1280
idx, reload_counter = self.make_combined_index_with_missing()
1281
self.assertEqual(2, idx.key_count())
1282
self.assertEqual([1, 1, 0], reload_counter)
1284
def test_key_count_no_reload(self):
1285
idx, reload_counter = self.make_combined_index_with_missing()
1286
idx._reload_func = None
1287
# Without a _reload_func we just raise the exception
1288
self.assertRaises(errors.NoSuchFile, idx.key_count)
1290
def test_key_count_reloads_and_fails(self):
1291
# We have deleted all underlying indexes, so we will try to reload, but
1292
# still fail. This is mostly to test we don't get stuck in an infinite
1293
# loop trying to reload
1294
idx, reload_counter = self.make_combined_index_with_missing(
1296
self.assertRaises(errors.NoSuchFile, idx.key_count)
1297
self.assertEqual([2, 1, 1], reload_counter)
1299
def test_iter_entries_reloads(self):
1300
index, reload_counter = self.make_combined_index_with_missing()
1301
result = list(index.iter_entries([('1',), ('2',), ('3',)]))
1302
index3 = index._indices[0]
1303
self.assertEqual([(index3, ('1',), ''), (index3, ('2',), '')],
1305
self.assertEqual([1, 1, 0], reload_counter)
1307
def test_iter_entries_reloads_midway(self):
1308
# The first index still looks present, so we get interrupted mid-way
1310
index, reload_counter = self.make_combined_index_with_missing(['2'])
1311
index1, index2 = index._indices
1312
result = list(index.iter_entries([('1',), ('2',), ('3',)]))
1313
index3 = index._indices[0]
1314
# We had already yielded '1', so we just go on to the next, we should
1315
# not yield '1' twice.
1316
self.assertEqual([(index1, ('1',), ''), (index3, ('2',), '')],
1318
self.assertEqual([1, 1, 0], reload_counter)
1320
def test_iter_entries_no_reload(self):
1321
index, reload_counter = self.make_combined_index_with_missing()
1322
index._reload_func = None
1323
# Without a _reload_func we just raise the exception
1324
self.assertListRaises(errors.NoSuchFile, index.iter_entries, [('3',)])
1326
def test_iter_entries_reloads_and_fails(self):
1327
index, reload_counter = self.make_combined_index_with_missing(
1329
self.assertListRaises(errors.NoSuchFile, index.iter_entries, [('3',)])
1330
self.assertEqual([2, 1, 1], reload_counter)
1332
def test_iter_all_entries_reloads(self):
1333
index, reload_counter = self.make_combined_index_with_missing()
1334
result = list(index.iter_all_entries())
1335
index3 = index._indices[0]
1336
self.assertEqual([(index3, ('1',), ''), (index3, ('2',), '')],
1338
self.assertEqual([1, 1, 0], reload_counter)
1340
def test_iter_all_entries_reloads_midway(self):
1341
index, reload_counter = self.make_combined_index_with_missing(['2'])
1342
index1, index2 = index._indices
1343
result = list(index.iter_all_entries())
1344
index3 = index._indices[0]
1345
# We had already yielded '1', so we just go on to the next, we should
1346
# not yield '1' twice.
1347
self.assertEqual([(index1, ('1',), ''), (index3, ('2',), '')],
1349
self.assertEqual([1, 1, 0], reload_counter)
1351
def test_iter_all_entries_no_reload(self):
1352
index, reload_counter = self.make_combined_index_with_missing()
1353
index._reload_func = None
1354
self.assertListRaises(errors.NoSuchFile, index.iter_all_entries)
1356
def test_iter_all_entries_reloads_and_fails(self):
1357
index, reload_counter = self.make_combined_index_with_missing(
1359
self.assertListRaises(errors.NoSuchFile, index.iter_all_entries)
1361
def test_iter_entries_prefix_reloads(self):
1362
index, reload_counter = self.make_combined_index_with_missing()
1363
result = list(index.iter_entries_prefix([('1',)]))
1364
index3 = index._indices[0]
1365
self.assertEqual([(index3, ('1',), '')], result)
1366
self.assertEqual([1, 1, 0], reload_counter)
1368
def test_iter_entries_prefix_reloads_midway(self):
1369
index, reload_counter = self.make_combined_index_with_missing(['2'])
1370
index1, index2 = index._indices
1371
result = list(index.iter_entries_prefix([('1',)]))
1372
index3 = index._indices[0]
1373
# We had already yielded '1', so we just go on to the next, we should
1374
# not yield '1' twice.
1375
self.assertEqual([(index1, ('1',), '')], result)
1376
self.assertEqual([1, 1, 0], reload_counter)
1378
def test_iter_entries_prefix_no_reload(self):
1379
index, reload_counter = self.make_combined_index_with_missing()
1380
index._reload_func = None
1381
self.assertListRaises(errors.NoSuchFile, index.iter_entries_prefix,
1384
def test_iter_entries_prefix_reloads_and_fails(self):
1385
index, reload_counter = self.make_combined_index_with_missing(
1387
self.assertListRaises(errors.NoSuchFile, index.iter_entries_prefix,
1391
def make_index_with_simple_nodes(self, name, num_nodes=1):
1392
"""Make an index named after 'name', with keys named after 'name' too.
1394
Nodes will have a value of '' and no references.
1397
(('index-%s-key-%s' % (name, n),), '', ())
1398
for n in range(1, num_nodes+1)]
1399
return self.make_index('index-%s' % name, 0, nodes=nodes)
1401
def test_reorder_after_iter_entries(self):
1402
# Four indices: [key1] in idx1, [key2,key3] in idx2, [] in idx3,
1404
idx = index.CombinedGraphIndex([])
1405
idx.insert_index(0, self.make_index_with_simple_nodes('1'), '1')
1406
idx.insert_index(1, self.make_index_with_simple_nodes('2'), '2')
1407
idx.insert_index(2, self.make_index_with_simple_nodes('3'), '3')
1408
idx.insert_index(3, self.make_index_with_simple_nodes('4'), '4')
1409
idx1, idx2, idx3, idx4 = idx._indices
1410
# Query a key from idx4 and idx2.
1411
self.assertLength(2, list(idx.iter_entries(
1412
[('index-4-key-1',), ('index-2-key-1',)])))
1413
# Now idx2 and idx4 should be moved to the front (and idx1 should
1414
# still be before idx3).
1415
self.assertEqual([idx2, idx4, idx1, idx3], idx._indices)
1416
self.assertEqual(['2', '4', '1', '3'], idx._index_names)
1418
def test_reorder_propagates_to_siblings(self):
1419
# Two CombinedGraphIndex objects, with the same number of indicies with
1421
cgi1 = index.CombinedGraphIndex([])
1422
cgi2 = index.CombinedGraphIndex([])
1423
cgi1.insert_index(0, self.make_index_with_simple_nodes('1-1'), 'one')
1424
cgi1.insert_index(1, self.make_index_with_simple_nodes('1-2'), 'two')
1425
cgi2.insert_index(0, self.make_index_with_simple_nodes('2-1'), 'one')
1426
cgi2.insert_index(1, self.make_index_with_simple_nodes('2-2'), 'two')
1427
index2_1, index2_2 = cgi2._indices
1428
cgi1.set_sibling_indices([cgi2])
1429
# Trigger a reordering in cgi1. cgi2 will be reordered as well.
1430
list(cgi1.iter_entries([('index-1-2-key-1',)]))
1431
self.assertEqual([index2_2, index2_1], cgi2._indices)
1432
self.assertEqual(['two', 'one'], cgi2._index_names)
1434
def test_validate_reloads(self):
1435
idx, reload_counter = self.make_combined_index_with_missing()
1437
self.assertEqual([1, 1, 0], reload_counter)
1439
def test_validate_reloads_midway(self):
1440
idx, reload_counter = self.make_combined_index_with_missing(['2'])
1443
def test_validate_no_reload(self):
1444
idx, reload_counter = self.make_combined_index_with_missing()
1445
idx._reload_func = None
1446
self.assertRaises(errors.NoSuchFile, idx.validate)
1448
def test_validate_reloads_and_fails(self):
1449
idx, reload_counter = self.make_combined_index_with_missing(
1451
self.assertRaises(errors.NoSuchFile, idx.validate)
1453
def test_find_ancestors_across_indexes(self):
1458
index1 = self.make_index('12', ref_lists=1, nodes=[
1459
(key1, 'value', ([],)),
1460
(key2, 'value', ([key1],)),
1462
index2 = self.make_index('34', ref_lists=1, nodes=[
1463
(key3, 'value', ([key2],)),
1464
(key4, 'value', ([key3],)),
1466
c_index = index.CombinedGraphIndex([index1, index2])
1467
parent_map, missing_keys = c_index.find_ancestry([key1], 0)
1468
self.assertEqual({key1: ()}, parent_map)
1469
self.assertEqual(set(), missing_keys)
1470
# Now look for a key from index2 which requires us to find the key in
1471
# the second index, and then continue searching for parents in the
1473
parent_map, missing_keys = c_index.find_ancestry([key3], 0)
1474
self.assertEqual({key1: (), key2: (key1,), key3: (key2,)}, parent_map)
1475
self.assertEqual(set(), missing_keys)
1477
def test_find_ancestors_missing_keys(self):
1482
index1 = self.make_index('12', ref_lists=1, nodes=[
1483
(key1, 'value', ([],)),
1484
(key2, 'value', ([key1],)),
1486
index2 = self.make_index('34', ref_lists=1, nodes=[
1487
(key3, 'value', ([key2],)),
1489
c_index = index.CombinedGraphIndex([index1, index2])
1490
# Searching for a key which is actually not present at all should
1491
# eventually converge
1492
parent_map, missing_keys = c_index.find_ancestry([key4], 0)
1493
self.assertEqual({}, parent_map)
1494
self.assertEqual(set([key4]), missing_keys)
1496
def test_find_ancestors_no_indexes(self):
1497
c_index = index.CombinedGraphIndex([])
1499
parent_map, missing_keys = c_index.find_ancestry([key1], 0)
1500
self.assertEqual({}, parent_map)
1501
self.assertEqual(set([key1]), missing_keys)
1503
def test_find_ancestors_ghost_parent(self):
1508
index1 = self.make_index('12', ref_lists=1, nodes=[
1509
(key1, 'value', ([],)),
1510
(key2, 'value', ([key1],)),
1512
index2 = self.make_index('34', ref_lists=1, nodes=[
1513
(key4, 'value', ([key2, key3],)),
1515
c_index = index.CombinedGraphIndex([index1, index2])
1516
# Searching for a key which is actually not present at all should
1517
# eventually converge
1518
parent_map, missing_keys = c_index.find_ancestry([key4], 0)
1519
self.assertEqual({key4: (key2, key3), key2: (key1,), key1: ()},
1521
self.assertEqual(set([key3]), missing_keys)
1523
def test__find_ancestors_empty_index(self):
1524
idx = self.make_index('test', ref_lists=1, key_elements=1, nodes=[])
1526
missing_keys = set()
1527
search_keys = idx._find_ancestors([('one',), ('two',)], 0, parent_map,
1529
self.assertEqual(set(), search_keys)
1530
self.assertEqual({}, parent_map)
1531
self.assertEqual(set([('one',), ('two',)]), missing_keys)
1534
class TestInMemoryGraphIndex(tests.TestCaseWithMemoryTransport):
1070
index = CombinedGraphIndex([])
1074
class TestInMemoryGraphIndex(TestCaseWithMemoryTransport):
1536
1076
def make_index(self, ref_lists=0, key_elements=1, nodes=[]):
1537
result = index.InMemoryGraphIndex(ref_lists, key_elements=key_elements)
1077
result = InMemoryGraphIndex(ref_lists, key_elements=key_elements)
1538
1078
result.add_nodes(nodes)