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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/**
* Copyright (C) 2019-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.
*/
#pragma once
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include "mongo/bson/bsonobj.h"
#include "mongo/db/exec/document_value/document.h"
#include "mongo/db/exec/document_value/value.h"
#include "mongo/db/exec/plan_stats.h"
#include "mongo/db/exec/sort_key_comparator.h"
#include "mongo/db/exec/working_set.h"
#include "mongo/db/pipeline/expression.h"
#include "mongo/db/query/sort_pattern.h"
#include "mongo/db/sorter/sorter.h"
#include "mongo/db/sorter/sorter_stats.h"
namespace mongo {
/**
* The SortExecutor class is the internal implementation of sorting for query execution. The
* caller should provide input documents by repeated calls to the add() function, and then
* complete the loading process with a single call to loadingDone(). Finally, getNext() should be
* called to return the documents one by one in sorted order.
*
* The template parameter is the type of data being sorted. In DocumentSource execution, we sort
* Document objects directly, but in the PlanStage layer we may sort WorkingSetMembers. The type of
* the sort key, on the other hand, is always Value.
*/
template <typename T>
class SortExecutor {
public:
using DocumentSorter = Sorter<Value, T>;
class Comparator {
public:
Comparator(const SortPattern& sortPattern) : _sortKeyComparator(sortPattern) {}
int operator()(const Value& lhs, const Value& rhs) const {
return _sortKeyComparator(lhs, rhs);
}
private:
SortKeyComparator _sortKeyComparator;
};
/**
* If the passed in limit is 0, this is treated as no limit.
*/
SortExecutor(SortPattern sortPattern,
uint64_t limit,
uint64_t maxMemoryUsageBytes,
std::string tempDir,
bool allowDiskUse,
bool moveSortedDataIntoIterator = false)
: _sortPattern(std::move(sortPattern)),
_tempDir(std::move(tempDir)),
_diskUseAllowed(allowDiskUse),
_moveSortedDataIntoIterator(moveSortedDataIntoIterator) {
_stats.sortPattern =
_sortPattern.serialize(SortPattern::SortKeySerialization::kForExplain).toBson();
_stats.limit = limit;
_stats.maxMemoryUsageBytes = maxMemoryUsageBytes;
if (allowDiskUse) {
_sorterFileStats = std::make_unique<SorterFileStats>(nullptr);
}
}
const SortPattern& sortPattern() const {
return _sortPattern;
}
/**
* Absorbs 'limit', enabling a top-k sort. It is safe to call this multiple times, it will keep
* the smallest limit.
*/
void setLimit(uint64_t limit) {
if (!_stats.limit || limit < _stats.limit)
_stats.limit = limit;
}
uint64_t getLimit() const {
return _stats.limit;
}
bool hasLimit() const {
return _stats.limit > 0;
}
bool wasDiskUsed() const {
return _stats.spills > 0;
}
/**
* Returns true if the loading phase has been explicitly completed, and then the stream of
* documents has subsequently been exhausted by "get next" calls.
*/
bool isEOF() const {
return _isEOF;
}
const SortStats& stats() const {
if (_sorter) {
_stats.memoryUsageBytes = _sorter->stats().memUsage();
}
return _stats;
}
SorterFileStats* getSorterFileStats() const {
if (!_sorterFileStats) {
return nullptr;
}
return _sorterFileStats.get();
}
long long spilledDataStorageSize() const {
if (!_sorterFileStats) {
return 0;
}
return _sorterFileStats->bytesSpilled();
}
/**
* Add data item to be sorted of type T with sort key specified by Value to the sort executor.
* Should only be called before 'loadingDone()' is called.
*/
void add(const Value& sortKey, const T& data) {
ensureSorter();
_sorter->add(sortKey, data);
}
/**
* Signals to the sort executor that there will be no more input documents.
*/
void loadingDone() {
ensureSorter();
_output.reset(_sorter->done());
_stats.keysSorted += _sorter->stats().numSorted();
_stats.spills += _sorter->stats().spilledRanges();
_stats.totalDataSizeBytes += _sorter->stats().bytesSorted();
_stats.spilledDataStorageSize += spilledDataStorageSize();
_stats.memoryUsageBytes = 0;
_sorter.reset();
}
/**
* Returns true if there are more results which can be returned via 'getNext()', or false to
* indicate end-of-stream. Should only be called after 'loadingDone()' is called.
*/
bool hasNext() {
if (_isEOF) {
return false;
}
if (!_output->more()) {
clearSortTable();
_isEOF = true;
return false;
}
return true;
}
/**
* Returns the next data item in the sorted stream, which is a pair consisting of the sort key
* and the corresponding item being sorted. Illegal to call if there is no next item;
* end-of-stream must be detected with 'hasNext()'.
*/
std::pair<Value, T> getNext() {
return _output->next();
}
uint64_t getMaxMemoryBytes() const {
return _stats.maxMemoryUsageBytes;
}
/**
* Pauses Loading and creates an iterator which can be used to get the current state in
* read-only mode. The stream code needs this to pause and get the current internal state which
* can be used to store it to a persistent storage which will constitute a checkpoint for
* streaming processing.
*/
void pauseLoading() {
invariant(!_paused);
_paused = true;
ensureSorter();
_output.reset(_sorter->pause());
}
/**
* Resumes Loading. This will remove the iterator created in pauseLoading().
*/
void resumeLoading() {
invariant(_paused);
_paused = false;
ensureSorter();
clearSortTable();
_sorter->resume();
_isEOF = false;
}
private:
/*
* '_output' is a DocumentSorter::Iterator that can have the following iterator values:
* (1) InMemIterator, (2) InMemReadOnlyIterator, (3) FileIterator, or (4) MergeIterator
* If '_output' is an InMemIterator or an InMemReadOnlyIterator, the sort table will be cleared
* in memory. If '_output' is an MergeIterator, the spilled sorted data will be cleared.
* However, the sort table needs to be cleared through a call to reset(). Otherwise, '_output'
* is a FileIterator and the sort table needs to be cleared through a call to reset().
*/
void clearSortTable() {
_output.reset();
}
SortOptions makeSortOptions() const {
SortOptions opts;
opts.moveSortedDataIntoIterator = _moveSortedDataIntoIterator;
if (_stats.limit) {
opts.limit = _stats.limit;
}
opts.maxMemoryUsageBytes = _stats.maxMemoryUsageBytes;
if (_diskUseAllowed) {
opts.extSortAllowed = true;
opts.tempDir = _tempDir;
opts.sorterFileStats = _sorterFileStats.get();
}
return opts;
}
void ensureSorter() {
// This conditional should only pass if no documents were added to the sorter.
if (!_sorter) {
_sorter.reset(DocumentSorter::make(makeSortOptions(), Comparator(_sortPattern)));
}
}
const SortPattern _sortPattern;
const std::string _tempDir;
const bool _diskUseAllowed;
const bool _moveSortedDataIntoIterator;
std::unique_ptr<SorterFileStats> _sorterFileStats;
std::unique_ptr<DocumentSorter> _sorter;
std::unique_ptr<typename DocumentSorter::Iterator> _output;
mutable SortStats _stats;
bool _isEOF = false;
bool _paused = false;
};
} // namespace mongo
|