/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/knit.py

  • Committer: Wouter van Heyst
  • Date: 2011-05-18 14:45:15 UTC
  • mto: This revision was merged to the branch mainline in revision 5893.
  • Revision ID: larstiq@larstiq.dyndns.org-20110518144515-m5fzu0k5dw0pz9n7
Fix the pypy refcount bugs in per_transport tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
56
56
from itertools import izip
57
57
import operator
58
58
import os
59
 
import sys
60
59
 
61
60
from bzrlib.lazy_import import lazy_import
62
61
lazy_import(globals(), """
 
62
import gzip
 
63
 
63
64
from bzrlib import (
64
 
    annotate,
65
65
    debug,
66
66
    diff,
67
67
    graph as _mod_graph,
68
68
    index as _mod_index,
69
 
    lru_cache,
70
69
    pack,
71
 
    progress,
 
70
    patiencediff,
72
71
    static_tuple,
73
72
    trace,
74
73
    tsort,
75
74
    tuned_gzip,
76
75
    ui,
77
76
    )
 
77
 
 
78
from bzrlib.repofmt import pack_repo
78
79
""")
79
80
from bzrlib import (
 
81
    annotate,
80
82
    errors,
81
83
    osutils,
82
 
    patiencediff,
83
84
    )
84
85
from bzrlib.errors import (
85
 
    FileExists,
86
86
    NoSuchFile,
87
 
    KnitError,
88
87
    InvalidRevisionId,
89
88
    KnitCorrupt,
90
89
    KnitHeaderError,
91
90
    RevisionNotPresent,
92
 
    RevisionAlreadyPresent,
93
91
    SHA1KnitCorrupt,
94
92
    )
95
93
from bzrlib.osutils import (
96
94
    contains_whitespace,
97
 
    contains_linebreaks,
98
95
    sha_string,
99
96
    sha_strings,
100
97
    split_lines,
101
98
    )
102
99
from bzrlib.versionedfile import (
 
100
    _KeyRefs,
103
101
    AbsentContentFactory,
104
102
    adapter_registry,
105
103
    ConstantMapper,
106
104
    ContentFactory,
107
 
    ChunkedContentFactory,
108
105
    sort_groupcompress,
109
 
    VersionedFile,
110
 
    VersionedFiles,
 
106
    VersionedFilesWithFallbacks,
111
107
    )
112
108
 
113
109
 
805
801
        writer.begin()
806
802
        index = _KnitGraphIndex(graph_index, lambda:True, parents=parents,
807
803
            deltas=delta, add_callback=graph_index.add_nodes)
808
 
        access = _DirectPackAccess({})
 
804
        access = pack_repo._DirectPackAccess({})
809
805
        access.set_writer(writer, graph_index, (transport, 'newpack'))
810
806
        result = KnitVersionedFiles(index, access,
811
807
            max_delta_chain=max_delta_chain)
849
845
                in all_build_index_memos.itervalues()])
850
846
 
851
847
 
852
 
class KnitVersionedFiles(VersionedFiles):
 
848
class KnitVersionedFiles(VersionedFilesWithFallbacks):
853
849
    """Storage for many versioned files using knit compression.
854
850
 
855
851
    Backend storage is managed by indices and data objects.
882
878
            self._factory = KnitAnnotateFactory()
883
879
        else:
884
880
            self._factory = KnitPlainFactory()
885
 
        self._fallback_vfs = []
 
881
        self._immediate_fallback_vfs = []
886
882
        self._reload_func = reload_func
887
883
 
888
884
    def __repr__(self):
891
887
            self._index,
892
888
            self._access)
893
889
 
 
890
    def without_fallbacks(self):
 
891
        """Return a clone of this object without any fallbacks configured."""
 
892
        return KnitVersionedFiles(self._index, self._access,
 
893
            self._max_delta_chain, self._factory.annotated,
 
894
            self._reload_func)
 
895
 
894
896
    def add_fallback_versioned_files(self, a_versioned_files):
895
897
        """Add a source of texts for texts not present in this knit.
896
898
 
897
899
        :param a_versioned_files: A VersionedFiles object.
