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