1134
1128
log.append(self._index)
1135
1129
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))
1131
index = CombinedGraphIndex([])
1132
index1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1133
index.insert_index(0, ClearCacheProxy(index1))
1134
index2 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1135
index.insert_index(1, ClearCacheProxy(index2))
1142
1136
# CombinedGraphIndex should call 'clear_cache()' on all children
1144
self.assertEqual(sorted([idx1, idx2]), sorted(log))
1138
self.assertEqual(sorted([index1, index2]), sorted(log))
1146
1140
def test_iter_all_entries_empty(self):
1147
idx = index.CombinedGraphIndex([])
1148
self.assertEqual([], list(idx.iter_all_entries()))
1141
index = CombinedGraphIndex([])
1142
self.assertEqual([], list(index.iter_all_entries()))
1150
1144
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()))
1145
index1 = self.make_index('name')
1146
index = CombinedGraphIndex([index1])
1147
self.assertEqual([], list(index.iter_all_entries()))
1155
1149
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()))
1150
index1 = self.make_index('name', nodes=[(('name', ), 'data', ())])
1151
index = CombinedGraphIndex([index1])
1152
self.assertEqual([(index1, ('name', ), 'data')],
1153
list(index.iter_all_entries()))
1161
1155
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()))
1156
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1157
index2 = self.make_index('name2', nodes=[(('2', ), '', ())])
1158
index = CombinedGraphIndex([index1, index2])
1159
self.assertEqual([(index1, ('name', ), 'data'),
1160
(index2, ('2', ), '')],
1161
list(index.iter_all_entries()))
1169
1163
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', )])))
1164
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1165
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1166
index = CombinedGraphIndex([index1, index2])
1167
self.assertEqual([(index1, ('name', ), 'data')],
1168
list(index.iter_entries([('name', )])))
1176
1170
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()))
1171
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1172
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1173
index = CombinedGraphIndex([index1, index2])
1174
self.assertEqual([(index1, ('name', ), 'data')],
1175
list(index.iter_all_entries()))
1183
1177
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)])))
1178
index1 = self.make_index('1', 1, key_elements=2, nodes=[
1179
(('name', 'fin1'), 'data', ([('ref', 'erence')], ))])
1180
index2 = self.make_index('2', 1, key_elements=2, nodes=[
1181
(('name', 'fin2'), 'beta', ([], )),
1182
(('ref', 'erence'), 'refdata', ([], ))])
1183
index = CombinedGraphIndex([index1, index2])
1184
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
1185
(index2, ('ref', 'erence'), 'refdata', ((), ))]),
1186
set(index.iter_entries_prefix([('name', 'fin1'), ('ref', 'erence')])))
1187
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
1188
(index2, ('name', 'fin2'), 'beta', ((), ))]),
1189
set(index.iter_entries_prefix([('name', None)])))
1200
1191
def test_iter_nothing_empty(self):
1201
idx = index.CombinedGraphIndex([])
1202
self.assertEqual([], list(idx.iter_entries([])))
1192
index = CombinedGraphIndex([])
1193
self.assertEqual([], list(index.iter_entries([])))
1204
1195
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([])))
1196
index1 = self.make_index('name')
1197
index = CombinedGraphIndex([index1])
1198
self.assertEqual([], list(index.iter_entries([])))
1209
1200
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', )])))
1201
index1 = self.make_index('1', 1, nodes=[
1202
(('name', ), 'data', ([('ref', )], ))])
1203
index2 = self.make_index('2', 1, nodes=[
1204
(('ref', ), 'refdata', ((), ))])
1205
index = CombinedGraphIndex([index1, index2])
1206
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref', ), ), )),
1207
(index2, ('ref', ), 'refdata', ((), ))]),
1208
set(index.iter_entries([('name', ), ('ref', )])))
1218
1210
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', )])))
1211
index1 = self.make_index('1', 1, nodes=[
1212
(('name', ), 'data', ([('ref', )], )),
1213
(('ref', ), 'refdata', ([], ))])
1214
index2 = self.make_index('2', 1, nodes=[
1215
(('ref', ), 'refdata', ([], ))])
1216
index = CombinedGraphIndex([index1, index2])
1217
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref',),),)),
1218
(index1, ('ref', ), 'refdata', ((), ))]),
1219
set(index.iter_entries([('name', ), ('ref', )])))
1228
1221
def test_iter_missing_entry_empty(self):
1229
idx = index.CombinedGraphIndex([])
1230
self.assertEqual([], list(idx.iter_entries([('a', )])))
1222
index = CombinedGraphIndex([])
1223
self.assertEqual([], list(index.iter_entries([('a', )])))
1232
1225
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', )])))
1226
index1 = self.make_index('1')
1227
index = CombinedGraphIndex([index1])
1228
self.assertEqual([], list(index.iter_entries([('a', )])))
1237
1230
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', )])))
1231
index1 = self.make_index('1')
1232
index2 = self.make_index('2')
1233
index = CombinedGraphIndex([index1, index2])
1234
self.assertEqual([], list(index.iter_entries([('a', )])))
1243
1236
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', )])))
1237
index1 = self.make_index('1', nodes=[(('key', ), '', ())])
1238
index2 = self.make_index('2', nodes=[])
1239
index = CombinedGraphIndex([index1, index2])
1240
self.assertEqual([(index1, ('key', ), '')],
1241
list(index.iter_entries([('key', )])))
1249
1242
# and in the other direction
1250
idx = index.CombinedGraphIndex([idx2, idx1])
1251
self.assertEqual([(idx1, ('key', ), '')],
1252
list(idx.iter_entries([('key', )])))
1243
index = CombinedGraphIndex([index2, index1])
1244
self.assertEqual([(index1, ('key', ), '')],
1245
list(index.iter_entries([('key', )])))
1254
1247
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())
1248
index1 = self.make_index('1', nodes=[])
1249
index2 = self.make_index('2', nodes=[])
1250
index = CombinedGraphIndex([index1, index2])
1251
self.assertEqual(0, index.key_count())
1260
1253
def test_key_count_sums_index_keys(self):
1261
idx1 = self.make_index('1', nodes=[
1254
index1 = self.make_index('1', nodes=[
1262
1255
(('1',), '', ()),
1263
1256
(('2',), '', ())])
1264
idx2 = self.make_index('2', nodes=[(('1',), '', ())])
1265
idx = index.CombinedGraphIndex([idx1, idx2])
1266
self.assertEqual(3, idx.key_count())
1257
index2 = self.make_index('2', nodes=[(('1',), '', ())])
1258
index = CombinedGraphIndex([index1, index2])
1259
self.assertEqual(3, index.key_count())
1268
1261
def test_validate_bad_child_index_errors(self):
1269
1262
trans = self.get_transport()
1270
1263
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)
1264
index1 = GraphIndex(trans, 'name', 13)
1265
index = CombinedGraphIndex([index1])
1266
self.assertRaises(errors.BadIndexFormatSignature, index.validate)
1275
1268
def test_validate_empty(self):
1276
idx = index.CombinedGraphIndex([])
1269
index = CombinedGraphIndex([])
1279
1272
def test_key_count_reloads(self):
1280
idx, reload_counter = self.make_combined_index_with_missing()
1281
self.assertEqual(2, idx.key_count())
1273
index, reload_counter = self.make_combined_index_with_missing()
1274
self.assertEqual(2, index.key_count())
1282
1275
self.assertEqual([1, 1, 0], reload_counter)
1284
1277
def test_key_count_no_reload(self):
1285
idx, reload_counter = self.make_combined_index_with_missing()
1286
idx._reload_func = None
1278
index, reload_counter = self.make_combined_index_with_missing()
1279
index._reload_func = None
1287
1280
# Without a _reload_func we just raise the exception
1288
self.assertRaises(errors.NoSuchFile, idx.key_count)
1281
self.assertRaises(errors.NoSuchFile, index.key_count)
1290
1283
def test_key_count_reloads_and_fails(self):
1291
1284
# We have deleted all underlying indexes, so we will try to reload, but
1292
1285
# still fail. This is mostly to test we don't get stuck in an infinite
1293
1286
# loop trying to reload
1294
idx, reload_counter = self.make_combined_index_with_missing(
1296
self.assertRaises(errors.NoSuchFile, idx.key_count)
1287
index, reload_counter = self.make_combined_index_with_missing(
1289
self.assertRaises(errors.NoSuchFile, index.key_count)
1297
1290
self.assertEqual([2, 1, 1], reload_counter)
1299
1292
def test_iter_entries_reloads(self):