/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/groupcompress.py

  • Committer: John Arbash Meinel
  • Date: 2009-07-08 14:37:25 UTC
  • mfrom: (4516 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4517.
  • Revision ID: john@arbash-meinel.com-20090708143725-sc9sjy3mz4cxwxzz
Merge bzr.dev 4516

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Core compression logic for compressing streams of related files."""
18
18
 
19
 
from itertools import izip
20
 
from cStringIO import StringIO
21
19
import time
22
20
import zlib
23
21
try:
28
26
from bzrlib import (
29
27
    annotate,
30
28
    debug,
31
 
    diff,
32
29
    errors,
33
30
    graph as _mod_graph,
 
31
    knit,
34
32
    osutils,
35
33
    pack,
36
 
    patiencediff,
37
34
    trace,
38
35
    )
39
36
from bzrlib.graph import Graph
40
 
from bzrlib.knit import _DirectPackAccess
41
37
from bzrlib.btree_index import BTreeBuilder
42
38
from bzrlib.lru_cache import LRUSizeCache
43
39
from bzrlib.tsort import topo_sort
108
104
        self._z_content_length = None
109
105
        self._content_length = None
110
106
        self._content = None
 
107
        self._content_chunks = None
111
108
 
112
109
    def __len__(self):
113
110
        # This is the maximum number of bytes this object will reference if
137
134
                % (num_bytes, self._content_length))
138
135
        # Expand the content if required
139
136
        if self._content is None:
 
137
            if self._content_chunks is not None:
 
138
                self._content = ''.join(self._content_chunks)
 
139
                self._content_chunks = None
 
140
        if self._content is None:
140
141
            if self._z_content is None:
141
142
                raise AssertionError('No content to decompress')
142
143
            if self._z_content == '':
273
274
            bytes = apply_delta_to_source(self._content, content_start, end)
274
275
        return bytes
275
276
 
 
277
    def set_chunked_content(self, content_chunks, length):
 
278
        """Set the content of this block to the given chunks."""
 
279
        # If we have lots of short lines, it is may be more efficient to join
 
280
        # the content ahead of time. If the content is <10MiB, we don't really
 
281
        # care about the extra memory consumption, so we can just pack it and
 
282
        # be done. However, timing showed 18s => 17.9s for repacking 1k revs of
 
283
        # mysql, which is below the noise margin
 
284
        self._content_length = length
 
285
        self._content_chunks = content_chunks
 
286
        self._content = None
 
287
        self._z_content = None
 
288
 
276
289
    def set_content(self, content):
277
290
        """Set the content of this block."""
278
291
        self._content_length = len(content)
279
292
        self._content = content
280
293
        self._z_content = None
281
294
 
 
295
    def _create_z_content_using_lzma(self):
 
296
        if self._content_chunks is not None:
 
297
            self._content = ''.join(self._content_chunks)
 
298
            self._content_chunks = None
 
299
        if self._content is None:
 
300
            raise AssertionError('Nothing to compress')
 
301
        self._z_content = pylzma.compress(self._content)
 
302
        self._z_content_length = len(self._z_content)
 
303
 
 
304
    def _create_z_content_from_chunks(self):
 
305
        compressor = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION)
 
306
        compressed_chunks = map(compressor.compress, self._content_chunks)
 
307
        compressed_chunks.append(compressor.flush())
 
308
        self._z_content = ''.join(compressed_chunks)
 
309
        self._z_content_length = len(self._z_content)
 
310
 
 
311
    def _create_z_content(self):
 
312
        if self._z_content is not None:
 
313
            return
 
314
        if _USE_LZMA:
 
315
            self._create_z_content_using_lzma()
 
316
            return
 
317
        if self._content_chunks is not None:
 
318
            self._create_z_content_from_chunks()
 
319
            return
 
320
        self._z_content = zlib.compress(self._content)
 
321
        self._z_content_length = len(self._z_content)
 
322
 
282
323
    def to_bytes(self):
283
324
        """Encode the information into a byte stream."""
284
 
        compress = zlib.compress
285
 
        if _USE_LZMA:
286
 
            compress = pylzma.compress
287
 
        if self._z_content is None:
288
 
            if self._content is None:
289
 
                raise AssertionError('Nothing to compress')
290
 
            self._z_content = compress(self._content)
291
 
            self._z_content_length = len(self._z_content)
 
325
        self._create_z_content()
292
326
        if _USE_LZMA:
293
327
            header = self.GCB_LZ_HEADER
294
328
        else:
324
358
                raise ValueError('invalid content_len %d for record @ pos %d'
325
359
                                 % (content_len, pos - len_len - 1))
326
360
            if kind == 'f': # Fulltext
327
 
                result.append(('f', content_len))
 
361
                if include_text:
 
362
                    text = self._content[pos:pos+content_len]
 
363
                    result.append(('f', content_len, text))
 
364
                else:
 
365
                    result.append(('f', content_len))
328
366
            elif kind == 'd': # Delta
329
367
                delta_content = self._content[pos:pos+content_len]
330
368
                delta_info = []
339
377
                        (offset, length,
340
378
                         delta_pos) = decode_copy_instruction(delta_content, c,
341
379
                                                              delta_pos)
342
 
                        delta_info.append(('c', offset, length))
 
380
                        if include_text:
 
381
                            text = self._content[offset:offset+length]
 
382
                            delta_info.append(('c', offset, length, text))
 
383
                        else:
 
384
                            delta_info.append(('c', offset, length))
343
385
                        measured_len += length
344
386
                    else: # Insert
345
387
                        if include_text:
746
788
 
747
789
        After calling this, the compressor should no longer be used
748
790
        """
749
 
        content = ''.join(self.chunks)
 
791
        # TODO: this causes us to 'bloat' to 2x the size of content in the
 
792
        #       group. This has an impact for 'commit' of large objects.
 
793
        #       One possibility is to use self._content_chunks, and be lazy and
 
794
        #       only fill out self._content as a full string when we actually
 
795
        #       need it. That would at least drop the peak memory consumption
 
796
        #       for 'commit' down to ~1x the size of the largest file, at a
 
797
        #       cost of increased complexity within this code. 2x is still <<
 
798
        #       3x the size of the largest file, so we are doing ok.
 
799
        self._block.set_chunked_content(self.chunks, self.endpoint)
750
800
        self.chunks = None
751
801
        self._delta_index = None
752
 
        self._block.set_content(content)
753
802
        return self._block
754
803
 
755
804
    def pop_last(self):
889
938
        self.endpoint = endpoint
890
939
 
891
940
 
892
 
def make_pack_factory(graph, delta, keylength):
 
941
def make_pack_factory(graph, delta, keylength, inconsistency_fatal=True):
893
942
    """Create a factory for creating a pack based groupcompress.
894
943
 
895
944
    This is only functional enough to run interface tests, it doesn't try to
910
959
        writer = pack.ContainerWriter(stream.write)
911
960
        writer.begin()
912
961
        index = _GCGraphIndex(graph_index, lambda:True, parents=parents,
913
 
            add_callback=graph_index.add_nodes)
914
 
        access = _DirectPackAccess({})
 
962
            add_callback=graph_index.add_nodes,
 
963
            inconsistency_fatal=inconsistency_fatal)
 
964
        access = knit._DirectPackAccess({})
915
965
        access.set_writer(writer, graph_index, (transport, 'newpack'))
916
966
        result = GroupCompressVersionedFiles(index, access, delta)
917
967
        result.stream = stream
992
1042
                                               nostore_sha=nostore_sha))[0]
993
1043
        return sha1, length, None
994
1044
 
 
1045
    def _add_text(self, key, parents, text, nostore_sha=None, random_id=False):
 
1046
        """See VersionedFiles._add_text()."""
 
1047
        self._index._check_write_ok()
 
1048
        self._check_add(key, None, random_id, check_content=False)
 
1049
        if text.__class__ is not str:
 
1050
            raise errors.BzrBadParameterUnicode("text")
 
1051
        if parents is None:
 
1052
            # The caller might pass None if there is no graph data, but kndx
 
1053
            # indexes can't directly store that, so we give them
 
1054
            # an empty tuple instead.
 
1055
            parents = ()
 
1056
        # double handling for now. Make it work until then.
 
1057
        length = len(text)
 
1058
        record = FulltextContentFactory(key, parents, None, text)
 
1059
        sha1 = list(self._insert_record_stream([record], random_id=random_id,
 
1060
                                               nostore_sha=nostore_sha))[0]
 
1061
        return sha1, length, None
 
1062
 
995
1063
    def add_fallback_versioned_files(self, a_versioned_files):
996
1064
        """Add a source of texts for texts not present in this knit.
997
1065
 
1006
1074
        if not parent_map:
1007
1075
            raise errors.RevisionNotPresent(key, self)
1008
1076
        if parent_map[key] is not None:
1009
 
            search = graph._make_breadth_first_searcher([key])
1010
 
            keys = set()
1011
 
            while True:
1012
 
                try:
1013
 
                    present, ghosts = search.next_with_ghosts()
1014
 
                except StopIteration:
1015
 
                    break
1016
 
                keys.update(present)
1017
 
            parent_map = self.get_parent_map(keys)
 
1077
            parent_map = dict((k, v) for k, v in graph.iter_ancestry([key])
 
1078
                              if v is not None)
 
1079
            keys = parent_map.keys()
1018
1080
        else:
1019
1081
            keys = [key]
1020
1082
            parent_map = {key:()}
1021
 
        # So we used Graph(self) to load the parent_map, but now that we have
1022
 
        # it, we can just query the parent map directly, so create a new Graph
1023
 
        # object
1024
 
        graph = _mod_graph.Graph(_mod_graph.DictParentsProvider(parent_map))
1025
 
        head_cache = _mod_graph.FrozenHeadsCache(graph)
 
1083
        # We used Graph(self) to load the parent_map, but now that we have it,
 
1084
        # we can just query the parent map directly, so create a KnownGraph
 
1085
        heads_provider = _mod_graph.KnownGraph(parent_map)
1026
1086
        parent_cache = {}
1027
1087
        reannotate = annotate.reannotate
1028
1088
        for record in self.get_record_stream(keys, 'topological', True):
1030
1090
            lines = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
1031
1091
            parent_lines = [parent_cache[parent] for parent in parent_map[key]]
1032
1092
            parent_cache[key] = list(
1033
 
                reannotate(parent_lines, lines, key, None, head_cache))
 
1093
                reannotate(parent_lines, lines, key, None, heads_provider))
