~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to multiparent.py

  • Committer: Aaron Bentley
  • Date: 2007-04-13 20:34:16 UTC
  • mto: (2520.4.1 bzr.mpbundle)
  • mto: This revision was merged to the branch mainline in revision 2631.
  • Revision ID: abentley@panoramicfeedback.com-20070413203416-7jeoifz1gq41ashn
Implement save, load, snapshot-by-size

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from bzrlib.lazy_import import lazy_import
 
2
 
 
3
lazy_import(globals(), """
1
4
import errno
2
 
from itertools import chain
 
5
import itertools
3
6
import os
4
7
from StringIO import StringIO
5
 
import sys
6
8
 
7
9
from bzrlib import (
8
10
    patiencediff,
9
11
    trace,
10
12
    ui,
11
13
    )
 
14
from bzrlib.util import bencode
 
15
""")
12
16
from bzrlib.tuned_gzip import GzipFile
13
17
 
14
18
 
276
280
            else:
277
281
                parent_lines = self.get_line_list(parent_ids)
278
282
            diff = MultiParent.from_lines(lines, parent_lines)
279
 
            snapdiff = MultiParent([NewText(lines)])
280
283
            if diff.is_snapshot():
281
284
                self._snapshots.add(version_id)
282
 
            elif diff.patch_len() >= snapdiff.patch_len():
283
 
                trace.note("Forcing snapshot")
284
 
                self._snapshots.add(version_id)
285
285
        self.add_diff(diff, version_id, parent_ids)
286
286
        self._lines[version_id] = lines
287
287
 
 
288
    def make_snapshot(self, version_id):
 
289
        snapdiff = MultiParent([NewText(self.cache_version(version_id))])
 
290
        self._snapshots.add(version_id)
 
291
 
288
292
    def import_versionedfile(self, vf, snapshots, no_cache=True,
289
293
                             single_parent=False, verify=False):
290
294
        """Import all revisions of a versionedfile
348
352
                    build_ancestors[version_id] = potential_build_ancestors
349
353
        return snapshots
350
354
 
 
355
    def select_by_size(self, num):
 
356
        versions = []
 
357
        new_snapshots = set()
 
358
        num -= len(self._snapshots)
 
359
        for version_id in self.versions():
 
360
            if version_id in self._snapshots:
 
361
                continue
 
362
            diff_len = self.get_diff(version_id).patch_len()
 
363
            snapshot_len = MultiParent([NewText(
 
364
                self.cache_version(version_id))]).patch_len()
 
365
            versions.append((diff_len - snapshot_len, version_id))
 
366
        versions.sort()
 
367
        return [v for n, v in versions[:num]]
351
368
 
352
369
    def clear_cache(self):
353
370
        self._lines.clear()
395
412
 
396
413
    def get_diff(self, version_id):
397
414
        start, count = self._diff_offset[version_id]
398
 
        infile = open(self._filename, 'rb')
 
415
        infile = open(self._filename + '.mpknit', 'rb')
399
416
        try:
400
417
            infile.seek(start)
401
418
            sio = StringIO(infile.read(count))
409
426
            zip_file.close()
410
427
 
411
428
    def add_diff(self, diff, version_id, parent_ids):
412
 
        outfile = open(self._filename, 'ab')
 
429
        outfile = open(self._filename + '.mpknit', 'ab')
413
430
        try:
414
431
            start = outfile.tell()
415
432
            try:
416
433
                zipfile = GzipFile(None, mode='ab', fileobj=outfile)
417
 
                zipfile.writelines(chain(['version %s\n' % version_id],
418
 
                                       diff.to_patch()))
 
434
                zipfile.writelines(itertools.chain(
 
435
                    ['version %s\n' % version_id], diff.to_patch()))
419
436
            finally:
420
437
                zipfile.close()
421
438
            end = outfile.tell()
426
443
 
427
444
    def destroy(self):
428
445
        try:
429
 
            os.unlink(self._filename)
430
 
        except OSError, e:
431
 
            if e.errno != errno.ENOENT:
432
 
                raise
 
446
            os.unlink(self._filename + '.mpknit')
 
447
        except OSError, e:
 
448
            if e.errno != errno.ENOENT:
 
449
                raise
 
450
        try:
 
451
            os.unlink(self._filename + '.mpidx')
 
452
        except OSError, e:
 
453
            if e.errno != errno.ENOENT:
 
454
                raise
 
455
 
 
456
    def save(self):
 
457
        open(self._filename + '.mpidx', 'wb').write(bencode.bencode(
 
458
            (self._parents, list(self._snapshots), self._diff_offset)))
 
459
 
 
460
    def load(self):
 
461
        self._parents, snapshots, self._diff_offset = bencode.bdecode(
 
462
            open(self._filename + '.mpidx', 'rb').read())
 
463
        self._snapshots = set(snapshots)
433
464
 
434
465
 
435
466
class _Reconstructor(object):