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

« back to all changes in this revision

Viewing changes to src/mongo/db/fts/fts_index_format.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_index_format.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/base/init.h"
 
22
#include "mongo/db/fts/fts_index_format.h"
 
23
#include "mongo/util/mongoutils/str.h"
 
24
 
 
25
namespace mongo {
 
26
 
 
27
    namespace fts {
 
28
 
 
29
        namespace {
 
30
            BSONObj nullObj;
 
31
            BSONElement nullElt;
 
32
        }
 
33
 
 
34
        MONGO_INITIALIZER( FTSIndexFormat )( InitializerContext* context ) {
 
35
            BSONObjBuilder b;
 
36
            b.appendNull( "" );
 
37
            nullObj = b.obj();
 
38
            nullElt = nullObj.firstElement();
 
39
            return Status::OK();
 
40
        }
 
41
 
 
42
        void FTSIndexFormat::getKeys( const FTSSpec& spec,
 
43
                                      const BSONObj& obj,
 
44
                                      BSONObjSet* keys ) {
 
45
 
 
46
            int extraSize = 0;
 
47
            vector<BSONElement> extrasBefore;
 
48
            vector<BSONElement> extrasAfter;
 
49
 
 
50
            // compute the non FTS key elements
 
51
            for ( unsigned i = 0; i < spec.numExtraBefore(); i++ ) {
 
52
                BSONElement e = obj.getFieldDotted(spec.extraBefore(i));
 
53
                if ( e.eoo() )
 
54
                    e = nullElt;
 
55
                uassert( 16675, "cannot have a multi-key as a prefix to a text index",
 
56
                         e.type() != Array );
 
57
                extrasBefore.push_back(e);
 
58
                extraSize += e.size();
 
59
            }
 
60
            for ( unsigned i = 0; i < spec.numExtraAfter(); i++ ) {
 
61
                BSONElement e = obj.getFieldDotted(spec.extraAfter(i));
 
62
                if ( e.eoo() )
 
63
                    e = nullElt;
 
64
                extrasAfter.push_back(e);
 
65
                extraSize += e.size();
 
66
            }
 
67
 
 
68
 
 
69
            TermFrequencyMap term_freqs;
 
70
            spec.scoreDocument( obj, &term_freqs );
 
71
 
 
72
            // create index keys from raw scores
 
73
            // only 1 per string
 
74
 
 
75
            uassert( 16732,
 
76
                     mongoutils::str::stream() << "too many unique keys for a single document to"
 
77
                     << " have a text index, max is " << term_freqs.size() << obj["_id"],
 
78
                     term_freqs.size() <= 400000 );
 
79
 
 
80
            long long keyBSONSize = 0;
 
81
            const int MaxKeyBSONSizeMB = 4;
 
82
 
 
83
            for ( TermFrequencyMap::const_iterator i = term_freqs.begin();
 
84
                  i != term_freqs.end();
 
85
                  ++i ) {
 
86
 
 
87
                const string& term = i->first;
 
88
                double weight = i->second;
 
89
 
 
90
                // guess the total size of the btree entry based on the size of the weight, term tuple
 
91
                int guess =
 
92
                    5 /* bson overhead */ +
 
93
                    10 /* weight */ +
 
94
                    8 /* term overhead */ +
 
95
                    term.size() +
 
96
                    extraSize;
 
97
 
 
98
                BSONObjBuilder b(guess); // builds a BSON object with guess length.
 
99
                for ( unsigned k = 0; k < extrasBefore.size(); k++ )
 
100
                    b.appendAs( extrasBefore[k], "" );
 
101
                _appendIndexKey( b, weight, term );
 
102
                for ( unsigned k = 0; k < extrasAfter.size(); k++ )
 
103
                    b.appendAs( extrasAfter[k], "" );
 
104
                BSONObj res = b.obj();
 
105
 
 
106
                verify( guess >= res.objsize() );
 
107
 
 
108
                keys->insert( res );
 
109
 
 
110
                keyBSONSize += res.objsize();
 
111
 
 
112
                uassert( 16733,
 
113
                         mongoutils::str::stream()
 
114
                         << "trying to index text where term list is too big, max is "
 
115
                         << MaxKeyBSONSizeMB << "mb " << obj["_id"],
 
116
                         keyBSONSize <= ( MaxKeyBSONSizeMB * 1024 * 1024 ) );
 
117
 
 
118
            }
 
119
        }
 
120
 
 
121
        BSONObj FTSIndexFormat::getIndexKey( double weight,
 
122
                                             const string& term,
 
123
                                             const BSONObj& indexPrefix ) {
 
124
            BSONObjBuilder b;
 
125
 
 
126
            BSONObjIterator i( indexPrefix );
 
127
            while ( i.more() )
 
128
                b.appendAs( i.next(), "" );
 
129
 
 
130
            _appendIndexKey( b, weight, term );
 
131
            return b.obj();
 
132
        }
 
133
 
 
134
        void FTSIndexFormat::_appendIndexKey( BSONObjBuilder& b, double weight, const string& term ) {
 
135
            verify( weight >= 0 && weight <= MAX_WEIGHT ); // FTSmaxweight =  defined in fts_header
 
136
            b.append( "", term );
 
137
            b.append( "", weight );
 
138
        }
 
139
    }
 
140
}