1034
1094
        return parent_cache[key]
1035
1095
 
1036
1096
    def check(self, progress_bar=None):
1513
1573
 
1514
1574
        :return: An iterator over (line, key).
1515
1575
        """
1516
 
        if pb is None:
1517
 
            pb = progress.DummyProgress()
1518
1576
        keys = set(keys)
1519
1577
        total = len(keys)
1520
1578
        # we don't care about inclusions, the caller cares.
1524
1582
            'unordered', True)):
1525
1583
            # XXX: todo - optimise to use less than full texts.
1526
1584
            key = record.key
1527
 
            pb.update('Walking content', key_idx, total)
 
1585
            if pb is not None:
 
1586
                pb.update('Walking content', key_idx, total)
1528
1587
            if record.storage_kind == 'absent':
1529
1588
                raise errors.RevisionNotPresent(key, self)
1530
1589
            lines = osutils.split_lines(record.get_bytes_as('fulltext'))
1531
1590
            for line in lines:
1532
1591
                yield line, key
1533
 
        pb.update('Walking content', total, total)
 
1592
        if pb is not None:
 
1593
            pb.update('Walking content', total, total)
1534
1594
 
1535
1595
    def keys(self):
1536
1596
        """See VersionedFiles.keys."""
1547
1607
    """Mapper from GroupCompressVersionedFiles needs into GraphIndex storage."""
1548
1608
 
1549
1609
    def __init__(self, graph_index, is_locked, parents=True,
1550
 
        add_callback=None):
 
1610
        add_callback=None, track_external_parent_refs=False,
 
1611
        inconsistency_fatal=True):
1551
1612
        """Construct a _GCGraphIndex on a graph_index.
