~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Knit versionedfile implementation.

A knit is a versioned file implementation that supports efficient append only
updates.

Knit file layout:
lifeless: the data file is made up of "delta records".  each delta record has a delta header 
that contains; (1) a version id, (2) the size of the delta (in lines), and (3)  the digest of 
the -expanded data- (ie, the delta applied to the parent).  the delta also ends with a 
end-marker; simply "end VERSION"

delta can be line or full contents.a
... the 8's there are the index number of the annotation.
version robertc@robertcollins.net-20051003014215-ee2990904cc4c7ad 7 c7d23b2a5bd6ca00e8e266cec0ec228158ee9f9e
59,59,3
8
8         if ie.executable:
8             e.set('executable', 'yes')
130,130,2
8         if elt.get('executable') == 'yes':
8             ie.executable = True
end robertc@robertcollins.net-20051003014215-ee2990904cc4c7ad 


whats in an index:
09:33 < jrydberg> lifeless: each index is made up of a tuple of; version id, options, position, size, parents
09:33 < jrydberg> lifeless: the parents are currently dictionary compressed
09:33 < jrydberg> lifeless: (meaning it currently does not support ghosts)
09:33 < lifeless> right
09:33 < jrydberg> lifeless: the position and size is the range in the data file


so the index sequence is the dictionary compressed sequence number used
in the deltas to provide line annotation

"""

# TODOS:
# 10:16 < lifeless> make partial index writes safe
# 10:16 < lifeless> implement 'knit.check()' like weave.check()
# 10:17 < lifeless> record known ghosts so we can detect when they are filled in rather than the current 'reweave 
#                    always' approach.
# move sha1 out of the content so that join is faster at verifying parents
# record content length ?
                  

from copy import copy
from cStringIO import StringIO
from itertools import izip, chain
import operator
import os
import sys
import warnings
from zlib import Z_DEFAULT_COMPRESSION

import bzrlib
from bzrlib.lazy_import import lazy_import
lazy_import(globals(), """
from bzrlib import (
    annotate,
    graph as _mod_graph,
    lru_cache,
    pack,
    trace,
    )
