969
802
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):
806
class TestCombinedGraphIndex(TestCaseWithMemoryTransport):
1068
808
def make_index(self, name, ref_lists=0, key_elements=1, nodes=[]):
1069
builder = index.GraphIndexBuilder(ref_lists, key_elements=key_elements)
809
builder = GraphIndexBuilder(ref_lists, key_elements=key_elements)
1070
810
for key, value, references in nodes:
1071
811
builder.add_node(key, value, references)
1072
812
stream = builder.finish()
1073
813
trans = self.get_transport()
1074
814
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
815
return GraphIndex(trans, name, size)
1110
817
def test_open_missing_index_no_error(self):
1111
818
trans = self.get_transport()
1112
idx1 = index.GraphIndex(trans, 'missing', 100)
1113
idx = index.CombinedGraphIndex([idx1])
819
index1 = GraphIndex(trans, 'missing', 100)
820
index = CombinedGraphIndex([index1])
1115
822
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))
823
index = CombinedGraphIndex([])
824
index1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
825
index.insert_index(0, index1)
826
self.assertEqual([(index1, ('key', ), '')], list(index.iter_all_entries()))
1146
828
def test_iter_all_entries_empty(self):
1147
idx = index.CombinedGraphIndex([])
1148
self.assertEqual([], list(idx.iter_all_entries()))
829
index = CombinedGraphIndex([])
830
self.assertEqual([], list(index.iter_all_entries()))
1150
832
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()))
833
index1 = self.make_index('name')
834
index = CombinedGraphIndex([index1])
835
self.assertEqual([], list(index.iter_all_entries()))
1155
837
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()))
838
index1 = self.make_index('name', nodes=[(('name', ), 'data', ())])
839
index = CombinedGraphIndex([index1])
840
self.assertEqual([(index1, ('name', ), 'data')],
841
list(index.iter_all_entries()))
1161
843
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()))
844
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
845
index2 = self.make_index('name2', nodes=[(('2', ), '', ())])
846
index = CombinedGraphIndex([index1, index2])
847
self.assertEqual([(index1, ('name', ), 'data'),
848
(index2, ('2', ), '')],
849
list(index.iter_all_entries()))
1169
851
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', )])))
852
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
853
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
854
index = CombinedGraphIndex([index1, index2])
855
self.assertEqual([(index1, ('name', ), 'data')],
856
list(index.iter_entries([('name', )])))
1176
858
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()))
859
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
860
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
861
index = CombinedGraphIndex([index1, index2])
862
self.assertEqual([(index1, ('name', ), 'data')],
863
list(index.iter_all_entries()))
1183
865
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)])))
866
index1 = self.make_index('1', 1, key_elements=2, nodes=[
867
(('name', 'fin1'), 'data', ([('ref', 'erence')], ))])
868
index2 = self.make_index('2', 1, key_elements=2, nodes=[
869
(('name', 'fin2'), 'beta', ([], )),
870
(('ref', 'erence'), 'refdata', ([], ))])
871
index = CombinedGraphIndex([index1, index2])
872
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
873
(index2, ('ref', 'erence'), 'refdata', ((), ))]),
874
set(index.iter_entries_prefix([('name', 'fin1'), ('ref', 'erence')])))
875
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
876
(index2, ('name', 'fin2'), 'beta', ((), ))]),
877
set(index.iter_entries_prefix([('name', None)])))
1200
879
def test_iter_nothing_empty(self):
1201
idx = index.CombinedGraphIndex([])
1202
self.assertEqual([], list(idx.iter_entries([])))
880
index = CombinedGraphIndex([])
881
self.assertEqual([], list(index.iter_entries([])))
1204
883
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([])))
884
index1 = self.make_index('name')
885
index = CombinedGraphIndex([index1])
886
self.assertEqual([], list(index.iter_entries([])))
1209
888
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', )])))
889
index1 = self.make_index('1', 1, nodes=[
890
(('name', ), 'data', ([('ref', )], ))])
891
index2 = self.make_index('2', 1, nodes=[
892
(('ref', ), 'refdata', ((), ))])
893
index = CombinedGraphIndex([index1, index2])
894
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref', ), ), )),
895
(index2, ('ref', ), 'refdata', ((), ))]),
896
set(index.iter_entries([('name', ), ('ref', )])))
1218
898
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', )])))
899
index1 = self.make_index('1', 1, nodes=[
900
(('name', ), 'data', ([('ref', )], )),
901
(('ref', ), 'refdata', ([], ))])
902
index2 = self.make_index('2', 1, nodes=[
903
(('ref', ), 'refdata', ([], ))])
904
index = CombinedGraphIndex([index1, index2])
905
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref',),),)),
906
(index1, ('ref', ), 'refdata', ((), ))]),
907
set(index.iter_entries([('name', ), ('ref', )])))
1228
909
def test_iter_missing_entry_empty(self):
1229
idx = index.CombinedGraphIndex([])
1230
self.assertEqual([], list(idx.iter_entries([('a', )])))
910
index = CombinedGraphIndex([])
911
self.assertEqual([], list(index.iter_entries([('a', )])))
1232
913
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', )])))
914
index1 = self.make_index('1')
915
index = CombinedGraphIndex([index1])
916
self.assertEqual([], list(index.iter_entries([('a', )])))
1237
918
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', )])))
919
index1 = self.make_index('1')
920
index2 = self.make_index('2')
921
index = CombinedGraphIndex([index1, index2])
922
self.assertEqual([], list(index.iter_entries([('a', )])))
1243
924
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', )])))
925
index1 = self.make_index('1', nodes=[(('key', ), '', ())])
926
index2 = self.make_index('2', nodes=[])
927
index = CombinedGraphIndex([index1, index2])
928
self.assertEqual([(index1, ('key', ), '')],
929
list(index.iter_entries([('key', )])))
1249
930
# and in the other direction
1250
idx = index.CombinedGraphIndex([idx2, idx1])
1251
self.assertEqual([(idx1, ('key', ), '')],
1252
list(idx.iter_entries([('key', )])))
931
index = CombinedGraphIndex([index2, index1])
932
self.assertEqual([(index1, ('key', ), '')],
933
list(index.iter_entries([('key', )])))
1254
935
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())
936
index1 = self.make_index('1', nodes=[])
937
index2 = self.make_index('2', nodes=[])
938
index = CombinedGraphIndex([index1, index2])
939
self.assertEqual(0, index.key_count())
1260
941
def test_key_count_sums_index_keys(self):
1261
idx1 = self.make_index('1', nodes=[
942
index1 = self.make_index('1', nodes=[
1262
943
(('1',), '', ()),
1263
944
(('2',), '', ())])
1264
idx2 = self.make_index('2', nodes=[(('1',), '', ())])
1265
idx = index.CombinedGraphIndex([idx1, idx2])
1266
self.assertEqual(3, idx.key_count())
945
index2 = self.make_index('2', nodes=[(('1',), '', ())])
946
index = CombinedGraphIndex([index1, index2])
947
self.assertEqual(3, index.key_count())
1268
949
def test_validate_bad_child_index_errors(self):
1269
950
trans = self.get_transport()
1270
951
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)
952
index1 = GraphIndex(trans, 'name', 13)
953
index = CombinedGraphIndex([index1])
954
self.assertRaises(errors.BadIndexFormatSignature, index.validate)
1275
956
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):
957
index = CombinedGraphIndex([])
961
class TestInMemoryGraphIndex(TestCaseWithMemoryTransport):
1536
963
def make_index(self, ref_lists=0, key_elements=1, nodes=[]):
1537
result = index.InMemoryGraphIndex(ref_lists, key_elements=key_elements)
964
result = InMemoryGraphIndex(ref_lists, key_elements=key_elements)
1538
965
result.add_nodes(nodes)