330
317
transport.append_bytes(packname, bytes)
331
318
writer = pack.ContainerWriter(write_data)
333
access = pack_repo._DirectPackAccess({})
320
access = _DirectPackAccess({})
334
321
access.set_writer(writer, index, (transport, packname))
335
322
return access, writer
337
def make_pack_file(self):
338
"""Create a pack file with 2 records."""
339
access, writer = self._get_access(packname='packname', index='foo')
341
memos.extend(access.add_raw_records([('key1', 10)], '1234567890'))
342
memos.extend(access.add_raw_records([('key2', 5)], '12345'))
346
def test_pack_collection_pack_retries(self):
347
"""An explicit pack of a pack collection succeeds even when a
348
concurrent pack happens.
350
builder = self.make_branch_builder('.')
351
builder.start_series()
352
builder.build_snapshot('rev-1', None, [
353
('add', ('', 'root-id', 'directory', None)),
354
('add', ('file', 'file-id', 'file', 'content\nrev 1\n')),
356
builder.build_snapshot('rev-2', ['rev-1'], [
357
('modify', ('file-id', 'content\nrev 2\n')),
359
builder.build_snapshot('rev-3', ['rev-2'], [
360
('modify', ('file-id', 'content\nrev 3\n')),
362
self.addCleanup(builder.finish_series)
363
b = builder.get_branch()
364
self.addCleanup(b.lock_write().unlock)
366
collection = repo._pack_collection
367
# Concurrently repack the repo.
368
reopened_repo = repo.bzrdir.open_repository()
373
def make_vf_for_retrying(self):
374
"""Create 3 packs and a reload function.
376
Originally, 2 pack files will have the data, but one will be missing.
377
And then the third will be used in place of the first two if reload()
380
:return: (versioned_file, reload_counter)
381
versioned_file a KnitVersionedFiles using the packs for access
383
builder = self.make_branch_builder('.', format="1.9")
384
builder.start_series()
385
builder.build_snapshot('rev-1', None, [
386
('add', ('', 'root-id', 'directory', None)),
387
('add', ('file', 'file-id', 'file', 'content\nrev 1\n')),
389
builder.build_snapshot('rev-2', ['rev-1'], [
390
('modify', ('file-id', 'content\nrev 2\n')),
392
builder.build_snapshot('rev-3', ['rev-2'], [
393
('modify', ('file-id', 'content\nrev 3\n')),
395
builder.finish_series()
396
b = builder.get_branch()
398
self.addCleanup(b.unlock)
399
# Pack these three revisions into another pack file, but don't remove
402
collection = repo._pack_collection
403
collection.ensure_loaded()
404
orig_packs = collection.packs
405
packer = knitpack_repo.KnitPacker(collection, orig_packs, '.testpack')
406
new_pack = packer.pack()
407
# forget about the new pack
411
# Set up a reload() function that switches to using the new pack file
412
new_index = new_pack.revision_index
413
access_tuple = new_pack.access_tuple()
414
reload_counter = [0, 0, 0]
416
reload_counter[0] += 1
417
if reload_counter[1] > 0:
418
# We already reloaded, nothing more to do
419
reload_counter[2] += 1
421
reload_counter[1] += 1
422
vf._index._graph_index._indices[:] = [new_index]
423
vf._access._indices.clear()
424
vf._access._indices[new_index] = access_tuple
426
# Delete one of the pack files so the data will need to be reloaded. We
427
# will delete the file with 'rev-2' in it
428
trans, name = orig_packs[1].access_tuple()
430
# We don't have the index trigger reloading because we want to test
431
# that we reload when the .pack disappears
432
vf._access._reload_func = reload
433
return vf, reload_counter
435
def make_reload_func(self, return_val=True):
438
reload_called[0] += 1
440
return reload_called, reload
442
def make_retry_exception(self):
443
# We raise a real exception so that sys.exc_info() is properly
446
raise _TestException('foobar')
447
except _TestException, e:
448
retry_exc = errors.RetryWithNewPacks(None, reload_occurred=False,
449
exc_info=sys.exc_info())
450
# GZ 2010-08-10: Cycle with exc_info affects 3 tests
453
324
def test_read_from_several_packs(self):
454
325
access, writer = self._get_access()
492
363
self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
494
def test_missing_index_raises_retry(self):
495
memos = self.make_pack_file()
496
transport = self.get_transport()
497
reload_called, reload_func = self.make_reload_func()
498
# Note that the index key has changed from 'foo' to 'bar'
499
access = pack_repo._DirectPackAccess({'bar':(transport, 'packname')},
500
reload_func=reload_func)
501
e = self.assertListRaises(errors.RetryWithNewPacks,
502
access.get_raw_records, memos)
503
# Because a key was passed in which does not match our index list, we
504
# assume that the listing was already reloaded
505
self.assertTrue(e.reload_occurred)
506
self.assertIsInstance(e.exc_info, tuple)
507
self.assertIs(e.exc_info[0], KeyError)
508
self.assertIsInstance(e.exc_info[1], KeyError)
510
def test_missing_index_raises_key_error_with_no_reload(self):
511
memos = self.make_pack_file()
512
transport = self.get_transport()
513
# Note that the index key has changed from 'foo' to 'bar'
514
access = pack_repo._DirectPackAccess({'bar':(transport, 'packname')})
515
e = self.assertListRaises(KeyError, access.get_raw_records, memos)
517
def test_missing_file_raises_retry(self):
518
memos = self.make_pack_file()
519
transport = self.get_transport()
520
reload_called, reload_func = self.make_reload_func()
521
# Note that the 'filename' has been changed to 'different-packname'
522
access = pack_repo._DirectPackAccess(
523
{'foo':(transport, 'different-packname')},
524
reload_func=reload_func)
525
e = self.assertListRaises(errors.RetryWithNewPacks,
526
access.get_raw_records, memos)
527
# The file has gone missing, so we assume we need to reload
528
self.assertFalse(e.reload_occurred)
529
self.assertIsInstance(e.exc_info, tuple)
530
self.assertIs(e.exc_info[0], errors.NoSuchFile)
531
self.assertIsInstance(e.exc_info[1], errors.NoSuchFile)
532
self.assertEqual('different-packname', e.exc_info[1].path)
534
def test_missing_file_raises_no_such_file_with_no_reload(self):
535
memos = self.make_pack_file()
536
transport = self.get_transport()
537
# Note that the 'filename' has been changed to 'different-packname'
538
access = pack_repo._DirectPackAccess(
539
{'foo': (transport, 'different-packname')})
540
e = self.assertListRaises(errors.NoSuchFile,
541
access.get_raw_records, memos)
543
def test_failing_readv_raises_retry(self):
544
memos = self.make_pack_file()
545
transport = self.get_transport()
546
failing_transport = MockReadvFailingTransport(
547
[transport.get_bytes('packname')])
548
reload_called, reload_func = self.make_reload_func()
549
access = pack_repo._DirectPackAccess(
550
{'foo': (failing_transport, 'packname')},
551
reload_func=reload_func)
552
# Asking for a single record will not trigger the Mock failure
553
self.assertEqual(['1234567890'],
554
list(access.get_raw_records(memos[:1])))
555
self.assertEqual(['12345'],
556
list(access.get_raw_records(memos[1:2])))
557
# A multiple offset readv() will fail mid-way through
558
e = self.assertListRaises(errors.RetryWithNewPacks,
559
access.get_raw_records, memos)
560
# The file has gone missing, so we assume we need to reload
561
self.assertFalse(e.reload_occurred)
562
self.assertIsInstance(e.exc_info, tuple)
563
self.assertIs(e.exc_info[0], errors.NoSuchFile)
564
self.assertIsInstance(e.exc_info[1], errors.NoSuchFile)
565
self.assertEqual('packname', e.exc_info[1].path)
567
def test_failing_readv_raises_no_such_file_with_no_reload(self):
568
memos = self.make_pack_file()
569
transport = self.get_transport()
570
failing_transport = MockReadvFailingTransport(
571
[transport.get_bytes('packname')])
572
reload_called, reload_func = self.make_reload_func()
573
access = pack_repo._DirectPackAccess(
574
{'foo':(failing_transport, 'packname')})
575
# Asking for a single record will not trigger the Mock failure
576
self.assertEqual(['1234567890'],
577
list(access.get_raw_records(memos[:1])))
578
self.assertEqual(['12345'],
579
list(access.get_raw_records(memos[1:2])))
580
# A multiple offset readv() will fail mid-way through
581
e = self.assertListRaises(errors.NoSuchFile,
582
access.get_raw_records, memos)
584
def test_reload_or_raise_no_reload(self):
585
access = pack_repo._DirectPackAccess({}, reload_func=None)
586
retry_exc = self.make_retry_exception()
587
# Without a reload_func, we will just re-raise the original exception
588
self.assertRaises(_TestException, access.reload_or_raise, retry_exc)
590
def test_reload_or_raise_reload_changed(self):
591
reload_called, reload_func = self.make_reload_func(return_val=True)
592
access = pack_repo._DirectPackAccess({}, reload_func=reload_func)
593
retry_exc = self.make_retry_exception()
594
access.reload_or_raise(retry_exc)
595
self.assertEqual([1], reload_called)
596
retry_exc.reload_occurred=True
597
access.reload_or_raise(retry_exc)
598
self.assertEqual([2], reload_called)
600
def test_reload_or_raise_reload_no_change(self):
601
reload_called, reload_func = self.make_reload_func(return_val=False)
602
access = pack_repo._DirectPackAccess({}, reload_func=reload_func)
603
retry_exc = self.make_retry_exception()
604
# If reload_occurred is False, then we consider it an error to have
605
# reload_func() return False (no changes).
606
self.assertRaises(_TestException, access.reload_or_raise, retry_exc)
607
self.assertEqual([1], reload_called)
608
retry_exc.reload_occurred=True
609
# If reload_occurred is True, then we assume nothing changed because
610
# it had changed earlier, but didn't change again
611
access.reload_or_raise(retry_exc)
612
self.assertEqual([2], reload_called)
614
def test_annotate_retries(self):
615
vf, reload_counter = self.make_vf_for_retrying()
616
# It is a little bit bogus to annotate the Revision VF, but it works,
617
# as we have ancestry stored there
619
reload_lines = vf.annotate(key)
620
self.assertEqual([1, 1, 0], reload_counter)
621
plain_lines = vf.annotate(key)
622
self.assertEqual([1, 1, 0], reload_counter) # No extra reloading
623
if reload_lines != plain_lines:
624
self.fail('Annotation was not identical with reloading.')
625
# Now delete the packs-in-use, which should trigger another reload, but
626
# this time we just raise an exception because we can't recover
627
for trans, name in vf._access._indices.itervalues():
629
self.assertRaises(errors.NoSuchFile, vf.annotate, key)
630
self.assertEqual([2, 1, 1], reload_counter)
632
def test__get_record_map_retries(self):
633
vf, reload_counter = self.make_vf_for_retrying()
634
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
635
records = vf._get_record_map(keys)
636
self.assertEqual(keys, sorted(records.keys()))
637
self.assertEqual([1, 1, 0], reload_counter)
638
# Now delete the packs-in-use, which should trigger another reload, but
639
# this time we just raise an exception because we can't recover
640
for trans, name in vf._access._indices.itervalues():
642
self.assertRaises(errors.NoSuchFile, vf._get_record_map, keys)
643
self.assertEqual([2, 1, 1], reload_counter)
645
def test_get_record_stream_retries(self):
646
vf, reload_counter = self.make_vf_for_retrying()
647
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
648
record_stream = vf.get_record_stream(keys, 'topological', False)
649
record = record_stream.next()
650
self.assertEqual(('rev-1',), record.key)
651
self.assertEqual([0, 0, 0], reload_counter)
652
record = record_stream.next()
653
self.assertEqual(('rev-2',), record.key)
654
self.assertEqual([1, 1, 0], reload_counter)
655
record = record_stream.next()
656
self.assertEqual(('rev-3',), record.key)
657
self.assertEqual([1, 1, 0], reload_counter)
658
# Now delete all pack files, and see that we raise the right error
659
for trans, name in vf._access._indices.itervalues():
661
self.assertListRaises(errors.NoSuchFile,
662
vf.get_record_stream, keys, 'topological', False)
664
def test_iter_lines_added_or_present_in_keys_retries(self):
665
vf, reload_counter = self.make_vf_for_retrying()
666
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
667
# Unfortunately, iter_lines_added_or_present_in_keys iterates the
668
# result in random order (determined by the iteration order from a
669
# set()), so we don't have any solid way to trigger whether data is
670
# read before or after. However we tried to delete the middle node to
671
# exercise the code well.
672
# What we care about is that all lines are always yielded, but not
675
reload_lines = sorted(vf.iter_lines_added_or_present_in_keys(keys))
676
self.assertEqual([1, 1, 0], reload_counter)
677
# Now do it again, to make sure the result is equivalent
678
plain_lines = sorted(vf.iter_lines_added_or_present_in_keys(keys))
679
self.assertEqual([1, 1, 0], reload_counter) # No extra reloading
680
self.assertEqual(plain_lines, reload_lines)
681
self.assertEqual(21, len(plain_lines))
682
# Now delete all pack files, and see that we raise the right error
683
for trans, name in vf._access._indices.itervalues():
685
self.assertListRaises(errors.NoSuchFile,
686
vf.iter_lines_added_or_present_in_keys, keys)
687
self.assertEqual([2, 1, 1], reload_counter)
689
def test_get_record_stream_yields_disk_sorted_order(self):
690
# if we get 'unordered' pick a semi-optimal order for reading. The
691
# order should be grouped by pack file, and then by position in file
692
repo = self.make_repository('test', format='pack-0.92')
694
self.addCleanup(repo.unlock)
695
repo.start_write_group()
697
vf.add_lines(('f-id', 'rev-5'), [('f-id', 'rev-4')], ['lines\n'])
698
vf.add_lines(('f-id', 'rev-1'), [], ['lines\n'])
699
vf.add_lines(('f-id', 'rev-2'), [('f-id', 'rev-1')], ['lines\n'])
700
repo.commit_write_group()
701
# We inserted them as rev-5, rev-1, rev-2, we should get them back in
703
stream = vf.get_record_stream([('f-id', 'rev-1'), ('f-id', 'rev-5'),
704
('f-id', 'rev-2')], 'unordered', False)
705
keys = [r.key for r in stream]
706
self.assertEqual([('f-id', 'rev-5'), ('f-id', 'rev-1'),
707
('f-id', 'rev-2')], keys)
708
repo.start_write_group()
709
vf.add_lines(('f-id', 'rev-4'), [('f-id', 'rev-3')], ['lines\n'])
710
vf.add_lines(('f-id', 'rev-3'), [('f-id', 'rev-2')], ['lines\n'])
711
vf.add_lines(('f-id', 'rev-6'), [('f-id', 'rev-5')], ['lines\n'])
712
repo.commit_write_group()
713
# Request in random order, to make sure the output order isn't based on
715
request_keys = set(('f-id', 'rev-%d' % i) for i in range(1, 7))
716
stream = vf.get_record_stream(request_keys, 'unordered', False)
717
keys = [r.key for r in stream]
718
# We want to get the keys back in disk order, but it doesn't matter
719
# which pack we read from first. So this can come back in 2 orders
720
alt1 = [('f-id', 'rev-%d' % i) for i in [4, 3, 6, 5, 1, 2]]
721
alt2 = [('f-id', 'rev-%d' % i) for i in [5, 1, 2, 4, 3, 6]]
722
if keys != alt1 and keys != alt2:
723
self.fail('Returned key order did not match either expected order.'
724
' expected %s or %s, not %s'
725
% (alt1, alt2, keys))
728
366
class LowLevelKnitDataTests(TestCase):
1319
899
class LowLevelKnitIndexTests_c(LowLevelKnitIndexTests):
1321
_test_needs_features = [compiled_knit_feature]
901
_test_needs_features = [CompiledKnitFeature]
1323
903
def get_knit_index(self, transport, name, mode):
1324
904
mapper = ConstantMapper(name)
1325
from bzrlib._knit_load_data_pyx import _load_data_c
1326
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
1327
911
allow_writes = lambda: mode == 'w'
1328
return _KndxIndex(transport, mapper, lambda:None,
1329
allow_writes, lambda:True)
1332
class Test_KnitAnnotator(TestCaseWithMemoryTransport):
1334
def make_annotator(self):
1335
factory = knit.make_pack_factory(True, True, 1)
1336
vf = factory(self.get_transport())
1337
return knit._KnitAnnotator(vf)
1339
def test__expand_fulltext(self):
1340
ann = self.make_annotator()
1341
rev_key = ('rev-id',)
1342
ann._num_compression_children[rev_key] = 1
1343
res = ann._expand_record(rev_key, (('parent-id',),), None,
1344
['line1\n', 'line2\n'], ('fulltext', True))
1345
# The content object and text lines should be cached appropriately
1346
self.assertEqual(['line1\n', 'line2'], res)
1347
content_obj = ann._content_objects[rev_key]
1348
self.assertEqual(['line1\n', 'line2\n'], content_obj._lines)
1349
self.assertEqual(res, content_obj.text())
1350
self.assertEqual(res, ann._text_cache[rev_key])
1352
def test__expand_delta_comp_parent_not_available(self):
1353
# Parent isn't available yet, so we return nothing, but queue up this
1354
# node for later processing
1355
ann = self.make_annotator()
1356
rev_key = ('rev-id',)
1357
parent_key = ('parent-id',)
1358
record = ['0,1,1\n', 'new-line\n']
1359
details = ('line-delta', False)
1360
res = ann._expand_record(rev_key, (parent_key,), parent_key,
1362
self.assertEqual(None, res)
1363
self.assertTrue(parent_key in ann._pending_deltas)
1364
pending = ann._pending_deltas[parent_key]
1365
self.assertEqual(1, len(pending))
1366
self.assertEqual((rev_key, (parent_key,), record, details), pending[0])
1368
def test__expand_record_tracks_num_children(self):
1369
ann = self.make_annotator()
1370
rev_key = ('rev-id',)
1371
rev2_key = ('rev2-id',)
1372
parent_key = ('parent-id',)
1373
record = ['0,1,1\n', 'new-line\n']
1374
details = ('line-delta', False)
1375
ann._num_compression_children[parent_key] = 2
1376
ann._expand_record(parent_key, (), None, ['line1\n', 'line2\n'],
1377
('fulltext', False))
1378
res = ann._expand_record(rev_key, (parent_key,), parent_key,
1380
self.assertEqual({parent_key: 1}, ann._num_compression_children)
1381
# Expanding the second child should remove the content object, and the
1382
# num_compression_children entry
1383
res = ann._expand_record(rev2_key, (parent_key,), parent_key,
1385
self.assertFalse(parent_key in ann._content_objects)
1386
self.assertEqual({}, ann._num_compression_children)
1387
# We should not cache the content_objects for rev2 and rev, because
1388
# they do not have compression children of their own.
1389
self.assertEqual({}, ann._content_objects)
1391
def test__expand_delta_records_blocks(self):
1392
ann = self.make_annotator()
1393
rev_key = ('rev-id',)
1394
parent_key = ('parent-id',)
1395
record = ['0,1,1\n', 'new-line\n']
1396
details = ('line-delta', True)
1397
ann._num_compression_children[parent_key] = 2
1398
ann._expand_record(parent_key, (), None,
1399
['line1\n', 'line2\n', 'line3\n'],
1400
('fulltext', False))
1401
ann._expand_record(rev_key, (parent_key,), parent_key, record, details)
1402
self.assertEqual({(rev_key, parent_key): [(1, 1, 1), (3, 3, 0)]},
1403
ann._matching_blocks)
1404
rev2_key = ('rev2-id',)
1405
record = ['0,1,1\n', 'new-line\n']
1406
details = ('line-delta', False)
1407
ann._expand_record(rev2_key, (parent_key,), parent_key, record, details)
1408
self.assertEqual([(1, 1, 2), (3, 3, 0)],
1409
ann._matching_blocks[(rev2_key, parent_key)])
1411
def test__get_parent_ann_uses_matching_blocks(self):
1412
ann = self.make_annotator()
1413
rev_key = ('rev-id',)
1414
parent_key = ('parent-id',)
1415
parent_ann = [(parent_key,)]*3
1416
block_key = (rev_key, parent_key)
1417
ann._annotations_cache[parent_key] = parent_ann
1418
ann._matching_blocks[block_key] = [(0, 1, 1), (3, 3, 0)]
1419
# We should not try to access any parent_lines content, because we know
1420
# we already have the matching blocks
1421
par_ann, blocks = ann._get_parent_annotations_and_matches(rev_key,
1422
['1\n', '2\n', '3\n'], parent_key)
1423
self.assertEqual(parent_ann, par_ann)
1424
self.assertEqual([(0, 1, 1), (3, 3, 0)], blocks)
1425
self.assertEqual({}, ann._matching_blocks)
1427
def test__process_pending(self):
1428
ann = self.make_annotator()
1429
rev_key = ('rev-id',)
1432
record = ['0,1,1\n', 'new-line\n']
1433
details = ('line-delta', False)
1434
p1_record = ['line1\n', 'line2\n']
1435
ann._num_compression_children[p1_key] = 1
1436
res = ann._expand_record(rev_key, (p1_key,p2_key), p1_key,
1438
self.assertEqual(None, res)
1439
# self.assertTrue(p1_key in ann._pending_deltas)
1440
self.assertEqual({}, ann._pending_annotation)
1441
# Now insert p1, and we should be able to expand the delta
1442
res = ann._expand_record(p1_key, (), None, p1_record,
1443
('fulltext', False))
1444
self.assertEqual(p1_record, res)
1445
ann._annotations_cache[p1_key] = [(p1_key,)]*2
1446
res = ann._process_pending(p1_key)
1447
self.assertEqual([], res)
1448
self.assertFalse(p1_key in ann._pending_deltas)
1449
self.assertTrue(p2_key in ann._pending_annotation)
1450
self.assertEqual({p2_key: [(rev_key, (p1_key, p2_key))]},
1451
ann._pending_annotation)
1452
# Now fill in parent 2, and pending annotation should be satisfied
1453
res = ann._expand_record(p2_key, (), None, [], ('fulltext', False))
1454
ann._annotations_cache[p2_key] = []
1455
res = ann._process_pending(p2_key)
1456
self.assertEqual([rev_key], res)
1457
self.assertEqual({}, ann._pending_annotation)
1458
self.assertEqual({}, ann._pending_deltas)
1460
def test_record_delta_removes_basis(self):
1461
ann = self.make_annotator()
1462
ann._expand_record(('parent-id',), (), None,
1463
['line1\n', 'line2\n'], ('fulltext', False))
1464
ann._num_compression_children['parent-id'] = 2
1466
def test_annotate_special_text(self):
1467
ann = self.make_annotator()
1469
rev1_key = ('rev-1',)
1470
rev2_key = ('rev-2',)
1471
rev3_key = ('rev-3',)
1472
spec_key = ('special:',)
1473
vf.add_lines(rev1_key, [], ['initial content\n'])
1474
vf.add_lines(rev2_key, [rev1_key], ['initial content\n',
1477
vf.add_lines(rev3_key, [rev1_key], ['initial content\n',
1480
spec_text = ('initial content\n'
1484
ann.add_special_text(spec_key, [rev2_key, rev3_key], spec_text)
1485
anns, lines = ann.annotate(spec_key)
1486
self.assertEqual([(rev1_key,),
1487
(rev2_key, rev3_key),
1491
self.assertEqualDiff(spec_text, ''.join(lines))
912
return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
1494
915
class KnitTests(TestCaseWithTransport):
1805
1203
# change options in the second record
1806
1204
self.assertRaises(errors.KnitCorrupt, index.add_records,
1807
1205
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)]),
1808
(('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
1206
(('tip',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1809
1207
self.assertEqual([], self.caught_entries)
1811
def make_g_index_missing_compression_parent(self):
1812
graph_index = self.make_g_index('missing_comp', 2,
1813
[(('tip', ), ' 100 78',
1814
([('missing-parent', ), ('ghost', )], [('missing-parent', )]))])
1817
def make_g_index_missing_parent(self):
1818
graph_index = self.make_g_index('missing_parent', 2,
1819
[(('parent', ), ' 100 78', ([], [])),
1820
(('tip', ), ' 100 78',
1821
([('parent', ), ('missing-parent', )], [('parent', )])),
1825
def make_g_index_no_external_refs(self):
1826
graph_index = self.make_g_index('no_external_refs', 2,
1827
[(('rev', ), ' 100 78',
1828
([('parent', ), ('ghost', )], []))])
1831
def test_add_good_unvalidated_index(self):
1832
unvalidated = self.make_g_index_no_external_refs()
1833
combined = CombinedGraphIndex([unvalidated])
1834
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1835
index.scan_unvalidated_index(unvalidated)
1836
self.assertEqual(frozenset(), index.get_missing_compression_parents())
1838
def test_add_missing_compression_parent_unvalidated_index(self):
1839
unvalidated = self.make_g_index_missing_compression_parent()
1840
combined = CombinedGraphIndex([unvalidated])
1841
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1842
index.scan_unvalidated_index(unvalidated)
1843
# This also checks that its only the compression parent that is
1844
# examined, otherwise 'ghost' would also be reported as a missing
1847
frozenset([('missing-parent',)]),
1848
index.get_missing_compression_parents())
1850
def test_add_missing_noncompression_parent_unvalidated_index(self):
1851
unvalidated = self.make_g_index_missing_parent()
1852
combined = CombinedGraphIndex([unvalidated])
1853
index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1854
track_external_parent_refs=True)
1855
index.scan_unvalidated_index(unvalidated)
1857
frozenset([('missing-parent',)]), index.get_missing_parents())
1859
def test_track_external_parent_refs(self):
1860
g_index = self.make_g_index('empty', 2, [])
1861
combined = CombinedGraphIndex([g_index])
1862
index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1863
add_callback=self.catch_add, track_external_parent_refs=True)
1864
self.caught_entries = []
1866
(('new-key',), 'fulltext,no-eol', (None, 50, 60),
1867
[('parent-1',), ('parent-2',)])])
1869
frozenset([('parent-1',), ('parent-2',)]),
1870
index.get_missing_parents())
1872
def test_add_unvalidated_index_with_present_external_references(self):
1873
index = self.two_graph_index(deltas=True)
1874
# Ugly hack to get at one of the underlying GraphIndex objects that
1875
# two_graph_index built.
1876
unvalidated = index._graph_index._indices[1]
1877
# 'parent' is an external ref of _indices[1] (unvalidated), but is
1878
# present in _indices[0].
1879
index.scan_unvalidated_index(unvalidated)
1880
self.assertEqual(frozenset(), index.get_missing_compression_parents())
1882
def make_new_missing_parent_g_index(self, name):
1883
missing_parent = name + '-missing-parent'
1884
graph_index = self.make_g_index(name, 2,
1885
[((name + 'tip', ), ' 100 78',
1886
([(missing_parent, ), ('ghost', )], [(missing_parent, )]))])
1889
def test_add_mulitiple_unvalidated_indices_with_missing_parents(self):
1890
g_index_1 = self.make_new_missing_parent_g_index('one')
1891
g_index_2 = self.make_new_missing_parent_g_index('two')
1892
combined = CombinedGraphIndex([g_index_1, g_index_2])
1893
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1894
index.scan_unvalidated_index(g_index_1)
1895
index.scan_unvalidated_index(g_index_2)
1897
frozenset([('one-missing-parent',), ('two-missing-parent',)]),
1898
index.get_missing_compression_parents())
1900
def test_add_mulitiple_unvalidated_indices_with_mutual_dependencies(self):
1901
graph_index_a = self.make_g_index('one', 2,
1902
[(('parent-one', ), ' 100 78', ([('non-compression-parent',)], [])),
1903
(('child-of-two', ), ' 100 78',
1904
([('parent-two',)], [('parent-two',)]))])
1905
graph_index_b = self.make_g_index('two', 2,
1906
[(('parent-two', ), ' 100 78', ([('non-compression-parent',)], [])),
1907
(('child-of-one', ), ' 100 78',
1908
([('parent-one',)], [('parent-one',)]))])
1909
combined = CombinedGraphIndex([graph_index_a, graph_index_b])
1910
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1911
index.scan_unvalidated_index(graph_index_a)
1912
index.scan_unvalidated_index(graph_index_b)
1914
frozenset([]), index.get_missing_compression_parents())
1917
1210
class TestNoParentsGraphIndexKnit(KnitTests):
1918
1211
"""Tests for knits using _KnitGraphIndex with no parents."""
2092
1377
self.assertEqual([], self.caught_entries)
2095
class TestKnitVersionedFiles(KnitTests):
2097
def assertGroupKeysForIo(self, exp_groups, keys, non_local_keys,
2098
positions, _min_buffer_size=None):
2099
kvf = self.make_test_knit()
2100
if _min_buffer_size is None:
2101
_min_buffer_size = knit._STREAM_MIN_BUFFER_SIZE
2102
self.assertEqual(exp_groups, kvf._group_keys_for_io(keys,
2103
non_local_keys, positions,
2104
_min_buffer_size=_min_buffer_size))
2106
def assertSplitByPrefix(self, expected_map, expected_prefix_order,
2108
split, prefix_order = KnitVersionedFiles._split_by_prefix(keys)
2109
self.assertEqual(expected_map, split)
2110
self.assertEqual(expected_prefix_order, prefix_order)
2112
def test__group_keys_for_io(self):
2113
ft_detail = ('fulltext', False)
2114
ld_detail = ('line-delta', False)
2122
f_a: (ft_detail, (f_a, 0, 100), None),
2123
f_b: (ld_detail, (f_b, 100, 21), f_a),
2124
f_c: (ld_detail, (f_c, 180, 15), f_b),
2125
g_a: (ft_detail, (g_a, 121, 35), None),
2126
g_b: (ld_detail, (g_b, 156, 12), g_a),
2127
g_c: (ld_detail, (g_c, 195, 13), g_a),
2129
self.assertGroupKeysForIo([([f_a], set())],
2130
[f_a], [], positions)
2131
self.assertGroupKeysForIo([([f_a], set([f_a]))],
2132
[f_a], [f_a], positions)
2133
self.assertGroupKeysForIo([([f_a, f_b], set([]))],
2134
[f_a, f_b], [], positions)
2135
self.assertGroupKeysForIo([([f_a, f_b], set([f_b]))],
2136
[f_a, f_b], [f_b], positions)
2137
self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
2138
[f_a, g_a, f_b, g_b], [], positions)
2139
self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
2140
[f_a, g_a, f_b, g_b], [], positions,
2141
_min_buffer_size=150)
2142
self.assertGroupKeysForIo([([f_a, f_b], set()), ([g_a, g_b], set())],
2143
[f_a, g_a, f_b, g_b], [], positions,
2144
_min_buffer_size=100)
2145
self.assertGroupKeysForIo([([f_c], set()), ([g_b], set())],
2146
[f_c, g_b], [], positions,
2147
_min_buffer_size=125)
2148
self.assertGroupKeysForIo([([g_b, f_c], set())],
2149
[g_b, f_c], [], positions,
2150
_min_buffer_size=125)
2152
def test__split_by_prefix(self):
2153
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2154
'g': [('g', 'b'), ('g', 'a')],
2156
[('f', 'a'), ('g', 'b'),
2157
('g', 'a'), ('f', 'b')])
2159
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2160
'g': [('g', 'b'), ('g', 'a')],
2162
[('f', 'a'), ('f', 'b'),
2163
('g', 'b'), ('g', 'a')])
2165
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2166
'g': [('g', 'b'), ('g', 'a')],
2168
[('f', 'a'), ('f', 'b'),
2169
('g', 'b'), ('g', 'a')])
2171
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2172
'g': [('g', 'b'), ('g', 'a')],
2173
'': [('a',), ('b',)]
2175
[('f', 'a'), ('g', 'b'),
2177
('g', 'a'), ('f', 'b')])
2180
1380
class TestStacking(KnitTests):
2182
1382
def get_basis_and_test_knit(self):
2569
1762
multiparent.NewText(['foo\n']),
2570
1763
multiparent.ParentText(1, 0, 2, 1)])],
2572
self.assertEqual(3, len(basis.calls))
1765
self.assertEqual(4, len(basis.calls))
2573
1766
self.assertEqual([
2574
1767
("get_parent_map", set([key_left, key_right])),
2575
1768
("get_parent_map", set([key_left, key_right])),
1769
("get_parent_map", set([key_left, key_right])),
2578
last_call = basis.calls[-1]
1772
last_call = basis.calls[3]
2579
1773
self.assertEqual('get_record_stream', last_call[0])
2580
1774
self.assertEqual(set([key_left, key_right]), set(last_call[1]))
2581
self.assertEqual('topological', last_call[2])
1775
self.assertEqual('unordered', last_call[2])
2582
1776
self.assertEqual(True, last_call[3])
2585
class TestNetworkBehaviour(KnitTests):
2586
"""Tests for getting data out of/into knits over the network."""
2588
def test_include_delta_closure_generates_a_knit_delta_closure(self):
2589
vf = self.make_test_knit(name='test')
2590
# put in three texts, giving ft, delta, delta
2591
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2592
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2593
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2594
# But heuristics could interfere, so check what happened:
2595
self.assertEqual(['knit-ft-gz', 'knit-delta-gz', 'knit-delta-gz'],
2596
[record.storage_kind for record in
2597
vf.get_record_stream([('base',), ('d1',), ('d2',)],
2598
'topological', False)])
2599
# generate a stream of just the deltas include_delta_closure=True,
2600
# serialise to the network, and check that we get a delta closure on the wire.
2601
stream = vf.get_record_stream([('d1',), ('d2',)], 'topological', True)
2602
netb = [record.get_bytes_as(record.storage_kind) for record in stream]
2603
# The first bytes should be a memo from _ContentMapGenerator, and the
2604
# second bytes should be empty (because its a API proxy not something
2605
# for wire serialisation.
2606
self.assertEqual('', netb[1])
2608
kind, line_end = network_bytes_to_kind_and_offset(bytes)
2609
self.assertEqual('knit-delta-closure', kind)
2612
class TestContentMapGenerator(KnitTests):
2613
"""Tests for ContentMapGenerator"""
2615
def test_get_record_stream_gives_records(self):
2616
vf = self.make_test_knit(name='test')
2617
# put in three texts, giving ft, delta, delta
2618
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2619
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2620
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2621
keys = [('d1',), ('d2',)]
2622
generator = _VFContentMapGenerator(vf, keys,
2623
global_map=vf.get_parent_map(keys))
2624
for record in generator.get_record_stream():
2625
if record.key == ('d1',):
2626
self.assertEqual('d1\n', record.get_bytes_as('fulltext'))
2628
self.assertEqual('d2\n', record.get_bytes_as('fulltext'))
2630
def test_get_record_stream_kinds_are_raw(self):
2631
vf = self.make_test_knit(name='test')
2632
# put in three texts, giving ft, delta, delta
2633
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2634
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2635
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2636
keys = [('base',), ('d1',), ('d2',)]
2637
generator = _VFContentMapGenerator(vf, keys,
2638
global_map=vf.get_parent_map(keys))
2639
kinds = {('base',): 'knit-delta-closure',
2640
('d1',): 'knit-delta-closure-ref',
2641
('d2',): 'knit-delta-closure-ref',
2643
for record in generator.get_record_stream():
2644
self.assertEqual(kinds[record.key], record.storage_kind)