1097
1134
log.append(self._index)
1098
1135
return self._index.clear_cache()
1100
index = CombinedGraphIndex([])
1101
index1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1102
index.insert_index(0, ClearCacheProxy(index1))
1103
index2 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1104
index.insert_index(1, ClearCacheProxy(index2))
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))
1105
1142
# CombinedGraphIndex should call 'clear_cache()' on all children
1107
self.assertEqual(sorted([index1, index2]), sorted(log))
1144
self.assertEqual(sorted([idx1, idx2]), sorted(log))
1109
1146
def test_iter_all_entries_empty(self):
1110
index = CombinedGraphIndex([])
1111
self.assertEqual([], list(index.iter_all_entries()))
1147
idx = index.CombinedGraphIndex([])
1148
self.assertEqual([], list(idx.iter_all_entries()))
1113
1150
def test_iter_all_entries_children_empty(self):
1114
index1 = self.make_index('name')
1115
index = CombinedGraphIndex([index1])
1116
self.assertEqual([], list(index.iter_all_entries()))
1151
idx1 = self.make_index('name')
1152
idx = index.CombinedGraphIndex([idx1])
1153
self.assertEqual([], list(idx.iter_all_entries()))
1118
1155
def test_iter_all_entries_simple(self):
1119
index1 = self.make_index('name', nodes=[(('name', ), 'data', ())])
1120
index = CombinedGraphIndex([index1])
1121
self.assertEqual([(index1, ('name', ), 'data')],
1122
list(index.iter_all_entries()))
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()))
1124
1161
def test_iter_all_entries_two_indices(self):
1125
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1126
index2 = self.make_index('name2', nodes=[(('2', ), '', ())])
1127
index = CombinedGraphIndex([index1, index2])
1128
self.assertEqual([(index1, ('name', ), 'data'),
1129
(index2, ('2', ), '')],
1130
list(index.iter_all_entries()))
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()))
1132
1169
def test_iter_entries_two_indices_dup_key(self):
1133
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1134
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1135
index = CombinedGraphIndex([index1, index2])
1136
self.assertEqual([(index1, ('name', ), 'data')],
1137
list(index.iter_entries([('name', )])))
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', )])))
1139
1176
def test_iter_all_entries_two_indices_dup_key(self):
1140
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1141
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1142
index = CombinedGraphIndex([index1, index2])
1143
self.assertEqual([(index1, ('name', ), 'data')],
1144
list(index.iter_all_entries()))
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()))
1146
1183
def test_iter_key_prefix_2_key_element_refs(self):
1147
index1 = self.make_index('1', 1, key_elements=2, nodes=[
1148
(('name', 'fin1'), 'data', ([('ref', 'erence')], ))])
1149
index2 = self.make_index('2', 1, key_elements=2, nodes=[
1150
(('name', 'fin2'), 'beta', ([], )),
1151
(('ref', 'erence'), 'refdata', ([], ))])
1152
index = CombinedGraphIndex([index1, index2])
1153
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
1154
(index2, ('ref', 'erence'), 'refdata', ((), ))]),
1155
set(index.iter_entries_prefix([('name', 'fin1'), ('ref', 'erence')])))
1156
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
1157
(index2, ('name', 'fin2'), 'beta', ((), ))]),
1158
set(index.iter_entries_prefix([('name', None)])))
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)])))
1160
1200
def test_iter_nothing_empty(self):
1161
index = CombinedGraphIndex([])
1162
self.assertEqual([], list(index.iter_entries([])))
1201
idx = index.CombinedGraphIndex([])
1202
self.assertEqual([], list(idx.iter_entries([])))
1164
1204
def test_iter_nothing_children_empty(self):
1165
index1 = self.make_index('name')
1166
index = CombinedGraphIndex([index1])
1167
self.assertEqual([], list(index.iter_entries([])))
1205
idx1 = self.make_index('name')
1206
idx = index.CombinedGraphIndex([idx1])
1207
self.assertEqual([], list(idx.iter_entries([])))
1169
1209
def test_iter_all_keys(self):
1170
index1 = self.make_index('1', 1, nodes=[
1171
(('name', ), 'data', ([('ref', )], ))])
1172
index2 = self.make_index('2', 1, nodes=[
1173
(('ref', ), 'refdata', ((), ))])
1174
index = CombinedGraphIndex([index1, index2])
1175
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref', ), ), )),
1176
(index2, ('ref', ), 'refdata', ((), ))]),
1177
set(index.iter_entries([('name', ), ('ref', )])))
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', )])))
1179
1218
def test_iter_all_keys_dup_entry(self):
1180
index1 = self.make_index('1', 1, nodes=[
1181
(('name', ), 'data', ([('ref', )], )),
1182
(('ref', ), 'refdata', ([], ))])
1183
index2 = self.make_index('2', 1, nodes=[
1184
(('ref', ), 'refdata', ([], ))])
1185
index = CombinedGraphIndex([index1, index2])
1186
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref',),),)),
1187
(index1, ('ref', ), 'refdata', ((), ))]),
1188
set(index.iter_entries([('name', ), ('ref', )])))
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', )])))
1190
1228
def test_iter_missing_entry_empty(self):
1191
index = CombinedGraphIndex([])
1192
self.assertEqual([], list(index.iter_entries([('a', )])))
1229
idx = index.CombinedGraphIndex([])
1230
self.assertEqual([], list(idx.iter_entries([('a', )])))
1194
1232
def test_iter_missing_entry_one_index(self):
1195
index1 = self.make_index('1')
1196
index = CombinedGraphIndex([index1])
1197
self.assertEqual([], list(index.iter_entries([('a', )])))
1233
idx1 = self.make_index('1')
1234
idx = index.CombinedGraphIndex([idx1])
1235
self.assertEqual([], list(idx.iter_entries([('a', )])))
1199
1237
def test_iter_missing_entry_two_index(self):
1200
index1 = self.make_index('1')
1201
index2 = self.make_index('2')
1202
index = CombinedGraphIndex([index1, index2])
1203
self.assertEqual([], list(index.iter_entries([('a', )])))
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', )])))
1205
1243
def test_iter_entry_present_one_index_only(self):
1206
index1 = self.make_index('1', nodes=[(('key', ), '', ())])
1207
index2 = self.make_index('2', nodes=[])
1208
index = CombinedGraphIndex([index1, index2])
1209
self.assertEqual([(index1, ('key', ), '')],
1210
list(index.iter_entries([('key', )])))
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', )])))
1211
1249
# and in the other direction
1212
index = CombinedGraphIndex([index2, index1])
1213
self.assertEqual([(index1, ('key', ), '')],
1214
list(index.iter_entries([('key', )])))
1250
idx = index.CombinedGraphIndex([idx2, idx1])
1251
self.assertEqual([(idx1, ('key', ), '')],
1252
list(idx.iter_entries([('key', )])))
1216
1254
def test_key_count_empty(self):
1217
index1 = self.make_index('1', nodes=[])
1218
index2 = self.make_index('2', nodes=[])
1219
index = CombinedGraphIndex([index1, index2])
1220
self.assertEqual(0, index.key_count())
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())
1222
1260
def test_key_count_sums_index_keys(self):
1223
index1 = self.make_index('1', nodes=[
1261
idx1 = self.make_index('1', nodes=[
1224
1262
(('1',), '', ()),
1225
1263
(('2',), '', ())])
1226
index2 = self.make_index('2', nodes=[(('1',), '', ())])
1227
index = CombinedGraphIndex([index1, index2])
1228
self.assertEqual(3, index.key_count())
1264
idx2 = self.make_index('2', nodes=[(('1',), '', ())])
1265
idx = index.CombinedGraphIndex([idx1, idx2])
1266
self.assertEqual(3, idx.key_count())
1230
1268
def test_validate_bad_child_index_errors(self):
1231
1269
trans = self.get_transport()
1232
1270
trans.put_bytes('name', "not an index\n")
1233
index1 = GraphIndex(trans, 'name', 13)
1234
index = CombinedGraphIndex([index1])
1235
self.assertRaises(errors.BadIndexFormatSignature, index.validate)
1271
idx1 = index.GraphIndex(trans, 'name', 13)
1272
idx = index.CombinedGraphIndex([idx1])
1273
self.assertRaises(errors.BadIndexFormatSignature, idx.validate)
1237
1275
def test_validate_empty(self):
1238
index = CombinedGraphIndex([])
1276
idx = index.CombinedGraphIndex([])
1241
1279
def test_key_count_reloads(self):
1242
index, reload_counter = self.make_combined_index_with_missing()
1243
self.assertEqual(2, index.key_count())
1280
idx, reload_counter = self.make_combined_index_with_missing()
1281
self.assertEqual(2, idx.key_count())
1244
1282
self.assertEqual([1, 1, 0], reload_counter)
1246
1284
def test_key_count_no_reload(self):
1247
index, reload_counter = self.make_combined_index_with_missing()
1248
index._reload_func = None
1285
idx, reload_counter = self.make_combined_index_with_missing()
1286
idx._reload_func = None
1249
1287
# Without a _reload_func we just raise the exception
1250
self.assertRaises(errors.NoSuchFile, index.key_count)
1288
self.assertRaises(errors.NoSuchFile, idx.key_count)
1252
1290
def test_key_count_reloads_and_fails(self):
1253
1291
# We have deleted all underlying indexes, so we will try to reload, but
1254
1292
# still fail. This is mostly to test we don't get stuck in an infinite
1255
1293
# loop trying to reload
1256
index, reload_counter = self.make_combined_index_with_missing(
1258
self.assertRaises(errors.NoSuchFile, index.key_count)
1294
idx, reload_counter = self.make_combined_index_with_missing(
1296
self.assertRaises(errors.NoSuchFile, idx.key_count)
1259
1297
self.assertEqual([2, 1, 1], reload_counter)
1261
1299
def test_iter_entries_reloads(self):
1349
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)
1352
1434
def test_validate_reloads(self):
1353
index, reload_counter = self.make_combined_index_with_missing()
1435
idx, reload_counter = self.make_combined_index_with_missing()
1355
1437
self.assertEqual([1, 1, 0], reload_counter)
1357
1439
def test_validate_reloads_midway(self):
1358
index, reload_counter = self.make_combined_index_with_missing(['2'])
1440
idx, reload_counter = self.make_combined_index_with_missing(['2'])
1361
1443
def test_validate_no_reload(self):
1362
index, reload_counter = self.make_combined_index_with_missing()
1363
index._reload_func = None
1364
self.assertRaises(errors.NoSuchFile, index.validate)
1444
idx, reload_counter = self.make_combined_index_with_missing()
1445
idx._reload_func = None
1446
self.assertRaises(errors.NoSuchFile, idx.validate)
1366
1448
def test_validate_reloads_and_fails(self):
1367
index, reload_counter = self.make_combined_index_with_missing(
1369
self.assertRaises(errors.NoSuchFile, index.validate)
1449
idx, reload_counter = self.make_combined_index_with_missing(
1451
self.assertRaises(errors.NoSuchFile, idx.validate)
1371
1453
def test_find_ancestors_across_indexes(self):
1372
1454
key1 = ('key-1',)