~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to src/mongo/db/fts/fts_query.cpp

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// fts_query.cpp
 
2
 
 
3
/**
 
4
*    Copyright (C) 2012 10gen Inc.
 
5
*
 
6
*    This program is free software: you can redistribute it and/or  modify
 
7
*    it under the terms of the GNU Affero General Public License, version 3,
 
8
*    as published by the Free Software Foundation.
 
9
*
 
10
*    This program is distributed in the hope that it will be useful,
 
11
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
*    GNU Affero General Public License for more details.
 
14
*
 
15
*    You should have received a copy of the GNU Affero General Public License
 
16
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
#include "mongo/pch.h"
 
20
 
 
21
#include "mongo/db/fts/fts_query.h"
 
22
#include "mongo/db/fts/tokenizer.h"
 
23
#include "mongo/util/mongoutils/str.h"
 
24
#include "mongo/util/stringutils.h"
 
25
 
 
26
namespace mongo {
 
27
 
 
28
    namespace fts {
 
29
 
 
30
        using namespace mongoutils;
 
31
 
 
32
        Status FTSQuery::parse(const string& query, const string& language) {
 
33
            _search = query;
 
34
            _language = language;
 
35
 
 
36
            const StopWords* stopWords = StopWords::getStopWords( language );
 
37
            Stemmer stemmer( language );
 
38
 
 
39
            bool inNegation = false;
 
40
            bool inPhrase = false;
 
41
 
 
42
            unsigned quoteOffset = 0;
 
43
 
 
44
            Tokenizer i( _language, query );
 
45
            while ( i.more() ) {
 
46
                Token t = i.next();
 
47
 
 
48
                if ( t.type == Token::TEXT ) {
 
49
                    string s = t.data.toString();
 
50
 
 
51
                    if ( inPhrase && inNegation ) {
 
52
                        // don't add term
 
53
                    }
 
54
                    else {
 
55
                        _addTerm( stopWords, stemmer, s, inNegation );
 
56
                    }
 
57
 
 
58
                    if ( inNegation && !inPhrase )
 
59
                        inNegation = false;
 
60
                }
 
61
                else if ( t.type == Token::DELIMITER ) {
 
62
                    char c = t.data[0];
 
63
                    if ( c == '-' ) {
 
64
                        if ( t.previousWhiteSpace )
 
65
                            inNegation = true;
 
66
                    }
 
67
                    else if ( c == '"' ) {
 
68
                        if ( inPhrase ) {
 
69
                            // end of a phrase
 
70
                            unsigned phraseStart = quoteOffset + 1;
 
71
                            unsigned phraseLength = t.offset - phraseStart;
 
72
                            StringData phrase = StringData( query ).substr( phraseStart,
 
73
                                                                            phraseLength );
 
74
                            if ( inNegation )
 
75
                                _negatedPhrases.push_back( tolowerString( phrase ) );
 
76
                            else
 
77
                                _phrases.push_back( tolowerString( phrase ) );
 
78
                            inNegation = false;
 
79
                            inPhrase = false;
 
80
                        }
 
81
                        else {
 
82
                            // start of a phrase
 
83
                            inPhrase = true;
 
84
                            quoteOffset = t.offset;
 
85
                        }
 
86
                    }
 
87
                }
 
88
                else {
 
89
                    abort();
 
90
                }
 
91
            }
 
92
 
 
93
            return Status::OK();
 
94
        }
 
95
 
 
96
        void FTSQuery::_addTerm( const StopWords* sw, Stemmer& stemmer, const string& term, bool negated ) {
 
97
            string word = tolowerString( term );
 
98
            if ( sw->isStopWord( word ) )
 
99
                return;
 
100
            word = stemmer.stem( word );
 
101
            if ( negated )
 
102
                _negatedTerms.insert( word );
 
103
            else
 
104
                _terms.push_back( word );
 
105
        }
 
106
 
 
107
        namespace {
 
108
            void _debugHelp( stringstream& ss, const set<string>& s, const string& sep ) {
 
109
                bool first = true;
 
110
                for ( set<string>::const_iterator i = s.begin(); i != s.end(); ++i ) {
 
111
                    if ( first )
 
112
                        first = false;
 
113
                    else
 
114
                        ss << sep;
 
115
                    ss << *i;
 
116
                }
 
117
            }
 
118
 
 
119
            void _debugHelp( stringstream& ss, const vector<string>& v, const string& sep ) {
 
120
                set<string> s( v.begin(), v.end() );
 
121
                _debugHelp( ss, s, sep );
 
122
            }
 
123
 
 
124
            void _debugHelp( stringstream& ss, const unordered_set<string>& v, const string& sep ) {
 
125
                set<string> s( v.begin(), v.end() );
 
126
                _debugHelp( ss, s, sep );
 
127
            }
 
128
 
 
129
        }
 
130
 
 
131
        string FTSQuery::toString() const {
 
132
            stringstream ss;
 
133
            ss << "FTSQuery\n";
 
134
 
 
135
            ss << "  terms: ";
 
136
            _debugHelp( ss, getTerms(), ", " );
 
137
            ss << "\n";
 
138
 
 
139
            ss << "  negated terms: ";
 
140
            _debugHelp( ss, getNegatedTerms(), ", " );
 
141
            ss << "\n";
 
142
 
 
143
            ss << "  phrases: ";
 
144
            _debugHelp( ss, getPhr(), ", " );
 
145
            ss << "\n";
 
146
 
 
147
            ss << "  negated phrases: ";
 
148
            _debugHelp( ss, getNegatedPhr(), ", " );
 
149
            ss << "\n";
 
150
 
 
151
            return ss.str();
 
152
        }
 
153
 
 
154
        string FTSQuery::debugString() const {
 
155
            stringstream ss;
 
156
 
 
157
            _debugHelp( ss, getTerms(), "|" );
 
158
            ss << "||";
 
159
 
 
160
            _debugHelp( ss, getNegatedTerms(), "|" );
 
161
            ss << "||";
 
162
 
 
163
            _debugHelp( ss, getPhr(), "|" );
 
164
            ss << "||";
 
165
 
 
166
            _debugHelp( ss, getNegatedPhr(), "|" );
 
167
 
 
168
            return ss.str();
 
169
        }
 
170
    }
 
171
}