~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_index.py

  • Committer: John Ferlito
  • Date: 2009-09-02 04:31:45 UTC
  • mto: (4665.7.1 serve-init)
  • mto: This revision was merged to the branch mainline in revision 4913.
  • Revision ID: johnf@inodes.org-20090902043145-gxdsfw03ilcwbyn5
Add a debian init script for bzr --serve

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for indices."""
18
18
 
350
350
        builder.add_node(('k', 'ey'), 'data', ([('reference', 'tokey')], ))
351
351
        builder.add_node(('reference', 'tokey'), 'data', ([],))
352
352
 
 
353
    def test_set_optimize(self):
 
354
        builder = GraphIndexBuilder(reference_lists=1, key_elements=2)
 
355
        builder.set_optimize(for_size=True)
 
356
        self.assertTrue(builder._optimize_for_size)
 
357
        builder.set_optimize(for_size=False)
 
358
        self.assertFalse(builder._optimize_for_size)
 
359
 
353
360
 
354
361
class TestGraphIndex(TestCaseWithMemoryTransport):
355
362
 
551
558
        # not create a new transport request, and should return False (cannot
552
559
        # be in the index) - even when the byte location we ask for is outside
553
560
        # the parsed region
554
 
        # 
 
561
        #
555
562
        result = index._lookup_keys_via_location([(4000, self.make_key(40))])
556
563
        self.assertEqual(
557
564
            [((4000, self.make_key(40)),
915
922
        index = self.make_index(nodes=[(('key', ), 'value', ())])
916
923
        index.validate()
917
924
 
 
925
    # XXX: external_references tests are duplicated in test_btree_index.  We
 
926
    # probably should have per_graph_index tests...
 
927
    def test_external_references_no_refs(self):
 
928
        index = self.make_index(ref_lists=0, nodes=[])
 
929
        self.assertRaises(ValueError, index.external_references, 0)
 
930
 
 
931
    def test_external_references_no_results(self):
 
932
        index = self.make_index(ref_lists=1, nodes=[
 
933
            (('key',), 'value', ([],))])
 
934
        self.assertEqual(set(), index.external_references(0))
 
935
 
 
936
    def test_external_references_missing_ref(self):
 
937
        missing_key = ('missing',)
 
938
        index = self.make_index(ref_lists=1, nodes=[
 
939
            (('key',), 'value', ([missing_key],))])
 
940
        self.assertEqual(set([missing_key]), index.external_references(0))
 
941
 
 
942
    def test_external_references_multiple_ref_lists(self):
 
943
        missing_key = ('missing',)
 
944
        index = self.make_index(ref_lists=2, nodes=[
 
945
            (('key',), 'value', ([], [missing_key]))])
 
946
        self.assertEqual(set([]), index.external_references(0))
 
947
        self.assertEqual(set([missing_key]), index.external_references(1))
 
948
 
 
949
    def test_external_references_two_records(self):
 
950
        index = self.make_index(ref_lists=1, nodes=[
 
951
            (('key-1',), 'value', ([('key-2',)],)),
 
952
            (('key-2',), 'value', ([],)),
 
953
            ])
 
954
        self.assertEqual(set([]), index.external_references(0))
 
955
 
 
956
    def test__find_ancestors(self):
 
957
        key1 = ('key-1',)
 
958
        key2 = ('key-2',)
 
959
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
 
960
            (key1, 'value', ([key2],)),
 
961
            (key2, 'value', ([],)),
 
962
            ])
 
963
        parent_map = {}
 
964
        missing_keys = set()
 
965
        search_keys = index._find_ancestors([key1], 0, parent_map, missing_keys)
 
966
        self.assertEqual({key1: (key2,)}, parent_map)
 
967
        self.assertEqual(set(), missing_keys)
 
968
        self.assertEqual(set([key2]), search_keys)
 
969
        search_keys = index._find_ancestors(search_keys, 0, parent_map,
 
970
                                            missing_keys)
 
971
        self.assertEqual({key1: (key2,), key2: ()}, parent_map)
 
972
        self.assertEqual(set(), missing_keys)
 
973
        self.assertEqual(set(), search_keys)
 
974
 
 
975
    def test__find_ancestors_w_missing(self):
 
976
        key1 = ('key-1',)
 
977
        key2 = ('key-2',)
 
978
        key3 = ('key-3',)
 
979
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
 
980
            (key1, 'value', ([key2],)),
 
981
            (key2, 'value', ([],)),
 
982
            ])
 
983
        parent_map = {}
 
984
        missing_keys = set()
 
985
        search_keys = index._find_ancestors([key2, key3], 0, parent_map,
 
986
                                            missing_keys)
 
987
        self.assertEqual({key2: ()}, parent_map)
 
988
        self.assertEqual(set([key3]), missing_keys)
 
989
        self.assertEqual(set(), search_keys)
 
990
 
 
991
    def test__find_ancestors_dont_search_known(self):
 
992
        key1 = ('key-1',)
 
993
        key2 = ('key-2',)
 
994
        key3 = ('key-3',)
 
995
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
 
996
            (key1, 'value', ([key2],)),
 
997
            (key2, 'value', ([key3],)),
 
998
            (key3, 'value', ([],)),
 
999
            ])
 
1000
        # We already know about key2, so we won't try to search for key3
 
1001
        parent_map = {key2: (key3,)}
 
1002
        missing_keys = set()
 
1003
        search_keys = index._find_ancestors([key1], 0, parent_map,
 
1004
                                            missing_keys)
 
1005
        self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
 
1006
        self.assertEqual(set(), missing_keys)
 
1007
        self.assertEqual(set(), search_keys)
 
1008
 
918
1009
 
919
1010
class TestCombinedGraphIndex(TestCaseWithMemoryTransport):
920
1011
 
927
1018
        size = trans.put_file(name, stream)
928
1019
        return GraphIndex(trans, name, size)
929
1020
 
 
1021
    def make_combined_index_with_missing(self, missing=['1', '2']):
 
1022
        """Create a CombinedGraphIndex which will have missing indexes.
 
