1
2 /**
3 * Copyright (C) 2018-present MongoDB, Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the Server Side Public License, version 1,
7 * as published by MongoDB, Inc.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * Server Side Public License for more details.
13 *
14 * You should have received a copy of the Server Side Public License
15 * along with this program. If not, see
16 * <http://www.mongodb.com/licensing/server-side-public-license>.
17 *
18 * As a special exception, the copyright holders give permission to link the
19 * code of portions of this program with the OpenSSL library under certain
20 * conditions as described in each individual source file and distribute
21 * linked combinations including the program with the OpenSSL library. You
22 * must comply with the Server Side Public License in all respects for
23 * all of the code used other than as permitted herein. If you modify file(s)
24 * with this exception, you may extend this exception to your version of the
25 * file(s), but you are not obligated to do so. If you do not wish to do so,
26 * delete this exception statement from your version. If you delete this
27 * exception statement from all source files in the program, then also delete
28 * it in the license file.
29 */
30
31 #include "mongo/db/index/s2_common.h"
32
33 #include <cstdlib>
34
35 #include "mongo/bson/bsonobjbuilder.h"
36 #include "mongo/db/geo/geometry_container.h"
37 #include "mongo/db/query/collation/collator_interface.h"
38 #include "third_party/s2/s2cellid.h"
39 #include "third_party/s2/s2regioncoverer.h"
40
41 namespace mongo {
42
43 // Points will only be indexed at this level
44 const int kPointIndexedLevel = S2::kMaxCellLevel;
45
toString() const46 std::string S2IndexingParams::toString() const {
47 std::stringstream ss;
48 ss << "maxKeysPerInsert: " << maxKeysPerInsert << std::endl;
49 ss << "maxCellsInCovering: " << maxCellsInCovering << std::endl;
50 ss << "finestIndexedLevel: " << finestIndexedLevel << std::endl;
51 ss << "coarsestIndexedLevel: " << coarsestIndexedLevel << std::endl;
52 ss << "indexVersion: " << indexVersion << std::endl;
53 if (collator) {
54 ss << "collation: " << collator->getSpec().toBSON() << std::endl;
55 }
56 return ss.str();
57 }
58
configureCoverer(const GeometryContainer & geoContainer,S2RegionCoverer * coverer) const59 void S2IndexingParams::configureCoverer(const GeometryContainer& geoContainer,
60 S2RegionCoverer* coverer) const {
61 // Points indexed to the finest level was introduced in version 3
62 // For backwards compatibility, only do this if the version is > 2
63 if (indexVersion >= S2_INDEX_VERSION_3 && geoContainer.isPoint()) {
64 coverer->set_min_level(kPointIndexedLevel);
65 coverer->set_max_level(kPointIndexedLevel);
66 } else {
67 coverer->set_min_level(coarsestIndexedLevel);
68 coverer->set_max_level(finestIndexedLevel);
69 }
70
71 // This is advisory; the two above are strict.
72 coverer->set_max_cells(maxCellsInCovering);
73 }
74
S2CellIdToIndexKey(const S2CellId & cellId,S2IndexVersion indexVersion)75 BSONObj S2CellIdToIndexKey(const S2CellId& cellId, S2IndexVersion indexVersion) {
76 // The range of an unsigned long long is
77 // |-----------------|------------------|
78 // 0 2^32 2^64 - 1
79 // 000... 100... 111...
80 // The range of a signed long long is
81 // |-----------------|------------------|
82 // -2^63 0 2^63 - 1
83 // 100... 000... 011...
84 // S2 gives us an unsigned long long, and we need
85 // to use signed long longs for the index.
86 //
87 // The relative ordering may be changed with unsigned
88 // numbers around 2^32 being converted to signed
89 //
90 // However, because a single cell cannot span over
91 // more than once face, individual intervals will
92 // never cross that threshold. Thus, scans will still
93 // produce the same results.
94 if (indexVersion >= S2_INDEX_VERSION_3) {
95 // The size of an index BSONObj in S2 index version 3 is 15 bytes.
96 // total size (4 bytes) | type code 0x12 (1) | field name "" 0x00 (1) |
97 // long long cell id (8) | EOO (1)
98 BSONObjBuilder b(15);
99 b.append("", static_cast<long long>(cellId.id()));
100 return b.obj();
101 }
102
103 // The size of an index BSONObj in older versions is 10 ~ 40 bytes.
104 // total size (4 bytes) | type code 0x12 (1) | field name "" 0x00 (1) |
105 // cell id string (2 ~ 32) 0x00 (1) | EOO (1)
106 BSONObjBuilder b;
107 b.append("", cellId.ToString());
108 // Return a copy so its buffer size fits the object size.
109 return b.obj().copy();
110 }
111 } // namespace mongo
112