~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/repository.py

(jelmer) Add HPSS call for ``Repository.iter_files_bytes``. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
963
963
    New in 2.5.
964
964
    """
965
965
 
966
 
    def do_repository_request(self, repository, lock_token, write_group_tokens,
967
 
            revision_id):
 
966
    def do_repository_request(self, repository, lock_token, revision_id,
 
967
            *write_group_tokens):
968
968
        """Add a revision signature text.
969
969
 
970
970
        :param repository: Repository to operate on
971
971
        :param lock_token: Lock token
 
972
        :param revision_id: Revision for which to add signature
972
973
        :param write_group_tokens: Write group tokens
973
 
        :param revision_id: Revision for which to add signature
974
974
        """
975
975
        self._lock_token = lock_token
 
976
        self._revision_id = revision_id
976
977
        self._write_group_tokens = write_group_tokens
977
 
        self._revision_id = revision_id
978
978
        return None
979
979
 
980
980
    def do_body(self, body_bytes):
988
988
        try:
989
989
            self._repository.resume_write_group(self._write_group_tokens)
990
990
            try:
991
 
                self._repository.add_signature_text(self._revision_id, body_bytes)
 
991
                self._repository.add_signature_text(self._revision_id,
 
992
                    body_bytes)
992
993
            finally:
993
994
                new_write_group_tokens = self._repository.suspend_write_group()
994
995
        finally:
995
996
            self._repository.unlock()
996
 
        return SuccessfulSmartServerResponse(('ok', ) + tuple(new_write_group_tokens))
 
997
        return SuccessfulSmartServerResponse(
 
998
            ('ok', ) + tuple(new_write_group_tokens))
997
999
 
998
1000
 
999
1001
class SmartServerRepositoryStartWriteGroup(SmartServerRepositoryRequest):
1126
1128
        return SuccessfulSmartServerResponse(("ok", ), )
1127
1129
 
1128
1130
 
 
1131
class SmartServerRepositoryIterFilesBytes(SmartServerRepositoryRequest):
 
1132
    """Iterate over the contents of files.
 
1133
 
 
1134
    The client sends a list of desired files to stream, one
 
1135
    per line, and as tuples of file id and revision, separated by
 
1136
    \0.
 
1137
 
 
1138
    The server replies with a stream. Each entry is preceded by a header,
 
1139
    which can either be:
 
1140
 
 
1141
    * "ok\x00IDX\n" where IDX is the index of the entry in the desired files
 
1142
        list sent by the client. This header is followed by the contents of
 
1143
        the file, bzip2-compressed.
 
1144
    * "absent\x00FILEID\x00REVISION\x00IDX" to indicate a text is missing.
 
1145
        The client can then raise an appropriate RevisionNotPresent error
 
1146
        or check its fallback repositories.
 
1147
 
 
1148
    New in 2.5.
 
1149
    """
 
1150
 
 
1151
    def body_stream(self, repository, desired_files):
 
1152
        self._repository.lock_read()
 
1153
        try:
 
1154
            text_keys = {}
 
1155
            for i, key in enumerate(desired_files):
 
1156
                text_keys[key] = i
 
1157
            for record in repository.texts.get_record_stream(text_keys,
 
1158
                    'unordered', True):
 
1159
                identifier = text_keys[record.key]
 
1160
                if record.storage_kind == 'absent':
 
1161
                    yield "absent\0%s\0%s\0%d\n" % (record.key[0],
 
1162
                        record.key[1], identifier)
 
1163
                    # FIXME: Way to abort early?
 
1164
                    continue
 
1165
                yield "ok\0%d\n" % identifier
 
1166
                compressor = zlib.compressobj()
 
1167
                for bytes in record.get_bytes_as('chunked'):
 
1168
                    data = compressor.compress(bytes)
 
1169
                    if data:
 
1170
                        yield data
 
1171
                data = compressor.flush()
 
1172
                if data:
 
1173
                    yield data
 
1174
        finally:
 
1175
            self._repository.unlock()
 
1176
 
 
1177
    def do_body(self, body_bytes):
 
1178
        desired_files = [
 
1179
            tuple(l.split("\0")) for l in body_bytes.splitlines()]
 
1180
        return SuccessfulSmartServerResponse(('ok', ),
 
1181
            body_stream=self.body_stream(self._repository, desired_files))
 
1182
 
 
1183
    def do_repository_request(self, repository):
 
1184
        # Signal that we want a body
 
1185
        return None
 
1186
 
 
1187
 
1129
1188
class SmartServerRepositoryIterRevisions(SmartServerRepositoryRequest):
1130
1189
    """Stream a list of revisions.
1131
1190