337
321
access.set_writer(writer, index, (transport, packname))
338
322
return access, writer
340
def make_pack_file(self):
341
"""Create a pack file with 2 records."""
342
access, writer = self._get_access(packname='packname', index='foo')
344
memos.extend(access.add_raw_records([('key1', 10)], '1234567890'))
345
memos.extend(access.add_raw_records([('key2', 5)], '12345'))
349
def make_vf_for_retrying(self):
350
"""Create 3 packs and a reload function.
352
Originally, 2 pack files will have the data, but one will be missing.
353
And then the third will be used in place of the first two if reload()
356
:return: (versioned_file, reload_counter)
357
versioned_file a KnitVersionedFiles using the packs for access
359
builder = self.make_branch_builder('.', format="1.9")
360
builder.start_series()
361
builder.build_snapshot('rev-1', None, [
362
('add', ('', 'root-id', 'directory', None)),
363
('add', ('file', 'file-id', 'file', 'content\nrev 1\n')),
365
builder.build_snapshot('rev-2', ['rev-1'], [
366
('modify', ('file-id', 'content\nrev 2\n')),
368
builder.build_snapshot('rev-3', ['rev-2'], [
369
('modify', ('file-id', 'content\nrev 3\n')),
371
builder.finish_series()
372
b = builder.get_branch()
374
self.addCleanup(b.unlock)
375
# Pack these three revisions into another pack file, but don't remove
378
collection = repo._pack_collection
379
collection.ensure_loaded()
380
orig_packs = collection.packs
381
packer = pack_repo.Packer(collection, orig_packs, '.testpack')
382
new_pack = packer.pack()
383
# forget about the new pack
387
# Set up a reload() function that switches to using the new pack file
388
new_index = new_pack.revision_index
389
access_tuple = new_pack.access_tuple()
390
reload_counter = [0, 0, 0]
392
reload_counter[0] += 1
393
if reload_counter[1] > 0:
394
# We already reloaded, nothing more to do
395
reload_counter[2] += 1
397
reload_counter[1] += 1
398
vf._index._graph_index._indices[:] = [new_index]
399
vf._access._indices.clear()
400
vf._access._indices[new_index] = access_tuple
402
# Delete one of the pack files so the data will need to be reloaded. We
403
# will delete the file with 'rev-2' in it
404
trans, name = orig_packs[1].access_tuple()
406
# We don't have the index trigger reloading because we want to test
407
# that we reload when the .pack disappears
408
vf._access._reload_func = reload
409
return vf, reload_counter
411
def make_reload_func(self, return_val=True):
414
reload_called[0] += 1
416
return reload_called, reload
418
def make_retry_exception(self):
419
# We raise a real exception so that sys.exc_info() is properly
422
raise _TestException('foobar')
423
except _TestException, e:
424
retry_exc = errors.RetryWithNewPacks(None, reload_occurred=False,
425
exc_info=sys.exc_info())
428
324
def test_read_from_several_packs(self):
429
325
access, writer = self._get_access()
467
363
self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
469
def test_missing_index_raises_retry(self):
470
memos = self.make_pack_file()
471
transport = self.get_transport()
472
reload_called, reload_func = self.make_reload_func()
473
# Note that the index key has changed from 'foo' to 'bar'
474
access = _DirectPackAccess({'bar':(transport, 'packname')},
475
reload_func=reload_func)
476
e = self.assertListRaises(errors.RetryWithNewPacks,
477
access.get_raw_records, memos)
478
# Because a key was passed in which does not match our index list, we
479
# assume that the listing was already reloaded
480
self.assertTrue(e.reload_occurred)
481
self.assertIsInstance(e.exc_info, tuple)
482
self.assertIs(e.exc_info[0], KeyError)
483
self.assertIsInstance(e.exc_info[1], KeyError)
485
def test_missing_index_raises_key_error_with_no_reload(self):
486
memos = self.make_pack_file()
487
transport = self.get_transport()
488
# Note that the index key has changed from 'foo' to 'bar'
489
access = _DirectPackAccess({'bar':(transport, 'packname')})
490
e = self.assertListRaises(KeyError, access.get_raw_records, memos)
492
def test_missing_file_raises_retry(self):
493
memos = self.make_pack_file()
494
transport = self.get_transport()
495
reload_called, reload_func = self.make_reload_func()
496
# Note that the 'filename' has been changed to 'different-packname'
497
access = _DirectPackAccess({'foo':(transport, 'different-packname')},
498
reload_func=reload_func)
499
e = self.assertListRaises(errors.RetryWithNewPacks,
500
access.get_raw_records, memos)
501
# The file has gone missing, so we assume we need to reload
502
self.assertFalse(e.reload_occurred)
503
self.assertIsInstance(e.exc_info, tuple)
504
self.assertIs(e.exc_info[0], errors.NoSuchFile)
505
self.assertIsInstance(e.exc_info[1], errors.NoSuchFile)
506
self.assertEqual('different-packname', e.exc_info[1].path)
508
def test_missing_file_raises_no_such_file_with_no_reload(self):
509
memos = self.make_pack_file()
510
transport = self.get_transport()
511
# Note that the 'filename' has been changed to 'different-packname'
512
access = _DirectPackAccess({'foo':(transport, 'different-packname')})
513
e = self.assertListRaises(errors.NoSuchFile,
514
access.get_raw_records, memos)
516
def test_failing_readv_raises_retry(self):
517
memos = self.make_pack_file()
518
transport = self.get_transport()
519
failing_transport = MockReadvFailingTransport(
520
[transport.get_bytes('packname')])
521
reload_called, reload_func = self.make_reload_func()
522
access = _DirectPackAccess({'foo':(failing_transport, 'packname')},
523
reload_func=reload_func)
524
# Asking for a single record will not trigger the Mock failure
525
self.assertEqual(['1234567890'],
526
list(access.get_raw_records(memos[:1])))
527
self.assertEqual(['12345'],
528
list(access.get_raw_records(memos[1:2])))
529
# A multiple offset readv() will fail mid-way through
530
e = self.assertListRaises(errors.RetryWithNewPacks,
531
access.get_raw_records, memos)
532
# The file has gone missing, so we assume we need to reload
533
self.assertFalse(e.reload_occurred)
534
self.assertIsInstance(e.exc_info, tuple)
535
self.assertIs(e.exc_info[0], errors.NoSuchFile)
536
self.assertIsInstance(e.exc_info[1], errors.NoSuchFile)
537
self.assertEqual('packname', e.exc_info[1].path)
539
def test_failing_readv_raises_no_such_file_with_no_reload(self):
540
memos = self.make_pack_file()
541
transport = self.get_transport()
542
failing_transport = MockReadvFailingTransport(
543
[transport.get_bytes('packname')])
544
reload_called, reload_func = self.make_reload_func()
545
access = _DirectPackAccess({'foo':(failing_transport, 'packname')})
546
# Asking for a single record will not trigger the Mock failure
547
self.assertEqual(['1234567890'],
548
list(access.get_raw_records(memos[:1])))
549
self.assertEqual(['12345'],
550
list(access.get_raw_records(memos[1:2])))
551
# A multiple offset readv() will fail mid-way through
552
e = self.assertListRaises(errors.NoSuchFile,
553
access.get_raw_records, memos)
555
def test_reload_or_raise_no_reload(self):
556
access = _DirectPackAccess({}, reload_func=None)
557
retry_exc = self.make_retry_exception()
558
# Without a reload_func, we will just re-raise the original exception
559
self.assertRaises(_TestException, access.reload_or_raise, retry_exc)
561
def test_reload_or_raise_reload_changed(self):
562
reload_called, reload_func = self.make_reload_func(return_val=True)
563
access = _DirectPackAccess({}, reload_func=reload_func)
564
retry_exc = self.make_retry_exception()
565
access.reload_or_raise(retry_exc)
566
self.assertEqual([1], reload_called)
567
retry_exc.reload_occurred=True
568
access.reload_or_raise(retry_exc)
569
self.assertEqual([2], reload_called)
571
def test_reload_or_raise_reload_no_change(self):
572
reload_called, reload_func = self.make_reload_func(return_val=False)
573
access = _DirectPackAccess({}, reload_func=reload_func)
574
retry_exc = self.make_retry_exception()
575
# If reload_occurred is False, then we consider it an error to have
576
# reload_func() return False (no changes).
577
self.assertRaises(_TestException, access.reload_or_raise, retry_exc)
578
self.assertEqual([1], reload_called)
579
retry_exc.reload_occurred=True
580
# If reload_occurred is True, then we assume nothing changed because
581
# it had changed earlier, but didn't change again
582
access.reload_or_raise(retry_exc)
583
self.assertEqual([2], reload_called)
585
def test_annotate_retries(self):
586
vf, reload_counter = self.make_vf_for_retrying()
587
# It is a little bit bogus to annotate the Revision VF, but it works,
588
# as we have ancestry stored there
590
reload_lines = vf.annotate(key)
591
self.assertEqual([1, 1, 0], reload_counter)
592
plain_lines = vf.annotate(key)
593
self.assertEqual([1, 1, 0], reload_counter) # No extra reloading
594
if reload_lines != plain_lines:
595
self.fail('Annotation was not identical with reloading.')
596
# Now delete the packs-in-use, which should trigger another reload, but
597
# this time we just raise an exception because we can't recover
598
for trans, name in vf._access._indices.itervalues():
600
self.assertRaises(errors.NoSuchFile, vf.annotate, key)
601
self.assertEqual([2, 1, 1], reload_counter)
603
def test__get_record_map_retries(self):
604
vf, reload_counter = self.make_vf_for_retrying()
605
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
606
records = vf._get_record_map(keys)
607
self.assertEqual(keys, sorted(records.keys()))
608
self.assertEqual([1, 1, 0], reload_counter)
609
# Now delete the packs-in-use, which should trigger another reload, but
610
# this time we just raise an exception because we can't recover
611
for trans, name in vf._access._indices.itervalues():
613
self.assertRaises(errors.NoSuchFile, vf._get_record_map, keys)
614
self.assertEqual([2, 1, 1], reload_counter)
616
def test_get_record_stream_retries(self):
617
vf, reload_counter = self.make_vf_for_retrying()
618
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
619
record_stream = vf.get_record_stream(keys, 'topological', False)
620
record = record_stream.next()
621
self.assertEqual(('rev-1',), record.key)
622
self.assertEqual([0, 0, 0], reload_counter)
623
record = record_stream.next()
624
self.assertEqual(('rev-2',), record.key)
625
self.assertEqual([1, 1, 0], reload_counter)
626
record = record_stream.next()
627
self.assertEqual(('rev-3',), record.key)
628
self.assertEqual([1, 1, 0], reload_counter)
629
# Now delete all pack files, and see that we raise the right error
630
for trans, name in vf._access._indices.itervalues():
632
self.assertListRaises(errors.NoSuchFile,
633
vf.get_record_stream, keys, 'topological', False)
635
def test_iter_lines_added_or_present_in_keys_retries(self):
636
vf, reload_counter = self.make_vf_for_retrying()
637
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
638
# Unfortunately, iter_lines_added_or_present_in_keys iterates the
639
# result in random order (determined by the iteration order from a
640
# set()), so we don't have any solid way to trigger whether data is
641
# read before or after. However we tried to delete the middle node to
642
# exercise the code well.
643
# What we care about is that all lines are always yielded, but not
646
reload_lines = sorted(vf.iter_lines_added_or_present_in_keys(keys))
647
self.assertEqual([1, 1, 0], reload_counter)
648
# Now do it again, to make sure the result is equivalent
649
plain_lines = sorted(vf.iter_lines_added_or_present_in_keys(keys))
650
self.assertEqual([1, 1, 0], reload_counter) # No extra reloading
651
self.assertEqual(plain_lines, reload_lines)
652
self.assertEqual(21, len(plain_lines))
653
# Now delete all pack files, and see that we raise the right error
654
for trans, name in vf._access._indices.itervalues():
656
self.assertListRaises(errors.NoSuchFile,
657
vf.iter_lines_added_or_present_in_keys, keys)
658
self.assertEqual([2, 1, 1], reload_counter)
660
def test_get_record_stream_yields_disk_sorted_order(self):
661
# if we get 'unordered' pick a semi-optimal order for reading. The
662
# order should be grouped by pack file, and then by position in file
663
repo = self.make_repository('test', format='pack-0.92')
665
self.addCleanup(repo.unlock)
666
repo.start_write_group()
668
vf.add_lines(('f-id', 'rev-5'), [('f-id', 'rev-4')], ['lines\n'])
669
vf.add_lines(('f-id', 'rev-1'), [], ['lines\n'])
670
vf.add_lines(('f-id', 'rev-2'), [('f-id', 'rev-1')], ['lines\n'])
671
repo.commit_write_group()
672
# We inserted them as rev-5, rev-1, rev-2, we should get them back in
674
stream = vf.get_record_stream([('f-id', 'rev-1'), ('f-id', 'rev-5'),
675
('f-id', 'rev-2')], 'unordered', False)
676
keys = [r.key for r in stream]
677
self.assertEqual([('f-id', 'rev-5'), ('f-id', 'rev-1'),
678
('f-id', 'rev-2')], keys)
679
repo.start_write_group()
680
vf.add_lines(('f-id', 'rev-4'), [('f-id', 'rev-3')], ['lines\n'])
681
vf.add_lines(('f-id', 'rev-3'), [('f-id', 'rev-2')], ['lines\n'])
682
vf.add_lines(('f-id', 'rev-6'), [('f-id', 'rev-5')], ['lines\n'])
683
repo.commit_write_group()
684
# Request in random order, to make sure the output order isn't based on
686
request_keys = set(('f-id', 'rev-%d' % i) for i in range(1, 7))
687
stream = vf.get_record_stream(request_keys, 'unordered', False)
688
keys = [r.key for r in stream]
689
# We want to get the keys back in disk order, but it doesn't matter
690
# which pack we read from first. So this can come back in 2 orders
691
alt1 = [('f-id', 'rev-%d' % i) for i in [4, 3, 6, 5, 1, 2]]
692
alt2 = [('f-id', 'rev-%d' % i) for i in [5, 1, 2, 4, 3, 6]]
693
if keys != alt1 and keys != alt2:
694
self.fail('Returned key order did not match either expected order.'
695
' expected %s or %s, not %s'
696
% (alt1, alt2, keys))
699
366
class LowLevelKnitDataTests(TestCase):
1295
899
class LowLevelKnitIndexTests_c(LowLevelKnitIndexTests):
1297
_test_needs_features = [compiled_knit_feature]
901
_test_needs_features = [CompiledKnitFeature]
1299
903
def get_knit_index(self, transport, name, mode):
1300
904
mapper = ConstantMapper(name)
1301
from bzrlib._knit_load_data_pyx import _load_data_c
1302
self.overrideAttr(knit, '_load_data', _load_data_c)
905
orig = knit._load_data
907
knit._load_data = orig
908
self.addCleanup(reset)
909
from bzrlib._knit_load_data_c import _load_data_c
910
knit._load_data = _load_data_c
1303
911
allow_writes = lambda: mode == 'w'
1304
return _KndxIndex(transport, mapper, lambda:None,
1305
allow_writes, lambda:True)
1308
class Test_KnitAnnotator(TestCaseWithMemoryTransport):
1310
def make_annotator(self):
1311
factory = knit.make_pack_factory(True, True, 1)
1312
vf = factory(self.get_transport())
1313
return knit._KnitAnnotator(vf)
1315
def test__expand_fulltext(self):
1316
ann = self.make_annotator()
1317
rev_key = ('rev-id',)
1318
ann._num_compression_children[rev_key] = 1
1319
res = ann._expand_record(rev_key, (('parent-id',),), None,
1320
['line1\n', 'line2\n'], ('fulltext', True))
1321
# The content object and text lines should be cached appropriately
1322
self.assertEqual(['line1\n', 'line2'], res)
1323
content_obj = ann._content_objects[rev_key]
1324
self.assertEqual(['line1\n', 'line2\n'], content_obj._lines)
1325
self.assertEqual(res, content_obj.text())
1326
self.assertEqual(res, ann._text_cache[rev_key])
1328
def test__expand_delta_comp_parent_not_available(self):
1329
# Parent isn't available yet, so we return nothing, but queue up this
1330
# node for later processing
1331
ann = self.make_annotator()
1332
rev_key = ('rev-id',)
1333
parent_key = ('parent-id',)
1334
record = ['0,1,1\n', 'new-line\n']
1335
details = ('line-delta', False)
1336
res = ann._expand_record(rev_key, (parent_key,), parent_key,
1338
self.assertEqual(None, res)
1339
self.assertTrue(parent_key in ann._pending_deltas)
1340
pending = ann._pending_deltas[parent_key]
1341
self.assertEqual(1, len(pending))
1342
self.assertEqual((rev_key, (parent_key,), record, details), pending[0])
1344
def test__expand_record_tracks_num_children(self):
1345
ann = self.make_annotator()
1346
rev_key = ('rev-id',)
1347
rev2_key = ('rev2-id',)
1348
parent_key = ('parent-id',)
1349
record = ['0,1,1\n', 'new-line\n']
1350
details = ('line-delta', False)
1351
ann._num_compression_children[parent_key] = 2
1352
ann._expand_record(parent_key, (), None, ['line1\n', 'line2\n'],
1353
('fulltext', False))
1354
res = ann._expand_record(rev_key, (parent_key,), parent_key,
1356
self.assertEqual({parent_key: 1}, ann._num_compression_children)
1357
# Expanding the second child should remove the content object, and the
1358
# num_compression_children entry
1359
res = ann._expand_record(rev2_key, (parent_key,), parent_key,
1361
self.assertFalse(parent_key in ann._content_objects)
1362
self.assertEqual({}, ann._num_compression_children)
1363
# We should not cache the content_objects for rev2 and rev, because
1364
# they do not have compression children of their own.
1365
self.assertEqual({}, ann._content_objects)
1367
def test__expand_delta_records_blocks(self):
1368
ann = self.make_annotator()
1369
rev_key = ('rev-id',)
1370
parent_key = ('parent-id',)
1371
record = ['0,1,1\n', 'new-line\n']
1372
details = ('line-delta', True)
1373
ann._num_compression_children[parent_key] = 2
1374
ann._expand_record(parent_key, (), None,
1375
['line1\n', 'line2\n', 'line3\n'],
1376
('fulltext', False))
1377
ann._expand_record(rev_key, (parent_key,), parent_key, record, details)
1378
self.assertEqual({(rev_key, parent_key): [(1, 1, 1), (3, 3, 0)]},
1379
ann._matching_blocks)
1380
rev2_key = ('rev2-id',)
1381
record = ['0,1,1\n', 'new-line\n']
1382
details = ('line-delta', False)
1383
ann._expand_record(rev2_key, (parent_key,), parent_key, record, details)
1384
self.assertEqual([(1, 1, 2), (3, 3, 0)],
1385
ann._matching_blocks[(rev2_key, parent_key)])
1387
def test__get_parent_ann_uses_matching_blocks(self):
1388
ann = self.make_annotator()
1389
rev_key = ('rev-id',)
1390
parent_key = ('parent-id',)
1391
parent_ann = [(parent_key,)]*3
1392
block_key = (rev_key, parent_key)
1393
ann._annotations_cache[parent_key] = parent_ann
1394
ann._matching_blocks[block_key] = [(0, 1, 1), (3, 3, 0)]
1395
# We should not try to access any parent_lines content, because we know
1396
# we already have the matching blocks
1397
par_ann, blocks = ann._get_parent_annotations_and_matches(rev_key,
1398
['1\n', '2\n', '3\n'], parent_key)
1399
self.assertEqual(parent_ann, par_ann)
1400
self.assertEqual([(0, 1, 1), (3, 3, 0)], blocks)
1401
self.assertEqual({}, ann._matching_blocks)
1403
def test__process_pending(self):
1404
ann = self.make_annotator()
1405
rev_key = ('rev-id',)
1408
record = ['0,1,1\n', 'new-line\n']
1409
details = ('line-delta', False)
1410
p1_record = ['line1\n', 'line2\n']
1411
ann._num_compression_children[p1_key] = 1
1412
res = ann._expand_record(rev_key, (p1_key,p2_key), p1_key,
1414
self.assertEqual(None, res)
1415
# self.assertTrue(p1_key in ann._pending_deltas)
1416
self.assertEqual({}, ann._pending_annotation)
1417
# Now insert p1, and we should be able to expand the delta
1418
res = ann._expand_record(p1_key, (), None, p1_record,
1419
('fulltext', False))
1420
self.assertEqual(p1_record, res)
1421
ann._annotations_cache[p1_key] = [(p1_key,)]*2
1422
res = ann._process_pending(p1_key)
1423
self.assertEqual([], res)
1424
self.assertFalse(p1_key in ann._pending_deltas)
1425
self.assertTrue(p2_key in ann._pending_annotation)
1426
self.assertEqual({p2_key: [(rev_key, (p1_key, p2_key))]},
1427
ann._pending_annotation)
1428
# Now fill in parent 2, and pending annotation should be satisfied
1429
res = ann._expand_record(p2_key, (), None, [], ('fulltext', False))
1430
ann._annotations_cache[p2_key] = []
1431
res = ann._process_pending(p2_key)
1432
self.assertEqual([rev_key], res)
1433
self.assertEqual({}, ann._pending_annotation)
1434
self.assertEqual({}, ann._pending_deltas)
1436
def test_record_delta_removes_basis(self):
1437
ann = self.make_annotator()
1438
ann._expand_record(('parent-id',), (), None,
1439
['line1\n', 'line2\n'], ('fulltext', False))
1440
ann._num_compression_children['parent-id'] = 2
1442
def test_annotate_special_text(self):
1443
ann = self.make_annotator()
1445
rev1_key = ('rev-1',)
1446
rev2_key = ('rev-2',)
1447
rev3_key = ('rev-3',)
1448
spec_key = ('special:',)
1449
vf.add_lines(rev1_key, [], ['initial content\n'])
1450
vf.add_lines(rev2_key, [rev1_key], ['initial content\n',
1453
vf.add_lines(rev3_key, [rev1_key], ['initial content\n',
1456
spec_text = ('initial content\n'
1460
ann.add_special_text(spec_key, [rev2_key, rev3_key], spec_text)
1461
anns, lines = ann.annotate(spec_key)
1462
self.assertEqual([(rev1_key,),
1463
(rev2_key, rev3_key),
1467
self.assertEqualDiff(spec_text, ''.join(lines))
912
return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
1470
915
class KnitTests(TestCaseWithTransport):
1781
1203
# change options in the second record
1782
1204
self.assertRaises(errors.KnitCorrupt, index.add_records,
1783
1205
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)]),
1784
(('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
1206
(('tip',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1785
1207
self.assertEqual([], self.caught_entries)
1787
def make_g_index_missing_compression_parent(self):
1788
graph_index = self.make_g_index('missing_comp', 2,
1789
[(('tip', ), ' 100 78',
1790
([('missing-parent', ), ('ghost', )], [('missing-parent', )]))])
1793
def make_g_index_missing_parent(self):
1794
graph_index = self.make_g_index('missing_parent', 2,
1795
[(('parent', ), ' 100 78', ([], [])),
1796
(('tip', ), ' 100 78',
1797
([('parent', ), ('missing-parent', )], [('parent', )])),
1801
def make_g_index_no_external_refs(self):
1802
graph_index = self.make_g_index('no_external_refs', 2,
1803
[(('rev', ), ' 100 78',
1804
([('parent', ), ('ghost', )], []))])
1807
def test_add_good_unvalidated_index(self):
1808
unvalidated = self.make_g_index_no_external_refs()
1809
combined = CombinedGraphIndex([unvalidated])
1810
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1811
index.scan_unvalidated_index(unvalidated)
1812
self.assertEqual(frozenset(), index.get_missing_compression_parents())
1814
def test_add_missing_compression_parent_unvalidated_index(self):
1815
unvalidated = self.make_g_index_missing_compression_parent()
1816
combined = CombinedGraphIndex([unvalidated])
1817
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1818
index.scan_unvalidated_index(unvalidated)
1819
# This also checks that its only the compression parent that is
1820
# examined, otherwise 'ghost' would also be reported as a missing
1823
frozenset([('missing-parent',)]),
1824
index.get_missing_compression_parents())
1826
def test_add_missing_noncompression_parent_unvalidated_index(self):
1827
unvalidated = self.make_g_index_missing_parent()
1828
combined = CombinedGraphIndex([unvalidated])
1829
index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1830
track_external_parent_refs=True)
1831
index.scan_unvalidated_index(unvalidated)
1833
frozenset([('missing-parent',)]), index.get_missing_parents())
1835
def test_track_external_parent_refs(self):
1836
g_index = self.make_g_index('empty', 2, [])
1837
combined = CombinedGraphIndex([g_index])
1838
index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1839
add_callback=self.catch_add, track_external_parent_refs=True)
1840
self.caught_entries = []
1842
(('new-key',), 'fulltext,no-eol', (None, 50, 60),
1843
[('parent-1',), ('parent-2',)])])
1845
frozenset([('parent-1',), ('parent-2',)]),
1846
index.get_missing_parents())
1848
def test_add_unvalidated_index_with_present_external_references(self):
1849
index = self.two_graph_index(deltas=True)
1850
# Ugly hack to get at one of the underlying GraphIndex objects that
1851
# two_graph_index built.
1852
unvalidated = index._graph_index._indices[1]
1853
# 'parent' is an external ref of _indices[1] (unvalidated), but is
1854
# present in _indices[0].
1855
index.scan_unvalidated_index(unvalidated)
1856
self.assertEqual(frozenset(), index.get_missing_compression_parents())
1858
def make_new_missing_parent_g_index(self, name):
1859
missing_parent = name + '-missing-parent'
1860
graph_index = self.make_g_index(name, 2,
1861
[((name + 'tip', ), ' 100 78',
1862
([(missing_parent, ), ('ghost', )], [(missing_parent, )]))])
1865
def test_add_mulitiple_unvalidated_indices_with_missing_parents(self):
1866
g_index_1 = self.make_new_missing_parent_g_index('one')
1867
g_index_2 = self.make_new_missing_parent_g_index('two')
1868
combined = CombinedGraphIndex([g_index_1, g_index_2])
1869
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1870
index.scan_unvalidated_index(g_index_1)
1871
index.scan_unvalidated_index(g_index_2)
1873
frozenset([('one-missing-parent',), ('two-missing-parent',)]),
1874
index.get_missing_compression_parents())
1876
def test_add_mulitiple_unvalidated_indices_with_mutual_dependencies(self):
1877
graph_index_a = self.make_g_index('one', 2,
1878
[(('parent-one', ), ' 100 78', ([('non-compression-parent',)], [])),
1879
(('child-of-two', ), ' 100 78',
1880
([('parent-two',)], [('parent-two',)]))])
1881
graph_index_b = self.make_g_index('two', 2,
1882
[(('parent-two', ), ' 100 78', ([('non-compression-parent',)], [])),
1883
(('child-of-one', ), ' 100 78',
1884
([('parent-one',)], [('parent-one',)]))])
1885
combined = CombinedGraphIndex([graph_index_a, graph_index_b])
1886
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1887
index.scan_unvalidated_index(graph_index_a)
1888
index.scan_unvalidated_index(graph_index_b)
1890
frozenset([]), index.get_missing_compression_parents())
1893
1210
class TestNoParentsGraphIndexKnit(KnitTests):
1894
1211
"""Tests for knits using _KnitGraphIndex with no parents."""
2068
1377
self.assertEqual([], self.caught_entries)
2071
class TestKnitVersionedFiles(KnitTests):
2073
def assertGroupKeysForIo(self, exp_groups, keys, non_local_keys,
2074
positions, _min_buffer_size=None):
2075
kvf = self.make_test_knit()
2076
if _min_buffer_size is None:
2077
_min_buffer_size = knit._STREAM_MIN_BUFFER_SIZE
2078
self.assertEqual(exp_groups, kvf._group_keys_for_io(keys,
2079
non_local_keys, positions,
2080
_min_buffer_size=_min_buffer_size))
2082
def assertSplitByPrefix(self, expected_map, expected_prefix_order,
2084
split, prefix_order = KnitVersionedFiles._split_by_prefix(keys)
2085
self.assertEqual(expected_map, split)
2086
self.assertEqual(expected_prefix_order, prefix_order)
2088
def test__group_keys_for_io(self):
2089
ft_detail = ('fulltext', False)
2090
ld_detail = ('line-delta', False)
2098
f_a: (ft_detail, (f_a, 0, 100), None),
2099
f_b: (ld_detail, (f_b, 100, 21), f_a),
2100
f_c: (ld_detail, (f_c, 180, 15), f_b),
2101
g_a: (ft_detail, (g_a, 121, 35), None),
2102
g_b: (ld_detail, (g_b, 156, 12), g_a),
2103
g_c: (ld_detail, (g_c, 195, 13), g_a),
2105
self.assertGroupKeysForIo([([f_a], set())],
2106
[f_a], [], positions)
2107
self.assertGroupKeysForIo([([f_a], set([f_a]))],
2108
[f_a], [f_a], positions)
2109
self.assertGroupKeysForIo([([f_a, f_b], set([]))],
2110
[f_a, f_b], [], positions)
2111
self.assertGroupKeysForIo([([f_a, f_b], set([f_b]))],
2112
[f_a, f_b], [f_b], positions)
2113
self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
2114
[f_a, g_a, f_b, g_b], [], positions)
2115
self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
2116
[f_a, g_a, f_b, g_b], [], positions,
2117
_min_buffer_size=150)
2118
self.assertGroupKeysForIo([([f_a, f_b], set()), ([g_a, g_b], set())],
2119
[f_a, g_a, f_b, g_b], [], positions,
2120
_min_buffer_size=100)
2121
self.assertGroupKeysForIo([([f_c], set()), ([g_b], set())],
2122
[f_c, g_b], [], positions,
2123
_min_buffer_size=125)
2124
self.assertGroupKeysForIo([([g_b, f_c], set())],
2125
[g_b, f_c], [], positions,
2126
_min_buffer_size=125)
2128
def test__split_by_prefix(self):
2129
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2130
'g': [('g', 'b'), ('g', 'a')],
2132
[('f', 'a'), ('g', 'b'),
2133
('g', 'a'), ('f', 'b')])
2135
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2136
'g': [('g', 'b'), ('g', 'a')],
2138
[('f', 'a'), ('f', 'b'),
2139
('g', 'b'), ('g', 'a')])
2141
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2142
'g': [('g', 'b'), ('g', 'a')],
2144
[('f', 'a'), ('f', 'b'),
2145
('g', 'b'), ('g', 'a')])
2147
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2148
'g': [('g', 'b'), ('g', 'a')],
2149
'': [('a',), ('b',)]
2151
[('f', 'a'), ('g', 'b'),
2153
('g', 'a'), ('f', 'b')])
2156
1380
class TestStacking(KnitTests):
2158
1382
def get_basis_and_test_knit(self):
2545
1762
multiparent.NewText(['foo\n']),
2546
1763
multiparent.ParentText(1, 0, 2, 1)])],
2548
self.assertEqual(3, len(basis.calls))
1765
self.assertEqual(4, len(basis.calls))
2549
1766
self.assertEqual([
2550
1767
("get_parent_map", set([key_left, key_right])),
2551
1768
("get_parent_map", set([key_left, key_right])),
1769
("get_parent_map", set([key_left, key_right])),
2554
last_call = basis.calls[-1]
1772
last_call = basis.calls[3]
2555
1773
self.assertEqual('get_record_stream', last_call[0])
2556
1774
self.assertEqual(set([key_left, key_right]), set(last_call[1]))
2557
self.assertEqual('topological', last_call[2])
1775
self.assertEqual('unordered', last_call[2])
2558
1776
self.assertEqual(True, last_call[3])
2561
class TestNetworkBehaviour(KnitTests):
2562
"""Tests for getting data out of/into knits over the network."""
2564
def test_include_delta_closure_generates_a_knit_delta_closure(self):
2565
vf = self.make_test_knit(name='test')
2566
# put in three texts, giving ft, delta, delta
2567
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2568
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2569
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2570
# But heuristics could interfere, so check what happened:
2571
self.assertEqual(['knit-ft-gz', 'knit-delta-gz', 'knit-delta-gz'],
2572
[record.storage_kind for record in
2573
vf.get_record_stream([('base',), ('d1',), ('d2',)],
2574
'topological', False)])
2575
# generate a stream of just the deltas include_delta_closure=True,
2576
# serialise to the network, and check that we get a delta closure on the wire.
2577
stream = vf.get_record_stream([('d1',), ('d2',)], 'topological', True)
2578
netb = [record.get_bytes_as(record.storage_kind) for record in stream]
2579
# The first bytes should be a memo from _ContentMapGenerator, and the
2580
# second bytes should be empty (because its a API proxy not something
2581
# for wire serialisation.
2582
self.assertEqual('', netb[1])
2584
kind, line_end = network_bytes_to_kind_and_offset(bytes)
2585
self.assertEqual('knit-delta-closure', kind)
2588
class TestContentMapGenerator(KnitTests):
2589
"""Tests for ContentMapGenerator"""
2591
def test_get_record_stream_gives_records(self):
2592
vf = self.make_test_knit(name='test')
2593
# put in three texts, giving ft, delta, delta
2594
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2595
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2596
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2597
keys = [('d1',), ('d2',)]
2598
generator = _VFContentMapGenerator(vf, keys,
2599
global_map=vf.get_parent_map(keys))
2600
for record in generator.get_record_stream():
2601
if record.key == ('d1',):
2602
self.assertEqual('d1\n', record.get_bytes_as('fulltext'))
2604
self.assertEqual('d2\n', record.get_bytes_as('fulltext'))
2606
def test_get_record_stream_kinds_are_raw(self):
2607
vf = self.make_test_knit(name='test')
2608
# put in three texts, giving ft, delta, delta
2609
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2610
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2611
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2612
keys = [('base',), ('d1',), ('d2',)]
2613
generator = _VFContentMapGenerator(vf, keys,
2614
global_map=vf.get_parent_map(keys))
2615
kinds = {('base',): 'knit-delta-closure',
2616
('d1',): 'knit-delta-closure-ref',
2617
('d2',): 'knit-delta-closure-ref',
2619
for record in generator.get_record_stream():
2620
self.assertEqual(kinds[record.key], record.storage_kind)