898
900
        """
899
 
        self._fallback_vfs.append(a_versioned_files)
 
901
        self._immediate_fallback_vfs.append(a_versioned_files)
900
902
 
901
903
    def add_lines(self, key, parents, lines, parent_texts=None,
902
904
        left_matching_blocks=None, nostore_sha=None, random_id=False,
1069
1071
                    raise errors.KnitCorrupt(self,
1070
1072
                        "Missing basis parent %s for %s" % (
1071
1073
                        compression_parent, key))
1072
 
        for fallback_vfs in self._fallback_vfs:
 
1074
        for fallback_vfs in self._immediate_fallback_vfs:
1073
1075
            fallback_vfs.check()
1074
1076
 
1075
1077
    def _check_add(self, key, lines, random_id, check_content):
1192
1194
        generator = _VFContentMapGenerator(self, [key])
1193
1195
        return generator._get_content(key)
1194
1196
 
1195
 
    def get_known_graph_ancestry(self, keys):
1196
 
        """Get a KnownGraph instance with the ancestry of keys."""
1197
 
        parent_map, missing_keys = self._index.find_ancestry(keys)
1198
 
        for fallback in self._fallback_vfs:
1199
 
            if not missing_keys:
1200
 
                break
1201
 
            (f_parent_map, f_missing_keys) = fallback._index.find_ancestry(
1202
 
                                                missing_keys)
1203
 
            parent_map.update(f_parent_map)
1204
 
            missing_keys = f_missing_keys
1205
 
        kg = _mod_graph.KnownGraph(parent_map)
1206
 
        return kg
1207
 
 
1208
1197
    def get_parent_map(self, keys):
1209
1198
        """Get a map of the graph parents of keys.
1210
1199
 
1225
1214
            and so on.
1226
1215
        """
1227
1216
        result = {}
1228
 
        sources = [self._index] + self._fallback_vfs
 
1217
        sources = [self._index] + self._immediate_fallback_vfs
1229
1218
        source_results = []
1230
1219
        missing = set(keys)
1231
1220
        for source in sources:
1525
1514
                        yield KnitContentFactory(key, global_map[key],
1526
1515
                            record_details, None, raw_data, self._factory.annotated, None)
1527
1516
                else:
1528
 
                    vf = self._fallback_vfs[parent_maps.index(source) - 1]
 
1517
                    vf = self._immediate_fallback_vfs[parent_maps.index(source) - 1]
1529
1518
                    for record in vf.get_record_stream(keys, ordering,
1530
1519
                        include_delta_closure):
1531
1520
                        yield record
1541
1530
            # record entry 2 is the 'digest'.
1542
1531
            result[key] = details[2]
1543
1532
        missing.difference_update(set(result))
1544
 
        for source in self._fallback_vfs:
 
1533
        for source in self._immediate_fallback_vfs:
1545
1534
            if not missing:
1546
1535
                break
1547
1536
            new_result = source.get_sha1s(missing)
1618
1607
                raise RevisionNotPresent([record.key], self)
1619
1608
            elif ((record.storage_kind in knit_types)
1620
1609
                  and (compression_parent is None
1621
 
                       or not self._fallback_vfs
 
1610
                       or not self._immediate_fallback_vfs
1622
1611
                       or self._index.has_key(compression_parent)
1623
1612
                       or not self.has_key(compression_parent))):
1624
1613
                # we can insert the knit record literally if either it has no
1796
1785
        # vfs, and hope to find them there.  Note that if the keys are found
1797
1786
        # but had no changes or no content, the fallback may not return
1798
1787
        # anything.
1799
 
        if keys and not self._fallback_vfs:
 
1788
        if keys and not self._immediate_fallback_vfs:
1800
1789
            # XXX: strictly the second parameter is meant to be the file id
1801
1790
            # but it's not easily accessible here.
1802
1791
            raise RevisionNotPresent(keys, repr(self))
1803
 
        for source in self._fallback_vfs:
 
1792
        for source in self._immediate_fallback_vfs:
1804
1793
            if not keys:
1805
1794
                break
1806
1795
            source_keys = set()
1879
1868
        :return: the header and the decompressor stream.
1880
1869
                 as (stream, header_record)
1881
1870
        """
1882
 
        df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(raw_data))
 
1871
        df = gzip.GzipFile(mode='rb', fileobj=StringIO(raw_data))
1883
1872
        try:
1884
1873
            # Current serialise
1885
1874
            rec = self._check_header(key, df.readline())
1894
1883
        # 4168 calls in 2880 217 internal
1895
1884
        # 4168 calls to _parse_record_header in 2121
1896
1885
        # 4168 calls to readlines in 330
1897
 
        df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(data))
 
1886
        df = gzip.GzipFile(mode='rb', fileobj=StringIO(data))
1898
1887
        try:
