1094
1047
reload_counter = [0, 0, 0]
1096
1049
reload_counter[0] += 1
1097
new_indices = [idx3]
1098
if idx._indices == new_indices:
1050
new_indices = [index3]
1051
if index._indices == new_indices:
1099
1052
reload_counter[2] += 1
1101
1054
reload_counter[1] += 1
1102
idx._indices[:] = new_indices
1055
index._indices[:] = new_indices
1104
idx = index.CombinedGraphIndex([idx1, idx2], reload_func=reload)
1057
index = CombinedGraphIndex([index1, index2], reload_func=reload)
1105
1058
trans = self.get_transport()
1106
1059
for fname in missing:
1107
1060
trans.delete(fname)
1108
return idx, reload_counter
1061
return index, reload_counter
1110
1063
def test_open_missing_index_no_error(self):
1111
1064
trans = self.get_transport()
1112
idx1 = index.GraphIndex(trans, 'missing', 100)
1113
idx = index.CombinedGraphIndex([idx1])
1065
index1 = GraphIndex(trans, 'missing', 100)
1066
index = CombinedGraphIndex([index1])
1115
1068
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))
1069
index = CombinedGraphIndex([])
1070
index1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1071
index.insert_index(0, index1)
1072
self.assertEqual([(index1, ('key', ), '')], list(index.iter_all_entries()))
1146
1074
def test_iter_all_entries_empty(self):
1147
idx = index.CombinedGraphIndex([])
1148
self.assertEqual([], list(idx.iter_all_entries()))
1075
index = CombinedGraphIndex([])
1076
self.assertEqual([], list(index.iter_all_entries()))
1150
1078
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()))
1079
index1 = self.make_index('name')
1080
index = CombinedGraphIndex([index1])
1081
self.assertEqual([], list(index.iter_all_entries()))
1155
1083
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()))
1084
index1 = self.make_index('name', nodes=[(('name', ), 'data', ())])
1085
index = CombinedGraphIndex([index1])
1086
self.assertEqual([(index1, ('name', ), 'data')],
1087
list(index.iter_all_entries()))
1161
1089
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()))
1090
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1091
index2 = self.make_index('name2', nodes=[(('2', ), '', ())])
1092
index = CombinedGraphIndex([index1, index2])
1093
self.assertEqual([(index1, ('name', ), 'data'),
1094
(index2, ('2', ), '')],
1095
list(index.iter_all_entries()))
1169
1097
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', )])))
1098
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1099
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1100
index = CombinedGraphIndex([index1, index2])
1101
self.assertEqual([(index1, ('name', ), 'data')],
1102
list(index.iter_entries([('name', )])))
1176
1104
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()))
1105
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1106
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1107
index = CombinedGraphIndex([index1, index2])
1108
self.assertEqual([(index1, ('name', ), 'data')],
1109
list(index.iter_all_entries()))
1183
1111
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)])))
1112
index1 = self.make_index('1', 1, key_elements=2, nodes=[
1113
(('name', 'fin1'), 'data', ([('ref', 'erence')], ))])
1114
index2 = self.make_index('2', 1, key_elements=2, nodes=[
1115
(('name', 'fin2'), 'beta', ([], )),
1116
(('ref', 'erence'), 'refdata', ([], ))])
1117
index = CombinedGraphIndex([index1, index2])
1118
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
1119
(index2, ('ref', 'erence'), 'refdata', ((), ))]),
1120
set(index.iter_entries_prefix([('name', 'fin1'), ('ref', 'erence')])))
1121
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
1122
(index2, ('name', 'fin2'), 'beta', ((), ))]),
1123
set(index.iter_entries_prefix([('name', None)])))
1200
1125
def test_iter_nothing_empty(self):
1201
idx = index.CombinedGraphIndex([])
1202
self.assertEqual([], list(idx.iter_entries([])))
1126
index = CombinedGraphIndex([])
1127
self.assertEqual([], list(index.iter_entries([])))
1204
1129
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([])))
1130
index1 = self.make_index('name')
1131
index = CombinedGraphIndex([index1])
1132
self.assertEqual([], list(index.iter_entries([])))
1209
1134
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', )])))
1135
index1 = self.make_index('1', 1, nodes=[
1136
(('name', ), 'data', ([('ref', )], ))])
1137
index2 = self.make_index('2', 1, nodes=[
1138
(('ref', ), 'refdata', ((), ))])
1139
index = CombinedGraphIndex([index1, index2])
1140
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref', ), ), )),
1141
(index2, ('ref', ), 'refdata', ((), ))]),
1142
set(index.iter_entries([('name', ), ('ref', )])))
1218
1144
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', )])))
1145
index1 = self.make_index('1', 1, nodes=[
1146
(('name', ), 'data', ([('ref', )], )),
1147
(('ref', ), 'refdata', ([], ))])
1148
index2 = self.make_index('2', 1, nodes=[
1149
(('ref', ), 'refdata', ([], ))])
1150
index = CombinedGraphIndex([index1, index2])
1151
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref',),),)),
1152
(index1, ('ref', ), 'refdata', ((), ))]),
1153
set(index.iter_entries([('name', ), ('ref', )])))
1228
1155
def test_iter_missing_entry_empty(self):
1229
idx = index.CombinedGraphIndex([])
1230
self.assertEqual([], list(idx.iter_entries([('a', )])))
1156
index = CombinedGraphIndex([])
1157
self.assertEqual([], list(index.iter_entries([('a', )])))
1232
1159
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', )])))
1160
index1 = self.make_index('1')
1161
index = CombinedGraphIndex([index1])
1162
self.assertEqual([], list(index.iter_entries([('a', )])))
1237
1164
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', )])))
1165
index1 = self.make_index('1')
1166
index2 = self.make_index('2')
1167
index = CombinedGraphIndex([index1, index2])
1168
self.assertEqual([], list(index.iter_entries([('a', )])))
1243
1170
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', )])))
1171
index1 = self.make_index('1', nodes=[(('key', ), '', ())])
1172
index2 = self.make_index('2', nodes=[])
1173
index = CombinedGraphIndex([index1, index2])
1174
self.assertEqual([(index1, ('key', ), '')],
1175
list(index.iter_entries([('key', )])))
1249
1176
# and in the other direction
1250
idx = index.CombinedGraphIndex([idx2, idx1])
1251
self.assertEqual([(idx1, ('key', ), '')],
1252
list(idx.iter_entries([('key', )])))
1177
index = CombinedGraphIndex([index2, index1])
1178
self.assertEqual([(index1, ('key', ), '')],
1179
list(index.iter_entries([('key', )])))
1254
1181
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())
1182
index1 = self.make_index('1', nodes=[])
1183
index2 = self.make_index('2', nodes=[])
1184
index = CombinedGraphIndex([index1, index2])
1185
self.assertEqual(0, index.key_count())
1260
1187
def test_key_count_sums_index_keys(self):
1261
idx1 = self.make_index('1', nodes=[
1188
index1 = self.make_index('1', nodes=[
1262
1189
(('1',), '', ()),
1263
1190
(('2',), '', ())])
1264
idx2 = self.make_index('2', nodes=[(('1',), '', ())])
1265
idx = index.CombinedGraphIndex([idx1, idx2])
1266
self.assertEqual(3, idx.key_count())
1191
index2 = self.make_index('2', nodes=[(('1',), '', ())])
1192
index = CombinedGraphIndex([index1, index2])
1193
self.assertEqual(3, index.key_count())
1268
1195
def test_validate_bad_child_index_errors(self):
1269
1196
trans = self.get_transport()
1270
1197
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)
1198
index1 = GraphIndex(trans, 'name', 13)
1199
index = CombinedGraphIndex([index1])
1200
self.assertRaises(errors.BadIndexFormatSignature, index.validate)
1275
1202
def test_validate_empty(self):
1276
idx = index.CombinedGraphIndex([])
1203
index = CombinedGraphIndex([])
1279
1206
def test_key_count_reloads(self):
1280
idx, reload_counter = self.make_combined_index_with_missing()
1281
self.assertEqual(2, idx.key_count())
1207
index, reload_counter = self.make_combined_index_with_missing()
1208
self.assertEqual(2, index.key_count())
1282
1209
self.assertEqual([1, 1, 0], reload_counter)
1284
1211
def test_key_count_no_reload(self):
1285
idx, reload_counter = self.make_combined_index_with_missing()
1286
idx._reload_func = None
1212
index, reload_counter = self.make_combined_index_with_missing()
1213
index._reload_func = None
1287
1214
# Without a _reload_func we just raise the exception
1288
self.assertRaises(errors.NoSuchFile, idx.key_count)
1215
self.assertRaises(errors.NoSuchFile, index.key_count)
1290
1217
def test_key_count_reloads_and_fails(self):
1291
1218
# We have deleted all underlying indexes, so we will try to reload, but
1292
1219
# still fail. This is mostly to test we don't get stuck in an infinite
1293
1220
# loop trying to reload
1294
idx, reload_counter = self.make_combined_index_with_missing(
1296
self.assertRaises(errors.NoSuchFile, idx.key_count)
1221
index, reload_counter = self.make_combined_index_with_missing(
1223
self.assertRaises(errors.NoSuchFile, index.key_count)
1297
1224
self.assertEqual([2, 1, 1], reload_counter)
1299
1226
def test_iter_entries_reloads(self):