~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/repository.py

  • Committer: Jelmer Vernooij
  • Date: 2011-11-27 17:42:25 UTC
  • mto: This revision was merged to the branch mainline in revision 6311.
  • Revision ID: jelmer@samba.org-20111127174225-tspfeewl0gwxxumt
Add possible_transports in a couple more places.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Server-side repository related request implementations."""
18
 
 
19
 
from __future__ import absolute_import
 
17
"""Server-side repository related request implmentations."""
20
18
 
21
19
import bz2
22
20
import os
24
22
import sys
25
23
import tempfile
26
24
import threading
27
 
import zlib
28
25
 
29
26
from bzrlib import (
30
27
    bencode,
31
28
    errors,
32
29
    estimate_compressed_size,
33
 
    inventory as _mod_inventory,
34
 
    inventory_delta,
 
30
    graph,
35
31
    osutils,
36
32
    pack,
37
33
    trace,
38
34
    ui,
39
 
    vf_search,
40
35
    )
41
36
from bzrlib.bzrdir import BzrDir
42
37
from bzrlib.smart.request import (
47
42
from bzrlib.repository import _strip_NULL_ghosts, network_format_registry
48
43
from bzrlib import revision as _mod_revision
49
44
from bzrlib.versionedfile import (
50
 
    ChunkedContentFactory,
51
45
    NetworkRecordStream,
52
46
    record_to_fulltext_bytes,
53
47
    )
90
84
            they expected and get it from elsewhere.
91
85
        """
92
86
        if search_bytes == 'everything':
93
 
            return vf_search.EverythingResult(repository), None
 
87
            return graph.EverythingResult(repository), None
94
88
        lines = search_bytes.split('\n')
95
89
        if lines[0] == 'ancestry-of':
96
90
            heads = lines[1:]
97
 
            search_result = vf_search.PendingAncestryResult(heads, repository)
 
91
            search_result = graph.PendingAncestryResult(heads, repository)
98
92
            return search_result, None
99
93
        elif lines[0] == 'search':