1023
 
 
1024
        This creates a CGI which thinks it has 2 indexes, however they have
 
1025
        been deleted. If CGI._reload_func() is called, then it will repopulate
 
1026
        with a new index.
 
1027
 
 
1028
        :param missing: The underlying indexes to delete
 
1029
        :return: (CombinedGraphIndex, reload_counter)
 
1030
        """
 
1031
        index1 = self.make_index('1', nodes=[(('1',), '', ())])
 
1032
        index2 = self.make_index('2', nodes=[(('2',), '', ())])
 
1033
        index3 = self.make_index('3', nodes=[
 
1034
            (('1',), '', ()),
 
1035
            (('2',), '', ())])
 
1036
 
 
1037
        # total_reloads, num_changed, num_unchanged
 
1038
        reload_counter = [0, 0, 0]
 
1039
        def reload():
 
1040
            reload_counter[0] += 1
 
1041
            new_indices = [index3]
 
1042
            if index._indices == new_indices:
 
1043
                reload_counter[2] += 1
 
1044
                return False
 
1045
            reload_counter[1] += 1
 
1046
            index._indices[:] = new_indices
 
1047
            return True
 
1048
        index = CombinedGraphIndex([index1, index2], reload_func=reload)
 
1049
        trans = self.get_transport()
 
1050
        for fname in missing:
 
1051
            trans.delete(fname)
 
1052
        return index, reload_counter
 
1053
 
930
1054
    def test_open_missing_index_no_error(self):
931
1055
        trans = self.get_transport()
932
1056
        index1 = GraphIndex(trans, 'missing', 100)
1007
1131
        self.assertEqual(set([(index1, ('name', ), 'data', ((('ref', ), ), )),
1008
1132
            (index2, ('ref', ), 'refdata', ((), ))]),
1009
1133
            set(index.iter_entries([('name', ), ('ref', )])))
1010
 
 
 
1134
 
1011
1135
    def test_iter_all_keys_dup_entry(self):
1012
1136
        index1 = self.make_index('1', 1, nodes=[
1013
1137
            (('name', ), 'data', ([('ref', )], )),
1018
1142
        self.assertEqual(set([(index1, ('name', ), 'data', ((('ref',),),)),
1019
1143
            (index1, ('ref', ), 'refdata', ((), ))]),
1020
1144
            set(index.iter_entries([('name', ), ('ref', )])))
1021
 
 
 
1145
 
1022
1146
    def test_iter_missing_entry_empty(self):
1023
1147
        index = CombinedGraphIndex([])
1024
1148
        self.assertEqual([], list(index.iter_entries([('a', )])))
1033
1157
        index2 = self.make_index('2')
1034
1158
        index = CombinedGraphIndex([index1, index2])
1035
1159
        self.assertEqual([], list(index.iter_entries([('a', )])))
1036
 
 
 
1160
 
1037
1161
    def test_iter_entry_present_one_index_only(self):
1038
1162
        index1 = self.make_index('1', nodes=[(('key', ), '', ())])
1039
1163
        index2 = self.make_index('2', nodes=[])
1070
1194
        index = CombinedGraphIndex([])
1071
1195
        index.validate()
1072
1196
 
 
1197
    def test_key_count_reloads(self):
 
1198
        index, reload_counter = self.make_combined_index_with_missing()
 
1199
        self.assertEqual(2, index.key_count())
 
1200
        self.assertEqual([1, 1, 0], reload_counter)
 
1201
 
 
1202
    def test_key_count_no_reload(self):
 
1203
        index, reload_counter = self.make_combined_index_with_missing()
 
1204
        index._reload_func = None
 
1205
        # Without a _reload_func we just raise the exception
 
1206
        self.assertRaises(errors.NoSuchFile, index.key_count)
 
1207
 
 
1208
    def test_key_count_reloads_and_fails(self):
 
1209
        # We have deleted all underlying indexes, so we will try to reload, but
 
1210
        # still fail. This is mostly to test we don't get stuck in an infinite
 
1211
        # loop trying to reload
 
1212
        index, reload_counter = self.make_combined_index_with_missing(
 
1213
                                    ['1', '2', '3'])
 
1214
        self.assertRaises(errors.NoSuchFile, index.key_count)
 
1215
        self.assertEqual([2, 1, 1], reload_counter)
 
1216
 
 
1217
    def test_iter_entries_reloads(self):
 
1218
        index, reload_counter = self.make_combined_index_with_missing()
 
1219
        result = list(index.iter_entries([('1',), ('2',), ('3',)]))
 
1220
        index3 = index._indices[0]
 
1221
        self.assertEqual([(index3, ('1',), ''), (index3, ('2',), '')],
 
1222
                         result)
 
1223
        self.assertEqual([1, 1, 0], reload_counter)
 
1224
 
 
1225
    def test_iter_entries_reloads_midway(self):
 
1226
        # The first index still looks present, so we get interrupted mid-way
 
1227
        # through
 
1228
        index, reload_counter = self.make_combined_index_with_missing(['2'])
 
1229
        index1, index2 = index._indices
 
1230
        result = list(index.iter_entries([('1',), ('2',), ('3',)]))
 
1231
        index3 = index._indices[0]
 
1232
        # We had already yielded '1', so we just go on to the next, we should
 
1233
        # not yield '1' twice.
 
1234
        self.assertEqual([(index1, ('1',), ''), (index3, ('2',), '')],
 
1235
                         result)
 
1236
        self.assertEqual([1, 1, 0], reload_counter)
 
1237
 
 
1238
    def test_iter_entries_no_reload(self):
 
1239
        index, reload_counter = self.make_combined_index_with_missing()
 
1240
        index._reload_func = None
 
1241
        # Without a _reload_func we just raise the exception
 
1242
        self.assertListRaises(errors.NoSuchFile, index.iter_entries, [('3',)])
 
1243
 
 
1244
    def test_iter_entries_reloads_and_fails(self):
 
1245
        index, reload_counter = self.make_combined_index_with_missing(
 
1246
                                    ['1', '2', '3'])
 
1247
        self.assertListRaises(errors.NoSuchFile, index.iter_entries, [('3',)])
 
1248
        self.assertEqual([2, 1, 1], reload_counter)
 
1249
 
 
1250
    def test_iter_all_entries_reloads(self):
 
1251
        index, reload_counter = self.make_combined_index_with_missing()
 
1252
        result = list(index.iter_all_entries())
 
1253
        index3 = index._indices[0]
 
1254
        self.assertEqual([(index3, ('1',), ''), (index3, ('2',), '')],
 
1255
                         result)
 
1256
        self.assertEqual([1, 1, 0], reload_counter)
 
1257
 
 
1258
    def test_iter_all_entries_reloads_midway(self):
 
1259
        index, reload_counter = self.make_combined_index_with_missing(['2'])
 
1260
        index1, index2 = index._indices
 
1261
        result = list(index.iter_all_entries())
 
1262
        index3 = index._indices[0]
 
1263
        # We had already yielded '1', so we just go on to the next, we should
 
1264
        # not yield '1' twice.
 
1265
        self.assertEqual([(index1, ('1',), ''), (index3, ('2',), '')],
 
1266
                         result)
 
1267
        self.assertEqual([1, 1, 0], reload_counter)
 
1268
 
 
1269
    def test_iter_all_entries_no_reload(self):
 
1270
        index, reload_counter = self.make_combined_index_with_missing()
 
1271
        index._reload_func = None
 
1272
        self.assertListRaises(errors.NoSuchFile, index.iter_all_entries)
 
1273
 
 
1274
    def test_iter_all_entries_reloads_and_fails(self):
 
1275
        index, reload_counter = self.make_combined_index_with_missing(
 
1276
                                    ['1', '2', '3'])
 
1277
        self.assertListRaises(errors.NoSuchFile, index.iter_all_entries)
 
1278
 
 
1279
    def test_iter_entries_prefix_reloads(self):
 
1280
        index, reload_counter = self.make_combined_index_with_missing()
 
1281
        result = list(index.iter_entries_prefix([('1',)]))
 
1282
        index3 = index._indices[0]
 
1283
        self.assertEqual([(index3, ('1',), '')], result)
 
1284
        self.assertEqual([1, 1, 0], reload_counter)
 
1285
 
 
1286
    def test_iter_entries_prefix_reloads_midway(self):
 
1287
        index, reload_counter = self.make_combined_index_with_missing(['2'])
 
1288
        index1, index2 = index._indices
 
1289
        result = list(index.iter_entries_prefix([('1',)]))
 
1290
        index3 = index._indices[0]
 
1291
        # We had already yielded '1', so we just go on to the next, we should
 
1292
        # not yield '1' twice.
 
1293
        self.assertEqual([(index1, ('1',), '')], result)
 
1294
        self.assertEqual([1, 1, 0], reload_counter)
 
1295
 
 
1296
    def test_iter_entries_prefix_no_reload(self):
 
1297
        index, reload_counter = self.make_combined_index_with_missing()
 
1298
        index._reload_func = None
 
1299
        self.assertListRaises(errors.NoSuchFile, index.iter_entries_prefix,
 
1300
                                                 [('1',)])
 
1301
 
 
1302
    def test_iter_entries_prefix_reloads_and_fails(self):
 
1303
        index, reload_counter = self.make_combined_index_with_missing(
 
1304
                                    ['1', '2', '3'])
 
1305
        self.assertListRaises(errors.NoSuchFile, index.iter_entries_prefix,
 
1306
                                                 [('1',)])
 
1307
 
 
1308
    def test_validate_reloads(self):
 
1309
        index, reload_counter = self.make_combined_index_with_missing()
 
1310
        index.validate()
 
1311
        self.assertEqual([1, 1, 0], reload_counter)
 
1312
 
 
1313
    def test_validate_reloads_midway(self):
 
1314
        index, reload_counter = self.make_combined_index_with_missing(['2'])
 
1315
        index.validate()
 
1316
 
 
1317
    def test_validate_no_reload(self):
 
1318
        index, reload_counter = self.make_combined_index_with_missing()
 
1319
        index._reload_func = None
 
1320
        self.assertRaises(errors.NoSuchFile, index.validate)
 
1321
 
 
1322
    def test_validate_reloads_and_fails(self):
 
1323
        index, reload_counter = self.make_combined_index_with_missing(
 
1324
                                    ['1', '2', '3'])
 
1325
        self.assertRaises(errors.NoSuchFile, index.validate)
 
1326
 
 
1327
    def test_find_ancestors_across_indexes(self):
 
1328
        key1 = ('key-1',)
 
1329
        key2 = ('key-2',)
 
1330
        key3 = ('key-3',)
 
1331
        key4 = ('key-4',)
 
1332
        index1 = self.make_index('12', ref_lists=1, nodes=[
 
1333
            (key1, 'value', ([],)),
 
1334
            (key2, 'value', ([key1],)),
 
1335
            ])
 
1336
        index2 = self.make_index('34', ref_lists=1, nodes=[
 
1337
            (key3, 'value', ([key2],)),
 
1338
            (key4, 'value', ([key3],)),
 
1339
            ])
 
1340
        c_index = CombinedGraphIndex([index1, index2])
 
1341
        parent_map, missing_keys = c_index.find_ancestry([key1], 0)
 
1342
        self.assertEqual({key1: ()}, parent_map)
 
1343
        self.assertEqual(set(), missing_keys)
 
1344
        # Now look for a key from index2 which requires us to find the key in
 
1345
        # the second index, and then continue searching for parents in the
 
1346
        # first index
 
1347
        parent_map, missing_keys = c_index.find_ancestry([key3], 0)
 
1348
        self.assertEqual({key1: (), key2: (key1,), key3: (key2,)}, parent_map)
 
1349
        self.assertEqual(set(), missing_keys)
 
1350
 
 
1351
    def test_find_ancestors_missing_keys(self):
 
1352
        key1 = ('key-1',)
 
1353
        key2 = ('key-2',)
 
1354
        key3 = ('key-3',)
 
1355
        key4 = ('key-4',)
 
1356
        index1 = self.make_index('12', ref_lists=1, nodes=[
 
1357
            (key1, 'value', ([],)),
 
1358
            (key2, 'value', ([key1],)),
 
1359
            ])
 
1360
        index2 = self.make_index('34', ref_lists=1, nodes=[
 
1361
            (key3, 'value', ([key2],)),
 
1362
            ])
 
1363
        c_index = CombinedGraphIndex([index1, index2])
 
1364
        # Searching for a key which is actually not present at all should
 
1365
        # eventually converge
 
1366
        parent_map, missing_keys = c_index.find_ancestry([key4], 0)
 
1367
        self.assertEqual({}, parent_map)
 
1368
        self.assertEqual(set([key4]), missing_keys)
 
1369
 
 
1370
    def test_find_ancestors_no_indexes(self):
 
1371
        c_index = CombinedGraphIndex([])
 
1372
        key1 = ('key-1',)
 
1373
        parent_map, missing_keys = c_index.find_ancestry([key1], 0)
 
1374
        self.assertEqual({}, parent_map)
 
1375
        self.assertEqual(set([key1]), missing_keys)
 
1376
 
 
1377
    def test_find_ancestors_ghost_parent(self):
 
1378
        key1 = ('key-1',)
 
1379
        key2 = ('key-2',)
 
1380
        key3 = ('key-3',)
 
1381
        key4 = ('key-4',)
 
1382
        index1 = self.make_index('12', ref_lists=1, nodes=[
 
1383
            (key1, 'value', ([],)),
 
1384
            (key2, 'value', ([key1],)),
 
1385
            ])
 
1386
        index2 = self.make_index('34', ref_lists=1, nodes=[
 
1387
            (key4, 'value', ([key2, key3],)),
 
1388
            ])
 
1389
        c_index = CombinedGraphIndex([index1, index2])
 
1390
        # Searching for a key which is actually not present at all should
 
1391
        # eventually converge
 
1392
        parent_map, missing_keys = c_index.find_ancestry([key4], 0)
 
1393
        self.assertEqual({key4: (key2, key3), key2: (key1,), key1: ()},
 
1394
                         parent_map)
 
1395
        self.assertEqual(set([key3]), missing_keys)
 
1396
 
 
1397
    def test__find_ancestors_empty_index(self):
 
1398
        index = self.make_index('test', ref_lists=1, key_elements=1, nodes=[])
 
1399
        parent_map = {}
 
1400
        missing_keys = set()
 
1401
        search_keys = index._find_ancestors([('one',), ('two',)], 0, parent_map,
 
1402
                                            missing_keys)
 
1403
        self.assertEqual(set(), search_keys)
 
1404
        self.assertEqual({}, parent_map)
 
1405
        self.assertEqual(set([('one',), ('two',)]), missing_keys)
 
1406
 
1073
1407
 
1074
1408
class TestInMemoryGraphIndex(TestCaseWithMemoryTransport):
1075
1409