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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/platform/basic.h"
#include "mongo/db/index/sort_key_generator.h"
#include "mongo/bson/bsonobj_comparator.h"
#include "mongo/db/query/collation/collation_index_key.h"
namespace mongo {
SortKeyGenerator::SortKeyGenerator(const BSONObj& sortSpec, const CollatorInterface* collator)
: _collator(collator) {
BSONObjBuilder btreeBob;
for (auto&& elt : sortSpec) {
if (elt.isNumber()) {
btreeBob.append(elt);
_patternPartTypes.push_back(SortPatternPartType::kFieldPath);
} else {
// If this field of the sort pattern is non-numeric, we expect it to be a text-score
// meta sort.
invariant(elt.type() == BSONType::Object);
invariant(elt.embeddedObject().nFields() == 1);
auto metaElem = elt.embeddedObject().firstElement();
invariant(metaElem.fieldNameStringData() == "$meta"_sd);
if (metaElem.valueStringData() == "textScore"_sd) {
_patternPartTypes.push_back(SortPatternPartType::kMetaTextScore);
} else {
invariant(metaElem.valueStringData() == "randVal"_sd);
_patternPartTypes.push_back(SortPatternPartType::kMetaRandVal);
}
_sortHasMeta = true;
}
}
// The fake index key pattern used to generate Btree keys.
_sortSpecWithoutMeta = btreeBob.obj();
// If we're just sorting by meta, don't bother with all the key stuff.
if (_sortSpecWithoutMeta.isEmpty()) {
return;
}
// We'll need to treat arrays as if we were to create an index over them. that is, we may need
// to unnest the first level and consider each array element to decide the sort order. In order
// to do this, we make a BtreeKeyGenerator.
std::vector<const char*> fieldNames;
std::vector<BSONElement> fixed;
for (auto&& patternElt : _sortSpecWithoutMeta) {
fieldNames.push_back(patternElt.fieldName());
fixed.push_back(BSONElement());
}
constexpr bool isSparse = false;
_indexKeyGen = std::make_unique<BtreeKeyGenerator>(fieldNames, fixed, isSparse, _collator);
}
StatusWith<BSONObj> SortKeyGenerator::getSortKey(const WorkingSetMember& wsm) const {
if (wsm.hasObj()) {
SortKeyGenerator::Metadata metadata;
if (_sortHasMeta && wsm.metadata().hasTextScore()) {
metadata.textScore = wsm.metadata().getTextScore();
}
return getSortKeyFromDocument(wsm.obj.value(), &metadata);
}
return getSortKeyFromIndexKey(wsm);
}
StatusWith<BSONObj> SortKeyGenerator::getSortKeyFromIndexKey(const WorkingSetMember& member) const {
invariant(member.getState() == WorkingSetMember::RID_AND_IDX);
invariant(!_sortHasMeta);
BSONObjBuilder objBuilder;
for (BSONElement specElt : _sortSpecWithoutMeta) {
invariant(specElt.isNumber());
BSONElement sortKeyElt;
invariant(member.getFieldDotted(specElt.fieldName(), &sortKeyElt));
// If we were to call 'collationAwareIndexKeyAppend' with a non-simple collation and a
// 'sortKeyElt' representing a collated index key we would incorrectly encode for the
// collation twice. This is not currently possible as the query planner will ensure that
// the plan fetches the data before sort key generation in the case where the index has a
// non-simple collation.
CollationIndexKey::collationAwareIndexKeyAppend(sortKeyElt, _collator, &objBuilder);
}
return objBuilder.obj();
}
StatusWith<BSONObj> SortKeyGenerator::getSortKeyFromDocument(const BSONObj& obj,
const Metadata* metadata) const {
if (_sortHasMeta) {
invariant(metadata);
}
auto sortKeyNoMetadata = getSortKeyFromDocumentWithoutMetadata(obj);
if (!sortKeyNoMetadata.isOK()) {
return sortKeyNoMetadata;
}
if (!_sortHasMeta) {
// We don't have to worry about $meta sort, so the index key becomes the sort key.
return sortKeyNoMetadata;
}
BSONObjBuilder mergedKeyBob;
// Merge metadata into the key.
BSONObjIterator sortKeyIt(sortKeyNoMetadata.getValue());
for (auto type : _patternPartTypes) {
switch (type) {
case SortPatternPartType::kFieldPath: {
invariant(sortKeyIt.more());
mergedKeyBob.append(sortKeyIt.next());
continue;
}
case SortPatternPartType::kMetaTextScore: {
mergedKeyBob.append("", metadata->textScore);
continue;
}
case SortPatternPartType::kMetaRandVal: {
mergedKeyBob.append("", metadata->randVal);
continue;
}
default: { MONGO_UNREACHABLE; }
}
}
// We should have added a key component for each part of the index key pattern.
invariant(!sortKeyIt.more());
return mergedKeyBob.obj();
}
StatusWith<BSONObj> SortKeyGenerator::getSortKeyFromDocumentWithoutMetadata(
const BSONObj& obj) const {
// Not sorting by anything in the key, just bail out early.
if (_sortSpecWithoutMeta.isEmpty()) {
return BSONObj();
}
// We will sort 'obj' in the same order an index over '_sortSpecWithoutMeta' would have. This is
// tricky. Consider the sort pattern {a:1} and the document {a: [1, 10]}. We have potentially
// two keys we could use to sort on. Here we extract these keys.
//
// The keys themselves will incorporate the collation, with strings translated to their
// corresponding collation keys. Therefore, we use the simple string comparator when comparing
// the keys themselves.
const StringData::ComparatorInterface* stringComparator = nullptr;
BSONObjComparator patternCmp(
_sortSpecWithoutMeta, BSONObjComparator::FieldNamesMode::kConsider, stringComparator);
BSONObjSet keys = patternCmp.makeBSONObjSet();
try {
// There's no need to compute the prefixes of the indexed fields that cause the index to be
// multikey when getting the index keys for sorting.
MultikeyPaths* multikeyPaths = nullptr;
_indexKeyGen->getKeys(obj, &keys, multikeyPaths);
} catch (const AssertionException& e) {
// Probably a parallel array.
if (ErrorCodes::CannotIndexParallelArrays == e.code()) {
return Status(ErrorCodes::BadValue, "cannot sort with keys that are parallel arrays");
} else {
return e.toStatus();
}
} catch (...) {
return Status(ErrorCodes::InternalError, "unknown error during sort key generation");
}
// Key generator isn't sparse so we should at least get an all-null key.
invariant(!keys.empty());
// The sort key is the first index key, ordered according to the pattern '_sortSpecWithoutMeta'.
return *keys.begin();
}
} // namespace mongo
|