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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-04-24 10:31:28 UTC
  • mfrom: (1684.1.2 bzr.mbp.integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060424103128-a637f56a7c529bad
(mbp) tutorial improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
from __future__ import absolute_import
18
 
 
19
 
import errno
20
 
import os
21
 
 
22
 
from .lazy_import import lazy_import
23
 
 
24
 
lazy_import(globals(), """
25
 
import gzip
26
 
import itertools
27
 
import patiencediff
28
 
 
29
 
from breezy import (
30
 
    bencode,
31
 
    ui,
32
 
    )
33
 
""")
34
 
from . import (
35
 
    errors,
36
 
    )
37
 
from .i18n import gettext
38
 
from .sixish import (
39
 
    BytesIO,
40
 
    range,
41
 
    )
42
 
 
43
 
 
44
 
def topo_iter_keys(vf, keys=None):
45
 
    if keys is None:
46
 
        keys = vf.keys()
47
 
    parents = vf.get_parent_map(keys)
48
 
    return _topo_iter(parents, keys)
49
 
 
50
 
 
51
 
def topo_iter(vf, versions=None):
52
 
    if versions is None:
53
 
        versions = vf.versions()
54
 
    parents = vf.get_parent_map(versions)
55
 
    return _topo_iter(parents, versions)
56
 
 
57
 
 
58
 
def _topo_iter(parents, versions):
59
 
    seen = set()
60
 
    descendants = {}
61
 
 
62
 
    def pending_parents(version):
63
 
        if parents[version] is None:
64
 
            return []
65
 
        return [v for v in parents[version] if v in versions and
66
 
                v not in seen]
67
 
    for version_id in versions:
68
 
        if parents[version_id] is None:
69
 
            # parentless
70
 
            continue
71
 
        for parent_id in parents[version_id]:
72
 
            descendants.setdefault(parent_id, []).append(version_id)
73
 
    cur = [v for v in versions if len(pending_parents(v)) == 0]
74
 
    while len(cur) > 0:
75
 
        next = []
76
 
        for version_id in cur:
77
 
            if version_id in seen:
78
 
                continue
79
 
            if len(pending_parents(version_id)) != 0:
80
 
                continue
81
 
            next.extend(descendants.get(version_id, []))
82
 
            yield version_id
83
 
            seen.add(version_id)
84
 
        cur = next
85
 
 
86
 
 
87
 
class MultiParent(object):
88
 
    """A multi-parent diff"""
89
 
 
90
 
    __slots__ = ['hunks']
91
 
 
92
 
    def __init__(self, hunks=None):
93
 
        if hunks is not None:
94
 
            self.hunks = hunks
95
 
        else:
96
 
            self.hunks = []
97
 
 
98
 
    def __repr__(self):
99
 
        return "MultiParent(%r)" % self.hunks
100
 
 
101
 
    def __eq__(self, other):
102
 
        if self.__class__ is not other.__class__:
103
 
            return False
104
 
        return (self.hunks == other.hunks)
105
 
 
106
 
    @staticmethod
107
 
    def from_lines(text, parents=(), left_blocks=None):
108
 
        """Produce a MultiParent from a list of lines and parents"""
109
 
        def compare(parent):
110
 
            matcher = patiencediff.PatienceSequenceMatcher(None, parent,
111
 
                                                           text)
112
 
            return matcher.get_matching_blocks()
113
 
        if len(parents) > 0:
114
 
            if left_blocks is None:
115
 
                left_blocks = compare(parents[0])
116
 
            parent_comparisons = [left_blocks] + [compare(p) for p in
117
 
                                                  parents[1:]]
118
 
        else:
119
 
            parent_comparisons = []
120
 
        cur_line = 0
121
 
        new_text = NewText([])
122
 
        parent_text = []
123
 
        block_iter = [iter(i) for i in parent_comparisons]
124
 
        diff = MultiParent([])
125
 
 
126
 
        def next_block(p):
127
 
            try:
128
 
                return next(block_iter[p])
129
 
            except StopIteration:
130
 
                return None
131
 
        cur_block = [next_block(p) for p, i in enumerate(block_iter)]
132
 
        while cur_line < len(text):
133
 
            best_match = None
134
 
            for p, block in enumerate(cur_block):
135
 
                if block is None:
136
 
                    continue
137
 
                i, j, n = block
138
 
                while j + n <= cur_line:
139
 
                    block = cur_block[p] = next_block(p)
140
 
                    if block is None:
141
 
                        break
142
 
                    i, j, n = block
143
 
                if block is None:
144
 
                    continue
145
 
                if j > cur_line:
146
 
                    continue
147
 
                offset = cur_line - j
148
 
                i += offset
149
 
                j = cur_line
150
 
                n -= offset
151
 
                if n == 0:
152
 
                    continue
153
 
                if best_match is None or n > best_match.num_lines:
154
 
                    best_match = ParentText(p, i, j, n)
155
 
            if best_match is None:
156
 
                new_text.lines.append(text[cur_line])
157
 
                cur_line += 1
158
 
            else:
159
 
                if len(new_text.lines) > 0:
160
 
                    diff.hunks.append(new_text)
161
 
                    new_text = NewText([])
162
 
                diff.hunks.append(best_match)
163
 
                cur_line += best_match.num_lines
164
 
        if len(new_text.lines) > 0:
165
 
            diff.hunks.append(new_text)
166
 
        return diff
167
 
 
168
 
    def get_matching_blocks(self, parent, parent_len):
169
 
        for hunk in self.hunks:
170
 
            if not isinstance(hunk, ParentText) or hunk.parent != parent:
171
 
                continue
172
 
            yield (hunk.parent_pos, hunk.child_pos, hunk.num_lines)
173
 
        yield parent_len, self.num_lines(), 0
174
 
 
175
 
    def to_lines(self, parents=()):
176
 
        """Contruct a fulltext from this diff and its parents"""
177
 
        mpvf = MultiMemoryVersionedFile()
178
 
        for num, parent in enumerate(parents):
179
 
            mpvf.add_version(BytesIO(parent).readlines(), num, [])
180
 
        mpvf.add_diff(self, 'a', list(range(len(parents))))
181
 
        return mpvf.get_line_list(['a'])[0]
182
 
 
183
 
    @classmethod
184
 
    def from_texts(cls, text, parents=()):
185
 
        """Produce a MultiParent from a text and list of parent text"""
186
 
        return cls.from_lines(BytesIO(text).readlines(),
187
 
                              [BytesIO(p).readlines() for p in parents])
188
 
 
189
 
    def to_patch(self):
190
 
        """Yield text lines for a patch"""
191
 
        for hunk in self.hunks:
192
 
            for line in hunk.to_patch():
193
 
                yield line
194
 
 
195
 
    def patch_len(self):
196
 
        return len(b''.join(self.to_patch()))
197
 
 
198
 
    def zipped_patch_len(self):
199
 
        return len(gzip_string(self.to_patch()))
200
 
 
201
 
    @classmethod
202
 
    def from_patch(cls, text):
203
 
        """Create a MultiParent from its string form"""
204
 
        return cls._from_patch(BytesIO(text))
205
 
 
206
 
    @staticmethod
207
 
    def _from_patch(lines):
208
 
        """This is private because it is essential to split lines on \n only"""
209
 
        line_iter = iter(lines)
210
 
        hunks = []
211
 
        cur_line = None
212
 
        while True:
213
 
            try:
214
 
                cur_line = next(line_iter)
215
 
            except StopIteration:
216
 
                break
217
 
            first_char = cur_line[0:1]
218
 
            if first_char == b'i':
219
 
                num_lines = int(cur_line.split(b' ')[1])
220
 
                hunk_lines = [next(line_iter) for _ in range(num_lines)]
221
 
                hunk_lines[-1] = hunk_lines[-1][:-1]
222
 
                hunks.append(NewText(hunk_lines))
223
 
            elif first_char == b'\n':
224
 
                hunks[-1].lines[-1] += b'\n'
225
 
            else:
226
 
                if not (first_char == b'c'):
227
 
                    raise AssertionError(first_char)
228
 
                parent, parent_pos, child_pos, num_lines =\
229
 
                    [int(v) for v in cur_line.split(b' ')[1:]]
230
 
                hunks.append(ParentText(parent, parent_pos, child_pos,
231
 
                                        num_lines))
232
 
        return MultiParent(hunks)
233
 
 
234
 
    def range_iterator(self):
235
 
        """Iterate through the hunks, with range indicated
236
 
 
237
 
        kind is "new" or "parent".
238
 
        for "new", data is a list of lines.
239
 
        for "parent", data is (parent, parent_start, parent_end)
240
 
        :return: a generator of (start, end, kind, data)
241
 
        """
242
 
        start = 0
243
 
        for hunk in self.hunks:
244
 
            if isinstance(hunk, NewText):
245
 
                kind = 'new'
246
 
                end = start + len(hunk.lines)
247
 
                data = hunk.lines
248
 
            else:
249
 
                kind = 'parent'
250
 
                start = hunk.child_pos
251
 
                end = start + hunk.num_lines
252
 
                data = (hunk.parent, hunk.parent_pos, hunk.parent_pos +
253
 
                        hunk.num_lines)
254
 
            yield start, end, kind, data
255
 
            start = end
256
 
 
257
 
    def num_lines(self):
258
 
        """The number of lines in the output text"""
259
 
        extra_n = 0
260
 
        for hunk in reversed(self.hunks):
261
 
            if isinstance(hunk, ParentText):
262
 
                return hunk.child_pos + hunk.num_lines + extra_n
263
 
            extra_n += len(hunk.lines)
264
 
        return extra_n
265
 
 
266
 
    def is_snapshot(self):
267
 
        """Return true of this hunk is effectively a fulltext"""
268
 
        if len(self.hunks) != 1:
269
 
            return False
270
 
        return (isinstance(self.hunks[0], NewText))
271
 
 
272
 
 
273
 
class NewText(object):
274
 
    """The contents of text that is introduced by this text"""
275
 
 
276
 
    __slots__ = ['lines']
277
 
 
278
 
    def __init__(self, lines):
279
 
        self.lines = lines
280
 
 
281
 
    def __eq__(self, other):
282
 
        if self.__class__ is not other.__class__:
283
 
            return False
284
 
        return (other.lines == self.lines)
285
 
 
286
 
    def __repr__(self):
287
 
        return 'NewText(%r)' % self.lines
288
 
 
289
 
    def to_patch(self):
290
 
        yield b'i %d\n' % len(self.lines)
291
 
        for line in self.lines:
292
 
            yield line
293
 
        yield b'\n'
294
 
 
295
 
 
296
 
class ParentText(object):
297
 
    """A reference to text present in a parent text"""
298
 
 
299
 
    __slots__ = ['parent', 'parent_pos', 'child_pos', 'num_lines']
300
 
 
301
 
    def __init__(self, parent, parent_pos, child_pos, num_lines):
302
 
        self.parent = parent
303
 
        self.parent_pos = parent_pos
304
 
        self.child_pos = child_pos
305
 
        self.num_lines = num_lines
306
 
 
307
 
    def _as_dict(self):
308
 
        return {b'parent': self.parent,
309
 
                b'parent_pos': self.parent_pos,
310
 
                b'child_pos': self.child_pos,
311
 
                b'num_lines': self.num_lines}
312
 
 
313
 
    def __repr__(self):
314
 
        return ('ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'
315
 
                ' %(num_lines)r)' % self._as_dict())
316
 
 
317
 
    def __eq__(self, other):
318
 
        if self.__class__ is not other.__class__:
319
 
            return False
320
 
        return self._as_dict() == other._as_dict()
321
 
 
322
 
    def to_patch(self):
323
 
        yield (b'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'
324
 
               % self._as_dict())
325
 
 
326
 
 
327
 
class BaseVersionedFile(object):
328
 
    """Pseudo-VersionedFile skeleton for MultiParent"""
329
 
 
330
 
    def __init__(self, snapshot_interval=25, max_snapshots=None):
331
 
        self._lines = {}
332
 
        self._parents = {}
333
 
        self._snapshots = set()
334
 
        self.snapshot_interval = snapshot_interval
335
 
        self.max_snapshots = max_snapshots
336
 
 
337
 
    def versions(self):
338
 
        return iter(self._parents)
339
 
 
340
 
    def has_version(self, version):
341
 
        return version in self._parents
342
 
 
343
 
    def do_snapshot(self, version_id, parent_ids):
344
 
        """Determine whether to perform a snapshot for this version"""
345
 
        if self.snapshot_interval is None:
346
 
            return False
347
 
        if self.max_snapshots is not None and\
348
 
                len(self._snapshots) == self.max_snapshots:
349
 
            return False
350
 
        if len(parent_ids) == 0:
351
 
            return True
352
 
        for ignored in range(self.snapshot_interval):
353
 
            if len(parent_ids) == 0:
354
 
                return False
355
 
            version_ids = parent_ids
356
 
            parent_ids = []
357
 
            for version_id in version_ids:
358
 
                if version_id not in self._snapshots:
359
 
                    parent_ids.extend(self._parents[version_id])
360
 
        else:
361
 
            return True
362
 
 
363
 
    def add_version(self, lines, version_id, parent_ids,
364
 
                    force_snapshot=None, single_parent=False):
365
 
        """Add a version to the versionedfile
366
 
 
367
 
        :param lines: The list of lines to add.  Must be split on '\n'.
368
 
        :param version_id: The version_id of the version to add
369
 
        :param force_snapshot: If true, force this version to be added as a
370
 
            snapshot version.  If false, force this version to be added as a
371
 
            diff.  If none, determine this automatically.
372
 
        :param single_parent: If true, use a single parent, rather than
373
 
            multiple parents.
374
 
        """
375
 
        if force_snapshot is None:
376
 
            do_snapshot = self.do_snapshot(version_id, parent_ids)
377
 
        else:
378
 
            do_snapshot = force_snapshot
379
 
        if do_snapshot:
380
 
            self._snapshots.add(version_id)
381
 
            diff = MultiParent([NewText(lines)])
382
 
        else:
383
 
            if single_parent:
384
 
                parent_lines = self.get_line_list(parent_ids[:1])
385
 
            else:
386
 
                parent_lines = self.get_line_list(parent_ids)
387
 
            diff = MultiParent.from_lines(lines, parent_lines)
388
 
            if diff.is_snapshot():
389
 
                self._snapshots.add(version_id)
390
 
        self.add_diff(diff, version_id, parent_ids)
391
 
        self._lines[version_id] = lines
392
 
 
393
 
    def get_parents(self, version_id):
394
 
        return self._parents[version_id]
395
 
 
396
 
    def make_snapshot(self, version_id):
397
 
        snapdiff = MultiParent([NewText(self.cache_version(version_id))])
398
 
        self.add_diff(snapdiff, version_id, self._parents[version_id])
399
 
        self._snapshots.add(version_id)
400
 
 
401
 
    def import_versionedfile(self, vf, snapshots, no_cache=True,
402
 
                             single_parent=False, verify=False):
403
 
        """Import all revisions of a versionedfile
404
 
 
405
 
        :param vf: The versionedfile to import
406
 
        :param snapshots: If provided, the revisions to make snapshots of.
407
 
            Otherwise, this will be auto-determined
408
 
        :param no_cache: If true, clear the cache after every add.
409
 
        :param single_parent: If true, omit all but one parent text, (but
410
 
            retain parent metadata).
411
 
        """
412
 
        if not (no_cache or not verify):
413
 
            raise ValueError()
414
 
        revisions = set(vf.versions())
415
 
        total = len(revisions)
416
 
        with ui.ui_factory.nested_progress_bar() as pb:
417
 
            while len(revisions) > 0:
418
 
                added = set()
419
 
                for revision in revisions:
420
 
                    parents = vf.get_parents(revision)
421
 
                    if [p for p in parents if p not in self._parents] != []:
422
 
                        continue
423
 
                    lines = [a + b' ' + l for a, l in
424
 
                             vf.annotate(revision)]
425
 
                    if snapshots is None:
426
 
                        force_snapshot = None
427
 
                    else:
428
 
                        force_snapshot = (revision in snapshots)
429
 
                    self.add_version(lines, revision, parents, force_snapshot,
430
 
                                     single_parent)
431
 
                    added.add(revision)
432
 
                    if no_cache:
433
 
                        self.clear_cache()
434
 
                        vf.clear_cache()
435
 
                        if verify:
436
 
                            if not (lines == self.get_line_list([revision])[0]):
437
 
                                raise AssertionError()
438
 
                            self.clear_cache()
439
 
                    pb.update(gettext('Importing revisions'),
440
 
                              (total - len(revisions)) + len(added), total)
441
 
                revisions = [r for r in revisions if r not in added]
442
 
 
443
 
    def select_snapshots(self, vf):
444
 
        """Determine which versions to add as snapshots"""
445
 
        build_ancestors = {}
446
 
        snapshots = set()
447
 
        for version_id in topo_iter(vf):
448
 
            potential_build_ancestors = set(vf.get_parents(version_id))
449
 
            parents = vf.get_parents(version_id)
450
 
            if len(parents) == 0:
451
 
                snapshots.add(version_id)
452
 
                build_ancestors[version_id] = set()
453
 
            else:
454
 
                for parent in vf.get_parents(version_id):
455
 
                    potential_build_ancestors.update(build_ancestors[parent])
456
 
                if len(potential_build_ancestors) > self.snapshot_interval:
457
 
                    snapshots.add(version_id)
458
 
                    build_ancestors[version_id] = set()
459
 
                else:
460
 
                    build_ancestors[version_id] = potential_build_ancestors
461
 
        return snapshots
462
 
 
463
 
    def select_by_size(self, num):
464
 
        """Select snapshots for minimum output size"""
465
 
        num -= len(self._snapshots)
466
 
        new_snapshots = self.get_size_ranking()[-num:]
467
 
        return [v for n, v in new_snapshots]
468
 
 
469
 
    def get_size_ranking(self):
470
 
        """Get versions ranked by size"""
471
 
        versions = []
472
 
        for version_id in self.versions():
473
 
            if version_id in self._snapshots:
474
 
                continue
475
 
            diff_len = self.get_diff(version_id).patch_len()
476
 
            snapshot_len = MultiParent([NewText(
477
 
                self.cache_version(version_id))]).patch_len()
478
 
            versions.append((snapshot_len - diff_len, version_id))
479
 
        versions.sort()
480
 
        return versions
481
 
 
482
 
    def import_diffs(self, vf):
483
 
        """Import the diffs from another pseudo-versionedfile"""
484
 
        for version_id in vf.versions():
485
 
            self.add_diff(vf.get_diff(version_id), version_id,
486
 
                          vf._parents[version_id])
487
 
 
488
 
    def get_build_ranking(self):
489
 
        """Return revisions sorted by how much they reduce build complexity"""
490
 
        could_avoid = {}
491
 
        referenced_by = {}
492
 
        for version_id in topo_iter(self):
493
 
            could_avoid[version_id] = set()
494
 
            if version_id not in self._snapshots:
495
 
                for parent_id in self._parents[version_id]:
496
 
                    could_avoid[version_id].update(could_avoid[parent_id])
497
 
                could_avoid[version_id].update(self._parents)
498
 
                could_avoid[version_id].discard(version_id)
499
 
            for avoid_id in could_avoid[version_id]:
500
 
                referenced_by.setdefault(avoid_id, set()).add(version_id)
501
 
        available_versions = list(self.versions())
502
 
        ranking = []
503
 
        while len(available_versions) > 0:
504
 
            available_versions.sort(key=lambda x:
505
 
                                    len(could_avoid[x]) *
506
 
                                    len(referenced_by.get(x, [])))
507
 
            selected = available_versions.pop()
508
 
            ranking.append(selected)
509
 
            for version_id in referenced_by[selected]:
510
 
                could_avoid[version_id].difference_update(
511
 
                    could_avoid[selected])
512
 
            for version_id in could_avoid[selected]:
513
 
                referenced_by[version_id].difference_update(
514
 
                    referenced_by[selected]
515
 
                )
516
 
        return ranking
517
 
 
518
 
    def clear_cache(self):
519
 
        self._lines.clear()
520
 
 
521
 
    def get_line_list(self, version_ids):
522
 
        return [self.cache_version(v) for v in version_ids]
523
 
 
524
 
    def cache_version(self, version_id):
525
 
        try:
526
 
            return self._lines[version_id]
527
 
        except KeyError:
528
 
            pass
529
 
        diff = self.get_diff(version_id)
530
 
        lines = []
531
 
        reconstructor = _Reconstructor(self, self._lines, self._parents)
532
 
        reconstructor.reconstruct_version(lines, version_id)
533
 
        self._lines[version_id] = lines
534
 
        return lines
535
 
 
536
 
 
537
 
class MultiMemoryVersionedFile(BaseVersionedFile):
538
 
    """Memory-backed pseudo-versionedfile"""
539
 
 
540
 
    def __init__(self, snapshot_interval=25, max_snapshots=None):
541
 
        BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots)