""")
from bzrlib import (
    cache_utf8,
    debug,
    diff,
    errors,
    osutils,
    patiencediff,
    progress,
    merge,
    ui,
    )
from bzrlib.errors import (
    FileExists,
    NoSuchFile,
    KnitError,
    InvalidRevisionId,
    KnitCorrupt,
    KnitHeaderError,
    RevisionNotPresent,
    RevisionAlreadyPresent,
    )
from bzrlib.graph import Graph
from bzrlib.osutils import (
    contains_whitespace,
    contains_linebreaks,
    sha_string,
    sha_strings,
    split_lines,
    )
from bzrlib.tsort import topo_sort
from bzrlib.tuned_gzip import GzipFile, bytes_to_gzip
import bzrlib.ui
from bzrlib.versionedfile import (
    AbsentContentFactory,
    adapter_registry,
    ContentFactory,
    InterVersionedFile,
    VersionedFile,
    )
import bzrlib.weave


# TODO: Split out code specific to this format into an associated object.

# TODO: Can we put in some kind of value to check that the index and data
# files belong together?

# TODO: accommodate binaries, perhaps by storing a byte count

# TODO: function to check whole file

# TODO: atomically append data, then measure backwards from the cursor
# position after writing to work out where it was located.  we may need to
# bypass python file buffering.

DATA_SUFFIX = '.knit'
INDEX_SUFFIX = '.kndx'


class KnitAdapter(object):
    """Base class for knit record adaption."""

    def __init__(self, basis_vf):
        """Create an adapter which accesses full texts from basis_vf.
        
        :param basis_vf: A versioned file to access basis texts of deltas from.
            May be None for adapters that do not need to access basis texts.
        """
        self._data = _KnitData(None)
        self._annotate_factory = KnitAnnotateFactory()
        self._plain_factory = KnitPlainFactory()
        self._basis_vf = basis_vf


class FTAnnotatedToUnannotated(KnitAdapter):
    """An adapter from FT annotated knits to unannotated ones."""

    def get_bytes(self, factory, annotated_compressed_bytes):
        rec, contents = \
            self._data._parse_record_unchecked(annotated_compressed_bytes)
        content = self._annotate_factory.parse_fulltext(contents, rec[1])
        size, bytes = self._data._record_to_data(rec[1], rec[3], content.text())
        return bytes


class DeltaAnnotatedToUnannotated(KnitAdapter):
    """An adapter for deltas from annotated to unannotated."""

    def get_bytes(self, factory, annotated_compressed_bytes):
        rec, contents = \
            self._data._parse_record_unchecked(annotated_compressed_bytes)
        delta = self._annotate_factory.parse_line_delta(contents, rec[1],
            plain=True)
        contents = self._plain_factory.lower_line_delta(delta)
        size, bytes = self._data._record_to_data(rec[1], rec[3], contents)
        return bytes


class FTAnnotatedToFullText(KnitAdapter):
    """An adapter from FT annotated knits to unannotated ones."""

    def get_bytes(self, factory, annotated_compressed_bytes):
        rec, contents = \
            self._data._parse_record_unchecked(annotated_compressed_bytes)
        content, delta = self._annotate_factory.parse_record(factory.key[0],
            contents, factory._build_details, None)
        return ''.join(content.text())


class DeltaAnnotatedToFullText(KnitAdapter):
    """An adapter for deltas from annotated to unannotated."""

    def get_bytes(self, factory, annotated_compressed_bytes):
        rec, contents = \
            self._data._parse_record_unchecked(annotated_compressed_bytes)
        delta = self._annotate_factory.parse_line_delta(contents, rec[1],
            plain=True)
        compression_parent = factory.parents[0][0]
        basis_lines = self._basis_vf.get_lines(compression_parent)
        # Manually apply the delta because we have one annotated content and
        # one plain.
        basis_content = PlainKnitContent(basis_lines, compression_parent)
        basis_content.apply_delta(delta, rec[1])
        basis_content._should_strip_eol = factory._build_details[1]
        return ''.join(basis_content.text())


class FTPlainToFullText(KnitAdapter):
    """An adapter from FT plain knits to unannotated ones."""

    def get_bytes(self, factory, compressed_bytes):
        rec, contents = \
            self._data._parse_record_unchecked(compressed_bytes)
        content, delta = self._plain_factory.parse_record(factory.key[0],
            contents, factory._build_details, None)
        return ''.join(content.text())


class DeltaPlainToFullText(KnitAdapter):
    """An adapter for deltas from annotated to unannotated."""

    def get_bytes(self, factory, compressed_bytes):
        rec, contents = \
            self._data._parse_record_unchecked(compressed_bytes)
        delta = self._plain_factory.parse_line_delta(contents, rec[1])
        compression_parent = factory.parents[0][0]
        basis_lines = self._basis_vf.get_lines(compression_parent)
        basis_content = PlainKnitContent(basis_lines, compression_parent)
        # Manually apply the delta because we have one annotated content and
        # one plain.
        content, _ = self._plain_factory.parse_record(rec[1], contents,
            factory._build_details, basis_content)
        return ''.join(content.text())


class KnitContentFactory(ContentFactory):
    """Content factory for streaming from knits.
    
    :seealso ContentFactory:
    """

    def __init__(self, version, parents, build_details, sha1, raw_record,
        annotated, knit=None):
        """Create a KnitContentFactory for version.
        
        :param version: The version.
        :param parents: The parents.
        :param build_details: The build details as returned from
            get_build_details.
        :param sha1: The sha1 expected from the full text of this object.
        :param raw_record: The bytes of the knit data from disk.
        :param annotated: True if the raw data is annotated.
        """
        ContentFactory.__init__(self)
        self.sha1 = sha1
        self.key = (version,)
        self.parents = tuple((parent,) for parent in parents)
        if build_details[0] == 'line-delta':
            kind = 'delta'
        else:
            kind = 'ft'
        if annotated:
            annotated_kind = 'annotated-'
        else:
            annotated_kind = ''
        self.storage_kind = 'knit-%s%s-gz' % (annotated_kind, kind)
        self._raw_record = raw_record
        self._build_details = build_details
        self._knit = knit

    def get_bytes_as(self, storage_kind):
        if storage_kind == self.storage_kind:
            return self._raw_record
        if storage_kind == 'fulltext' and self._knit is not None:
            return self._knit.get_text(self.key[0])
        else:
            raise errors.UnavailableRepresentation(self.key, storage_kind,
                self.storage_kind)


class KnitContent(object):
    """Content of a knit version to which deltas can be applied."""

    def __init__(self):
        self._should_strip_eol = False

    def apply_delta(self, delta, new_version_id):
        """Apply delta to this object to become new_version_id."""
        raise NotImplementedError(self.apply_delta)

    def cleanup_eol(self, copy_on_mutate=True):
        if self._should_strip_eol:
            if copy_on_mutate:
                self._lines = self._lines[:]
            self.strip_last_line_newline()

    def line_delta_iter(self, new_lines):
        """Generate line-based delta from this content to new_lines."""
        new_texts = new_lines.text()
        old_texts = self.text()
        s = patiencediff.PatienceSequenceMatcher(None, old_texts, new_texts)
        for tag, i1, i2, j1, j2 in s.get_opcodes():
            if tag == 'equal':
                continue
            # ofrom, oto, length, data
            yield i1, i2, j2 - j1, new_lines._lines[j1:j2]

    def line_delta(self, new_lines):
        return list(self.line_delta_iter(new_lines))

    @staticmethod
    def get_line_delta_blocks(knit_delta, source, target):
        """Extract SequenceMatcher.get_matching_blocks() from a knit delta"""
        target_len = len(target)
        s_pos = 0
        t_pos = 0
        for s_begin, s_end, t_len, new_text in knit_delta:
            true_n = s_begin - s_pos
            n = true_n
            if n > 0:
                # knit deltas do not provide reliable info about whether the
                # last line of a file matches, due to eol handling.
                if source[s_pos + n -1] != target[t_pos + n -1]:
                    n-=1
                if n > 0:
                    yield s_pos, t_pos, n
            t_pos += t_len + true_n
            s_pos = s_end
        n = target_len - t_pos
        if n > 0:
            if source[s_pos + n -1] != target[t_pos + n -1]:
                n-=1
            if n > 0:
                yield s_pos, t_pos, n
        yield s_pos + (target_len - t_pos), target_len, 0


class AnnotatedKnitContent(KnitContent):
    """Annotated content."""

    def __init__(self, lines):
        KnitContent.__init__(self)
        self._lines = lines

    def annotate(self):
        """Return a list of (origin, text) for each content line."""
        return list(self._lines)

    def apply_delta(self, delta, new_version_id):
        """Apply delta to this object to become new_version_id."""
        offset = 0
        lines = self._lines
        for start, end, count, delta_lines in delta:
            lines[offset+start:offset+end] = delta_lines
            offset = offset + (start - end) + count

    def strip_last_line_newline(self):
        line = self._lines[-1][1].rstrip('\n')
        self._lines[-1] = (self._lines[-1][0], line)
        self._should_strip_eol = False

    def text(self):
        try:
            lines = [text for origin, text in self._lines]
        except ValueError, e:
            # most commonly (only?) caused by the internal form of the knit
            # missing annotation information because of a bug - see thread
            # around 20071015
            raise KnitCorrupt(self,
                "line in annotated knit missing annotation information: %s"
                % (e,))

        if self._should_strip_eol:
            lines[-1] = lines[-1].rstrip('\n')
        return lines

    def copy(self):
        return AnnotatedKnitContent(self._lines[:])


class PlainKnitContent(KnitContent):
    """Unannotated content.
    
    When annotate[_iter] is called on this content, the same version is reported
    for all lines. Generally, annotate[_iter] is not useful on PlainKnitContent
    objects.
    """

    def __init__(self, lines, version_id):
        KnitContent.__init__(self)
        self._lines = lines
        self._version_id = version_id

    def annotate(self):
        """Return a list of (origin, text) for each content line."""
        return [(self._version_id, line) for line in self._lines]

    def apply_delta(self, delta, new_version_id):
        """Apply delta to this object to become new_version_id."""
        offset = 0
        lines = self._lines
        for start, end, count, delta_lines in delta:
            lines[offset+start:offset+end] = delta_lines
            offset = offset + (start - end) + count
        self._version_id = new_version_id

    def copy(self):
        return PlainKnitContent(self._lines[:], self._version_id)

    def strip_last_line_newline(self):
        self._lines[-1] = self._lines[-1].rstrip('\n')
        self._should_strip_eol = False

    def text(self):
        lines = self._lines
        if self._should_strip_eol:
            lines = lines[:]
            lines[-1] = lines[-1].rstrip('\n')
        return lines


class _KnitFactory(object):
    """Base class for common Factory functions."""

    def parse_record(self, version_id, record, record_details,
                     base_content, copy_base_content=True):
        """Parse a record into a full content object.

        :param version_id: The official version id for this content
        :param record: The data returned by read_records_iter()
        :param record_details: Details about the record returned by
            get_build_details
        :param base_content: If get_build_details returns a compression_parent,
            you must return a base_content here, else use None
        :param copy_base_content: When building from the base_content, decide
            you can either copy it and return a new object, or modify it in
            place.
        :return: (content, delta) A Content object and possibly a line-delta,
            delta may be None
        """
        method, noeol = record_details
        if method == 'line-delta':
            if copy_base_content:
                content = base_content.copy()
            else:
                content = base_content
            delta = self.parse_line_delta(record, version_id)
            content.apply_delta(delta, version_id)
        else:
            content = self.parse_fulltext(record, version_id)
            delta = None
        content._should_strip_eol = noeol
        return (content, delta)


class KnitAnnotateFactory(_KnitFactory):
    """Factory for creating annotated Content objects."""

    annotated = True

    def make(self, lines, version_id):
        num_lines = len(lines)
        return AnnotatedKnitContent(zip([version_id] * num_lines, lines))

    def parse_fulltext(self, content, version_id):
        """Convert fulltext to internal representation

        fulltext content is of the format
        revid(utf8) plaintext\n
        internal representation is of the format:
        (revid, plaintext)
        """
        # TODO: jam 20070209 The tests expect this to be returned as tuples,
        #       but the code itself doesn't really depend on that.
        #       Figure out a way to not require the overhead of turning the
        #       list back into tuples.
        lines = [tuple(line.split(' ', 1)) for line in content]
        return AnnotatedKnitContent(lines)

    def parse_line_delta_iter(self, lines):
        return iter(self.parse_line_delta(lines))

    def parse_line_delta(self, lines, version_id, plain=False):
        """Convert a line based delta into internal representation.

        line delta is in the form of:
        intstart intend intcount
        1..count lines:
        revid(utf8) newline\n
        internal representation is
        (start, end, count, [1..count tuples (revid, newline)])

        :param plain: If True, the lines are returned as a plain
            list without annotations, not as a list of (origin, content) tuples, i.e.
            (start, end, count, [1..count newline])
        """
        result = []
        lines = iter(lines)
        next = lines.next

        cache = {}
        def cache_and_return(line):
            origin, text = line.split(' ', 1)
            return cache.setdefault(origin, origin), text

        # walk through the lines parsing.
        # Note that the plain test is explicitly pulled out of the
        # loop to minimise any performance impact
        if plain:
            for header in lines:
                start, end, count = [int(n) for n in header.split(',')]
                contents = [next().split(' ', 1)[1] for i in xrange(count)]
                result.append((start, end, count, contents))
        else:
            for header in lines:
                start, end, count = [int(n) for n in header.split(',')]
                contents = [tuple(next().split(' ', 1)) for i in xrange(count)]
                result.append((start, end, count, contents))
        return result

    def get_fulltext_content(self, lines):
        """Extract just the content lines from a fulltext."""
        return (line.split(' ', 1)[1] for line in lines)

    def get_linedelta_content(self, lines):
        """Extract just the content from a line delta.

        This doesn't return all of the extra information stored in a delta.
        Only the actual content lines.
        """
        lines = iter(lines)
        next = lines.next
        for header in lines:
            header = header.split(',')
            count = int(header[2])
            for i in xrange(count):
                origin, text = next().split(' ', 1)
                yield text

    def lower_fulltext(self, content):
        """convert a fulltext content record into a serializable form.

        see parse_fulltext which this inverts.
        """
        # TODO: jam 20070209 We only do the caching thing to make sure that
        #       the origin is a valid utf-8 line, eventually we could remove it
        return ['%s %s' % (o, t) for o, t in content._lines]

    def lower_line_delta(self, delta):
        """convert a delta into a serializable form.

        See parse_line_delta which this inverts.
        """
        # TODO: jam 20070209 We only do the caching thing to make sure that
        #       the origin is a valid utf-8 line, eventually we could remove it
        out = []
        for start, end, c, lines in delta:
            out.append('%d,%d,%d\n' % (start, end, c))
            out.extend(origin + ' ' + text
                       for origin, text in lines)
        return out

    def annotate(self, knit, version_id):
        content = knit._get_content(version_id)
        return content.annotate()


class KnitPlainFactory(_KnitFactory):
    """Factory for creating plain Content objects."""

    annotated = False

    def make(self, lines, version_id):
        return PlainKnitContent(lines, version_id)

    def parse_fulltext(self, content, version_id):
        """This parses an unannotated fulltext.

        Note that this is not a noop - the internal representation
        has (versionid, line) - its just a constant versionid.
        """
        return self.make(content, version_id)

    def parse_line_delta_iter(self, lines, version_id):
        cur = 0
        num_lines = len(lines)
        while cur < num_lines:
            header = lines[cur]
            cur += 1
            start, end, c = [int(n) for n in header.split(',')]
            yield start, end, c, lines[cur:cur+c]
            cur += c

    def parse_line_delta(self, lines, version_id):
        return list(self.parse_line_delta_iter(lines, version_id))

    def get_fulltext_content(self, lines):
        """Extract just the content lines from a fulltext."""
        return iter(lines)

    def get_linedelta_content(self, lines):
        """Extract just the content from a line delta.

        This doesn't return all of the extra information stored in a delta.
        Only the actual content lines.
        """
        lines = iter(lines)
        next = lines.next
        for header in lines:
            header = header.split(',')
            count = int(header[2])
            for i in xrange(count):
                yield next()

    def lower_fulltext(self, content):
        return content.text()

    def lower_line_delta(self, delta):
        out = []
        for start, end, c, lines in delta:
            out.append('%d,%d,%d\n' % (start, end, c))
            out.extend(lines)
        return out

    def annotate(self, knit, version_id):
        annotator = _KnitAnnotator(knit)
        return annotator.annotate(version_id)


def make_empty_knit(transport, relpath):
    """Construct a empty knit at the specified location."""
    k = make_file_knit(transport, relpath, 'w', KnitPlainFactory)


def make_file_knit(name, transport, file_mode=None, access_mode='w',
    factory=None, delta=True, create=False, create_parent_dir=False,
    delay_create=False, dir_mode=None, get_scope=None):
    """Factory to create a KnitVersionedFile for a .knit/.kndx file pair."""
    if factory is None:
        factory = KnitAnnotateFactory()
    if get_scope is None:
        get_scope = lambda:None
    index = _KnitIndex(transport, name + INDEX_SUFFIX,
        access_mode, create=create, file_mode=file_mode,
        create_parent_dir=create_parent_dir, delay_create=delay_create,
        dir_mode=dir_mode, get_scope=get_scope)
    access = _KnitAccess(transport, name + DATA_SUFFIX, file_mode,
        dir_mode, ((create and not len(index)) and delay_create),
        create_parent_dir)
    return KnitVersionedFile(name, transport, factory=factory,
        create=create, delay_create=delay_create, index=index,
        access_method=access)


def get_suffixes():
    """Return the suffixes used by file based knits."""
    return [DATA_SUFFIX, INDEX_SUFFIX]
make_file_knit.get_suffixes = get_suffixes


class KnitVersionedFile(VersionedFile):
    """Weave-like structure with faster random access.

    A knit stores a number of texts and a summary of the relationships
    between them.  Texts are identified by a string version-id.  Texts
    are normally stored and retrieved as a series of lines, but can
    also be passed as single strings.

    Lines are stored with the trailing newline (if any) included, to
    avoid special cases for files with no final newline.  Lines are
    composed of 8-bit characters, not unicode.  The combination of
    these approaches should mean any 'binary' file can be safely
    stored and retrieved.
    """

    def __init__(self, relpath, transport, file_mode=None,
        factory=None, delta=True, create=False, create_parent_dir=False,
        delay_create=False, dir_mode=None, index=None, access_method=None):
        """Construct a knit at location specified by relpath.
        
        :param create: If not True, only open an existing knit.
        :param create_parent_dir: If True, create the parent directory if 
            creating the file fails. (This is used for stores with 
            hash-prefixes that may not exist yet)
        :param delay_create: The calling code is aware that the knit won't 
            actually be created until the first data is stored.
        :param index: An index to use for the knit.
        """
        super(KnitVersionedFile, self).__init__()
        self.transport = transport
        self.filename = relpath
        self.factory = factory or KnitAnnotateFactory()
        self.delta = delta

        self._max_delta_chain = 200

        if None in (access_method, index):
            raise ValueError("No default access_method or index any more")
        self._index = index
        _access = access_method
        if create and not len(self) and not delay_create:
            _access.create()
        self._data = _KnitData(_access)

    def __repr__(self):
        return '%s(%s)' % (self.__class__.__name__,
                           self.transport.abspath(self.filename))
    
    def _check_should_delta(self, first_parents):
        """Iterate back through the parent listing, looking for a fulltext.

        This is used when we want to decide whether to add a delta or a new
        fulltext. It searches for _max_delta_chain parents. When it finds a
        fulltext parent, it sees if the total size of the deltas leading up to
        it is large enough to indicate that we want a new full text anyway.

        Return True if we should create a new delta, False if we should use a
        full text.
        """
        delta_size = 0
        fulltext_size = None
        delta_parents = first_parents
        for count in xrange(self._max_delta_chain):
            parent = delta_parents[0]
            method = self._index.get_method(parent)
            index, pos, size = self._index.get_position(parent)
            if method == 'fulltext':
                fulltext_size = size
                break
            delta_size += size
            delta_parents = self._index.get_parent_map([parent])[parent]
        else:
            # We couldn't find a fulltext, so we must create a new one
            return False

        return fulltext_size > delta_size

    def _check_write_ok(self):
        return self._index._check_write_ok()

    def _add_raw_records(self, records, data):
        """Add all the records 'records' with data pre-joined in 'data'.

        :param records: A list of tuples(version_id, options, parents, size).
        :param data: The data for the records. When it is written, the records
                     are adjusted to have pos pointing into data by the sum of
                     the preceding records sizes.
        """
        # write all the data
        raw_record_sizes = [record[3] for record in records]
        positions = self._data.add_raw_records(raw_record_sizes, data)
        index_entries = []
        for (version_id, options, parents, _), access_memo in zip(
            records, positions):
            index_entries.append((version_id, options, access_memo, parents))
        self._index.add_versions(index_entries)

    def copy_to(self, name, transport):
        """See VersionedFile.copy_to()."""
        # copy the current index to a temp index to avoid racing with local
        # writes
        transport.put_file_non_atomic(name + INDEX_SUFFIX + '.tmp',
                self.transport.get(self._index._filename))
        # copy the data file
        f = self._data._open_file()
        try:
            transport.put_file(name + DATA_SUFFIX, f)
        finally:
            f.close()
        # move the copied index into place
        transport.move(name + INDEX_SUFFIX + '.tmp', name + INDEX_SUFFIX)

    def get_data_stream(self, required_versions):
        """Get a data stream for the specified versions.

        Versions may be returned in any order, not necessarily the order
        specified.  They are returned in a partial order by compression
        parent, so that the deltas can be applied as the data stream is
        inserted; however note that compression parents will not be sent
        unless they were specifically requested, as the client may already
        have them.

        :param required_versions: The exact set of versions to be extracted.
            Unlike some other knit methods, this is not used to generate a
            transitive closure, rather it is used precisely as given.
        
        :returns: format_signature, list of (version, options, length, parents),
            reader_callable.
        """
        required_version_set = frozenset(required_versions)
        version_index = {}
        # list of revisions that can just be sent without waiting for their
        # compression parent
        ready_to_send = []
        # map from revision to the children based on it
        deferred = {}
        # first, read all relevant index data, enough to sort into the right
        # order to return
        for version_id in required_versions:
            options = self._index.get_options(version_id)
            parents = self._index.get_parents_with_ghosts(version_id)
            index_memo = self._index.get_position(version_id)
            version_index[version_id] = (index_memo, options, parents)
            if ('line-delta' in options
                and parents[0] in required_version_set):
                # must wait until the parent has been sent
                deferred.setdefault(parents[0], []). \
                    append(version_id)
            else:
                # either a fulltext, or a delta whose parent the client did
                # not ask for and presumably already has
                ready_to_send.append(version_id)
        # build a list of results to return, plus instructions for data to
        # read from the file
        copy_queue_records = []
        temp_version_list = []
        while ready_to_send:
            # XXX: pushing and popping lists may be a bit inefficient
            version_id = ready_to_send.pop(0)
            (index_memo, options, parents) = version_index[version_id]
            copy_queue_records.append((version_id, index_memo))
            none, data_pos, data_size = index_memo
            temp_version_list.append((version_id, options, data_size,
                parents))
            if version_id in deferred:
                # now we can send all the children of this revision - we could
                # put them in anywhere, but we hope that sending them soon
                # after the fulltext will give good locality in the receiver
                ready_to_send[:0] = deferred.pop(version_id)
        if not (len(deferred) == 0):
            raise AssertionError("Still have compressed child versions waiting to be sent")
        # XXX: The stream format is such that we cannot stream it - we have to
        # know the length of all the data a-priori.
        raw_datum = []
        result_version_list = []
        for (version_id, raw_data, _), \
            (version_id2, options, _, parents) in \
            izip(self._data.read_records_iter_raw(copy_queue_records),
                 temp_version_list):
            if not (version_id == version_id2):
                raise AssertionError('logic error, inconsistent results')
            raw_datum.append(raw_data)
            result_version_list.append(
                (version_id, options, len(raw_data), parents))
        # provide a callback to get data incrementally.
        pseudo_file = StringIO(''.join(raw_datum))
        def read(length):
            if length is None:
                return pseudo_file.read()
            else:
                return pseudo_file.read(length)
        return (self.get_format_signature(), result_version_list, read)

    def get_record_stream(self, versions, ordering, include_delta_closure):
        """Get a stream of records for versions.

        :param versions: The versions to include. Each version is a tuple
            (version,).
        :param ordering: Either 'unordered' or 'topological'. A topologically
            sorted stream has compression parents strictly before their
            children.
        :param include_delta_closure: If True then the closure across any
            compression parents will be included (in the opaque data).
        :return: An iterator of ContentFactory objects, each of which is only
            valid until the iterator is advanced.
        """
        if include_delta_closure:
            # Nb: what we should do is plan the data to stream to allow
            # reconstruction of all the texts without excessive buffering,
            # including re-sending common bases as needed. This makes the most
            # sense when we start serialising these streams though, so for now
            # we just fallback to individual text construction behind the
            # abstraction barrier.
            knit = self
        else:
            knit = None
        # We end up doing multiple index lookups here for parents details and
        # disk layout details - we need a unified api ?
        parent_map = self.get_parent_map(versions)
        absent_versions = set(versions) - set(parent_map)
        if ordering == 'topological':
            present_versions = topo_sort(parent_map)
        else:
            # List comprehension to keep the requested order (as that seems
            # marginally useful, at least until we start doing IO optimising
            # here.
            present_versions = [version for version in versions if version in
                parent_map]
        position_map = self._get_components_positions(present_versions)
        records = [(version, position_map[version][1]) for version in
            present_versions]
        record_map = {}
        for version in absent_versions:
            yield AbsentContentFactory((version,))
        for version, raw_data, sha1 in \
                self._data.read_records_iter_raw(records):
            (record_details, index_memo, _) = position_map[version]
            yield KnitContentFactory(version, parent_map[version],
                record_details, sha1, raw_data, self.factory.annotated, knit)

    def _extract_blocks(self, version_id, source, target):
        if self._index.get_method(version_id) != 'line-delta':
            return None
        parent, sha1, noeol, delta = self.get_delta(version_id)
        return KnitContent.get_line_delta_blocks(delta, source, target)

    def get_delta(self, version_id):
        """Get a delta for constructing version from some other version."""
        self.check_not_reserved_id(version_id)
        parents = self.get_parent_map([version_id])[version_id]
        if len(parents):
            parent = parents[0]
        else:
            parent = None
        index_memo = self._index.get_position(version_id)
        data, sha1 = self._data.read_records(((version_id, index_memo),))[version_id]
        noeol = 'no-eol' in self._index.get_options(version_id)
        if 'fulltext' == self._index.get_method(version_id):
            new_content = self.factory.parse_fulltext(data, version_id)
            if parent is not None:
                reference_content = self._get_content(parent)
                old_texts = reference_content.text()
            else:
                old_texts = []
            new_texts = new_content.text()
            delta_seq = patiencediff.PatienceSequenceMatcher(None, old_texts,
                                                             new_texts)
            return parent, sha1, noeol, self._make_line_delta(delta_seq, new_content)
        else:
            delta = self.factory.parse_line_delta(data, version_id)
            return parent, sha1, noeol, delta

    def get_format_signature(self):
        """See VersionedFile.get_format_signature()."""
        if self.factory.annotated:
            annotated_part = "annotated"
        else:
            annotated_part = "plain"
        return "knit-%s" % (annotated_part,)
        
    def get_sha1s(self, version_ids):
        """See VersionedFile.get_sha1s()."""
        record_map = self._get_record_map(version_ids)
        # record entry 2 is the 'digest'.
        return [record_map[v][2] for v in version_ids]

    def insert_data_stream(self, (format, data_list, reader_callable)):
        """Insert knit records from a data stream into this knit.

        If a version in the stream is already present in this knit, it will not
        be inserted a second time.  It will be checked for consistency with the
        stored version however, and may cause a KnitCorrupt error to be raised
        if the data in the stream disagrees with the already stored data.
        
        :seealso: get_data_stream
        """
        if format != self.get_format_signature():
            if 'knit' in debug.debug_flags:
                trace.mutter(
                    'incompatible format signature inserting to %r', self)
            source = self._knit_from_datastream(
                (format, data_list, reader_callable))
            stream = source.get_record_stream(source.versions(), 'unordered', False)
            self.insert_record_stream(stream)
            return

        for version_id, options, length, parents in data_list:
            if self.has_version(version_id):
                # First check: the list of parents.
                my_parents = self.get_parents_with_ghosts(version_id)
                if tuple(my_parents) != tuple(parents):
                    # XXX: KnitCorrupt is not quite the right exception here.
                    raise KnitCorrupt(
                        self.filename,
                        'parents list %r from data stream does not match '
                        'already recorded parents %r for %s'
                        % (parents, my_parents, version_id))

                # Also check the SHA-1 of the fulltext this content will
                # produce.
                raw_data = reader_callable(length)
                my_fulltext_sha1 = self.get_sha1s([version_id])[0]
                df, rec = self._data._parse_record_header(version_id, raw_data)
                stream_fulltext_sha1 = rec[3]
                if my_fulltext_sha1 != stream_fulltext_sha1:
                    # Actually, we don't know if it's this knit that's corrupt,
                    # or the data stream we're trying to insert.
                    raise KnitCorrupt(
                        self.filename, 'sha-1 does not match %s' % version_id)
            else:
                if 'line-delta' in options:
                    # Make sure that this knit record is actually useful: a
                    # line-delta is no use unless we have its parent.
                    # Fetching from a broken repository with this problem
                    # shouldn't break the target repository.
                    #
                    # See https://bugs.launchpad.net/bzr/+bug/164443
                    if not self._index.has_version(parents[0]):
                        raise KnitCorrupt(
                            self.filename,
                            'line-delta from stream '
                            'for version %s '
                            'references '
                            'missing parent %s\n'
                            'Try running "bzr check" '
                            'on the source repository, and "bzr reconcile" '
                            'if necessary.' %
                            (version_id, parents[0]))
                    if not self.delta:
                        # We received a line-delta record for a non-delta knit.
                        # Convert it to a fulltext.
                        gzip_bytes = reader_callable(length)
                        self._convert_line_delta_to_fulltext(
                            gzip_bytes, version_id, parents)
                        continue

                self._add_raw_records(
                    [(version_id, options, parents, length)],
                    reader_callable(length))

    def _convert_line_delta_to_fulltext(self, gzip_bytes, version_id, parents):
        lines, sha1 = self._data._parse_record(version_id, gzip_bytes)
        delta = self.factory.parse_line_delta(lines, version_id)
        content = self.factory.make(self.get_lines(parents[0]), parents[0])
        content.apply_delta(delta, version_id)
        digest, len, content = self.add_lines(
            version_id, parents, content.text())
        if digest != sha1:
            raise errors.VersionedFileInvalidChecksum(version_id)

    def _knit_from_datastream(self, (format, data_list, reader_callable)):
        """Create a knit object from a data stream.

        This method exists to allow conversion of data streams that do not
        match the signature of this knit. Generally it will be slower and use
        more memory to use this method to insert data, but it will work.

        :seealso: get_data_stream for details on datastreams.
        :return: A knit versioned file which can be used to join the datastream
            into self.
        """
        if format == "knit-plain":
            factory = KnitPlainFactory()
        elif format == "knit-annotated":
            factory = KnitAnnotateFactory()
        else:
            raise errors.KnitDataStreamUnknown(format)
        index = _StreamIndex(data_list, self._index)
        access = _StreamAccess(reader_callable, index, self, factory)
        return KnitVersionedFile(self.filename, self.transport,
            factory=factory, index=index, access_method=access)

    def insert_record_stream(self, stream):
        """Insert a record stream into this versioned file.

        :param stream: A stream of records to insert. 
        :return: None
        :seealso VersionedFile.get_record_stream:
        """
        def get_adapter(adapter_key):
            try:
                return adapters[adapter_key]
            except KeyError:
                adapter_factory = adapter_registry.get(adapter_key)
                adapter = adapter_factory(self)
                adapters[adapter_key] = adapter
                return adapter
        if self.factory.annotated:
            # self is annotated, we need annotated knits to use directly.
            annotated = "annotated-"
            convertibles = []
        else:
            # self is not annotated, but we can strip annotations cheaply.
            annotated = ""
            convertibles = set(["knit-annotated-delta-gz",
                "knit-annotated-ft-gz"])
        # The set of types we can cheaply adapt without needing basis texts.
        native_types = set()
        native_types.add("knit-%sdelta-gz" % annotated)
        native_types.add("knit-%sft-gz" % annotated)
        knit_types = native_types.union(convertibles)
        adapters = {}
        # Buffer all index entries that we can't add immediately because their
        # basis parent is missing. We don't buffer all because generating
        # annotations may require access to some of the new records. However we
        # can't generate annotations from new deltas until their basis parent
        # is present anyway, so we get away with not needing an index that
        # includes the new keys.
        # key = basis_parent, value = index entry to add
        buffered_index_entries = {}
        for record in stream:
            # Raise an error when a record is missing.
            if record.storage_kind == 'absent':
                raise RevisionNotPresent([record.key[0]], self)
            # adapt to non-tuple interface
            parents = [parent[0] for parent in record.parents]
            if record.storage_kind in knit_types:
                if record.storage_kind not in native_types:
                    try:
                        adapter_key = (record.storage_kind, "knit-delta-gz")
                        adapter = get_adapter(adapter_key)
                    except KeyError:
                        adapter_key = (record.storage_kind, "knit-ft-gz")
                        adapter = get_adapter(adapter_key)
                    bytes = adapter.get_bytes(
                        record, record.get_bytes_as(record.storage_kind))
                else:
                    bytes = record.get_bytes_as(record.storage_kind)
                options = [record._build_details[0]]
                if record._build_details[1]:
                    options.append('no-eol')
                # Just blat it across.
                # Note: This does end up adding data on duplicate keys. As
                # modern repositories use atomic insertions this should not
                # lead to excessive growth in the event of interrupted fetches.
                # 'knit' repositories may suffer excessive growth, but as a
                # deprecated format this is tolerable. It can be fixed if
                # needed by in the kndx index support raising on a duplicate
                # add with identical parents and options.
                access_memo = self._data.add_raw_records([len(bytes)], bytes)[0]
                index_entry = (record.key[0], options, access_memo, parents)
                buffered = False
                if 'fulltext' not in options:
                    basis_parent = parents[0]
                    if not self.has_version(basis_parent):
                        pending = buffered_index_entries.setdefault(
                            basis_parent, [])
                        pending.append(index_entry)
                        buffered = True
                if not buffered:
                    self._index.add_versions([index_entry])
            elif record.storage_kind == 'fulltext':
                self.add_lines(record.key[0], parents,
                    split_lines(record.get_bytes_as('fulltext')))
            else:
                adapter_key = record.storage_kind, 'fulltext'
                adapter = get_adapter(adapter_key)
                lines = split_lines(adapter.get_bytes(
                    record, record.get_bytes_as(record.storage_kind)))
                try:
                    self.add_lines(record.key[0], parents, lines)
                except errors.RevisionAlreadyPresent:
                    pass
            # Add any records whose basis parent is now available.
            added_keys = [record.key[0]]
            while added_keys:
                key = added_keys.pop(0)
                if key in buffered_index_entries:
                    index_entries = buffered_index_entries[key]
                    self._index.add_versions(index_entries)
                    added_keys.extend(
                        [index_entry[0] for index_entry in index_entries])
                    del buffered_index_entries[key]
        # If there were any deltas which had a missing basis parent, error.
        if buffered_index_entries:
            raise errors.RevisionNotPresent(buffered_index_entries.keys()[0],
                self)

    def versions(self):
        """See VersionedFile.versions."""
        if 'evil' in debug.debug_flags:
            trace.mutter_callsite(2, "versions scales with size of history")
        return self._index.get_versions()

    def has_version(self, version_id):
        """See VersionedFile.has_version."""
        if 'evil' in debug.debug_flags:
            trace.mutter_callsite(2, "has_version is a LBYL scenario")
        return self._index.has_version(version_id)

    __contains__ = has_version

    def _merge_annotations(self, content, parents, parent_texts={},
                           delta=None, annotated=None,
                           left_matching_blocks=None):
        """Merge annotations for content.  This is done by comparing
        the annotations based on changed to the text.
        """
        if left_matching_blocks is not None:
            delta_seq = diff._PrematchedMatcher(left_matching_blocks)
        else:
            delta_seq = None
        if annotated:
            for parent_id in parents:
                merge_content = self._get_content(parent_id, parent_texts)
                if (parent_id == parents[0] and delta_seq is not None):
                    seq = delta_seq
                else:
                    seq = patiencediff.PatienceSequenceMatcher(
                        None, merge_content.text(), content.text())
                for i, j, n in seq.get_matching_blocks():
                    if n == 0:
                        continue
                    # this appears to copy (origin, text) pairs across to the
                    # new content for any line that matches the last-checked
                    # parent.
                    content._lines[j:j+n] = merge_content._lines[i:i+n]
        if delta:
            if delta_seq is None:
                reference_content = self._get_content(parents[0], parent_texts)
                new_texts = content.text()
                old_texts = reference_content.text()
                delta_seq = patiencediff.PatienceSequenceMatcher(
                                                 None, old_texts, new_texts)
            return self._make_line_delta(delta_seq, content)

    def _make_line_delta(self, delta_seq, new_content):
        """Generate a line delta from delta_seq and new_content."""
        diff_hunks = []
        for op in delta_seq.get_opcodes():
            if op[0] == 'equal':
                continue
            diff_hunks.append((op[1], op[2], op[4]-op[3], new_content._lines[op[3]:op[4]]))
        return diff_hunks

    def _get_components_positions(self, version_ids):
        """Produce a map of position data for the components of versions.

        This data is intended to be used for retrieving the knit records.

        A dict of version_id to (record_details, index_memo, next, parents) is
        returned.
        method is the way referenced data should be applied.
        index_memo is the handle to pass to the data access to actually get the
            data
        next is the build-parent of the version, or None for fulltexts.
        parents is the version_ids of the parents of this version
        """
        component_data = {}
        pending_components = version_ids
        while pending_components:
            build_details = self._index.get_build_details(pending_components)
            current_components = set(pending_components)
            pending_components = set()
            for version_id, details in build_details.iteritems():
                (index_memo, compression_parent, parents,
                 record_details) = details
                method = record_details[0]
                if compression_parent is not None:
                    pending_components.add(compression_parent)
                component_data[version_id] = (record_details, index_memo,
                                              compression_parent)
            missing = current_components.difference(build_details)
            if missing:
                raise errors.RevisionNotPresent(missing.pop(), self.filename)
        return component_data
       
    def _get_content(self, version_id, parent_texts={}):
        """Returns a content object that makes up the specified
        version."""
        cached_version = parent_texts.get(version_id, None)
        if cached_version is not None:
            if not self.has_version(version_id):
                raise RevisionNotPresent(version_id, self.filename)
            return cached_version

        text_map, contents_map = self._get_content_maps([version_id])
        return contents_map[version_id]

    def _check_versions_present(self, version_ids):
        """Check that all specified versions are present."""
        self._index.check_versions_present(version_ids)

    def _add_lines_with_ghosts(self, version_id, parents, lines, parent_texts,
        nostore_sha, random_id, check_content, left_matching_blocks):
        """See VersionedFile.add_lines_with_ghosts()."""
        self._check_add(version_id, lines, random_id, check_content)
        return self._add(version_id, lines, parents, self.delta,
            parent_texts, left_matching_blocks, nostore_sha, random_id)

    def _add_lines(self, version_id, parents, lines, parent_texts,
        left_matching_blocks, nostore_sha, random_id, check_content):
        """See VersionedFile.add_lines."""
        self._check_add(version_id, lines, random_id, check_content)
        self._check_versions_present(parents)
        return self._add(version_id, lines[:], parents, self.delta,
            parent_texts, left_matching_blocks, nostore_sha, random_id)

    def _check_add(self, version_id, lines, random_id, check_content):
        """check that version_id and lines are safe to add."""
        if contains_whitespace(version_id):
            raise InvalidRevisionId(version_id, self.filename)
        self.check_not_reserved_id(version_id)
        # Technically this could be avoided if we are happy to allow duplicate
        # id insertion when other things than bzr core insert texts, but it
        # seems useful for folk using the knit api directly to have some safety
        # blanket that we can disable.
        if not random_id and self.has_version(version_id):
            raise RevisionAlreadyPresent(version_id, self.filename)
        if check_content:
            self._check_lines_not_unicode(lines)
            self._check_lines_are_lines(lines)

    def _add(self, version_id, lines, parents, delta, parent_texts,
        left_matching_blocks, nostore_sha, random_id):
        """Add a set of lines on top of version specified by parents.

        If delta is true, compress the text as a line-delta against
        the first parent.

        Any versions not present will be converted into ghosts.
        """
        # first thing, if the content is something we don't need to store, find
        # that out.
        line_bytes = ''.join(lines)
        digest = sha_string(line_bytes)
        if nostore_sha == digest:
            raise errors.ExistingContent

        present_parents = []
        if parent_texts is None:
            parent_texts = {}
        for parent in parents:
            if self.has_version(parent):
                present_parents.append(parent)

        # can only compress against the left most present parent.
        if (delta and
            (len(present_parents) == 0 or
             present_parents[0] != parents[0])):
            delta = False

        text_length = len(line_bytes)
        options = []
        if lines:
            if lines[-1][-1] != '\n':
                # copy the contents of lines.
                lines = lines[:]
                options.append('no-eol')
                lines[-1] = lines[-1] + '\n'
                line_bytes += '\n'

        if delta:
            # To speed the extract of texts the delta chain is limited
            # to a fixed number of deltas.  This should minimize both
            # I/O and the time spend applying deltas.
            delta = self._check_should_delta(present_parents)

        content = self.factory.make(lines, version_id)
        if delta or (self.factory.annotated and len(present_parents) > 0):
            # Merge annotations from parent texts if needed.
            delta_hunks = self._merge_annotations(content, present_parents,
                parent_texts, delta, self.factory.annotated,
                left_matching_blocks)

        if delta:
            options.append('line-delta')
            store_lines = self.factory.lower_line_delta(delta_hunks)
            size, bytes = self._data._record_to_data(version_id, digest,
                store_lines)
        else:
            options.append('fulltext')
            # isinstance is slower and we have no hierarchy.
            if self.factory.__class__ == KnitPlainFactory:
                # Use the already joined bytes saving iteration time in
                # _record_to_data.
                size, bytes = self._data._record_to_data(version_id, digest,
                    lines, [line_bytes])
            else:
                # get mixed annotation + content and feed it into the
                # serialiser.
                store_lines = self.factory.lower_fulltext(content)
                size, bytes = self._data._record_to_data(version_id, digest,
                    store_lines)

        access_memo = self._data.add_raw_records([size], bytes)[0]
        self._index.add_versions(
            ((version_id, options, access_memo, parents),),
            random_id=random_id)
        return digest, text_length, content

    def check(self, progress_bar=None):
        """See VersionedFile.check()."""
        # This doesn't actually test extraction of everything, but that will
        # impact 'bzr check' substantially, and needs to be integrated with
        # care. However, it does check for the obvious problem of a delta with
        # no basis.
        versions = self.versions()
        parent_map = self.get_parent_map(versions)
        for version in versions:
            if self._index.get_method(version) != 'fulltext':
                compression_parent = parent_map[version][0]
                if compression_parent not in parent_map:
                    raise errors.KnitCorrupt(self,
                        "Missing basis parent %s for %s" % (
                        compression_parent, version))

    def get_lines(self, version_id):
        """See VersionedFile.get_lines()."""
        return self.get_line_list([version_id])[0]

    def _get_record_map(self, version_ids):
        """Produce a dictionary of knit records.
        
        :return: {version_id:(record, record_details, digest, next)}
            record
                data returned from read_records
            record_details
                opaque information to pass to parse_record
            digest
                SHA1 digest of the full text after all steps are done
            next
                build-parent of the version, i.e. the leftmost ancestor.
                Will be None if the record is not a delta.
        """
        position_map = self._get_components_positions(version_ids)
        # c = component_id, r = record_details, i_m = index_memo, n = next
        records = [(c, i_m) for c, (r, i_m, n)
                             in position_map.iteritems()]
        record_map = {}
        for component_id, record, digest in \
                self._data.read_records_iter(records):
            (record_details, index_memo, next) = position_map[component_id]
            record_map[component_id] = record, record_details, digest, next

        return record_map

    def get_text(self, version_id):
        """See VersionedFile.get_text"""
        return self.get_texts([version_id])[0]

    def get_texts(self, version_ids):
        return [''.join(l) for l in self.get_line_list(version_ids)]

    def get_line_list(self, version_ids):
        """Return the texts of listed versions as a list of strings."""
        for version_id in version_ids:
            self.check_not_reserved_id(version_id)
        text_map, content_map = self._get_content_maps(version_ids)
        return [text_map[v] for v in version_ids]

    _get_lf_split_line_list = get_line_list

    def _get_content_maps(self, version_ids):
        """Produce maps of text and KnitContents
        
        :return: (text_map, content_map) where text_map contains the texts for
        the requested versions and content_map contains the KnitContents.
        Both dicts take version_ids as their keys.
        """
        # FUTURE: This function could be improved for the 'extract many' case
        # by tracking each component and only doing the copy when the number of
        # children than need to apply delta's to it is > 1 or it is part of the
        # final output.
        version_ids = list(version_ids)
        multiple_versions = len(version_ids) != 1
        record_map = self._get_record_map(version_ids)

        text_map = {}
        content_map = {}
        final_content = {}
        for version_id in version_ids:
            components = []
            cursor = version_id
            while cursor is not None:
                record, record_details, digest, next = record_map[cursor]
                components.append((cursor, record, record_details, digest))
                if cursor in content_map:
                    break
                cursor = next

            content = None
            for (component_id, record, record_details,
                 digest) in reversed(components):
                if component_id in content_map:
                    content = content_map[component_id]
                else:
                    content, delta = self.factory.parse_record(version_id,
                        record, record_details, content,
                        copy_base_content=multiple_versions)
                    if multiple_versions:
                        content_map[component_id] = content

            content.cleanup_eol(copy_on_mutate=multiple_versions)
            final_content[version_id] = content

            # digest here is the digest from the last applied component.
            text = content.text()
            actual_sha = sha_strings(text)
            if actual_sha != digest:
                raise KnitCorrupt(self.filename,
                    '\n  sha-1 %s'
                    '\n  of reconstructed text does not match'
                    '\n  expected %s'
                    '\n  for version %s' %
                    (actual_sha, digest, version_id))
            text_map[version_id] = text
        return text_map, final_content

    def iter_lines_added_or_present_in_versions(self, version_ids=None, 
                                                pb=None):
        """See VersionedFile.iter_lines_added_or_present_in_versions()."""
        if version_ids is None:
            version_ids = self.versions()
        if pb is None:
            pb = progress.DummyProgress()
        # we don't care about inclusions, the caller cares.
        # but we need to setup a list of records to visit.
        # we need version_id, position, length
        version_id_records = []
        requested_versions = set(version_ids)
        # filter for available versions
        for version_id in requested_versions:
            if not self.has_version(version_id):
                raise RevisionNotPresent(version_id, self.filename)
        # get a in-component-order queue:
        for version_id in self.versions():
            if version_id in requested_versions:
                index_memo = self._index.get_position(version_id)
                version_id_records.append((version_id, index_memo))

        total = len(version_id_records)
        for version_idx, (version_id, data, sha_value) in \
            enumerate(self._data.read_records_iter(version_id_records)):
            pb.update('Walking content.', version_idx, total)
            method = self._index.get_method(version_id)
            if method == 'fulltext':
                line_iterator = self.factory.get_fulltext_content(data)
            elif method == 'line-delta':
                line_iterator = self.factory.get_linedelta_content(data)
            else:
                raise ValueError('invalid method %r' % (method,))
            # XXX: It might be more efficient to yield (version_id,
            # line_iterator) in the future. However for now, this is a simpler
            # change to integrate into the rest of the codebase. RBC 20071110
            for line in line_iterator:
                yield line, version_id

        pb.update('Walking content.', total, total)
        
    def num_versions(self):
        """See VersionedFile.num_versions()."""
        return self._index.num_versions()

    __len__ = num_versions

    def annotate(self, version_id):
        """See VersionedFile.annotate."""
        return self.factory.annotate(self, version_id)

    def get_parent_map(self, version_ids):
        """See VersionedFile.get_parent_map."""
        return self._index.get_parent_map(version_ids)

    def get_ancestry(self, versions, topo_sorted=True):
        """See VersionedFile.get_ancestry."""
        if isinstance(versions, basestring):
            versions = [versions]
        if not versions:
            return []
        return self._index.get_ancestry(versions, topo_sorted)

    def get_ancestry_with_ghosts(self, versions):
        """See VersionedFile.get_ancestry_with_ghosts."""
        if isinstance(versions, basestring):
            versions = [versions]
        if not versions:
            return []
        return self._index.get_ancestry_with_ghosts(versions)

    def plan_merge(self, ver_a, ver_b):
        """See VersionedFile.plan_merge."""
        ancestors_b = set(self.get_ancestry(ver_b, topo_sorted=False))
        ancestors_a = set(self.get_ancestry(ver_a, topo_sorted=False))
        annotated_a = self.annotate(ver_a)
        annotated_b = self.annotate(ver_b)
        return merge._plan_annotate_merge(annotated_a, annotated_b,
                                          ancestors_a, ancestors_b)


class _KnitComponentFile(object):
    """One of the files used to implement a knit database"""

    def __init__(self, transport, filename, mode, file_mode=None,
                 create_parent_dir=False, dir_mode=None):
        self._transport = transport
        self._filename = filename
        self._mode = mode
        self._file_mode = file_mode
        self._dir_mode = dir_mode
        self._create_parent_dir = create_parent_dir
        self._need_to_create = False

    def _full_path(self):
        """Return the full path to this file."""
        return self._transport.base + self._filename

    def check_header(self, fp):
        line = fp.readline()
        if line == '':
            # An empty file can actually be treated as though the file doesn't
            # exist yet.
            raise errors.NoSuchFile(self._full_path())
        if line != self.HEADER:
            raise KnitHeaderError(badline=line,
                              filename=self._transport.abspath(self._filename))

    def __repr__(self):
        return '%s(%s)' % (self.__class__.__name__, self._filename)


class _KnitIndex(_KnitComponentFile):
    """Manages knit index file.

    The index is already kept in memory and read on startup, to enable
    fast lookups of revision information.  The cursor of the index
    file is always pointing to the end, making it easy to append
    entries.

    _cache is a cache for fast mapping from version id to a Index
    object.

    _history is a cache for fast mapping from indexes to version ids.

    The index data format is dictionary compressed when it comes to
    parent references; a index entry may only have parents that with a
    lover index number.  As a result, the index is topological sorted.

    Duplicate entries may be written to the index for a single version id
    if this is done then the latter one completely replaces the former:
    this allows updates to correct version and parent information. 
    Note that the two entries may share the delta, and that successive
    annotations and references MUST point to the first entry.

    The index file on disc contains a header, followed by one line per knit
    record. The same revision can be present in an index file more than once.
    The first occurrence gets assigned a sequence number starting from 0. 
    
    The format of a single line is
    REVISION_ID FLAGS BYTE_OFFSET LENGTH( PARENT_ID|PARENT_SEQUENCE_ID)* :\n
    REVISION_ID is a utf8-encoded revision id
    FLAGS is a comma separated list of flags about the record. Values include 
        no-eol, line-delta, fulltext.
    BYTE_OFFSET is the ascii representation of the byte offset in the data file
        that the the compressed data starts at.
    LENGTH is the ascii representation of the length of the data file.
    PARENT_ID a utf-8 revision id prefixed by a '.' that is a parent of
        REVISION_ID.
    PARENT_SEQUENCE_ID the ascii representation of the sequence number of a
        revision id already in the knit that is a parent of REVISION_ID.
    The ' :' marker is the end of record marker.
    
    partial writes:
    when a write is interrupted to the index file, it will result in a line
    that does not end in ' :'. If the ' :' is not present at the end of a line,
    or at the end of the file, then the record that is missing it will be
    ignored by the parser.

    When writing new records to the index file, the data is preceded by '\n'
    to ensure that records always start on new lines even if the last write was
    interrupted. As a result its normal for the last line in the index to be
    missing a trailing newline. One can be added with no harmful effects.
    """

    HEADER = "# bzr knit index 8\n"

    # speed of knit parsing went from 280 ms to 280 ms with slots addition.
    # __slots__ = ['_cache', '_history', '_transport', '_filename']

    def _cache_version(self, version_id, options, pos, size, parents):
        """Cache a version record in the history array and index cache.

        This is inlined into _load_data for performance. KEEP IN SYNC.
        (It saves 60ms, 25% of the __init__ overhead on local 4000 record
         indexes).
        """
        # only want the _history index to reference the 1st index entry
        # for version_id
        if version_id not in self._cache:
            index = len(self._history)
            self._history.append(version_id)
        else:
            index = self._cache[version_id][5]
        self._cache[version_id] = (version_id,
                                   options,
                                   pos,
                                   size,
                                   parents,
                                   index)

    def _check_write_ok(self):
        if self._get_scope() != self._scope:
            raise errors.OutSideTransaction()
        if self._mode != 'w':
            raise errors.ReadOnlyObjectDirtiedError(self)

    def __init__(self, transport, filename, mode, create=False, file_mode=None,
        create_parent_dir=False, delay_create=False, dir_mode=None,
        get_scope=None):
        _KnitComponentFile.__init__(self, transport, filename, mode,
                                    file_mode=file_mode,
                                    create_parent_dir=create_parent_dir,
                                    dir_mode=dir_mode)
        self._cache = {}
        # position in _history is the 'official' index for a revision
        # but the values may have come from a newer entry.
        # so - wc -l of a knit index is != the number of unique names
        # in the knit.
        self._history = []
        try:
            fp = self._transport.get(self._filename)
            try:
                # _load_data may raise NoSuchFile if the target knit is
                # completely empty.
                _load_data(self, fp)
            finally:
                fp.close()
        except NoSuchFile:
            if mode != 'w' or not create:
                raise
            elif delay_create:
                self._need_to_create = True
            else:
                self._transport.put_bytes_non_atomic(
                    self._filename, self.HEADER, mode=self._file_mode)
        self._scope = get_scope()
        self._get_scope = get_scope

    def get_ancestry(self, versions, topo_sorted=True):
        """See VersionedFile.get_ancestry."""
        # get a graph of all the mentioned versions:
        graph = {}
        pending = set(versions)
        cache = self._cache
        while pending:
            version = pending.pop()
            # trim ghosts
            try:
                parents = [p for p in cache[version][4] if p in cache]
            except KeyError:
                raise RevisionNotPresent(version, self._filename)
            # if not completed and not a ghost
            pending.update([p for p in parents if p not in graph])
            graph[version] = parents
        if not topo_sorted:
            return graph.keys()
        return topo_sort(graph.items())

    def get_ancestry_with_ghosts(self, versions):
        """See VersionedFile.get_ancestry_with_ghosts."""
        # get a graph of all the mentioned versions:
        self.check_versions_present(versions)
        cache = self._cache
        graph = {}
        pending = set(versions)
        while pending:
            version = pending.pop()
            try:
                parents = cache[version][4]
            except KeyError:
                # ghost, fake it
                graph[version] = []
            else:
                # if not completed
                pending.update([p for p in parents if p not in graph])
                graph[version] = parents
        return topo_sort(graph.items())

    def get_build_details(self, version_ids):
        """Get the method, index_memo and compression parent for version_ids.

        Ghosts are omitted from the result.

        :param version_ids: An iterable of version_ids.
        :return: A dict of version_id:(index_memo, compression_parent,
                                       parents, record_details).
            index_memo
                opaque structure to pass to read_records to extract the raw
                data
            compression_parent
                Content that this record is built upon, may be None
            parents
                Logical parents of this node
            record_details
                extra information about the content which needs to be passed to
                Factory.parse_record
        """
        result = {}
        for version_id in version_ids:
            if version_id not in self._cache:
                # ghosts are omitted
                continue
            method = self.get_method(version_id)
            parents = self.get_parents_with_ghosts(version_id)
            if method == 'fulltext':
                compression_parent = None
            else:
                compression_parent = parents[0]
            noeol = 'no-eol' in self.get_options(version_id)
            index_memo = self.get_position(version_id)
            result[version_id] = (index_memo, compression_parent,
                                  parents, (method, noeol))
        return result

    def num_versions(self):
        return len(self._history)

    __len__ = num_versions

    def get_versions(self):
        """Get all the versions in the file. not topologically sorted."""
        return self._history

    def _version_list_to_index(self, versions):
        result_list = []
        cache = self._cache
        for version in versions:
            if version in cache:
                # -- inlined lookup() --
                result_list.append(str(cache[version][5]))
                # -- end lookup () --
            else:
                result_list.append('.' + version)
        return ' '.join(result_list)

    def add_version(self, version_id, options, index_memo, parents):
        """Add a version record to the index."""
        self.add_versions(((version_id, options, index_memo, parents),))

    def add_versions(self, versions, random_id=False):
        """Add multiple versions to the index.
        
        :param versions: a list of tuples:
                         (version_id, options, pos, size, parents).
        :param random_id: If True the ids being added were randomly generated
            and no check for existence will be performed.
        """
        lines = []
        orig_history = self._history[:]
        orig_cache = self._cache.copy()

        try:
            for version_id, options, (index, pos, size), parents in versions:
                line = "\n%s %s %s %s %s :" % (version_id,
                                               ','.join(options),
                                               pos,
                                               size,
                                               self._version_list_to_index(parents))
                lines.append(line)
                self._cache_version(version_id, options, pos, size, tuple(parents))
            if not self._need_to_create:
                self._transport.append_bytes(self._filename, ''.join(lines))
            else:
                sio = StringIO()
                sio.write(self.HEADER)
                sio.writelines(lines)
                sio.seek(0)
                self._transport.put_file_non_atomic(self._filename, sio,
                                    create_parent_dir=self._create_parent_dir,
                                    mode=self._file_mode,
                                    dir_mode=self._dir_mode)
                self._need_to_create = False
        except:
            # If any problems happen, restore the original values and re-raise
            self._history = orig_history
            self._cache = orig_cache
            raise

    def has_version(self, version_id):
        """True if the version is in the index."""
        return version_id in self._cache

    def get_position(self, version_id):
        """Return details needed to access the version.
        
        .kndx indices do not support split-out data, so return None for the 
        index field.

        :return: a tuple (None, data position, size) to hand to the access
            logic to get the record.
        """
        entry = self._cache[version_id]
        return None, entry[2], entry[3]

    def get_method(self, version_id):
        """Return compression method of specified version."""
        try:
            options = self._cache[version_id][1]
        except KeyError:
            raise RevisionNotPresent(version_id, self._filename)
        if 'fulltext' in options:
            return 'fulltext'
        else:
            if 'line-delta' not in options:
                raise errors.KnitIndexUnknownMethod(self._full_path(), options)
            return 'line-delta'

    def get_options(self, version_id):
        """Return a list representing options.

        e.g. ['foo', 'bar']
        """
        return self._cache[version_id][1]

    def get_parent_map(self, version_ids):
        """Passed through to by KnitVersionedFile.get_parent_map."""
        result = {}
        for version_id in version_ids:
            try:
                result[version_id] = tuple(self._cache[version_id][4])
            except KeyError:
                pass
        return result

    def get_parents_with_ghosts(self, version_id):
        """Return parents of specified version with ghosts."""
        try:
            return self.get_parent_map([version_id])[version_id]
        except KeyError:
            raise RevisionNotPresent(version_id, self)

    def check_versions_present(self, version_ids):
        """Check that all specified versions are present."""
        cache = self._cache
        for version_id in version_ids:
            if version_id not in cache:
                raise RevisionNotPresent(version_id, self._filename)


class KnitGraphIndex(object):
    """A knit index that builds on GraphIndex."""

    def __init__(self, graph_index, deltas=False, parents=True, add_callback=None):
        """Construct a KnitGraphIndex on a graph_index.

        :param graph_index: An implementation of bzrlib.index.GraphIndex.
        :param deltas: Allow delta-compressed records.
        :param add_callback: If not None, allow additions to the index and call
            this callback with a list of added GraphIndex nodes:
            [(node, value, node_refs), ...]
        :param parents: If True, record knits parents, if not do not record 
            parents.
        """
        self._graph_index = graph_index
        self._deltas = deltas
        self._add_callback = add_callback
        self._parents = parents
        if deltas and not parents:
            raise KnitCorrupt(self, "Cannot do delta compression without "
                "parent tracking.")

    def _check_write_ok(self):
        pass

    def _get_entries(self, keys, check_present=False):
        """Get the entries for keys.
        
        :param keys: An iterable of index keys, - 1-tuples.
        """
        keys = set(keys)
        found_keys = set()
        if self._parents:
            for node in self._graph_index.iter_entries(keys):
                yield node
                found_keys.add(node[1])
        else:
            # adapt parentless index to the rest of the code.
            for node in self._graph_index.iter_entries(keys):
                yield node[0], node[1], node[2], ()
                found_keys.add(node[1])
        if check_present:
            missing_keys = keys.difference(found_keys)
            if missing_keys:
                raise RevisionNotPresent(missing_keys.pop(), self)

    def _present_keys(self, version_ids):
        return set([
            node[1] for node in self._get_entries(version_ids)])

    def _parentless_ancestry(self, versions):
        """Honour the get_ancestry API for parentless knit indices."""
        wanted_keys = self._version_ids_to_keys(versions)
        present_keys = self._present_keys(wanted_keys)
        missing = set(wanted_keys).difference(present_keys)
        if missing:
            raise RevisionNotPresent(missing.pop(), self)
        return list(self._keys_to_version_ids(present_keys))

    def get_ancestry(self, versions, topo_sorted=True):
        """See VersionedFile.get_ancestry."""
        if not self._parents:
            return self._parentless_ancestry(versions)
        # XXX: This will do len(history) index calls - perhaps
        # it should be altered to be a index core feature?
        # get a graph of all the mentioned versions:
        graph = {}
        ghosts = set()
        versions = self._version_ids_to_keys(versions)
        pending = set(versions)
        while pending:
            # get all pending nodes
            this_iteration = pending
            new_nodes = self._get_entries(this_iteration)
            found = set()
            pending = set()
            for (index, key, value, node_refs) in new_nodes:
                # dont ask for ghosties - otherwise
                # we we can end up looping with pending
                # being entirely ghosted.
                graph[key] = [parent for parent in node_refs[0]
                    if parent not in ghosts]
                # queue parents
                for parent in graph[key]:
                    # dont examine known nodes again
                    if parent in graph:
                        continue
                    pending.add(parent)
                found.add(key)
            ghosts.update(this_iteration.difference(found))
        if versions.difference(graph):
            raise RevisionNotPresent(versions.difference(graph).pop(), self)
        if topo_sorted:
            result_keys = topo_sort(graph.items())
        else:
            result_keys = graph.iterkeys()
        return [key[0] for key in result_keys]

    def get_ancestry_with_ghosts(self, versions):
        """See VersionedFile.get_ancestry."""
        if not self._parents:
            return self._parentless_ancestry(versions)
        # XXX: This will do len(history) index calls - perhaps
        # it should be altered to be a index core feature?
        # get a graph of all the mentioned versions:
        graph = {}
        versions = self._version_ids_to_keys(versions)
        pending = set(versions)
        while pending:
            # get all pending nodes
            this_iteration = pending
            new_nodes = self._get_entries(this_iteration)
            pending = set()
            for (index, key, value, node_refs) in new_nodes:
                graph[key] = node_refs[0]
                # queue parents 
                for parent in graph[key]:
                    # dont examine known nodes again
                    if parent in graph:
                        continue
                    pending.add(parent)
            missing_versions = this_iteration.difference(graph)
            missing_needed = versions.intersection(missing_versions)
            if missing_needed:
                raise RevisionNotPresent(missing_needed.pop(), self)
            for missing_version in missing_versions:
                # add a key, no parents
                graph[missing_version] = []
                pending.discard(missing_version) # don't look for it
        result_keys = topo_sort(graph.items())
        return [key[0] for key in result_keys]

    def get_build_details(self, version_ids):
        """Get the method, index_memo and compression parent for version_ids.

        Ghosts are omitted from the result.

        :param version_ids: An iterable of version_ids.
        :return: A dict of version_id:(index_memo, compression_parent,
                                       parents, record_details).
            index_memo
                opaque structure to pass to read_records to extract the raw
                data
            compression_parent
                Content that this record is built upon, may be None
            parents
                Logical parents of this node
            record_details
                extra information about the content which needs to be passed to
                Factory.parse_record
        """
        result = {}
        entries = self._get_entries(self._version_ids_to_keys(version_ids), True)
        for entry in entries:
            version_id = self._keys_to_version_ids((entry[1],))[0]
            if not self._parents:
                parents = ()
            else:
                parents = self._keys_to_version_ids(entry[3][0])
            if not self._deltas:
                compression_parent = None
            else:
                compression_parent_key = self._compression_parent(entry)
                if compression_parent_key:
                    compression_parent = self._keys_to_version_ids(
                    (compression_parent_key,))[0]
                else:
                    compression_parent = None
            noeol = (entry[2][0] == 'N')
            if compression_parent:
                method = 'line-delta'
            else:
                method = 'fulltext'
            result[version_id] = (self._node_to_position(entry),
                                  compression_parent, parents,
                                  (method, noeol))
        return result

    def _compression_parent(self, an_entry):
        # return the key that an_entry is compressed against, or None
        # Grab the second parent list (as deltas implies parents currently)
        compression_parents = an_entry[3][1]
        if not compression_parents:
            return None
        return compression_parents[0]

    def _get_method(self, node):
        if not self._deltas:
            return 'fulltext'
        if self._compression_parent(node):
            return 'line-delta'
        else:
            return 'fulltext'

    def num_versions(self):
        return len(list(self._graph_index.iter_all_entries()))

    __len__ = num_versions

    def get_versions(self):
        """Get all the versions in the file. not topologically sorted."""
        return [node[1][0] for node in self._graph_index.iter_all_entries()]
    
    def has_version(self, version_id):
        """True if the version is in the index."""
        return len(self._present_keys(self._version_ids_to_keys([version_id]))) == 1

    def _keys_to_version_ids(self, keys):
        return tuple(key[0] for key in keys)

    def get_position(self, version_id):
        """Return details needed to access the version.
        
        :return: a tuple (index, data position, size) to hand to the access
            logic to get the record.
        """
        node = self._get_node(version_id)
        return self._node_to_position(node)

    def _node_to_position(self, node):
        """Convert an index value to position details."""
        bits = node[2][1:].split(' ')
        return node[0], int(bits[0]), int(bits[1])

    def get_method(self, version_id):
        """Return compression method of specified version."""
        return self._get_method(self._get_node(version_id))

    def _get_node(self, version_id):
        try:
            return list(self._get_entries(self._version_ids_to_keys([version_id])))[0]
        except IndexError:
            raise RevisionNotPresent(version_id, self)

    def get_options(self, version_id):
        """Return a list representing options.

        e.g. ['foo', 'bar']
        """
        node = self._get_node(version_id)
        options = [self._get_method(node)]
        if node[2][0] == 'N':
            options.append('no-eol')
        return options

    def get_parent_map(self, version_ids):
        """Passed through to by KnitVersionedFile.get_parent_map."""
        nodes = self._get_entries(self._version_ids_to_keys(version_ids))
        result = {}
        if self._parents:
            for node in nodes:
                result[node[1][0]] = self._keys_to_version_ids(node[3][0])
        else:
            for node in nodes:
                result[node[1][0]] = ()
        return result

    def get_parents_with_ghosts(self, version_id):
        """Return parents of specified version with ghosts."""
        try:
            return self.get_parent_map([version_id])[version_id]
        except KeyError:
            raise RevisionNotPresent(version_id, self)

    def check_versions_present(self, version_ids):
        """Check that all specified versions are present."""
        keys = self._version_ids_to_keys(version_ids)
        present = self._present_keys(keys)
        missing = keys.difference(present)
        if missing:
            raise RevisionNotPresent(missing.pop(), self)

    def add_version(self, version_id, options, access_memo, parents):
        """Add a version record to the index."""
        return self.add_versions(((version_id, options, access_memo, parents),))

    def add_versions(self, versions, random_id=False):
        """Add multiple versions to the index.
        
        This function does not insert data into the Immutable GraphIndex
        backing the KnitGraphIndex, instead it prepares data for insertion by
        the caller and checks that it is safe to insert then calls
        self._add_callback with the prepared GraphIndex nodes.

        :param versions: a list of tuples:
                         (version_id, options, pos, size, parents).
        :param random_id: If True the ids being added were randomly generated
            and no check for existence will be performed.
        """
        if not self._add_callback:
            raise errors.ReadOnlyError(self)
        # we hope there are no repositories with inconsistent parentage
        # anymore.
        # check for dups

        keys = {}
        for (version_id, options, access_memo, parents) in versions:
            index, pos, size = access_memo
            key = (version_id, )
            parents = tuple((parent, ) for parent in parents)
            if 'no-eol' in options:
                value = 'N'
            else:
                value = ' '
            value += "%d %d" % (pos, size)
            if not self._deltas:
                if 'line-delta' in options:
                    raise KnitCorrupt(self, "attempt to add line-delta in non-delta knit")
            if self._parents:
                if self._deltas:
                    if 'line-delta' in options:
                        node_refs = (parents, (parents[0],))
                    else:
                        node_refs = (parents, ())
                else:
                    node_refs = (parents, )
            else:
                if parents:
                    raise KnitCorrupt(self, "attempt to add node with parents "
                        "in parentless index.")
                node_refs = ()
            keys[key] = (value, node_refs)
        if not random_id:
            present_nodes = self._get_entries(keys)
            for (index, key, value, node_refs) in present_nodes:
                if (value, node_refs) != keys[key]:
                    raise KnitCorrupt(self, "inconsistent details in add_versions"
                        ": %s %s" % ((value, node_refs), keys[key]))
                del keys[key]
        result = []
        if self._parents:
            for key, (value, node_refs) in keys.iteritems():
                result.append((key, value, node_refs))
        else:
            for key, (value, node_refs) in keys.iteritems():
                result.append((key, value))
        self._add_callback(result)
        
    def _version_ids_to_keys(self, version_ids):
        return set((version_id, ) for version_id in version_ids)


class _KnitAccess(object):
    """Access to knit records in a .knit file."""

    def __init__(self, transport, filename, _file_mode, _dir_mode,
        _need_to_create, _create_parent_dir):
        """Create a _KnitAccess for accessing and inserting data.

        :param transport: The transport the .knit is located on.
        :param filename: The filename of the .knit.
        """
        self._transport = transport
        self._filename = filename
        self._file_mode = _file_mode
        self._dir_mode = _dir_mode
        self._need_to_create = _need_to_create
        self._create_parent_dir = _create_parent_dir

    def add_raw_records(self, sizes, raw_data):
        """Add raw knit bytes to a storage area.

        The data is spooled to whereever the access method is storing data.

        :param sizes: An iterable containing the size of each raw data segment.
        :param raw_data: A bytestring containing the data.
        :return: A list of memos to retrieve the record later. Each memo is a
            tuple - (index, pos, length), where the index field is always None
            for the .knit access method.
        """
        if not self._need_to_create:
            base = self._transport.append_bytes(self._filename, raw_data)
        else:
            self._transport.put_bytes_non_atomic(self._filename, raw_data,
                                   create_parent_dir=self._create_parent_dir,
                                   mode=self._file_mode,
                                   dir_mode=self._dir_mode)
            self._need_to_create = False
            base = 0
        result = []
        for size in sizes:
            result.append((None, base, size))
            base += size
        return result

    def create(self):
        """IFF this data access has its own storage area, initialise it.

        :return: None.
        """
        self._transport.put_bytes_non_atomic(self._filename, '',
                                             mode=self._file_mode)

    def open_file(self):
        """IFF this data access can be represented as a single file, open it.

        For knits that are not mapped to a single file on disk this will
        always return None.

        :return: None or a file handle.
        """
        try:
            return self._transport.get(self._filename)
        except NoSuchFile:
            pass
        return None

    def get_raw_records(self, memos_for_retrieval):
        """Get the raw bytes for a records.

        :param memos_for_retrieval: An iterable containing the (index, pos, 
            length) memo for retrieving the bytes. The .knit method ignores
            the index as there is always only a single file.
        :return: An iterator over the bytes of the records.
        """
        read_vector = [(pos, size) for (index, pos, size) in memos_for_retrieval]
        for pos, data in self._transport.readv(self._filename, read_vector):
            yield data


class _PackAccess(object):
    """Access to knit records via a collection of packs."""

    def __init__(self, index_to_packs, writer=None):
        """Create a _PackAccess object.

        :param index_to_packs: A dict mapping index objects to the transport
            and file names for obtaining data.
        :param writer: A tuple (pack.ContainerWriter, write_index) which
            contains the pack to write, and the index that reads from it will
            be associated with.
        """
        if writer:
            self.container_writer = writer[0]
            self.write_index = writer[1]
        else:
            self.container_writer = None
            self.write_index = None
        self.indices = index_to_packs

    def add_raw_records(self, sizes, raw_data):
        """Add raw knit bytes to a storage area.

        The data is spooled to the container writer in one bytes-record per
        raw data item.

        :param sizes: An iterable containing the size of each raw data segment.
        :param raw_data: A bytestring containing the data.
        :return: A list of memos to retrieve the record later. Each memo is a
            tuple - (index, pos, length), where the index field is the 
            write_index object supplied to the PackAccess object.
        """
        result = []
        offset = 0
        for size in sizes:
            p_offset, p_length = self.container_writer.add_bytes_record(
                raw_data[offset:offset+size], [])
            offset += size
            result.append((self.write_index, p_offset, p_length))
        return result

    def create(self):
        """Pack based knits do not get individually created."""

    def get_raw_records(self, memos_for_retrieval):
        """Get the raw bytes for a records.

        :param memos_for_retrieval: An iterable containing the (index, pos, 
            length) memo for retrieving the bytes. The Pack access method
            looks up the pack to use for a given record in its index_to_pack
            map.
        :return: An iterator over the bytes of the records.
        """
        # first pass, group into same-index requests
        request_lists = []
        current_index = None
        for (index, offset, length) in memos_for_retrieval:
            if current_index == index:
                current_list.append((offset, length))
            else:
                if current_index is not None:
                    request_lists.append((current_index, current_list))
                current_index = index
                current_list = [(offset, length)]
        # handle the last entry
        if current_index is not None:
            request_lists.append((current_index, current_list))
        for index, offsets in request_lists:
            transport, path = self.indices[index]
            reader = pack.make_readv_reader(transport, path, offsets)
            for names, read_func in reader.iter_records():
                yield read_func(None)

    def open_file(self):
        """Pack based knits have no single file."""
        return None

    def set_writer(self, writer, index, (transport, packname)):
        """Set a writer to use for adding data."""
        if index is not None:
            self.indices[index] = (transport, packname)
        self.container_writer = writer
        self.write_index = index


class _StreamAccess(object):
    """A Knit Access object that provides data from a datastream.
    
    It also provides a fallback to present as unannotated data, annotated data
    from a *backing* access object.

    This is triggered by a index_memo which is pointing to a different index
    than this was constructed with, and is used to allow extracting full
    unannotated texts for insertion into annotated knits.
    """

    def __init__(self, reader_callable, stream_index, backing_knit,
        orig_factory):
        """Create a _StreamAccess object.

        :param reader_callable: The reader_callable from the datastream.
            This is called to buffer all the data immediately, for 
            random access.
        :param stream_index: The index the data stream this provides access to
            which will be present in native index_memo's.
        :param backing_knit: The knit object that will provide access to 
            annotated texts which are not available in the stream, so as to
            create unannotated texts.
        :param orig_factory: The original content factory used to generate the
            stream. This is used for checking whether the thunk code for
            supporting _copy_texts will generate the correct form of data.
        """
        self.data = reader_callable(None)
        self.stream_index = stream_index
        self.backing_knit = backing_knit
        self.orig_factory = orig_factory

    def get_raw_records(self, memos_for_retrieval):
        """Get the raw bytes for a records.

        :param memos_for_retrieval: An iterable of memos from the
            _StreamIndex object identifying bytes to read; for these classes
            they are (from_backing_knit, index, start, end) and can point to
            either the backing knit or streamed data.
        :return: An iterator yielding a byte string for each record in 
            memos_for_retrieval.
        """
        # use a generator for memory friendliness
        for from_backing_knit, version_id, start, end in memos_for_retrieval:
            if not from_backing_knit:
                if version_id is not self.stream_index:
                    raise AssertionError()
                yield self.data[start:end]
                continue
            # we have been asked to thunk. This thunking only occurs when
            # we are obtaining plain texts from an annotated backing knit
            # so that _copy_texts will work.
            # We could improve performance here by scanning for where we need
            # to do this and using get_line_list, then interleaving the output
            # as desired. However, for now, this is sufficient.
            if self.orig_factory.__class__ != KnitPlainFactory:
                raise errors.KnitCorrupt(
                    self, 'Bad thunk request %r cannot be backed by %r' %
                        (version_id, self.orig_factory))
            lines = self.backing_knit.get_lines(version_id)
            line_bytes = ''.join(lines)
            digest = sha_string(line_bytes)
            # the packed form of the fulltext always has a trailing newline,
            # even if the actual text does not, unless the file is empty.  the
            # record options including the noeol flag are passed through by
            # _StreamIndex, so this is safe.
            if lines:
                if lines[-1][-1] != '\n':
                    lines[-1] = lines[-1] + '\n'
                    line_bytes += '\n'
            # We want plain data, because we expect to thunk only to allow text
            # extraction.
            size, bytes = self.backing_knit._data._record_to_data(version_id,
                digest, lines, line_bytes)
            yield bytes


class _StreamIndex(object):
    """A Knit Index object that uses the data map from a datastream."""

    def __init__(self, data_list, backing_index):
        """Create a _StreamIndex object.

        :param data_list: The data_list from the datastream.
        :param backing_index: The index which will supply values for nodes
            referenced outside of this stream.
        """
        self.data_list = data_list
        self.backing_index = backing_index
        self._by_version = {}
        pos = 0
        for key, options, length, parents in data_list:
            self._by_version[key] = options, (pos, pos + length), parents
            pos += length

    def get_ancestry(self, versions, topo_sorted):
        """Get an ancestry list for versions."""
        if topo_sorted:
            # Not needed for basic joins
            raise NotImplementedError(self.get_ancestry)
        # get a graph of all the mentioned versions:
        # Little ugly - basically copied from KnitIndex, but don't want to
        # accidentally incorporate too much of that index's code.
        ancestry = set()
        pending = set(versions)
        cache = self._by_version
        while pending:
            version = pending.pop()
            # trim ghosts
            try:
                parents = [p for p in cache[version][2] if p in cache]
            except KeyError:
                raise RevisionNotPresent(version, self)
            # if not completed and not a ghost
            pending.update([p for p in parents if p not in ancestry])
            ancestry.add(version)
        return list(ancestry)

    def get_build_details(self, version_ids):
        """Get the method, index_memo and compression parent for version_ids.

        Ghosts are omitted from the result.

        :param version_ids: An iterable of version_ids.
        :return: A dict of version_id:(index_memo, compression_parent,
                                       parents, record_details).
            index_memo
                opaque memo that can be passed to _StreamAccess.read_records
                to extract the raw data; for these classes it is
                (from_backing_knit, index, start, end) 
            compression_parent
                Content that this record is built upon, may be None
            parents
                Logical parents of this node
            record_details
                extra information about the content which needs to be passed to
                Factory.parse_record
        """
        result = {}
        for version_id in version_ids:
            try:
                method = self.get_method(version_id)
            except errors.RevisionNotPresent:
                # ghosts are omitted
                continue
            parent_ids = self.get_parents_with_ghosts(version_id)
            noeol = ('no-eol' in self.get_options(version_id))
            index_memo = self.get_position(version_id)
            from_backing_knit = index_memo[0]
            if from_backing_knit:
                # texts retrieved from the backing knit are always full texts
                method = 'fulltext'
            if method == 'fulltext':
                compression_parent = None
            else:
                compression_parent = parent_ids[0]
            result[version_id] = (index_memo, compression_parent,
                                  parent_ids, (method, noeol))
        return result

    def get_method(self, version_id):
        """Return compression method of specified version."""
        options = self.get_options(version_id)
        if 'fulltext' in options:
            return 'fulltext'
        elif 'line-delta' in options:
            return 'line-delta'
        else:
            raise errors.KnitIndexUnknownMethod(self, options)

    def get_options(self, version_id):
        """Return a list representing options.

        e.g. ['foo', 'bar']
        """
        try:
            return self._by_version[version_id][0]
        except KeyError:
            options = list(self.backing_index.get_options(version_id))
            if 'fulltext' in options:
                pass
            elif 'line-delta' in options:
                # Texts from the backing knit are always returned from the stream
                # as full texts
                options.remove('line-delta')
                options.append('fulltext')
            else:
                raise errors.KnitIndexUnknownMethod(self, options)
            return tuple(options)

    def get_parent_map(self, version_ids):
        """Passed through to by KnitVersionedFile.get_parent_map."""
        result = {}
        pending_ids = set()
        for version_id in version_ids:
            try:
                result[version_id] = self._by_version[version_id][2]
            except KeyError:
                pending_ids.add(version_id)
        result.update(self.backing_index.get_parent_map(pending_ids))
        return result

    def get_parents_with_ghosts(self, version_id):
        """Return parents of specified version with ghosts."""
        try:
            return self.get_parent_map([version_id])[version_id]
        except KeyError:
            raise RevisionNotPresent(version_id, self)

    def get_position(self, version_id):
        """Return details needed to access the version.
        
        _StreamAccess has the data as a big array, so we return slice
        coordinates into that (as index_memo's are opaque outside the
        index and matching access class).

        :return: a tuple (from_backing_knit, index, start, end) that can 
            be passed e.g. to get_raw_records.  
            If from_backing_knit is False, index will be self, otherwise it
            will be a version id.
        """
        try:
            start, end = self._by_version[version_id][1]
            return False, self, start, end
        except KeyError:
            # Signal to the access object to handle this from the backing knit.
            return (True, version_id, None, None)

    def get_versions(self):
        """Get all the versions in the stream."""
        return self._by_version.keys()


class _KnitData(object):
    """Manage extraction of data from a KnitAccess, caching and decompressing.
    
    The KnitData class provides the logic for parsing and using knit records,
    making use of an access method for the low level read and write operations.
    """

    def __init__(self, access):
        """Create a KnitData object.

        :param access: The access method to use. Access methods such as
            _KnitAccess manage the insertion of raw records and the subsequent
            retrieval of the same.
        """
        self._access = access
        self._checked = False

    def _open_file(self):
        return self._access.open_file()

    def _record_to_data(self, version_id, digest, lines, dense_lines=None):
        """Convert version_id, digest, lines into a raw data block.
        
        :param dense_lines: The bytes of lines but in a denser form. For
            instance, if lines is a list of 1000 bytestrings each ending in \n,
            dense_lines may be a list with one line in it, containing all the
            1000's lines and their \n's. Using dense_lines if it is already
            known is a win because the string join to create bytes in this
            function spends less time resizing the final string.
        :return: (len, a StringIO instance with the raw data ready to read.)
        """
        # Note: using a string copy here increases memory pressure with e.g.
        # ISO's, but it is about 3 seconds faster on a 1.2Ghz intel machine
        # when doing the initial commit of a mozilla tree. RBC 20070921
        bytes = ''.join(chain(
            ["version %s %d %s\n" % (version_id,
                                     len(lines),
                                     digest)],
            dense_lines or lines,
            ["end %s\n" % version_id]))
        compressed_bytes = bytes_to_gzip(bytes)
        return len(compressed_bytes), compressed_bytes

    def add_raw_records(self, sizes, raw_data):
        """Append a prepared record to the data file.
        
        :param sizes: An iterable containing the size of each raw data segment.
        :param raw_data: A bytestring containing the data.
        :return: a list of index data for the way the data was stored.
            See the access method add_raw_records documentation for more
            details.
        """
        return self._access.add_raw_records(sizes, raw_data)
        
    def _parse_record_header(self, version_id, raw_data):
        """Parse a record header for consistency.

        :return: the header and the decompressor stream.
                 as (stream, header_record)
        """
        df = GzipFile(mode='rb', fileobj=StringIO(raw_data))
        try:
            rec = self._check_header(version_id, df.readline())
        except Exception, e:
            raise KnitCorrupt(self._access,
                              "While reading {%s} got %s(%s)"
                              % (version_id, e.__class__.__name__, str(e)))
        return df, rec

    def _split_header(self, line):
        rec = line.split()
        if len(rec) != 4:
            raise KnitCorrupt(self._access,
                              'unexpected number of elements in record header')
        return rec

    def _check_header_version(self, rec, version_id):
        if rec[1] != version_id:
            raise KnitCorrupt(self._access,
                              'unexpected version, wanted %r, got %r'
                              % (version_id, rec[1]))

    def _check_header(self, version_id, line):
        rec = self._split_header(line)
        self._check_header_version(rec, version_id)
        return rec

    def _parse_record_unchecked(self, data):
        # profiling notes:
        # 4168 calls in 2880 217 internal
        # 4168 calls to _parse_record_header in 2121
        # 4168 calls to readlines in 330
        df = GzipFile(mode='rb', fileobj=StringIO(data))
        try:
            record_contents = df.readlines()
        except Exception, e:
            raise KnitCorrupt(self._access, "Corrupt compressed record %r, got %s(%s)" %
                (data, e.__class__.__name__, str(e)))
        header = record_contents.pop(0)
        rec = self._split_header(header)
        last_line = record_contents.pop()
        if len(record_contents) != int(rec[2]):
            raise KnitCorrupt(self._access,
                              'incorrect number of lines %s != %s'
                              ' for version {%s}'
                              % (len(record_contents), int(rec[2]),
                                 rec[1]))
        if last_line != 'end %s\n' % rec[1]:
            raise KnitCorrupt(self._access,
                              'unexpected version end line %r, wanted %r' 
                              % (last_line, rec[1]))
        df.close()
        return rec, record_contents

    def _parse_record(self, version_id, data):
        rec, record_contents = self._parse_record_unchecked(data)
        self._check_header_version(rec, version_id)
        return record_contents, rec[3]

    def read_records_iter_raw(self, records):
        """Read text records from data file and yield raw data.

        This unpacks enough of the text record to validate the id is
        as expected but thats all.

        Each item the iterator yields is (version_id, bytes,
        sha1_of_full_text).
        """
        # setup an iterator of the external records:
        # uses readv so nice and fast we hope.
        if len(records):
            # grab the disk data needed.
            needed_offsets = [index_memo for version_id, index_memo
                                           in records]
            raw_records = self._access.get_raw_records(needed_offsets)

        for version_id, index_memo in records:
            data = raw_records.next()
            # validate the header
            df, rec = self._parse_record_header(version_id, data)
            df.close()
            yield version_id, data, rec[3]

    def read_records_iter(self, records):
        """Read text records from data file and yield result.

        The result will be returned in whatever is the fastest to read.
        Not by the order requested. Also, multiple requests for the same
        record will only yield 1 response.
        :param records: A list of (version_id, pos, len) entries
        :return: Yields (version_id, contents, digest) in the order
                 read, not the order requested
        """
        if not records:
            return

        needed_records = sorted(set(records), key=operator.itemgetter(1))
        if not needed_records:
            return

        # The transport optimizes the fetching as well 
        # (ie, reads continuous ranges.)
        raw_data = self._access.get_raw_records(
            [index_memo for version_id, index_memo in needed_records])

        for (version_id, index_memo), data in \
                izip(iter(needed_records), raw_data):
            content, digest = self._parse_record(version_id, data)
            yield version_id, content, digest

    def read_records(self, records):
        """Read records into a dictionary."""
        components = {}
        for record_id, content, digest in \
                self.read_records_iter(records):
            components[record_id] = (content, digest)
        return components


class InterKnit(InterVersionedFile):
    """Optimised code paths for knit to knit operations."""
    
    _matching_file_from_factory = staticmethod(make_file_knit)
    _matching_file_to_factory = staticmethod(make_file_knit)
    
    @staticmethod
    def is_compatible(source, target):
        """Be compatible with knits.  """
        try:
            return (isinstance(source, KnitVersionedFile) and
                    isinstance(target, KnitVersionedFile))
        except AttributeError:
            return False

    def _copy_texts(self, pb, msg, version_ids, ignore_missing=False):
        """Copy texts to the target by extracting and adding them one by one.

        see join() for the parameter definitions.
        """
        version_ids = self._get_source_version_ids(version_ids, ignore_missing)
        # --- the below is factorable out with VersionedFile.join, but wait for
        # VersionedFiles, it may all be simpler then.
        graph = Graph(self.source)
        search = graph._make_breadth_first_searcher(version_ids)
        transitive_ids = set()
        map(transitive_ids.update, list(search))
        parent_map = self.source.get_parent_map(transitive_ids)
        order = topo_sort(parent_map.items())

        def size_of_content(content):
            return sum(len(line) for line in content.text())
        # Cache at most 10MB of parent texts
        parent_cache = lru_cache.LRUSizeCache(max_size=10*1024*1024,
                                              compute_size=size_of_content)
        # TODO: jam 20071116 It would be nice to have a streaming interface to
        #       get multiple texts from a source. The source could be smarter
        #       about how it handled intermediate stages.
        #       get_line_list() or make_mpdiffs() seem like a possibility, but
        #       at the moment they extract all full texts into memory, which
        #       causes us to store more than our 3x fulltext goal.
        #       Repository.iter_files_bytes() may be another possibility
        to_process = [version for version in order
                               if version not in self.target]
        total = len(to_process)
        pb = ui.ui_factory.nested_progress_bar()
        try:
            for index, version in enumerate(to_process):
                pb.update('Converting versioned data', index, total)
                sha1, num_bytes, parent_text = self.target.add_lines(version,
                    self.source.get_parents_with_ghosts(version),
                    self.source.get_lines(version),
                    parent_texts=parent_cache)
                parent_cache[version] = parent_text
        finally:
            pb.finished()
        return total

    def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
        """See InterVersionedFile.join."""
        # If the source and target are mismatched w.r.t. annotations vs
        # plain, the data needs to be converted accordingly
        if self.source.factory.annotated == self.target.factory.annotated:
            converter = None
        elif self.source.factory.annotated:
            converter = self._anno_to_plain_converter
        else:
            # We're converting from a plain to an annotated knit. Copy them
            # across by full texts.
            return self._copy_texts(pb, msg, version_ids, ignore_missing)

        version_ids = self._get_source_version_ids(version_ids, ignore_missing)
        if not version_ids:
            return 0

        pb = ui.ui_factory.nested_progress_bar()
        try:
            version_ids = list(version_ids)
            if None in version_ids:
                version_ids.remove(None)
    
            self.source_ancestry = set(self.source.get_ancestry(version_ids,
                topo_sorted=False))
            this_versions = set(self.target._index.get_versions())
            # XXX: For efficiency we should not look at the whole index,
            #      we only need to consider the referenced revisions - they
            #      must all be present, or the method must be full-text.
            #      TODO, RBC 20070919
            needed_versions = self.source_ancestry - this_versions
    
            if not needed_versions:
                return 0
            full_list = topo_sort(
                self.source.get_parent_map(self.source.versions()))
    
            version_list = [i for i in full_list if (not self.target.has_version(i)
                            and i in needed_versions)]
    
            # plan the join:
            copy_queue = []
            copy_queue_records = []
            copy_set = set()
            for version_id in version_list:
                options = self.source._index.get_options(version_id)
                parents = self.source._index.get_parents_with_ghosts(version_id)
                # check that its will be a consistent copy:
                for parent in parents:
                    # if source has the parent, we must :
                    # * already have it or
                    # * have it scheduled already
                    # otherwise we don't care
                    if not (self.target.has_version(parent) or
                            parent in copy_set or
                            not self.source.has_version(parent)):
                        raise AssertionError("problem joining parent %r "
                            "from %r to %r"
                            % (parent, self.source, self.target))
                index_memo = self.source._index.get_position(version_id)
                copy_queue_records.append((version_id, index_memo))
                copy_queue.append((version_id, options, parents))
                copy_set.add(version_id)

            # data suck the join:
            count = 0
            total = len(version_list)
            raw_datum = []
            raw_records = []
            for (version_id, raw_data, _), \
                (version_id2, options, parents) in \
                izip(self.source._data.read_records_iter_raw(copy_queue_records),
                     copy_queue):
                if not (version_id == version_id2):
                    raise AssertionError('logic error, inconsistent results')
                count = count + 1
                pb.update("Joining knit", count, total)
                if converter:
                    size, raw_data = converter(raw_data, version_id, options,
                        parents)
                else:
                    size = len(raw_data)
                raw_records.append((version_id, options, parents, size))
                raw_datum.append(raw_data)
            self.target._add_raw_records(raw_records, ''.join(raw_datum))
            return count
        finally:
            pb.finished()

    def _anno_to_plain_converter(self, raw_data, version_id, options,
                                 parents):
        """Convert annotated content to plain content."""
        data, digest = self.source._data._parse_record(version_id, raw_data)
        if 'fulltext' in options:
            content = self.source.factory.parse_fulltext(data, version_id)
            lines = self.target.factory.lower_fulltext(content)
        else:
            delta = self.source.factory.parse_line_delta(data, version_id,
                plain=True)
            lines = self.target.factory.lower_line_delta(delta)
        return self.target._data._record_to_data(version_id, digest, lines)


InterVersionedFile.register_optimiser(InterKnit)


class WeaveToKnit(InterVersionedFile):
    """Optimised code paths for weave to knit operations."""
    
    _matching_file_from_factory = bzrlib.weave.WeaveFile
    _matching_file_to_factory = staticmethod(make_file_knit)
    
    @staticmethod
    def is_compatible(source, target):
        """Be compatible with weaves to knits."""
        try:
            return (isinstance(source, bzrlib.weave.Weave) and
                    isinstance(target, KnitVersionedFile))
        except AttributeError:
            return False

    def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
        """See InterVersionedFile.join."""
        version_ids = self._get_source_version_ids(version_ids, ignore_missing)

        if not version_ids:
            return 0

        pb = ui.ui_factory.nested_progress_bar()
        try:
            version_ids = list(version_ids)
    
            self.source_ancestry = set(self.source.get_ancestry(version_ids))
            this_versions = set(self.target._index.get_versions())
            needed_versions = self.source_ancestry - this_versions
    
            if not needed_versions:
                return 0
            full_list = topo_sort(
                self.source.get_parent_map(self.source.versions()))
    
            version_list = [i for i in full_list if (not self.target.has_version(i)
                            and i in needed_versions)]
    
            # do the join:
            count = 0
            total = len(version_list)
            parent_map = self.source.get_parent_map(version_list)
            for version_id in version_list:
                pb.update("Converting to knit", count, total)
                parents = parent_map[version_id]
                # check that its will be a consistent copy:
                for parent in parents:
                    # if source has the parent, we must already have it
                    if not self.target.has_version(parent):
                        raise AssertionError("%r does not have parent %r"
                            % (self.target, parent))
                self.target.add_lines(
                    version_id, parents, self.source.get_lines(version_id))
                count = count + 1
            return count
        finally:
            pb.finished()


InterVersionedFile.register_optimiser(WeaveToKnit)


# Deprecated, use PatienceSequenceMatcher instead
KnitSequenceMatcher = patiencediff.PatienceSequenceMatcher


def annotate_knit(knit, revision_id):
    """Annotate a knit with no cached annotations.

    This implementation is for knits with no cached annotations.
    It will work for knits with cached annotations, but this is not
    recommended.
    """
    annotator = _KnitAnnotator(knit)
    return iter(annotator.annotate(revision_id))


class _KnitAnnotator(object):
    """Build up the annotations for a text."""

    def __init__(self, knit):
        self._knit = knit

        # Content objects, differs from fulltexts because of how final newlines
        # are treated by knits. the content objects here will always have a
        # final newline
        self._fulltext_contents = {}

        # Annotated lines of specific revisions
        self._annotated_lines = {}

        # Track the raw data for nodes that we could not process yet.
        # This maps the revision_id of the base to a list of children that will
        # annotated from it.
        self._pending_children = {}

        # Nodes which cannot be extracted
        self._ghosts = set()

        # Track how many children this node has, so we know if we need to keep
        # it
        self._annotate_children = {}
        self._compression_children = {}

        self._all_build_details = {}
        # The children => parent revision_id graph
        self._revision_id_graph = {}

        self._heads_provider = None

        self._nodes_to_keep_annotations = set()
        self._generations_until_keep = 100

    def set_generations_until_keep(self, value):
        """Set the number of generations before caching a node.

        Setting this to -1 will cache every merge node, setting this higher
        will cache fewer nodes.
        """
        self._generations_until_keep = value

    def _add_fulltext_content(self, revision_id, content_obj):
        self._fulltext_contents[revision_id] = content_obj
        # TODO: jam 20080305 It might be good to check the sha1digest here
        return content_obj.text()

    def _check_parents(self, child, nodes_to_annotate):
        """Check if all parents have been processed.

        :param child: A tuple of (rev_id, parents, raw_content)
        :param nodes_to_annotate: If child is ready, add it to
            nodes_to_annotate, otherwise put it back in self._pending_children
        """
        for parent_id in child[1]:
            if (parent_id not in self._annotated_lines):
                # This parent is present, but another parent is missing
                self._pending_children.setdefault(parent_id,
                                                  []).append(child)
                break
        else:
            # This one is ready to be processed
            nodes_to_annotate.append(child)

    def _add_annotation(self, revision_id, fulltext, parent_ids,
                        left_matching_blocks=None):
        """Add an annotation entry.

        All parents should already have been annotated.
        :return: A list of children that now have their parents satisfied.
        """
        a = self._annotated_lines
        annotated_parent_lines = [a[p] for p in parent_ids]
        annotated_lines = list(annotate.reannotate(annotated_parent_lines,
            fulltext, revision_id, left_matching_blocks,
            heads_provider=self._get_heads_provider()))
        self._annotated_lines[revision_id] = annotated_lines
        for p in parent_ids:
            ann_children = self._annotate_children[p]
            ann_children.remove(revision_id)
            if (not ann_children
                and p not in self._nodes_to_keep_annotations):
                del self._annotated_lines[p]
                del self._all_build_details[p]
                if p in self._fulltext_contents:
                    del self._fulltext_contents[p]
        # Now that we've added this one, see if there are any pending
        # deltas to be done, certainly this parent is finished
        nodes_to_annotate = []
        for child in self._pending_children.pop(revision_id, []):
            self._check_parents(child, nodes_to_annotate)
        return nodes_to_annotate

    def _get_build_graph(self, revision_id):
        """Get the graphs for building texts and annotations.

        The data you need for creating a full text may be different than the
        data you need to annotate that text. (At a minimum, you need both
        parents to create an annotation, but only need 1 parent to generate the
        fulltext.)

        :return: A list of (revision_id, index_memo) records, suitable for
            passing to read_records_iter to start reading in the raw data fro/
            the pack file.
        """
        if revision_id in self._annotated_lines:
            # Nothing to do
            return []
        pending = set([revision_id])
        records = []
        generation = 0
        kept_generation = 0
        while pending:
            # get all pending nodes
            generation += 1
            this_iteration = pending
            build_details = self._knit._index.get_build_details(this_iteration)
            self._all_build_details.update(build_details)
            # new_nodes = self._knit._index._get_entries(this_iteration)
            pending = set()
            for rev_id, details in build_details.iteritems():
                (index_memo, compression_parent, parents,
                 record_details) = details
                self._revision_id_graph[rev_id] = parents
                records.append((rev_id, index_memo))
                # Do we actually need to check _annotated_lines?
                pending.update(p for p in parents
                                 if p not in self._all_build_details)
                if compression_parent:
                    self._compression_children.setdefault(compression_parent,
                        []).append(rev_id)
                if parents:
                    for parent in parents:
                        self._annotate_children.setdefault(parent,
                            []).append(rev_id)
                    num_gens = generation - kept_generation
                    if ((num_gens >= self._generations_until_keep)
                        and len(parents) > 1):
                        kept_generation = generation
                        self._nodes_to_keep_annotations.add(rev_id)

            missing_versions = this_iteration.difference(build_details.keys())
            self._ghosts.update(missing_versions)
            for missing_version in missing_versions:
                # add a key, no parents
                self._revision_id_graph[missing_version] = ()
                pending.discard(missing_version) # don't look for it
        if self._ghosts.intersection(self._compression_children):
            raise KnitCorrupt(
                "We cannot have nodes which have a ghost compression parent:\n"
                "ghosts: %r\n"
                "compression children: %r"
                % (self._ghosts, self._compression_children))
        # Cleanout anything that depends on a ghost so that we don't wait for
        # the ghost to show up
        for node in self._ghosts:
            if node in self._annotate_children:
                # We won't be building this node
                del self._annotate_children[node]
        # Generally we will want to read the records in reverse order, because
        # we find the parent nodes after the children
        records.reverse()
        return records

    def _annotate_records(self, records):
        """Build the annotations for the listed records."""
        # We iterate in the order read, rather than a strict order requested
        # However, process what we can, and put off to the side things that
        # still need parents, cleaning them up when those parents are
        # processed.
        for (rev_id, record,
             digest) in self._knit._data.read_records_iter(records):
            if rev_id in self._annotated_lines:
                continue
            parent_ids = self._revision_id_graph[rev_id]
            parent_ids = [p for p in parent_ids if p not in self._ghosts]
            details = self._all_build_details[rev_id]
            (index_memo, compression_parent, parents,
             record_details) = details
            nodes_to_annotate = []
            # TODO: Remove the punning between compression parents, and
            #       parent_ids, we should be able to do this without assuming
            #       the build order
            if len(parent_ids) == 0:
                # There are no parents for this node, so just add it
                # TODO: This probably needs to be decoupled
                fulltext_content, delta = self._knit.factory.parse_record(
                    rev_id, record, record_details, None)
                fulltext = self._add_fulltext_content(rev_id, fulltext_content)
                nodes_to_annotate.extend(self._add_annotation(rev_id, fulltext,
                    parent_ids, left_matching_blocks=None))
            else:
                child = (rev_id, parent_ids, record)
                # Check if all the parents are present
                self._check_parents(child, nodes_to_annotate)
            while nodes_to_annotate:
                # Should we use a queue here instead of a stack?
                (rev_id, parent_ids, record) = nodes_to_annotate.pop()
                (index_memo, compression_parent, parents,
                 record_details) = self._all_build_details[rev_id]
                if compression_parent is not None:
                    comp_children = self._compression_children[compression_parent]
                    if rev_id not in comp_children:
                        raise AssertionError("%r not in compression children %r"
                            % (rev_id, comp_children))
                    # If there is only 1 child, it is safe to reuse this
                    # content
                    reuse_content = (len(comp_children) == 1
                        and compression_parent not in
                            self._nodes_to_keep_annotations)
                    if reuse_content:
                        # Remove it from the cache since it will be changing
                        parent_fulltext_content = self._fulltext_contents.pop(compression_parent)
                        # Make sure to copy the fulltext since it might be
                        # modified
                        parent_fulltext = list(parent_fulltext_content.text())
                    else:
                        parent_fulltext_content = self._fulltext_contents[compression_parent]
                        parent_fulltext = parent_fulltext_content.text()
                    comp_children.remove(rev_id)
                    fulltext_content, delta = self._knit.factory.parse_record(
                        rev_id, record, record_details,
                        parent_fulltext_content,
                        copy_base_content=(not reuse_content))
                    fulltext = self._add_fulltext_content(rev_id,
                                                          fulltext_content)
                    blocks = KnitContent.get_line_delta_blocks(delta,
                            parent_fulltext, fulltext)
                else:
                    fulltext_content = self._knit.factory.parse_fulltext(
                        record, rev_id)
                    fulltext = self._add_fulltext_content(rev_id,
                        fulltext_content)
                    blocks = None
                nodes_to_annotate.extend(
                    self._add_annotation(rev_id, fulltext, parent_ids,
                                     left_matching_blocks=blocks))

    def _get_heads_provider(self):
        """Create a heads provider for resolving ancestry issues."""
        if self._heads_provider is not None:
            return self._heads_provider
        parent_provider = _mod_graph.DictParentsProvider(
            self._revision_id_graph)
        graph_obj = _mod_graph.Graph(parent_provider)
        head_cache = _mod_graph.FrozenHeadsCache(graph_obj)
        self._heads_provider = head_cache
        return head_cache

    def annotate(self, revision_id):
        """Return the annotated fulltext at the given revision.

        :param revision_id: The revision id for this file
        """
        records = self._get_build_graph(revision_id)
        if revision_id in self._ghosts:
            raise errors.RevisionNotPresent(revision_id, self._knit)
        self._annotate_records(records)
        return self._annotated_lines[revision_id]


try:
    from bzrlib._knit_load_data_c import _load_data_c as _load_data
except ImportError:
    from bzrlib._knit_load_data_py import _load_data_py as _load_data