~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/search.py

add a lightly hacked copy of turbozpt (it's not much code)

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
 
import sets
21
 
import os
22
 
try:
23
 
    from bzrlib.plugins.search import errors
24
 
    from bzrlib.plugins.search import index as _mod_index
25
 
    from bzrlib.plugins.search.index import FileTextHit, RevisionHit
26
 
except ImportError:
27
 
    _mod_index = None
28
 
 
29
 
def search_revisions(branch, query_list, suggest=False):
30
 
    """
31
 
    Search using bzr-search plugin to find revisions matching the query.
32
 
    This can either suggest query terms, or revision ids.
33
 
    
34
 
    param branch: branch object to search in
35
 
    param query_list: string to search
36
 
    param suggest: Optional flag to request suggestions instead of results
37
 
    return: A list for results, either revision ids or terms
38
 
    """
39
 
    if _mod_index is None:
40
 
        return None # None indicates could-not-search
41
 
    try:
42
 
        index = _mod_index.open_index_branch(branch)
43
 
    except errors.NoSearchIndex:
44
 
        return None # None indicates could-not-search
45
 
    query = query_list.split(' ')
46
 
    query = [(term,) for term in query]
47
 
    revid_list = []
48
 
    index._branch.lock_read()
49
 
 
50
 
    try:
51
 
        if suggest:
52
 
            terms = index.suggest(query)
53
 
            terms = list(terms)
54
 
            terms.sort()
55
 
            return terms
56
 
        else:
57
 
            for result in index.search(query):
58
 
                if isinstance(result, FileTextHit):
59
 
                    revid_list.append(result.text_key[1])
60
 
                elif isinstance(result, RevisionHit):
61
 
                    revid_list.append(result.revision_key[0])
62
 
            return list(sets.Set(revid_list))
63
 
    finally:
64
 
        index._branch.unlock()