542
 
        self._diffs = {}
543
 
 
544
 
    def add_diff(self, diff, version_id, parent_ids):
545
 
        self._diffs[version_id] = diff
546
 
        self._parents[version_id] = parent_ids
547
 
 
548
 
    def get_diff(self, version_id):
549
 
        try:
550
 
            return self._diffs[version_id]
551
 
        except KeyError:
552
 
            raise errors.RevisionNotPresent(version_id, self)
553
 
 
554
 
    def destroy(self):
555
 
        self._diffs = {}
556
 
 
557
 
 
558
 
class MultiVersionedFile(BaseVersionedFile):
559
 
    """Disk-backed pseudo-versionedfile"""
560
 
 
561
 
    def __init__(self, filename, snapshot_interval=25, max_snapshots=None):
562
 
        BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots)
563
 
        self._filename = filename
564
 
        self._diff_offset = {}
565
 
 
566
 
    def get_diff(self, version_id):
567
 
        start, count = self._diff_offset[version_id]
568
 
        with open(self._filename + '.mpknit', 'rb') as infile:
569
 
            infile.seek(start)
570
 
            sio = BytesIO(infile.read(count))
571
 
        with gzip.GzipFile(None, mode='rb', fileobj=sio) as zip_file:
572
 
            file_version_id = zip_file.readline()
