/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 breezy/chk_serializer.py

  • Committer: Jelmer Vernooij
  • Author(s): Richard Wilbur
  • Date: 2017-05-30 23:37:11 UTC
  • mto: This revision was merged to the branch mainline in revision 6645.
  • Revision ID: jelmer@jelmer.uk-20170530233711-r0m0qp8hpkqzpopw
Fix order in which files are processed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Serializer object for CHK based inventory storage."""
18
18
 
19
 
from bzrlib import (
 
19
from __future__ import absolute_import
 
20
 
 
21
from . import lazy_import
 
22
lazy_import.lazy_import(globals(),
 
23
"""
 
24
from breezy import (
 
25
    xml_serializer,
 
26
    )
 
27
""")
 
28
from . import (
20
29
    bencode,
21
30
    cache_utf8,
22
 
    inventory,
 
31
    errors,
23
32
    revision as _mod_revision,
24
 
    xml6,
25
 
    xml7,
26
 
    )
 
33
    serializer,
 
34
    )
 
35
from .sixish import (
 
36
    BytesIO,
 
37
    )
 
38
 
27
39
 
28
40
 
29
41
def _validate_properties(props, _decode=cache_utf8._utf8_decode):
99
111
        #       However, to decode all 25k revisions of bzr takes approx 1.3s
100
112
        #       If we remove all extra validation that goes down to about 1.2s.
101
113
        #       Of that time, probably 0.6s is spend in bencode.bdecode().
102
 
        #       Regardless 'time bzr log' of everything is 7+s, so 1.3s to
 
114
        #       Regardless 'time brz log' of everything is 7+s, so 1.3s to
103
115
        #       extract revision texts isn't a majority of time.
104
116
        ret = bencode.bdecode(text)
105
117
        if not isinstance(ret, list):
131
143
        return self.read_revision_from_string(f.read())
132
144
 
133
145
 
134
 
class CHKSerializerSubtree(BEncodeRevisionSerializer1, xml7.Serializer_v7):
135
 
    """A CHKInventory based serializer that supports tree references"""
 
146
class CHKSerializer(serializer.Serializer):
 
147
    """A CHKInventory based serializer with 'plain' behaviour."""
136
148
 
137
 
    supported_kinds = set(['file', 'directory', 'symlink', 'tree-reference'])
138
149
    format_num = '9'
139
150
    revision_format_num = None
140
151
    support_altered_by_hack = False
141
 
 
142
 
    def _unpack_entry(self, elt, entry_cache=None, return_from_cache=False):
143
 
        kind = elt.tag
144
 
        if not kind in self.supported_kinds:
145
 
            raise AssertionError('unsupported entry kind %s' % kind)
146
 
        if kind == 'tree-reference':
147
 
            file_id = elt.attrib['file_id']
148
 
            name = elt.attrib['name']
149
 
            parent_id = elt.attrib['parent_id']
150
 
            revision = elt.get('revision')
151
 
            reference_revision = elt.get('reference_revision')
152
 
            return inventory.TreeReference(file_id, name, parent_id, revision,
153
 
                                           reference_revision)
 
152
    supported_kinds = {'file', 'directory', 'symlink', 'tree-reference'}
 
153
 
 
154
    def __init__(self, node_size, search_key_name):
 
155
        self.maximum_size = node_size
 
156
        self.search_key_name = search_key_name
 
157
 
 
158
    def _unpack_inventory(self, elt, revision_id=None, entry_cache=None,
 
159
                          return_from_cache=False):
 
160
        """Construct from XML Element"""
 
161
        inv = xml_serializer.unpack_inventory_flat(elt, self.format_num,
 
162
            xml_serializer.unpack_inventory_entry, entry_cache,
 
163
            return_from_cache)
 
164
        return inv
 
165
 
 
166
    def read_inventory_from_string(self, xml_string, revision_id=None,
 
167
                                   entry_cache=None, return_from_cache=False):
 
168
        """Read xml_string into an inventory object.
 
169
 
 
170
        :param xml_string: The xml to read.
 
171
        :param revision_id: If not-None, the expected revision id of the
 
172
            inventory.
 
173
        :param entry_cache: An optional cache of InventoryEntry objects. If
 
