364
348
:return: (versioned_file, reload_counter)
365
349
versioned_file a KnitVersionedFiles using the packs for access
367
tree = self.make_branch_and_memory_tree('tree')
370
tree.add([''], ['root-id'])
371
tree.commit('one', rev_id='rev-1')
372
tree.commit('two', rev_id='rev-2')
373
tree.commit('three', rev_id='rev-3')
374
# Pack these two revisions into another pack file, but don't remove
376
repo = tree.branch.repository
377
collection = repo._pack_collection
378
collection.ensure_loaded()
379
orig_packs = collection.packs
380
packer = pack_repo.Packer(collection, orig_packs, '.testpack')
381
new_pack = packer.pack()
383
vf = tree.branch.repository.revisions
386
tree.branch.repository.lock_read()
387
self.addCleanup(tree.branch.repository.unlock)
351
builder = self.make_branch_builder('.', format="1.9")
352
builder.start_series()
353
builder.build_snapshot('rev-1', None, [
354
('add', ('', 'root-id', 'directory', None)),
355
('add', ('file', 'file-id', 'file', 'content\nrev 1\n')),
357
builder.build_snapshot('rev-2', ['rev-1'], [
358
('modify', ('file-id', 'content\nrev 2\n')),
360
builder.build_snapshot('rev-3', ['rev-2'], [
361
('modify', ('file-id', 'content\nrev 3\n')),
363
builder.finish_series()
364
b = builder.get_branch()
366
self.addCleanup(b.unlock)
367
# Pack these three revisions into another pack file, but don't remove
370
collection = repo._pack_collection
371
collection.ensure_loaded()
372
orig_packs = collection.packs
373
packer = pack_repo.Packer(collection, orig_packs, '.testpack')
374
new_pack = packer.pack()
375
# forget about the new pack
389
379
# Set up a reload() function that switches to using the new pack file
390
380
new_index = new_pack.revision_index
391
381
access_tuple = new_pack.access_tuple()
1091
1077
call[1][1].getvalue())
1092
1078
self.assertEqual({'create_parent_dir': True}, call[2])
1080
def assertTotalBuildSize(self, size, keys, positions):
1081
self.assertEqual(size,
1082
knit._get_total_build_size(None, keys, positions))
1084
def test__get_total_build_size(self):
1086
('a',): (('fulltext', False), (('a',), 0, 100), None),
1087
('b',): (('line-delta', False), (('b',), 100, 21), ('a',)),
1088
('c',): (('line-delta', False), (('c',), 121, 35), ('b',)),
1089
('d',): (('line-delta', False), (('d',), 156, 12), ('b',)),
1091
self.assertTotalBuildSize(100, [('a',)], positions)
1092
self.assertTotalBuildSize(121, [('b',)], positions)
1093
# c needs both a & b
1094
self.assertTotalBuildSize(156, [('c',)], positions)
1095
# we shouldn't count 'b' twice
1096
self.assertTotalBuildSize(156, [('b',), ('c',)], positions)
1097
self.assertTotalBuildSize(133, [('d',)], positions)
1098
self.assertTotalBuildSize(168, [('c',), ('d',)], positions)
1094
1100
def test_get_position(self):
1095
1101
transport = MockTransport([
1096
1102
_KndxIndex.HEADER,
1272
1287
class LowLevelKnitIndexTests_c(LowLevelKnitIndexTests):
1274
_test_needs_features = [CompiledKnitFeature]
1289
_test_needs_features = [compiled_knit_feature]
1276
1291
def get_knit_index(self, transport, name, mode):
1277
1292
mapper = ConstantMapper(name)
1278
orig = knit._load_data
1280
knit._load_data = orig
1281
self.addCleanup(reset)
1282
from bzrlib._knit_load_data_c import _load_data_c
1283
knit._load_data = _load_data_c
1293
from bzrlib._knit_load_data_pyx import _load_data_c
1294
self.overrideAttr(knit, '_load_data', _load_data_c)
1284
1295
allow_writes = lambda: mode == 'w'
1285
return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
1296
return _KndxIndex(transport, mapper, lambda:None,
1297
allow_writes, lambda:True)
1300
class Test_KnitAnnotator(TestCaseWithMemoryTransport):
1302
def make_annotator(self):
1303
factory = knit.make_pack_factory(True, True, 1)
1304
vf = factory(self.get_transport())
1305
return knit._KnitAnnotator(vf)
1307
def test__expand_fulltext(self):
1308
ann = self.make_annotator()
1309
rev_key = ('rev-id',)
1310
ann._num_compression_children[rev_key] = 1
1311
res = ann._expand_record(rev_key, (('parent-id',),), None,
1312
['line1\n', 'line2\n'], ('fulltext', True))
1313
# The content object and text lines should be cached appropriately
1314
self.assertEqual(['line1\n', 'line2'], res)
1315
content_obj = ann._content_objects[rev_key]
1316
self.assertEqual(['line1\n', 'line2\n'], content_obj._lines)
1317
self.assertEqual(res, content_obj.text())
1318
self.assertEqual(res, ann._text_cache[rev_key])
1320
def test__expand_delta_comp_parent_not_available(self):
1321
# Parent isn't available yet, so we return nothing, but queue up this
1322
# node for later processing
1323
ann = self.make_annotator()
1324
rev_key = ('rev-id',)
1325
parent_key = ('parent-id',)
1326
record = ['0,1,1\n', 'new-line\n']
1327
details = ('line-delta', False)
1328
res = ann._expand_record(rev_key, (parent_key,), parent_key,
1330
self.assertEqual(None, res)
1331
self.assertTrue(parent_key in ann._pending_deltas)
1332
pending = ann._pending_deltas[parent_key]
1333
self.assertEqual(1, len(pending))
1334
self.assertEqual((rev_key, (parent_key,), record, details), pending[0])
1336
def test__expand_record_tracks_num_children(self):
1337
ann = self.make_annotator()
1338
rev_key = ('rev-id',)
1339
rev2_key = ('rev2-id',)
1340
parent_key = ('parent-id',)
1341
record = ['0,1,1\n', 'new-line\n']
1342
details = ('line-delta', False)
1343
ann._num_compression_children[parent_key] = 2
1344
ann._expand_record(parent_key, (), None, ['line1\n', 'line2\n'],
1345
('fulltext', False))
1346
res = ann._expand_record(rev_key, (parent_key,), parent_key,
1348
self.assertEqual({parent_key: 1}, ann._num_compression_children)
1349
# Expanding the second child should remove the content object, and the
1350
# num_compression_children entry
1351
res = ann._expand_record(rev2_key, (parent_key,), parent_key,
1353
self.assertFalse(parent_key in ann._content_objects)
1354
self.assertEqual({}, ann._num_compression_children)
1355
# We should not cache the content_objects for rev2 and rev, because
1356
# they do not have compression children of their own.
1357
self.assertEqual({}, ann._content_objects)
1359
def test__expand_delta_records_blocks(self):
1360
ann = self.make_annotator()
1361
rev_key = ('rev-id',)
1362
parent_key = ('parent-id',)
1363
record = ['0,1,1\n', 'new-line\n']
1364
details = ('line-delta', True)
1365
ann._num_compression_children[parent_key] = 2
1366
ann._expand_record(parent_key, (), None,
1367
['line1\n', 'line2\n', 'line3\n'],
1368
('fulltext', False))
1369
ann._expand_record(rev_key, (parent_key,), parent_key, record, details)
1370
self.assertEqual({(rev_key, parent_key): [(1, 1, 1), (3, 3, 0)]},
1371
ann._matching_blocks)
1372
rev2_key = ('rev2-id',)
1373
record = ['0,1,1\n', 'new-line\n']
1374
details = ('line-delta', False)
1375
ann._expand_record(rev2_key, (parent_key,), parent_key, record, details)
1376
self.assertEqual([(1, 1, 2), (3, 3, 0)],
1377
ann._matching_blocks[(rev2_key, parent_key)])
1379
def test__get_parent_ann_uses_matching_blocks(self):
1380
ann = self.make_annotator()
1381
rev_key = ('rev-id',)
1382
parent_key = ('parent-id',)
1383
parent_ann = [(parent_key,)]*3
1384
block_key = (rev_key, parent_key)
1385
ann._annotations_cache[parent_key] = parent_ann
1386
ann._matching_blocks[block_key] = [(0, 1, 1), (3, 3, 0)]
1387
# We should not try to access any parent_lines content, because we know
1388
# we already have the matching blocks
1389
par_ann, blocks = ann._get_parent_annotations_and_matches(rev_key,
1390
['1\n', '2\n', '3\n'], parent_key)
1391
self.assertEqual(parent_ann, par_ann)
1392
self.assertEqual([(0, 1, 1), (3, 3, 0)], blocks)
1393
self.assertEqual({}, ann._matching_blocks)
1395
def test__process_pending(self):
1396
ann = self.make_annotator()
1397
rev_key = ('rev-id',)
1400
record = ['0,1,1\n', 'new-line\n']
1401
details = ('line-delta', False)
1402
p1_record = ['line1\n', 'line2\n']
1403
ann._num_compression_children[p1_key] = 1
1404
res = ann._expand_record(rev_key, (p1_key,p2_key), p1_key,
1406
self.assertEqual(None, res)
1407
# self.assertTrue(p1_key in ann._pending_deltas)
1408
self.assertEqual({}, ann._pending_annotation)
1409
# Now insert p1, and we should be able to expand the delta
1410
res = ann._expand_record(p1_key, (), None, p1_record,
1411
('fulltext', False))
1412
self.assertEqual(p1_record, res)
1413
ann._annotations_cache[p1_key] = [(p1_key,)]*2
1414
res = ann._process_pending(p1_key)
1415
self.assertEqual([], res)
1416
self.assertFalse(p1_key in ann._pending_deltas)
1417
self.assertTrue(p2_key in ann._pending_annotation)
1418
self.assertEqual({p2_key: [(rev_key, (p1_key, p2_key))]},
1419
ann._pending_annotation)
1420
# Now fill in parent 2, and pending annotation should be satisfied
1421
res = ann._expand_record(p2_key, (), None, [], ('fulltext', False))
1422
ann._annotations_cache[p2_key] = []
1423
res = ann._process_pending(p2_key)
1424
self.assertEqual([rev_key], res)
1425
self.assertEqual({}, ann._pending_annotation)
1426
self.assertEqual({}, ann._pending_deltas)
1428
def test_record_delta_removes_basis(self):
1429
ann = self.make_annotator()
1430
ann._expand_record(('parent-id',), (), None,
1431
['line1\n', 'line2\n'], ('fulltext', False))
1432
ann._num_compression_children['parent-id'] = 2
1434
def test_annotate_special_text(self):
1435
ann = self.make_annotator()
1437
rev1_key = ('rev-1',)
1438
rev2_key = ('rev-2',)
1439
rev3_key = ('rev-3',)
1440
spec_key = ('special:',)
1441
vf.add_lines(rev1_key, [], ['initial content\n'])
1442
vf.add_lines(rev2_key, [rev1_key], ['initial content\n',
1445
vf.add_lines(rev3_key, [rev1_key], ['initial content\n',
1448
spec_text = ('initial content\n'
1452
ann.add_special_text(spec_key, [rev2_key, rev3_key], spec_text)
1453
anns, lines = ann.annotate(spec_key)
1454
self.assertEqual([(rev1_key,),
1455
(rev2_key, rev3_key),
1459
self.assertEqualDiff(spec_text, ''.join(lines))
1288
1462
class KnitTests(TestCaseWithTransport):
1601
1776
(('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
1602
1777
self.assertEqual([], self.caught_entries)
1779
def make_g_index_missing_compression_parent(self):
1780
graph_index = self.make_g_index('missing_comp', 2,
1781
[(('tip', ), ' 100 78',
1782
([('missing-parent', ), ('ghost', )], [('missing-parent', )]))])
1785
def make_g_index_missing_parent(self):
1786
graph_index = self.make_g_index('missing_parent', 2,
1787
[(('parent', ), ' 100 78', ([], [])),
1788
(('tip', ), ' 100 78',
1789
([('parent', ), ('missing-parent', )], [('parent', )])),
1793
def make_g_index_no_external_refs(self):
1794
graph_index = self.make_g_index('no_external_refs', 2,
1795
[(('rev', ), ' 100 78',
1796
([('parent', ), ('ghost', )], []))])
1799
def test_add_good_unvalidated_index(self):
1800
unvalidated = self.make_g_index_no_external_refs()
1801
combined = CombinedGraphIndex([unvalidated])
1802
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1803
index.scan_unvalidated_index(unvalidated)
1804
self.assertEqual(frozenset(), index.get_missing_compression_parents())
1806
def test_add_missing_compression_parent_unvalidated_index(self):
1807
unvalidated = self.make_g_index_missing_compression_parent()
1808
combined = CombinedGraphIndex([unvalidated])
1809
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1810
index.scan_unvalidated_index(unvalidated)
1811
# This also checks that its only the compression parent that is
1812
# examined, otherwise 'ghost' would also be reported as a missing
1815
frozenset([('missing-parent',)]),
1816
index.get_missing_compression_parents())
1818
def test_add_missing_noncompression_parent_unvalidated_index(self):
1819
unvalidated = self.make_g_index_missing_parent()
1820
combined = CombinedGraphIndex([unvalidated])
1821
index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1822
track_external_parent_refs=True)
1823
index.scan_unvalidated_index(unvalidated)
1825
frozenset([('missing-parent',)]), index.get_missing_parents())
1827
def test_track_external_parent_refs(self):
1828
g_index = self.make_g_index('empty', 2, [])
1829
combined = CombinedGraphIndex([g_index])
1830
index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1831
add_callback=self.catch_add, track_external_parent_refs=True)
1832
self.caught_entries = []
1834
(('new-key',), 'fulltext,no-eol', (None, 50, 60),
1835
[('parent-1',), ('parent-2',)])])
1837
frozenset([('parent-1',), ('parent-2',)]),
1838
index.get_missing_parents())
1840
def test_add_unvalidated_index_with_present_external_references(self):
1841
index = self.two_graph_index(deltas=True)
1842
# Ugly hack to get at one of the underlying GraphIndex objects that
1843
# two_graph_index built.
1844
unvalidated = index._graph_index._indices[1]
1845
# 'parent' is an external ref of _indices[1] (unvalidated), but is
1846
# present in _indices[0].
1847
index.scan_unvalidated_index(unvalidated)
1848
self.assertEqual(frozenset(), index.get_missing_compression_parents())
1850
def make_new_missing_parent_g_index(self, name):
1851
missing_parent = name + '-missing-parent'
1852
graph_index = self.make_g_index(name, 2,
1853
[((name + 'tip', ), ' 100 78',
1854
([(missing_parent, ), ('ghost', )], [(missing_parent, )]))])
1857
def test_add_mulitiple_unvalidated_indices_with_missing_parents(self):
1858
g_index_1 = self.make_new_missing_parent_g_index('one')
1859
g_index_2 = self.make_new_missing_parent_g_index('two')
1860
combined = CombinedGraphIndex([g_index_1, g_index_2])
1861
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1862
index.scan_unvalidated_index(g_index_1)
1863
index.scan_unvalidated_index(g_index_2)
1865
frozenset([('one-missing-parent',), ('two-missing-parent',)]),
1866
index.get_missing_compression_parents())
1868
def test_add_mulitiple_unvalidated_indices_with_mutual_dependencies(self):
1869
graph_index_a = self.make_g_index('one', 2,
1870
[(('parent-one', ), ' 100 78', ([('non-compression-parent',)], [])),
1871
(('child-of-two', ), ' 100 78',
1872
([('parent-two',)], [('parent-two',)]))])
1873
graph_index_b = self.make_g_index('two', 2,
1874
[(('parent-two', ), ' 100 78', ([('non-compression-parent',)], [])),
1875
(('child-of-one', ), ' 100 78',
1876
([('parent-one',)], [('parent-one',)]))])
1877
combined = CombinedGraphIndex([graph_index_a, graph_index_b])
1878
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1879
index.scan_unvalidated_index(graph_index_a)
1880
index.scan_unvalidated_index(graph_index_b)
1882
frozenset([]), index.get_missing_compression_parents())
1605
1885
class TestNoParentsGraphIndexKnit(KnitTests):
1606
1886
"""Tests for knits using _KnitGraphIndex with no parents."""
1772
2060
self.assertEqual([], self.caught_entries)
2063
class TestKnitVersionedFiles(KnitTests):
2065
def assertGroupKeysForIo(self, exp_groups, keys, non_local_keys,
2066
positions, _min_buffer_size=None):
2067
kvf = self.make_test_knit()
2068
if _min_buffer_size is None:
2069
_min_buffer_size = knit._STREAM_MIN_BUFFER_SIZE
2070
self.assertEqual(exp_groups, kvf._group_keys_for_io(keys,
2071
non_local_keys, positions,
2072
_min_buffer_size=_min_buffer_size))
2074
def assertSplitByPrefix(self, expected_map, expected_prefix_order,
2076
split, prefix_order = KnitVersionedFiles._split_by_prefix(keys)
2077
self.assertEqual(expected_map, split)
2078
self.assertEqual(expected_prefix_order, prefix_order)
2080
def test__group_keys_for_io(self):
2081
ft_detail = ('fulltext', False)
2082
ld_detail = ('line-delta', False)
2090
f_a: (ft_detail, (f_a, 0, 100), None),
2091
f_b: (ld_detail, (f_b, 100, 21), f_a),
2092
f_c: (ld_detail, (f_c, 180, 15), f_b),
2093
g_a: (ft_detail, (g_a, 121, 35), None),
2094
g_b: (ld_detail, (g_b, 156, 12), g_a),
2095
g_c: (ld_detail, (g_c, 195, 13), g_a),
2097
self.assertGroupKeysForIo([([f_a], set())],
2098
[f_a], [], positions)
2099
self.assertGroupKeysForIo([([f_a], set([f_a]))],
2100
[f_a], [f_a], positions)
2101
self.assertGroupKeysForIo([([f_a, f_b], set([]))],
2102
[f_a, f_b], [], positions)
2103
self.assertGroupKeysForIo([([f_a, f_b], set([f_b]))],
2104
[f_a, f_b], [f_b], positions)
2105
self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
2106
[f_a, g_a, f_b, g_b], [], positions)
2107
self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
2108
[f_a, g_a, f_b, g_b], [], positions,
2109
_min_buffer_size=150)
2110
self.assertGroupKeysForIo([([f_a, f_b], set()), ([g_a, g_b], set())],
2111
[f_a, g_a, f_b, g_b], [], positions,
2112
_min_buffer_size=100)
2113
self.assertGroupKeysForIo([([f_c], set()), ([g_b], set())],
2114
[f_c, g_b], [], positions,
2115
_min_buffer_size=125)
2116
self.assertGroupKeysForIo([([g_b, f_c], set())],
2117
[g_b, f_c], [], positions,
2118
_min_buffer_size=125)
2120
def test__split_by_prefix(self):
2121
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2122
'g': [('g', 'b'), ('g', 'a')],
2124
[('f', 'a'), ('g', 'b'),
2125
('g', 'a'), ('f', 'b')])
2127
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2128
'g': [('g', 'b'), ('g', 'a')],
2130
[('f', 'a'), ('f', 'b'),
2131
('g', 'b'), ('g', 'a')])
2133
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2134
'g': [('g', 'b'), ('g', 'a')],
2136
[('f', 'a'), ('f', 'b'),
2137
('g', 'b'), ('g', 'a')])
2139
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2140
'g': [('g', 'b'), ('g', 'a')],
2141
'': [('a',), ('b',)]
2143
[('f', 'a'), ('g', 'b'),
2145
('g', 'a'), ('f', 'b')])
1775
2148
class TestStacking(KnitTests):
1777
2150
def get_basis_and_test_knit(self):
2171
2546
last_call = basis.calls[-1]
2172
2547
self.assertEqual('get_record_stream', last_call[0])
2173
2548
self.assertEqual(set([key_left, key_right]), set(last_call[1]))
2174
self.assertEqual('unordered', last_call[2])
2549
self.assertEqual('topological', last_call[2])
2175
2550
self.assertEqual(True, last_call[3])
2553
class TestNetworkBehaviour(KnitTests):
2554
"""Tests for getting data out of/into knits over the network."""
2556
def test_include_delta_closure_generates_a_knit_delta_closure(self):
2557
vf = self.make_test_knit(name='test')
2558
# put in three texts, giving ft, delta, delta
2559
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2560
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2561
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2562
# But heuristics could interfere, so check what happened:
2563
self.assertEqual(['knit-ft-gz', 'knit-delta-gz', 'knit-delta-gz'],
2564
[record.storage_kind for record in
2565
vf.get_record_stream([('base',), ('d1',), ('d2',)],
2566
'topological', False)])
2567
# generate a stream of just the deltas include_delta_closure=True,
2568
# serialise to the network, and check that we get a delta closure on the wire.
2569
stream = vf.get_record_stream([('d1',), ('d2',)], 'topological', True)
2570
netb = [record.get_bytes_as(record.storage_kind) for record in stream]
2571
# The first bytes should be a memo from _ContentMapGenerator, and the
2572
# second bytes should be empty (because its a API proxy not something
2573
# for wire serialisation.
2574
self.assertEqual('', netb[1])
2576
kind, line_end = network_bytes_to_kind_and_offset(bytes)
2577
self.assertEqual('knit-delta-closure', kind)
2580
class TestContentMapGenerator(KnitTests):
2581
"""Tests for ContentMapGenerator"""
2583
def test_get_record_stream_gives_records(self):
2584
vf = self.make_test_knit(name='test')
2585
# put in three texts, giving ft, delta, delta
2586
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2587
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2588
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2589
keys = [('d1',), ('d2',)]
2590
generator = _VFContentMapGenerator(vf, keys,
2591
global_map=vf.get_parent_map(keys))
2592
for record in generator.get_record_stream():
2593
if record.key == ('d1',):
2594
self.assertEqual('d1\n', record.get_bytes_as('fulltext'))
2596
self.assertEqual('d2\n', record.get_bytes_as('fulltext'))
2598
def test_get_record_stream_kinds_are_raw(self):
2599
vf = self.make_test_knit(name='test')
2600
# put in three texts, giving ft, delta, delta
2601
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2602
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2603
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2604
keys = [('base',), ('d1',), ('d2',)]
2605
generator = _VFContentMapGenerator(vf, keys,
2606
global_map=vf.get_parent_map(keys))
2607
kinds = {('base',): 'knit-delta-closure',
2608
('d1',): 'knit-delta-closure-ref',
2609
('d2',): 'knit-delta-closure-ref',
2611
for record in generator.get_record_stream():
2612
self.assertEqual(kinds[record.key], record.storage_kind)