1552
1613
 
1553
1614
        :param graph_index: An implementation of bzrlib.index.GraphIndex.
1558
1619
        :param add_callback: If not None, allow additions to the index and call
1559
1620
            this callback with a list of added GraphIndex nodes:
1560
1621
            [(node, value, node_refs), ...]
 
1622
        :param track_external_parent_refs: As keys are added, keep track of the
 
1623
            keys they reference, so that we can query get_missing_parents(),
 
1624
            etc.
 
1625
        :param inconsistency_fatal: When asked to add records that are already
 
1626
            present, and the details are inconsistent with the existing
 
1627
            record, raise an exception instead of warning (and skipping the
 
1628
            record).
1561
1629
        """
1562
1630
        self._add_callback = add_callback
1563
1631
        self._graph_index = graph_index
1564
1632
        self._parents = parents
1565
1633
        self.has_graph = parents
1566
1634
        self._is_locked = is_locked
 
1635
        self._inconsistency_fatal = inconsistency_fatal
 
1636
        if track_external_parent_refs:
 
1637
            self._key_dependencies = knit._KeyRefs()
 
1638
        else:
 
1639
            self._key_dependencies = None
1567
1640
 
1568
1641
    def add_records(self, records, random_id=False):
1569
1642
        """Add multiple records to the index.