573
 
            content = zip_file.read()
574
 
            return MultiParent.from_patch(content)
575
 
 
576
 
    def add_diff(self, diff, version_id, parent_ids):
577
 
        with open(self._filename + '.mpknit', 'ab') as outfile:
578
 
            outfile.seek(0, 2)      # workaround for windows bug:
579
 
            # .tell() for files opened in 'ab' mode
580
 
            # before any write returns 0
581
 
            start = outfile.tell()
582
 
            with gzip.GzipFile(None, mode='ab', fileobj=outfile) as zipfile:
583
 
                zipfile.writelines(itertools.chain(
584
 
                    [b'version %s\n' % version_id], diff.to_patch()))
585
 
            end = outfile.tell()
586
 
        self._diff_offset[version_id] = (start, end - start)
587
 
        self._parents[version_id] = parent_ids
588
 
 
589
 
    def destroy(self):
590
 
        try:
591
 
            os.unlink(self._filename + '.mpknit')
592
 
        except OSError as e:
593
 
            if e.errno != errno.ENOENT:
594
 
                raise
595
 
        try:
596
 
            os.unlink(self._filename + '.mpidx')
597
 
        except OSError as e:
598
 
            if e.errno != errno.ENOENT:
599
 
                raise
600
 
 
601
 
    def save(self):
