48
50
transport = self._backing_transport.clone(path)
49
51
bzrdir = BzrDir.open_from_transport(transport)
50
repository = bzrdir.open_repository()
51
return self.do_repository_request(repository, *args)
52
# Save the repository for use with do_body.
53
self._repository = bzrdir.open_repository()
54
return self.do_repository_request(self._repository, *args)
56
def do_repository_request(self, repository, *args):
57
"""Override to provide an implementation for a verb."""
58
# No-op for verbs that take bodies (None as a result indicates a body
62
def recreate_search(self, repository, recipe_bytes):
63
lines = recipe_bytes.split('\n')
64
start_keys = set(lines[0].split(' '))
65
exclude_keys = set(lines[1].split(' '))
66
revision_count = int(lines[2])
67
repository.lock_read()
69
search = repository.get_graph()._make_breadth_first_searcher(
73
next_revs = search.next()
76
search.stop_searching_any(exclude_keys.intersection(next_revs))
77
search_result = search.get_result()
78
if search_result.get_recipe()[2] != revision_count:
79
# we got back a different amount of data than expected, this
80
# gets reported as NoSuchRevision, because less revisions
81
# indicates missing revisions, and more should never happen as
82
# the excludes list considers ghosts and ensures that ghost
83
# filling races are not a problem.
84
return (None, FailedSmartServerResponse(('NoSuchRevision',)))
90
class SmartServerRepositoryGetParentMap(SmartServerRepositoryRequest):
91
"""Bzr 1.2+ - get parent data for revisions during a graph search."""
93
def do_repository_request(self, repository, *revision_ids):
94
"""Get parent details for some revisions.
96
All the parents for revision_ids are returned. Additionally up to 64KB
97
of additional parent data found by performing a breadth first search
98
from revision_ids is returned. The verb takes a body containing the
99
current search state, see do_body for details.
101
:param repository: The repository to query in.
102
:param revision_ids: The utf8 encoded revision_id to answer for.
104
self._revision_ids = revision_ids
105
return None # Signal that we want a body.
107
def do_body(self, body_bytes):
108
"""Process the current search state and perform the parent lookup.
110
:return: A smart server response where the body contains an utf8
111
encoded flattened list of the parents of the revisions (the same
112
format as Repository.get_revision_graph) which has been bz2
115
repository = self._repository
116
repository.lock_read()
118
return self._do_repository_request(body_bytes)
122
def _do_repository_request(self, body_bytes):
123
repository = self._repository
124
revision_ids = set(self._revision_ids)
125
search, error = self.recreate_search(repository, body_bytes)
126
if error is not None:
128
# TODO might be nice to start up the search again; but thats not
129
# written or tested yet.
130
client_seen_revs = set(search.get_result().get_keys())
131
# Always include the requested ids.
132
client_seen_revs.difference_update(revision_ids)
134
repo_graph = repository.get_graph()
138
next_revs = revision_ids
139
first_loop_done = False
141
queried_revs.update(next_revs)
142
parent_map = repo_graph.get_parent_map(next_revs)
144
for revision_id, parents in parent_map.iteritems():
145
# adjust for the wire
146
if parents == (_mod_revision.NULL_REVISION,):
148
# prepare the next query
149
next_revs.update(parents)
150
if revision_id not in client_seen_revs:
151
# Client does not have this revision, give it to it.
152
# add parents to the result
153
result[revision_id] = parents
154
# Approximate the serialized cost of this revision_id.
155
size_so_far += 2 + len(revision_id) + sum(map(len, parents))
156
# get all the directly asked for parents, and then flesh out to
157
# 64K (compressed) or so. We do one level of depth at a time to
158
# stay in sync with the client. The 250000 magic number is
159
# estimated compression ratio taken from bzr.dev itself.
160
if first_loop_done and size_so_far > 250000:
163
# don't query things we've already queried
164
next_revs.difference_update(queried_revs)
165
first_loop_done = True
167
# sorting trivially puts lexographically similar revision ids together.
169
for revision, parents in sorted(result.items()):
170
lines.append(' '.join((revision, ) + tuple(parents)))
172
return SuccessfulSmartServerResponse(
173
('ok', ), bz2.compress('\n'.join(lines)))
54
176
class SmartServerRepositoryGetRevisionGraph(SmartServerRepositoryRequest):
249
372
class SmartServerRepositoryStreamKnitDataForRevisions(SmartServerRepositoryRequest):
373
"""Bzr <= 1.1 streaming pull, buffers all data on server."""
251
375
def do_repository_request(self, repository, *revision_ids):
252
stream = repository.get_data_stream(revision_ids)
253
filelike = StringIO()
254
pack = ContainerWriter(filelike.write)
376
repository.lock_read()
378
return self._do_repository_request(repository, revision_ids)
382
def _do_repository_request(self, repository, revision_ids):
383
stream = repository.get_data_stream_for_search(
384
repository.revision_ids_to_search_result(set(revision_ids)))
386
pack = ContainerSerialiser()
387
buffer.write(pack.begin())
257
389
for name_tuple, bytes in stream:
258
pack.add_bytes_record(bytes, [name_tuple])
390
buffer.write(pack.bytes_record(bytes, [name_tuple]))
259
391
except errors.RevisionNotPresent, e:
260
392
return FailedSmartServerResponse(('NoSuchRevision', e.revision_id))
393
buffer.write(pack.end())
394
return SuccessfulSmartServerResponse(('ok',), buffer.getvalue())
397
class SmartServerRepositoryStreamRevisionsChunked(SmartServerRepositoryRequest):
398
"""Bzr 1.1+ streaming pull."""
400
def do_body(self, body_bytes):
401
repository = self._repository
402
repository.lock_read()
404
search, error = self.recreate_search(repository, body_bytes)
405
if error is not None:
407
stream = repository.get_data_stream_for_search(search.get_result())
409
# On non-error, unlocking is done by the body stream handler.
412
return SuccessfulSmartServerResponse(('ok',),
413
body_stream=self.body_stream(stream, repository))
415
def body_stream(self, stream, repository):
416
pack = ContainerSerialiser()
419
for name_tuple, bytes in stream:
420
yield pack.bytes_record(bytes, [name_tuple])
421
except errors.RevisionNotPresent, e:
422
# This shouldn't be able to happen, but as we don't buffer
423
# everything it can in theory happen.
424
yield FailedSmartServerResponse(('NoSuchRevision', e.revision_id))
262
return SuccessfulSmartServerResponse(('ok',), filelike.getvalue())