~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/knit.py

  • Committer: Andrew Bennetts
  • Date: 2007-06-26 06:18:07 UTC
  • mto: (2535.4.4 streaming-smart-fetch)
  • mto: This revision was merged to the branch mainline in revision 2906.
  • Revision ID: andrew.bennetts@canonical.com-20070626061807-nuxew9ioocogpqs4
Simple implementation of Knit.insert_data_stream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
    KnitError,
85
85
    InvalidRevisionId,
86
86
    KnitCorrupt,
 
87
    KnitDataStreamIncompatible,
87
88
    KnitHeaderError,
88
89
    RevisionNotPresent,
89
90
    RevisionAlreadyPresent,
651
652
                        return True
652
653
        return False
653
654
 
 
655
    def insert_data_stream(self, (format, data_list, reader_callable)):
 
656
        """Insert knit records from a data stream into this knit.
 
657
 
 
658
        Existing versions will be ...
 
659
        
 
660
        :seealso: get_data_stream
 
661
        """
 
662
        if format != self.get_format_signature():
 
663
            raise KnitDataStreamIncompatible(
 
664
                format, self.get_format_signature())
 
665
        for version_id, options, length, parents in data_list:
 
666
            # do we have this version_id:
 
667
            #  * yes: consistency check
 
668
            #  * no: plan to insert it
 
669
            raw_data = reader_callable(length)
 
670
            if self.has_version(version_id):
 
671
                # Make sure that if we've already seen this version_id, that it has
 
672
                # the same content.
 
673
                # First, check the list of parents.
 
674
                my_parents = self.get_parents_with_ghosts(version_id)
 
675
                if my_parents != parents:
 
676
                    raise KnitCorrupt(
 
677
                        self.filename,
 
678
                        'parents list %r from data stream does not match '
 
679
                        'already recorded parents %r for %s'
 
680
                        % (parents, my_parents, version_id))
 
681
 
 
682
                # Also check the SHA-1 of the fulltext this content will
 
683
                # produce.
 
684
                my_fulltext_sha1 = self.get_sha1(version_id)
 
685
                df, rec = self._data._parse_record_header(version_id, raw_data)
 
686
                stream_fulltext_sha1 = rec[3]
 
687
                if my_fulltext_sha1 != stream_fulltext_sha1:
 
688
                    # Actually, we don't know if it's this knit that's corrupt,
 
689
                    # or the data stream we're trying to insert.
 
690
                    raise KnitCorrupt(
 
691
                        self.filename, 'sha-1 does not match %s' % version_id)
 
692
            else:
 
693
                # We don't have this record.  Insert it.
 
694
                self._add_raw_records([(version_id, options, parents, length)], raw_data)
 
695
 
654
696
    def versions(self):
655
697
        """See VersionedFile.versions."""
656
698
        return self._index.get_versions()