174
            supplied we will look up entries via (file_id, revision_id) which
 
175
            should map to a valid InventoryEntry (File/Directory/etc) object.
 
176
        :param return_from_cache: Return entries directly from the cache,
 
177
            rather than copying them first. This is only safe if the caller
 
178
            promises not to mutate the returned inventory entries, but it can
 
179
            make some operations significantly faster.
 
180
        """
 
181
        try:
 
182
            return self._unpack_inventory(
 
183
                xml_serializer.fromstring(xml_string), revision_id,
 
184
                entry_cache=entry_cache,
 
185
                return_from_cache=return_from_cache)
 
186
        except xml_serializer.ParseError as e:
 
187
            raise errors.UnexpectedInventoryFormat(e)
 
188
 
 
189
    def read_inventory(self, f, revision_id=None):
 
190
        """Read an inventory from a file-like object."""
 
191
        try:
 
192
            try:
 
193
                return self._unpack_inventory(self._read_element(f),
 
194
                    revision_id=None)
 
195
            finally:
 
196
                f.close()
 
197
        except xml_serializer.ParseError as e:
 
198
            raise errors.UnexpectedInventoryFormat(e)
 
199
 
 
200
    def write_inventory_to_lines(self, inv):
 
201
        """Return a list of lines with the encoded inventory."""
 
202
        return self.write_inventory(inv, None)
 
203
 
 
204
    def write_inventory_to_string(self, inv, working=False):
 
205
        """Just call write_inventory with a BytesIO and return the value.
 
206
 
 
207
        :param working: If True skip history data - text_sha1, text_size,
 
208
            reference_revision, symlink_target.
 
209
        """
 
210
        sio = BytesIO()
 
211
        self.write_inventory(inv, sio, working)
 
212
        return sio.getvalue()
 
213
 
 
214
    def write_inventory(self, inv, f, working=False):
 
215
        """Write inventory to a file.
 
216
 
 
217
        :param inv: the inventory to write.
 
218
        :param f: the file to write. (May be None if the lines are the desired
 
219
            output).
 
220
        :param working: If True skip history data - text_sha1, text_size,
 
221
            reference_revision, symlink_target.
 
222
        :return: The inventory as a list of lines.
 
223
        """
 
224
        output = []
 
225
        append = output.append
 
226
        if inv.revision_id is not None:
 
227
            revid1 = ' revision_id="'
 
228
            revid2 = xml_serializer.encode_and_escape(inv.revision_id)
154
229
        else:
155
 
            return xml7.Serializer_v7._unpack_entry(self, elt,
156
 
                entry_cache=entry_cache, return_from_cache=return_from_cache)
157
 
 
158
 
    def __init__(self, node_size, search_key_name):
159
 
        self.maximum_size = node_size
160
 
        self.search_key_name = search_key_name
161
 
 
162
 
 
163
 
class CHKSerializer(xml6.Serializer_v6):
164
 
    """A CHKInventory based serializer with 'plain' behaviour."""
165
 
 
166
 
    format_num = '9'
167
 
    revision_format_num = None
168
 
    support_altered_by_hack = False
169
 
 
170
 
    def __init__(self, node_size, search_key_name):
171
 
        self.maximum_size = node_size
172
 
        self.search_key_name = search_key_name
 
230
            revid1 = ""
 
231
            revid2 = ""
 
232
        append('<inventory format="%s"%s%s>\n' % (
 
233
            self.format_num, revid1, revid2))
 
234
        append('<directory file_id="%s name="%s revision="%s />\n' % (
 
235
            xml_serializer.encode_and_escape(inv.root.file_id),
 
236
            xml_serializer.encode_and_escape(inv.root.name),
 
237
            xml_serializer.encode_and_escape(inv.root.revision)))
 
238
        xml_serializer.serialize_inventory_flat(inv,
 
239
            append,
 
240
            root_id=None, supported_kinds=self.supported_kinds, 
 
241
            working=working)
 
242
        if f is not None:
 
243
            f.writelines(output)
 
244
        return output
173
245
 
174
246
 
175
247
chk_serializer_255_bigpage = CHKSerializer(65536, 'hash-255-way')