602
 
        open(self._filename + '.mpidx', 'wb').write(bencode.bencode(
603
 
            (self._parents, list(self._snapshots), self._diff_offset)))
604
 
 
605
 
    def load(self):
606
 
        self._parents, snapshots, self._diff_offset = bencode.bdecode(
607
 
            open(self._filename + '.mpidx', 'rb').read())
608
 
        self._snapshots = set(snapshots)
609
 
 
610
 
 
611
 
class _Reconstructor(object):
612
 
    """Build a text from the diffs, ancestry graph and cached lines"""
613
 
 
614
 
    def __init__(self, diffs, lines, parents):
615
 
        self.diffs = diffs
616
 
        self.lines = lines
617
 
        self.parents = parents
618
 
        self.cursor = {}
619
 
 
620
 
    def reconstruct(self, lines, parent_text, version_id):
621
 
        """Append the lines referred to by a ParentText to lines"""
622
 
        parent_id = self.parents[version_id][parent_text.parent]
623
 
        end = parent_text.parent_pos + parent_text.num_lines
624
 
        return self._reconstruct(lines, parent_id, parent_text.parent_pos,
625
 
                                 end)
626
 
 
627
 
    def _reconstruct(self, lines, req_version_id, req_start, req_end):
628
 
        """Append lines for the requested version_id range"""
