~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/versionedfile.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-12-11 20:23:00 UTC
  • mfrom: (3890.2.18 get_record_stream_chunked)
  • Revision ID: pqm@pqm.ubuntu.com-20081211202300-6dz1vo3phfsc23pj
(jam) Add ContentFactory.get_bytes_as('chunked') and
        osutils.chunks_to_lines()

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
    'bzrlib.knit', 'FTAnnotatedToUnannotated')
60
60
adapter_registry.register_lazy(('knit-annotated-ft-gz', 'fulltext'),
61
61
    'bzrlib.knit', 'FTAnnotatedToFullText')
 
62
# adapter_registry.register_lazy(('knit-annotated-ft-gz', 'chunked'),
 
63
#     'bzrlib.knit', 'FTAnnotatedToChunked')
62
64
 
63
65
 
64
66
class ContentFactory(object):
84
86
        self.parents = None
85
87
 
86
88
 
 
89
class ChunkedContentFactory(ContentFactory):
 
90
    """Static data content factory.
 
91
 
 
92
    This takes a 'chunked' list of strings. The only requirement on 'chunked' is
 
93
    that ''.join(lines) becomes a valid fulltext. A tuple of a single string
 
94
    satisfies this, as does a list of lines.
 
95
 
 
96
    :ivar sha1: None, or the sha1 of the content fulltext.
 
97
    :ivar storage_kind: The native storage kind of this factory. Always
 
98
        'chunked'
 
99
    :ivar key: The key of this content. Each key is a tuple with a single
 
100
        string in it.
 
101
    :ivar parents: A tuple of parent keys for self.key. If the object has
 
102
        no parent information, None (as opposed to () for an empty list of
 
103
        parents).
 
104
     """
 
105
 
 
106
    def __init__(self, key, parents, sha1, chunks):
 
107
        """Create a ContentFactory."""
 
108
        self.sha1 = sha1
 
109
        self.storage_kind = 'chunked'
 
110
        self.key = key
 
111
        self.parents = parents
 
112
        self._chunks = chunks
 
113
 
 
114
    def get_bytes_as(self, storage_kind):
 
115
        if storage_kind == 'chunked':
 
116
            return self._chunks
 
117
        elif storage_kind == 'fulltext':
 
118
            return ''.join(self._chunks)
 
119
        raise errors.UnavailableRepresentation(self.key, storage_kind,
 
120
            self.storage_kind)
 
121
 
 
122
 
87
123
class FulltextContentFactory(ContentFactory):
88
124
    """Static data content factory.
89
125
 
90
126
    This takes a fulltext when created and just returns that during
91
127
    get_bytes_as('fulltext').
92
 
    
 
128
 
93
129
    :ivar sha1: None, or the sha1 of the content fulltext.
94
130
    :ivar storage_kind: The native storage kind of this factory. Always
95
131
        'fulltext'.
111
147
    def get_bytes_as(self, storage_kind):
112
148
        if storage_kind == self.storage_kind:
113
149
            return self._text
 
150
        elif storage_kind == 'chunked':
 
151
            return (self._text,)
114
152
        raise errors.UnavailableRepresentation(self.key, storage_kind,
115
153
            self.storage_kind)
116
154
 
804
842
                                  if not mpvf.has_version(p))
805
843
        # It seems likely that adding all the present parents as fulltexts can
806
844
        # easily exhaust memory.
807
 
        split_lines = osutils.split_lines
 
845
        chunks_to_lines = osutils.chunks_to_lines
808
846
        for record in self.get_record_stream(needed_parents, 'unordered',
809
847
            True):
810
848
            if record.storage_kind == 'absent':
811
849
                continue
812
 
            mpvf.add_version(split_lines(record.get_bytes_as('fulltext')),
 
850
            mpvf.add_version(chunks_to_lines(record.get_bytes_as('chunked')),
813
851
                record.key, [])
814
852
        for (key, parent_keys, expected_sha1, mpdiff), lines in\
815
853
            zip(records, mpvf.get_line_list(versions)):
940
978
        ghosts = maybe_ghosts - set(self.get_parent_map(maybe_ghosts))
941
979
        knit_keys.difference_update(ghosts)
942
980
        lines = {}
943
 
        split_lines = osutils.split_lines
 
981
        chunks_to_lines = osutils.chunks_to_lines
944
982
        for record in self.get_record_stream(knit_keys, 'topological', True):
945
 
            lines[record.key] = split_lines(record.get_bytes_as('fulltext'))
 
983
            lines[record.key] = chunks_to_lines(record.get_bytes_as('chunked'))
946
984
            # line_block_dict = {}
947
985
            # for parent, blocks in record.extract_line_blocks():
948
986
            #   line_blocks[parent] = blocks
1251
1289
                lines = self._lines[key]
1252
1290
                parents = self._parents[key]
1253
1291
                pending.remove(key)
1254
 
                yield FulltextContentFactory(key, parents, None,
1255
 
                    ''.join(lines))
 
1292
                yield ChunkedContentFactory(key, parents, None, lines)
1256
1293
        for versionedfile in self.fallback_versionedfiles:
1257
1294
            for record in versionedfile.get_record_stream(
1258
1295
                pending, 'unordered', True):
1422
1459
            if lines is not None:
1423
1460
                if not isinstance(lines, list):
1424
1461
                    raise AssertionError
1425
 
                yield FulltextContentFactory((k,), None, 
 
1462
                yield ChunkedContentFactory((k,), None,
1426
1463
                        sha1=osutils.sha_strings(lines),
1427
 
                        text=''.join(lines))
 
1464
                        chunks=lines)
1428
1465
            else:
1429
1466
                yield AbsentContentFactory((k,))
1430
1467