100
94
            return self.recreate_search_from_recipe(repository, lines[1:],
124
118
                except StopIteration:
125
119
                    break
126
120
                search.stop_searching_any(exclude_keys.intersection(next_revs))
127
 
            (started_keys, excludes, included_keys) = search.get_state()
128
 
            if (not discard_excess and len(included_keys) != revision_count):
 
121
            search_result = search.get_result()
 
122
            if (not discard_excess and
 
123
                search_result.get_recipe()[3] != revision_count):
129
124
                # we got back a different amount of data than expected, this
130
125
                # gets reported as NoSuchRevision, because less revisions
131
126
                # indicates missing revisions, and more should never happen as
132
127
                # the excludes list considers ghosts and ensures that ghost
133
128
                # filling races are not a problem.
134
129
                return (None, FailedSmartServerResponse(('NoSuchRevision',)))
135
 
            search_result = vf_search.SearchResult(started_keys, excludes,
136
 
                len(included_keys), included_keys)
137
130
            return (search_result, None)
138
131
        finally:
139
132
            repository.unlock()
442
435
        return SuccessfulSmartServerResponse(('ok', ), body)
443
436
 
444
437
 
445
 
class SmartServerRepositoryGetRevisionSignatureText(
446
 
        SmartServerRepositoryRequest):
447
 
    """Return the signature text of a revision.
448
 
 
449
 
    New in 2.5.
450
 
    """
451
 
 
452
 
    def do_repository_request(self, repository, revision_id):
453
 
        """Return the result of repository.get_signature_text().
454
 
 
455
 
        :param repository: The repository to query in.
456
 
        :return: A smart server response of with the signature text as
457
 
            body.
458
 
        """
459
 
        try:
460
 
            text = repository.get_signature_text(revision_id)
461
 
        except errors.NoSuchRevision, err:
462
 
            return FailedSmartServerResponse(
463
 
                ('nosuchrevision', err.revision))
464
 
        return SuccessfulSmartServerResponse(('ok', ), text)
465
 
 
466
 
 
467
438
class SmartServerRepositoryIsShared(SmartServerRepositoryRequest):
468
439
 
469
440
    def do_repository_request(self, repository):
963
934
        self.do_insert_stream_request(repository, resume_tokens)
964
935
 
965
936
 
966
 
class SmartServerRepositoryAddSignatureText(SmartServerRepositoryRequest):
967
 
    """Add a revision signature text.
968
 
 
969
 
    New in 2.5.
970
 
    """
971
 
 
972
 
    def do_repository_request(self, repository, lock_token, revision_id,
973
 
            *write_group_tokens):
974
 
        """Add a revision signature text.
975
 
 
976
 
        :param repository: Repository to operate on
977
 
        :param lock_token: Lock token
978
 
        :param revision_id: Revision for which to add signature
979
 
        :param write_group_tokens: Write group tokens
980
 
        """
981
 
        self._lock_token = lock_token
982
 
        self._revision_id = revision_id
983
 
        self._write_group_tokens = write_group_tokens
984
 
        return None
985
 
 
986
 
    def do_body(self, body_bytes):
987
 
        """Add a signature text.
988
 
 
989
 
        :param body_bytes: GPG signature text
990
 
        :return: SuccessfulSmartServerResponse with arguments 'ok' and
991
 
            the list of new write group tokens.
992
 
        """
993
 
        self._repository.lock_write(token=self._lock_token)
994
 
        try:
995
 
            self._repository.resume_write_group(self._write_group_tokens)
996
 
            try:
997
 
                self._repository.add_signature_text(self._revision_id,
998
 
                    body_bytes)
999
 
            finally:
1000
 
                new_write_group_tokens = self._repository.suspend_write_group()
1001
 
        finally:
1002
 
            self._repository.unlock()
1003
 
        return SuccessfulSmartServerResponse(
1004
 
            ('ok', ) + tuple(new_write_group_tokens))
1005
 
 
1006
 
 
1007
937
class SmartServerRepositoryStartWriteGroup(SmartServerRepositoryRequest):
1008
938
    """Start a write group.
1009
939
 
1094
1024
            repository.unlock()
1095
1025
        return SuccessfulSmartServerResponse(('ok', ))
1096
1026
 
1097
 
 
1098
1027
class SmartServerRepositoryAllRevisionIds(SmartServerRepositoryRequest):
1099
1028
    """Retrieve all of the revision ids in a repository.
1100
1029
 
1104
1033
    def do_repository_request(self, repository):
1105
1034
        revids = repository.all_revision_ids()
1106
1035
        return SuccessfulSmartServerResponse(("ok", ), "\n".join(revids))
1107
 
 
1108
 
 
1109
 
class SmartServerRepositoryReconcile(SmartServerRepositoryRequest):
1110
 
    """Reconcile a repository.
1111
 
 
1112
 
    New in 2.5.
1113
 
    """
1114
 
 
1115
 
    def do_repository_request(self, repository, lock_token):
1116
 
        try:
1117
 
            repository.lock_write(token=lock_token)
1118
 
        except errors.TokenLockingNotSupported, e:
1119
 
            return FailedSmartServerResponse(
1120
 
                ('TokenLockingNotSupported', ))
1121
 
        try:
1122
 
            reconciler = repository.reconcile()
1123
 
        finally:
1124
 
            repository.unlock()
1125
 
        body = [
1126
 
            "garbage_inventories: %d\n" % reconciler.garbage_inventories,
1127
 
            "inconsistent_parents: %d\n" % reconciler.inconsistent_parents,
1128
 
            ]
1129
 
        return SuccessfulSmartServerResponse(('ok', ), "".join(body))
1130
 
 
1131
 
 
1132
 
class SmartServerRepositoryPack(SmartServerRepositoryRequest):
1133
 
    """Pack a repository.
1134
 
 
1135
 
    New in 2.5.
1136
 
    """
1137
 
 
1138
 
    def do_repository_request(self, repository, lock_token, clean_obsolete_packs):
1139
 
        self._repository = repository
1140
 
        self._lock_token = lock_token
1141
 
        if clean_obsolete_packs == 'True':
1142
 
            self._clean_obsolete_packs = True
1143
 
        else:
1144
 
            self._clean_obsolete_packs = False
1145
 
        return None
1146
 
 
1147
 
    def do_body(self, body_bytes):
1148
 
        if body_bytes == "":
1149
 
            hint = None
1150
 
        else:
1151
 
            hint = body_bytes.splitlines()
1152
 
        self._repository.lock_write(token=self._lock_token)
1153
 
        try:
1154
 
            self._repository.pack(hint, self._clean_obsolete_packs)
1155
 
        finally:
1156
 
            self._repository.unlock()
1157
 
        return SuccessfulSmartServerResponse(("ok", ), )
1158
 
 
1159
 
 
1160
 
class SmartServerRepositoryIterFilesBytes(SmartServerRepositoryRequest):
1161
 
    """Iterate over the contents of files.
1162
 
 
1163
 
    The client sends a list of desired files to stream, one
1164
 
    per line, and as tuples of file id and revision, separated by
1165
 
    \0.
1166
 
 
1167
 
    The server replies with a stream. Each entry is preceded by a header,
1168
 
    which can either be:
1169
 
 
1170
 
    * "ok\x00IDX\n" where IDX is the index of the entry in the desired files
1171
 
        list sent by the client. This header is followed by the contents of
1172
 
        the file, bzip2-compressed.
1173
 
    * "absent\x00FILEID\x00REVISION\x00IDX" to indicate a text is missing.
1174
 
        The client can then raise an appropriate RevisionNotPresent error
1175
 
        or check its fallback repositories.
1176
 
 
1177
 
    New in 2.5.
1178
 
    """
1179
 
 
1180
 
    def body_stream(self, repository, desired_files):
1181
 
        self._repository.lock_read()
1182
 
        try:
1183
 
            text_keys = {}
1184
 
            for i, key in enumerate(desired_files):
1185
 
                text_keys[key] = i
1186
 
            for record in repository.texts.get_record_stream(text_keys,
1187
 
                    'unordered', True):
1188
 
                identifier = text_keys[record.key]
1189
 
                if record.storage_kind == 'absent':
1190
 
                    yield "absent\0%s\0%s\0%d\n" % (record.key[0],
1191
 
                        record.key[1], identifier)
1192
 
                    # FIXME: Way to abort early?
1193
 
                    continue
1194
 
                yield "ok\0%d\n" % identifier
1195
 
                compressor = zlib.compressobj()
1196
 
                for bytes in record.get_bytes_as('chunked'):
1197
 
                    data = compressor.compress(bytes)
1198
 
                    if data:
1199
 
                        yield data
1200
 
                data = compressor.flush()
1201
 
                if data:
1202
 
                    yield data
1203
 
        finally:
1204
 
            self._repository.unlock()
1205
 
 
1206
 
    def do_body(self, body_bytes):
1207
 
        desired_files = [
1208
 
            tuple(l.split("\0")) for l in body_bytes.splitlines()]
1209
 
        return SuccessfulSmartServerResponse(('ok', ),
1210
 
            body_stream=self.body_stream(self._repository, desired_files))
1211
 
 
1212
 
    def do_repository_request(self, repository):
1213
 
        # Signal that we want a body
1214
 
        return None
1215
 
 
1216
 
 
1217
 
class SmartServerRepositoryIterRevisions(SmartServerRepositoryRequest):
1218
 
    """Stream a list of revisions.
1219
 
 
1220
 
    The client sends a list of newline-separated revision ids in the
1221
 
    body of the request and the server replies with the serializer format,
1222
 
    and a stream of bzip2-compressed revision texts (using the specified
1223
 
    serializer format).
1224
 
 
1225
 
    Any revisions the server does not have are omitted from the stream.
1226
 
 
1227
 
    New in 2.5.
1228
 
    """
1229
 
 
1230
 
    def do_repository_request(self, repository):
1231
 
        self._repository = repository
1232
 
        # Signal there is a body
1233
 
        return None
1234
 
 
1235
 
    def do_body(self, body_bytes):
1236
 
        revision_ids = body_bytes.split("\n")
1237
 
        return SuccessfulSmartServerResponse(
1238
 
            ('ok', self._repository.get_serializer_format()),
1239
 
            body_stream=self.body_stream(self._repository, revision_ids))
1240
 
 
1241
 
    def body_stream(self, repository, revision_ids):
1242
 
        self._repository.lock_read()
1243
 
        try:
1244
 
            for record in repository.revisions.get_record_stream(
1245
 
                [(revid,) for revid in revision_ids], 'unordered', True):
1246
 
                if record.storage_kind == 'absent':
1247
 
                    continue
1248
 
                yield zlib.compress(record.get_bytes_as('fulltext'))
1249
 
        finally:
1250
 
            self._repository.unlock()
1251
 
 
1252
 
 
1253
 
class SmartServerRepositoryGetInventories(SmartServerRepositoryRequest):
1254
 
    """Get the inventory deltas for a set of revision ids.
1255
 
 
1256
 
    This accepts a list of revision ids, and then sends a chain
1257
 
    of deltas for the inventories of those revisions. The first
1258
 
    revision will be empty.
1259
 
 
1260
 
    The server writes back zlibbed serialized inventory deltas,
1261
 
    in the ordering specified. The base for each delta is the
1262
 
    inventory generated by the previous delta.
1263
 
 
1264
 
    New in 2.5.
1265
 
    """
1266
 
 
1267
 
    def _inventory_delta_stream(self, repository, ordering, revids):
1268
 
        prev_inv = _mod_inventory.Inventory(root_id=None,
1269
 
            revision_id=_mod_revision.NULL_REVISION)
1270
 
        serializer = inventory_delta.InventoryDeltaSerializer(
1271
 
            repository.supports_rich_root(),
1272
 
            repository._format.supports_tree_reference)
1273
 
        repository.lock_read()
1274
 
        try:
1275
 
            for inv, revid in repository._iter_inventories(revids, ordering):
1276
 
                if inv is None:
1277
 
                    continue
1278
 
                inv_delta = inv._make_delta(prev_inv)
1279
 
                lines = serializer.delta_to_lines(
1280
 
                    prev_inv.revision_id, inv.revision_id, inv_delta)
1281
 
                yield ChunkedContentFactory(inv.revision_id, None, None, lines)
1282
 
                prev_inv = inv
1283
 
        finally:
1284
 
            repository.unlock()
1285
 
 
1286
 
    def body_stream(self, repository, ordering, revids):
1287
 
        substream = self._inventory_delta_stream(repository,
1288
 
            ordering, revids)
1289
 
        return _stream_to_byte_stream([('inventory-deltas', substream)],
1290
 
            repository._format)
1291
 
 
1292
 
    def do_body(self, body_bytes):
1293
 
        return SuccessfulSmartServerResponse(('ok', ),
1294
 
            body_stream=self.body_stream(self._repository, self._ordering,
1295
 
                body_bytes.splitlines()))
1296
 
 
1297
 
    def do_repository_request(self, repository, ordering):
1298
 
        if ordering == 'unordered':
1299
 
            # inventory deltas for a topologically sorted stream
1300
 
            # are likely to be smaller
1301
 
            ordering = 'topological'
1302
 
        self._ordering = ordering
1303
 
        # Signal that we want a body
1304
 
        return None