629
 
        # stack of pending range requests
630
 
        if req_start == req_end:
631
 
            return
632
 
        pending_reqs = [(req_version_id, req_start, req_end)]
633
 
        while len(pending_reqs) > 0:
634
 
            req_version_id, req_start, req_end = pending_reqs.pop()
635
 
            # lazily allocate cursors for versions
636
 
            if req_version_id in self.lines:
637
 
                lines.extend(self.lines[req_version_id][req_start:req_end])
638
 
                continue
639
 
            try:
640
 
                start, end, kind, data, iterator = self.cursor[req_version_id]
641
 
            except KeyError:
642
 
                iterator = self.diffs.get_diff(req_version_id).range_iterator()
643
 
                start, end, kind, data = next(iterator)
644
 
            if start > req_start:
645
 
                iterator = self.diffs.get_diff(req_version_id).range_iterator()
646
 
                start, end, kind, data = next(iterator)
647
 
 
648
 
            # find the first hunk relevant to the request
649
 
            while end <= req_start:
650
 
                start, end, kind, data = next(iterator)
651
 
            self.cursor[req_version_id] = start, end, kind, data, iterator
652
 
            # if the hunk can't satisfy the whole request, split it in two,
653
 
            # and leave the second half for later.
654
 
            if req_end > end:
655
 
                pending_reqs.append((req_version_id, end, req_end))
656
 
                req_end = end
657
 
            if kind == 'new':
658
 
                lines.extend(data[req_start - start: (req_end - start)])
659
 
            else:
660
 
                # If the hunk is a ParentText, rewrite it as a range request
661
 
                # for the parent, and make it the next pending request.
662
 
                parent, parent_start, parent_end = data
663
 
                new_version_id = self.parents[req_version_id][parent]
664
 
                new_start = parent_start + req_start - start
665
 
                new_end = parent_end + req_end - end
666
 
                pending_reqs.append((new_version_id, new_start, new_end))
667
 
 
668
 
    def reconstruct_version(self, lines, version_id):
669
 
        length = self.diffs.get_diff(version_id).num_lines()
670
 
        return self._reconstruct(lines, version_id, 0, length)
671
 
 
672
 
 
673
 
def gzip_string(lines):
674
 
    sio = BytesIO()
675
 
    with gzip.GzipFile(None, mode='wb', fileobj=sio) as data_file:
676
 
        data_file.writelines(lines)
677
 
    return sio.getvalue()