1590
1663
                if refs:
1591
1664
                    for ref in refs:
1592
1665
                        if ref:
1593
 
                            raise KnitCorrupt(self,
 
1666
                            raise errors.KnitCorrupt(self,
1594
1667
                                "attempt to add node with parents "
1595
1668
                                "in parentless index.")
1596
1669
                    refs = ()
1601
1674
            present_nodes = self._get_entries(keys)
1602
1675
            for (index, key, value, node_refs) in present_nodes:
1603
1676
                if node_refs != keys[key][1]:
1604
 
                    raise errors.KnitCorrupt(self, "inconsistent details in add_records"
1605
 
                        ": %s %s" % ((value, node_refs), keys[key]))
 
1677
                    details = '%s %s %s' % (key, (value, node_refs), keys[key])
 
1678
                    if self._inconsistency_fatal:
 
1679
                        raise errors.KnitCorrupt(self, "inconsistent details"
 
1680
                                                 " in add_records: %s" %
 
1681
                                                 details)
 
1682
                    else:
 
1683
                        trace.warning("inconsistent details in skipped"
 
1684
                                      " record: %s", details)
1606
1685
                del keys[key]
1607
1686
                changed = True
1608
1687
        if changed:
1614
1693
                for key, (value, node_refs) in keys.iteritems():
1615
1694
                    result.append((key, value))
1616
1695
            records = result
 
1696
        key_dependencies = self._key_dependencies
 
1697
        if key_dependencies is not None and self._parents:
 
1698
            for key, value, refs in records:
 
1699
                parents = refs[0]
 
1700
                key_dependencies.add_references(key, parents)
1617
1701
        self._add_callback(records)
1618
1702
 
1619
1703
    def _check_read(self):
1648
1732
        if check_present:
1649
1733
            missing_keys = keys.difference(found_keys)
1650
1734
            if missing_keys:
1651
 
                raise RevisionNotPresent(missing_keys.pop(), self)
 
1735
                raise errors.RevisionNotPresent(missing_keys.pop(), self)
1652
1736
 
1653
1737
    def get_parent_map(self, keys):
1654
1738
        """Get a map of the parents of keys.
1668
1752
                result[node[1]] = None
1669
1753
        return result
1670
1754
 
 
1755
    def get_missing_parents(self):
 
1756
        """Return the keys of missing parents."""
 
1757
        # Copied from _KnitGraphIndex.get_missing_parents
 
1758
        # We may have false positives, so filter those out.
 
1759
        self._key_dependencies.add_keys(
 
1760
            self.get_parent_map(self._key_dependencies.get_unsatisfied_refs()))
 
1761
        return frozenset(self._key_dependencies.get_unsatisfied_refs())
 
1762
 
1671
1763
    def get_build_details(self, keys):
1672
1764
        """Get the various build details for keys.
1673
1765
 
1719
1811
        delta_end = int(bits[3])
1720
1812
        return node[0], start, stop, basis_end, delta_end
1721
1813
 
 
1814
    def scan_unvalidated_index(self, graph_index):
 
1815
        """Inform this _GCGraphIndex that there is an unvalidated index.
 
1816
 
 
1817
        This allows this _GCGraphIndex to keep track of any missing
 
1818
        compression parents we may want to have filled in to make those
 
1819
        indices valid.
 
1820
 
 
1821
        :param graph_index: A GraphIndex
 
1822
        """
 
1823
        if self._key_dependencies is not None:
 
1824
            # Add parent refs from graph_index (and discard parent refs that
 
1825
            # the graph_index has).
 
1826
            add_refs = self._key_dependencies.add_references
 
1827
            for node in graph_index.iter_all_entries():
 
1828
                add_refs(node[1], node[3][0])
 
1829
 
 
1830
 
1722
1831
 
1723
1832
from bzrlib._groupcompress_py import (
1724
1833
    apply_delta,