1134
1097
log.append(self._index)
1135
1098
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))
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))
1142
1105
# CombinedGraphIndex should call 'clear_cache()' on all children
1144
self.assertEqual(sorted([idx1, idx2]), sorted(log))
1107
self.assertEqual(sorted([index1, index2]), sorted(log))
1146
1109
def test_iter_all_entries_empty(self):
1147
idx = index.CombinedGraphIndex([])
1148
self.assertEqual([], list(idx.iter_all_entries()))
1110
index = CombinedGraphIndex([])
1111
self.assertEqual([], list(index.iter_all_entries()))
1150
1113
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()))
1114
index1 = self.make_index('name')
1115
index = CombinedGraphIndex([index1])
1116
self.assertEqual([], list(index.iter_all_entries()))
1155
1118
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()))
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()))
1161
1124
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()))
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()))
1169
1132
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', )])))
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', )])))
1176
1139
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()))
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()))
1183
1146
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)])))
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)])))
1200
1160
def test_iter_nothing_empty(self):
1201
idx = index.CombinedGraphIndex([])
1202
self.assertEqual([], list(idx.iter_entries([])))
1161
index = CombinedGraphIndex([])
1162
self.assertEqual([], list(index.iter_entries([])))
1204
1164
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([])))
1165
index1 = self.make_index('name')
1166
index = CombinedGraphIndex([index1])
1167
self.assertEqual([], list(index.iter_entries([])))
1209
1169
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', )])))
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', )])))
1218
1179
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', )])))
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', )])))
1228
1190
def test_iter_missing_entry_empty(self):
1229
idx = index.CombinedGraphIndex([])
1230
self.assertEqual([], list(idx.iter_entries([('a', )])))
1191
index = CombinedGraphIndex([])
1192
self.assertEqual([], list(index.iter_entries([('a', )])))
1232
1194
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', )])))
1195
index1 = self.make_index('1')
1196
index = CombinedGraphIndex([index1])
1197
self.assertEqual([], list(index.iter_entries([('a', )])))
1237
1199
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', )])))
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', )])))
1243
1205
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', )])))
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', )])))
1249
1211
# and in the other direction
1250
idx = index.CombinedGraphIndex([idx2, idx1])
1251
self.assertEqual([(idx1, ('key', ), '')],
1252
list(idx.iter_entries([('key', )])))
1212
index = CombinedGraphIndex([index2, index1])
1213
self.assertEqual([(index1, ('key', ), '')],
1214
list(index.iter_entries([('key', )])))
1254
1216
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())
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())
1260
1222
def test_key_count_sums_index_keys(self):
1261
idx1 = self.make_index('1', nodes=[
1223
index1 = self.make_index('1', nodes=[
1262
1224
(('1',), '', ()),
1263
1225
(('2',), '', ())])
1264
idx2 = self.make_index('2', nodes=[(('1',), '', ())])
1265
idx = index.CombinedGraphIndex([idx1, idx2])
1266
self.assertEqual(3, idx.key_count())
1226
index2 = self.make_index('2', nodes=[(('1',), '', ())])
1227
index = CombinedGraphIndex([index1, index2])
1228
self.assertEqual(3, index.key_count())
1268
1230
def test_validate_bad_child_index_errors(self):
1269
1231
trans = self.get_transport()
1270
1232
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)
1233
index1 = GraphIndex(trans, 'name', 13)
1234
index = CombinedGraphIndex([index1])
1235
self.assertRaises(errors.BadIndexFormatSignature, index.validate)
1275
1237
def test_validate_empty(self):
1276
idx = index.CombinedGraphIndex([])
1238
index = CombinedGraphIndex([])
1279
1241
def test_key_count_reloads(self):
1280
idx, reload_counter = self.make_combined_index_with_missing()
1281
self.assertEqual(2, idx.key_count())
1242
index, reload_counter = self.make_combined_index_with_missing()
1243
self.assertEqual(2, index.key_count())
1282
1244
self.assertEqual([1, 1, 0], reload_counter)
1284
1246
def test_key_count_no_reload(self):
1285
idx, reload_counter = self.make_combined_index_with_missing()
1286
idx._reload_func = None
1247
index, reload_counter = self.make_combined_index_with_missing()
1248
index._reload_func = None
1287
1249
# Without a _reload_func we just raise the exception
1288
self.assertRaises(errors.NoSuchFile, idx.key_count)
1250
self.assertRaises(errors.NoSuchFile, index.key_count)
1290
1252
def test_key_count_reloads_and_fails(self):
1291
1253
# We have deleted all underlying indexes, so we will try to reload, but
1292
1254
# still fail. This is mostly to test we don't get stuck in an infinite
1293
1255
# loop trying to reload
1294
idx, reload_counter = self.make_combined_index_with_missing(
1296
self.assertRaises(errors.NoSuchFile, idx.key_count)
1256
index, reload_counter = self.make_combined_index_with_missing(
1258
self.assertRaises(errors.NoSuchFile, index.key_count)
1297
1259
self.assertEqual([2, 1, 1], reload_counter)
1299
1261
def test_iter_entries_reloads(self):
1387
1349
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
1352
def test_validate_reloads(self):
1435
idx, reload_counter = self.make_combined_index_with_missing()
1353
index, reload_counter = self.make_combined_index_with_missing()
1437
1355
self.assertEqual([1, 1, 0], reload_counter)
1439
1357
def test_validate_reloads_midway(self):
1440
idx, reload_counter = self.make_combined_index_with_missing(['2'])
1358
index, reload_counter = self.make_combined_index_with_missing(['2'])
1443
1361
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)
1362
index, reload_counter = self.make_combined_index_with_missing()
1363
index._reload_func = None
1364
self.assertRaises(errors.NoSuchFile, index.validate)
1448
1366
def test_validate_reloads_and_fails(self):
1449
idx, reload_counter = self.make_combined_index_with_missing(
1451
self.assertRaises(errors.NoSuchFile, idx.validate)
1367
index, reload_counter = self.make_combined_index_with_missing(
1369
self.assertRaises(errors.NoSuchFile, index.validate)
1453
1371
def test_find_ancestors_across_indexes(self):
1454
1372
key1 = ('key-1',)