~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/search.py

  • Committer: Matt Nordhoff
  • Date: 2009-11-27 18:30:24 UTC
  • Revision ID: mnordhoff@mattnordhoff.com-20091127183024-bmrr3ffaysmbfb19
In which Matthew fails at exit codes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (C) 2008  Canonical Ltd.
 
3
#                     (Authored by Martin Albisetti <argentina@gmail.com>)
 
4
# Copyright (C) 2008  Robert Collins
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
#
 
20
 
 
21
_mod_index = None
 
22
def import_search():
 
23
    global errors, _mod_index, FileTextHit, RevisionHit
 
24
    if _mod_index is not None:
 
25
        return
 
26
    try:
 
27
        from bzrlib.plugins.search import errors
 
28
        from bzrlib.plugins.search import index as _mod_index
 
29
        from bzrlib.plugins.search.index import FileTextHit, RevisionHit
 
30
    except ImportError:
 
31
        _mod_index = None
 
32
 
 
33
 
 
34
def search_revisions(branch, query_list, suggest=False):
 
35
    """
 
36
    Search using bzr-search plugin to find revisions matching the query.
 
37
    This can either suggest query terms, or revision ids.
 
38
 
 
39
    param branch: branch object to search in
 
40
    param query_list: string to search
 
41
    param suggest: Optional flag to request suggestions instead of results
 
42
    return: A list for results, either revision ids or terms
 
43
    """
 
44
    import_search()
 
45
    if _mod_index is None:
 
46
        return None # None indicates could-not-search
 
47
    try:
 
48
        index = _mod_index.open_index_branch(branch)
 
49
    except errors.NoSearchIndex:
 
50
        return None # None indicates could-not-search
 
51
    query = query_list.split(' ')
 
52
    query = [(term,) for term in query]
 
53
    revid_list = []
 
54
    index._branch.lock_read()
 
55
 
 
56
    try:
 
57
        if suggest:
 
58
            terms = index.suggest(query)
 
59
            terms = list(terms)
 
60
            terms.sort()
 
61
            return terms
 
62
        else:
 
63
            for result in index.search(query):
 
64
                if isinstance(result, FileTextHit):
 
65
                    revid_list.append(result.text_key[1])
 
66
                elif isinstance(result, RevisionHit):
 
67
                    revid_list.append(result.revision_key[0])
 
68
            return list(set(revid_list))
 
69
    finally:
 
70
        index._branch.unlock()