1899
1888
            record_contents = df.readlines()
1900
1889
        except Exception, e:
2015
2004
        """See VersionedFiles.keys."""
2016
2005
        if 'evil' in debug.debug_flags:
2017
2006
            trace.mutter_callsite(2, "keys scales with size of history")
2018
 
        sources = [self._index] + self._fallback_vfs
 
2007
        sources = [self._index] + self._immediate_fallback_vfs
2019
2008
        result = set()
2020
2009
        for source in sources:
2021
2010
            result.update(source.keys())
2061
2050
 
2062
2051
        missing_keys = set(nonlocal_keys)
2063
2052
        # Read from remote versioned file instances and provide to our caller.
2064
 
        for source in self.vf._fallback_vfs:
 
2053
        for source in self.vf._immediate_fallback_vfs:
2065
2054
            if not missing_keys:
2066
2055
                break
2067
2056
            # Loop over fallback repositories asking them for texts - ignore
2786
2775
        return key[:-1], key[-1]
2787
2776
 
2788
2777
 
2789
 
class _KeyRefs(object):
2790
 
 
2791
 
    def __init__(self, track_new_keys=False):
2792
 
        # dict mapping 'key' to 'set of keys referring to that key'
2793
 
        self.refs = {}
2794
 
        if track_new_keys:
2795
 
            # set remembering all new keys
2796
 
            self.new_keys = set()
2797
 
        else:
2798
 
            self.new_keys = None
2799
 
 
2800
 
    def clear(self):
2801
 
        if self.refs:
2802
 
            self.refs.clear()
2803
 
        if self.new_keys:
2804
 
            self.new_keys.clear()
2805
 
 
2806
 
    def add_references(self, key, refs):
2807
 
        # Record the new references
2808
 
        for referenced in refs:
2809
 
            try:
2810
 
                needed_by = self.refs[referenced]
2811
 
            except KeyError:
2812
 
                needed_by = self.refs[referenced] = set()
2813
 
            needed_by.add(key)
2814
 
        # Discard references satisfied by the new key
2815
 
        self.add_key(key)
2816
 
 
2817
 
    def get_new_keys(self):
2818
 
        return self.new_keys
2819
 
    
2820
 
    def get_unsatisfied_refs(self):
2821
 
        return self.refs.iterkeys()
2822
 
 
2823
 
    def _satisfy_refs_for_key(self, key):
2824
 
        try:
2825
 
            del self.refs[key]
2826
 
        except KeyError:
2827
 
            # No keys depended on this key.  That's ok.
2828
 
            pass
2829
 
 
2830
 
    def add_key(self, key):
2831
 
        # satisfy refs for key, and remember that we've seen this key.
2832
 
        self._satisfy_refs_for_key(key)
2833
 
        if self.new_keys is not None:
2834
 
            self.new_keys.add(key)
2835
 
 
2836
 
    def satisfy_refs_for_keys(self, keys):
2837
 
        for key in keys:
2838
 
            self._satisfy_refs_for_key(key)
2839
 
 
2840
 
    def get_referrers(self):
2841
 
        result = set()
2842
 
        for referrers in self.refs.itervalues():
2843
 
            result.update(referrers)
2844
 
        return result
2845
 
 
2846
 
 
2847
2778
class _KnitGraphIndex(object):
2848
2779
    """A KnitVersionedFiles index layered on GraphIndex."""
2849
2780
 
3417
3348
            raise exc_class, exc_value, exc_traceback
3418
3349
 
3419
3350
 
3420
 
# Deprecated, use PatienceSequenceMatcher instead
3421
 
KnitSequenceMatcher = patiencediff.PatienceSequenceMatcher
3422
 
 
3423
 
 
3424
3351
def annotate_knit(knit, revision_id):
3425
3352
    """Annotate a knit with no cached annotations.
3426
3353
 
3524
3451
        return records, ann_keys
3525
3452
 
3526
3453
    def _get_needed_texts(self, key, pb=None):
3527
 
        # if True or len(self._vf._fallback_vfs) > 0:
3528
 
        if len(self._vf._fallback_vfs) > 0:
 
3454
        # if True or len(self._vf._immediate_fallback_vfs) > 0:
 
3455
        if len(self._vf._immediate_fallback_vfs) > 0:
3529
3456
            # If we have fallbacks, go to the generic path
3530
3457
            for v in annotate.Annotator._get_needed_texts(self, key, pb=pb):
3531
3458
                yield v