summaryrefslogtreecommitdiff
path: root/src/mongo/db/index/2d_access_method.cpp
blob: 1d69796833ff888288f85bee5ef0ae8cd07bd34b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
*    Copyright (C) 2013 10gen Inc.
*
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mongo/db/index/2d_access_method.h"

#include <string>
#include <vector>

#include "mongo/db/geo/core.h"
#include "mongo/db/index_names.h"
#include "mongo/db/index/2d_common.h"
#include "mongo/db/index/2d_index_cursor.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/pdfile.h"

namespace mongo {

    static double configValueWithDefault(IndexDescriptor *desc, const string& name, double def) {
        BSONElement e = desc->getInfoElement(name);
        if (e.isNumber()) { return e.numberDouble(); }
        return def;
    }

    TwoDAccessMethod::TwoDAccessMethod(IndexDescriptor* descriptor)
        : BtreeBasedAccessMethod(descriptor) {

        BSONObjIterator i(descriptor->keyPattern());
        while (i.more()) {
            BSONElement e = i.next();
            if (e.type() == String && IndexNames::GEO_2D == e.valuestr()) {
                uassert(16800, "can't have 2 geo fields", _params.geo.size() == 0);
                uassert(16801, "2d has to be first in index", _params.other.size() == 0);
                _params.geo = e.fieldName();
            } else {
                int order = 1;
                if (e.isNumber()) {
                    order = static_cast<int>(e.Number());
                }
                _params.other.push_back(make_pair(e.fieldName(), order));
            }
        }
        uassert(16802, "no geo field specified", _params.geo.size());

        double bits =  configValueWithDefault(_descriptor, "bits", 26);  // for lat/long, ~ 1ft
        uassert(16803, "bits in geo index must be between 1 and 32", bits > 0 && bits <= 32);

        GeoHashConverter::Parameters params;
        params.bits = static_cast<unsigned>(bits);
        params.max = configValueWithDefault(_descriptor, "max", 180.0);
        params.min = configValueWithDefault(_descriptor, "min", -180.0);
        double numBuckets = (1024 * 1024 * 1024 * 4.0);
        params.scaling = numBuckets / (params.max - params.min);

        _params.geoHashConverter.reset(new GeoHashConverter(params));

        BSONObjBuilder b;
        b.appendNull("");
        _nullObj = b.obj();
        _nullElt = _nullObj.firstElement();
    }

    /** Finds the key objects to put in an index */
    void TwoDAccessMethod::getKeys(const BSONObj& obj, BSONObjSet* keys) {
        getKeys(obj, keys, NULL);
    }

    /** Finds all locations in a geo-indexed object */
    void TwoDAccessMethod::getKeys(const BSONObj& obj, vector<BSONObj>& locs) const {
        getKeys(obj, NULL, &locs);
    }

    /** Finds the key objects and/or locations for a geo-indexed object */
    void TwoDAccessMethod::getKeys(const BSONObj &obj, BSONObjSet* keys,
                                   vector<BSONObj>* locs) const {
        BSONElementMSet bSet;

        // Get all the nested location fields, but don't return individual elements from
        // the last array, if it exists.
        obj.getFieldsDotted(_params.geo.c_str(), bSet, false);

        if (bSet.empty())
            return;

        for (BSONElementMSet::iterator setI = bSet.begin(); setI != bSet.end(); ++setI) {
            BSONElement geo = *setI;

            GEODEBUG("Element " << geo << " found for query " << _geo.c_str());

            if (geo.eoo() || !geo.isABSONObj())
                continue;

            //
            // Grammar for location lookup:
            // locs ::= [loc,loc,...,loc]|{<k>:loc,<k>:loc,...,<k>:loc}|loc
            // loc  ::= { <k1> : #, <k2> : # }|[#, #]|{}
            //
            // Empty locations are ignored, preserving single-location semantics
            //

            BSONObj embed = geo.embeddedObject();
            if (embed.isEmpty())
                continue;

            // Differentiate between location arrays and locations
            // by seeing if the first element value is a number
            bool singleElement = embed.firstElement().isNumber();

            BSONObjIterator oi(embed);

            while (oi.more()) {
                BSONObj locObj;

                if (singleElement) {
                    locObj = embed;
                } else {
                    BSONElement locElement = oi.next();

                    uassert(16804, str::stream() << "location object expected, location "
                                                "array not in correct format",
                            locElement.isABSONObj());

                    locObj = locElement.embeddedObject();
                    if(locObj.isEmpty())
                        continue;
                }

                BSONObjBuilder b(64);

                // Remember the actual location object if needed
                if (locs)
                    locs->push_back(locObj);

                // Stop if we don't need to get anything but location objects
                if (!keys) {
                    if (singleElement) break;
                    else continue;
                }

                _params.geoHashConverter->hash(locObj, &obj).appendToBuilder(&b, "");
   
                // Go through all the other index keys
                for (vector<pair<string, int> >::const_iterator i = _params.other.begin();
                     i != _params.other.end(); ++i) {
                    // Get *all* fields for the index key
                    BSONElementSet eSet;
                    obj.getFieldsDotted(i->first, eSet);

                    if (eSet.size() == 0)
                        b.appendAs(_nullElt, "");
                    else if (eSet.size() == 1)
                        b.appendAs(*(eSet.begin()), "");
                    else {
                        // If we have more than one key, store as an array of the objects
                        BSONArrayBuilder aBuilder;

                        for (BSONElementSet::iterator ei = eSet.begin(); ei != eSet.end();
                             ++ei) {
                            aBuilder.append(*ei);
                        }

                        b.append("", aBuilder.arr());
                    }
                }
                keys->insert(b.obj());
                if(singleElement) break;
            }
        }
    }

    Status TwoDAccessMethod::newCursor(IndexCursor** out) {
        *out = new TwoDIndexCursor(this);
        return Status::OK();
    }

}  // namespace mongo