~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weave.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-03-09 06:39:13 UTC
  • mfrom: (1596.2.6 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060309063913-6d8ce700706d0802
Merge knit performance stage 1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
# the possible relationships.
68
68
 
69
69
 
 
70
from copy import copy
70
71
from cStringIO import StringIO
71
72
from difflib import SequenceMatcher
72
73
import os
180
181
    __slots__ = ['_weave', '_parents', '_sha1s', '_names', '_name_map',
181
182
                 '_weave_name']
182
183
    
183
 
    def __init__(self, weave_name=None):
 
184
    def __init__(self, weave_name=None, access_mode='w'):
 
185
        super(Weave, self).__init__(access_mode)
184
186
        self._weave = []
185
187
        self._parents = []
186
188
        self._sha1s = []
278
280
        """Please use Weave.clone_text now."""
279
281
        return self.clone_text(new_rev_id, old_rev_id, parents)
280
282
 
281
 
    def add_lines(self, version_id, parents, lines):
 
283
    def _add_lines(self, version_id, parents, lines):
282
284
        """See VersionedFile.add_lines."""
283
285
        return self._add(version_id, lines, map(self._lookup, parents))
284
286
 
406
408
                offset += 2 + (j2 - j1)
407
409
        return new_version
408
410
 
409
 
    def clone_text(self, new_version_id, old_version_id, parents):
 
411
    def _clone_text(self, new_version_id, old_version_id, parents):
410
412
        """See VersionedFile.clone_text."""
411
413
        old_lines = self.get_text(old_version_id)
412
414
        self.add_lines(new_version_id, parents, old_lines)
492
494
 
493
495
    @deprecated_method(zero_eight)
494
496
    def _walk(self):
495
 
        """_walk has become walk, a supported api."""
496
 
        return self.walk()
497
 
 
 
497
        """_walk has become visit, a supported api."""
 
498
        return self._walk_internal()
 
499
 
 
500
    def iter_lines_added_or_present_in_versions(self, version_ids=None):
 
501
        """See VersionedFile.iter_lines_added_or_present_in_versions()."""
 
502
        if version_ids is None:
 
503
            version_ids = self.versions()
 
504
        version_ids = set(version_ids)
 
505
        for lineno, inserted, deletes, line in self._walk_internal(version_ids):
 
506
            # if inserted not in version_ids then it was inserted before the
 
507
            # versions we care about, but because weaves cannot represent ghosts
 
508
            # properly, we do not filter down to that
 
509
            # if inserted not in version_ids: continue
 
510
            if line[-1] != '\n':
 
511
                yield line + '\n'
 
512
            else:
 
513
                yield line
 
514
 
 
515
    #@deprecated_method(zero_eight)
498
516
    def walk(self, version_ids=None):
499
517
        """See VersionedFile.walk."""
 
518
        return self._walk_internal(version_ids)
 
519
 
 
520
    def _walk_internal(self, version_ids=None):
 
521
        """Helper method for weave actions."""
500
522
        
501
523
        istack = []
502
524
        dset = set()
705
727
            update_text = 'checking %s' % (short_name,)
706
728
            update_text = update_text[:25]
707
729
 
708
 
        for lineno, insert, deleteset, line in self.walk():
 
730
        for lineno, insert, deleteset, line in self._walk_internal():
709
731
            if progress_bar:
710
732
                progress_bar.update(update_text, lineno, nlines)
711
733
 
845
867
        :param msg: An optional message for the progress
846
868
        """
847
869
        new_weave = _reweave(self, other, pb=pb, msg=msg)
 
870
        self._copy_weave_content(new_weave)
 
871
 
 
872
    def _copy_weave_content(self, otherweave):
 
873
        """adsorb the content from otherweave."""
848
874
        for attr in self.__slots__:
849
875
            if attr != '_weave_name':
850
 
                setattr(self, attr, getattr(new_weave, attr))
 
876
                setattr(self, attr, copy(getattr(otherweave, attr)))
851
877
 
852
878
 
853
879
class WeaveFile(Weave):
855
881
 
856
882
    WEAVE_SUFFIX = '.weave'
857
883
    
858
 
    def __init__(self, name, transport, mode=None, create=False):
 
884
    def __init__(self, name, transport, filemode=None, create=False, access_mode='w'):
859
885
        """Create a WeaveFile.
860
886
        
861
887
        :param create: If not True, only open an existing knit.
862
888
        """
863
 
        super(WeaveFile, self).__init__(name)
 
889
        super(WeaveFile, self).__init__(name, access_mode)
864
890
        self._transport = transport
865
 
        self._mode = mode
 
891
        self._filemode = filemode
866
892
        try:
867
893
            _read_weave_v5(self._transport.get(name + WeaveFile.WEAVE_SUFFIX), self)
868
894
        except errors.NoSuchFile:
871
897
            # new file, save it
872
898
            self._save()
873
899
 
874
 
    def add_lines(self, version_id, parents, lines):
 
900
    def _add_lines(self, version_id, parents, lines):
875
901
        """Add a version and save the weave."""
876
 
        super(WeaveFile, self).add_lines(version_id, parents, lines)
 
902
        super(WeaveFile, self)._add_lines(version_id, parents, lines)
877
903
        self._save()
878
904
 
 
905
    def _clone_text(self, new_version_id, old_version_id, parents):
 
906
        """See VersionedFile.clone_text."""
 
907
        super(WeaveFile, self)._clone_text(new_version_id, old_version_id, parents)
 
908
        self._save
 
909
 
879
910
    def copy_to(self, name, transport):
880
911
        """See VersionedFile.copy_to()."""
881
912
        # as we are all in memory always, just serialise to the new place.
882
913
        sio = StringIO()
883
914
        write_weave_v5(self, sio)
884
915
        sio.seek(0)
885
 
        transport.put(name + WeaveFile.WEAVE_SUFFIX, sio, self._mode)
 
916
        transport.put(name + WeaveFile.WEAVE_SUFFIX, sio, self._filemode)
886
917
 
887
 
    def create_empty(self, name, transport, mode=None):
888
 
        return WeaveFile(name, transport, mode, create=True)
 
918
    def create_empty(self, name, transport, filemode=None):
 
919
        return WeaveFile(name, transport, filemode, create=True)
889
920
 
890
921
    def _save(self):
891
922
        """Save the weave."""
 
923
        self._check_write_ok()
892
924
        sio = StringIO()
893
925
        write_weave_v5(self, sio)
894
926
        sio.seek(0)
895
927
        self._transport.put(self._weave_name + WeaveFile.WEAVE_SUFFIX,
896
928
                            sio,
897
 
                            self._mode)
 
929
                            self._filemode)
898
930
 
899
931
    @staticmethod
900
932
    def get_suffixes():
1232
1264
 
1233
1265
    def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
1234
1266
        """See InterVersionedFile.join."""
 
1267
        if self.target.versions() == []:
 
1268
            # optimised copy
 
1269
            self.target._copy_weave_content(self.source)
 
1270
            return
1235
1271
        try:
1236
1272
            self.target._join(self.source, pb, msg, version_ids, ignore_missing)
1237
1273
        except errors.WeaveParentMismatch: