~jelmer/bzr-search/hpss

« back to all changes in this revision

Viewing changes to remote.py

  • Committer: Jelmer Vernooij
  • Date: 2011-11-23 20:51:29 UTC
  • Revision ID: jelmer@samba.org-20111123205129-v15ef7vgkowp43g4
Add Index.search.

Show diffs side-by-side

added added

removed removed

Lines of Context:
138
138
        :return: An iterator of SearchResults for documents indexed by all
139
139
            terms in the termlist.
140
140
        """
141
 
        raise NotImplementedError(self.search)
 
141
        response, handler = self._call_expecting_body('Index.search', self._path,
 
142
            [term.encode('utf-8') for (term, ) in termlist])
 
143
        if response != ('ok', ):
 
144
            raise errors.UnexpectedSmartServerResponse(response)
 
145
        byte_stream = handler.read_streamed_body()
 
146
        data = ""
 
147
        for bytes in byte_stream:
 
148
            data += bytes
 
149
            lines = data.split("\n")
 
150
            data = lines.pop()
 
151
            for l in lines:
 
152
                if l[0] == 'r':
 
153
                    yield index.RevisionHit(self._branch.repository, (l[1:], ))
 
154
                elif l[0] == 't':
 
155
                    yield index.FileTextHit(self, self._branch.repository,
 
156
                        tuple(l[1:].split("\0")), termlist)
 
157
                elif l[0] == 'p':
 
158
                    yield index.PathHit(l[1:])
 
159
                else:
 
160
                    raise AssertionError("Unknown hit kind %r" % l[0])
142
161
 
143
162
    def suggest(self, termlist):
144
163
        """Generate suggestions for extending a search.
227
246
        return SuccessfulSmartServerResponse(
228
247
            ('ok',
229
248
                [suggestion.encode('utf-8') for (suggestion,) in suggestions]))
 
249
 
 
250
 
 
251
class SmartServerIndexRequestSearch(SmartServerIndexRequest):
 
252
    """Search for terms."""
 
253
 
 
254
    def body_stream(self, results):
 
255
        for hit in results:
 
256
            if isinstance(hit, index.FileTextHit):
 
257
                yield "r%s\0%s" % hit.text_key
 
258
            elif isinstance(hit, index.RevisionHit):
 
259
                yield "r%s\n" % hit.revision_key[0]
 
260
            elif isinstance(hit, index.PathHit):
 
261
                yield "p%s\n" % hit.path_utf8
 
262
            else:
 
263
                raise AssertionError("Unknown hit type %r" % hit)
 
264
 
 
265
    def do_with_index(self, index, termlist):
 
266
        termlist = [(term.decode('utf-8'), ) for term in termlist]
 
267
        results = index.search(termlist)
 
268
        return SuccessfulSmartServerResponse(
 
269
            ('ok',), body_stream=self.body_stream(results))