summaryrefslogtreecommitdiff
path: root/src/mongo/db/sorter/sorter.h
blob: b739a985f2ca0f225fe50953caa76843f71519fd (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
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
/*
*    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/>.
*
*    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 GNU Affero General 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 <deque>
#include <fstream>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "mongo/base/disallow_copying.h"
#include "mongo/bson/util/builder.h"

/**
 * This is the public API for the Sorter (both in-memory and external)
 *
 * Many of the classes in this file are templated on Key and Value types which
 * require the following public members:
 *
 * // A type carrying extra information used by the deserializer. Contents are
 * // up to you, but  it should be cheap to copy. Use an empty struct if your
 * // deserializer doesn't need extra data.
 * struct SorterDeserializeSettings {};
 *
 * // Serialize this object to the BufBuilder
 * void serializeForSorter(BufBuilder& buf) const;
 *
 * // Deserialize and return an object from the BufReader
 * static Type deserializeForSorter(BufReader& buf, const Type::SorterDeserializeSettings&);
 *
 * // How much memory is used by your type? Include sizeof(*this) and any memory you reference.
 * int memUsageForSorter() const;
 *
 * // For types with owned and unowned states, such as BSON, return an owned version.
 * // Return *this if your type doesn't have an unowned state
 * Type getOwned() const;
 *
 * Comparators are functors that that compare std::pair<Key, Value> and return an
 * int less than, equal to, or greater than 0 depending on how the two pairs
 * compare with the same semantics as memcmp.
 * Example for Key=BSONObj, Value=int:
 *
 * class MyComparator {
 * public:
 *     int operator()(const std::pair<BSONObj, int>& lhs,
 *                    const std::pair<BSONObj, int>& rhs) {
 *         int ret = lhs.first.woCompare(rhs.first, _ord);
 *         if (ret)
 *             return ret;
 *
 *        if (lhs.second >  rhs.second) return 1;
 *        if (lhs.second == rhs.second) return 0;
 *        return -1;
 *     }
 *     Ordering _ord;
 * };
 */

namespace mongo {
namespace sorter {
// Everything in this namespace is internal to the sorter
class FileDeleter;
}

/**
 * Runtime options that control the Sorter's behavior
 */
struct SortOptions {
    unsigned long long limit;    /// number of KV pairs to be returned. 0 for no limit.
    size_t maxMemoryUsageBytes;  /// Approximate.
    bool extSortAllowed;         /// If false, uassert if more mem needed than allowed.
    std::string tempDir;         /// Directory to directly place files in.
                                 /// Must be explicitly set if extSortAllowed is true.

    SortOptions() : limit(0), maxMemoryUsageBytes(64 * 1024 * 1024), extSortAllowed(false) {}

    /// Fluent API to support expressions like SortOptions().Limit(1000).ExtSortAllowed(true)

    SortOptions& Limit(unsigned long long newLimit) {
        limit = newLimit;
        return *this;
    }

    SortOptions& MaxMemoryUsageBytes(size_t newMaxMemoryUsageBytes) {
        maxMemoryUsageBytes = newMaxMemoryUsageBytes;
        return *this;
    }

    SortOptions& ExtSortAllowed(bool newExtSortAllowed = true) {
        extSortAllowed = newExtSortAllowed;
        return *this;
    }

    SortOptions& TempDir(const std::string& newTempDir) {
        tempDir = newTempDir;
        return *this;
    }
};

/// This is the output from the sorting framework
template <typename Key, typename Value>
class SortIteratorInterface {
    MONGO_DISALLOW_COPYING(SortIteratorInterface);

public:
    typedef std::pair<Key, Value> Data;

    // Unowned objects are only valid until next call to any method

    virtual bool more() = 0;
    virtual std::pair<Key, Value> next() = 0;

    virtual ~SortIteratorInterface() {}

    /// Returns an iterator that merges the passed in iterators
    template <typename Comparator>
    static SortIteratorInterface* merge(
        const std::vector<std::shared_ptr<SortIteratorInterface>>& iters,
        const SortOptions& opts,
        const Comparator& comp);

protected:
    SortIteratorInterface() {}  // can only be constructed as a base
};

/// This is the main way to input data to the sorting framework
template <typename Key, typename Value>
class Sorter {
    MONGO_DISALLOW_COPYING(Sorter);

public:
    typedef std::pair<Key, Value> Data;
    typedef SortIteratorInterface<Key, Value> Iterator;
    typedef std::pair<typename Key::SorterDeserializeSettings,
                      typename Value::SorterDeserializeSettings>
        Settings;

    template <typename Comparator>
    static Sorter* make(const SortOptions& opts,
                        const Comparator& comp,
                        const Settings& settings = Settings());

    virtual void add(const Key&, const Value&) = 0;
    virtual Iterator* done() = 0;  /// Can't add more data after calling done()

    virtual ~Sorter() {}

    // TEMP these are here for compatibility. Will be replaced with a general stats API
    bool usedDisk() {
        return _usedDisk;
    }
    virtual int numFiles() const = 0;
    virtual size_t memUsed() const = 0;

protected:
    bool _usedDisk{false};  // Keeps track of whether the sorter used disk or not
    Sorter() {}             // can only be constructed as a base
};

/// Writes pre-sorted data to a sorted file and hands-back an Iterator over that file.
template <typename Key, typename Value>
class SortedFileWriter {
    MONGO_DISALLOW_COPYING(SortedFileWriter);

public:
    typedef SortIteratorInterface<Key, Value> Iterator;
    typedef std::pair<typename Key::SorterDeserializeSettings,
                      typename Value::SorterDeserializeSettings>
        Settings;

    explicit SortedFileWriter(const SortOptions& opts, const Settings& settings = Settings());

    void addAlreadySorted(const Key&, const Value&);
    Iterator* done();  /// Can't add more data after calling done()

private:
    void spill();

    const Settings _settings;
    std::string _fileName;
    std::shared_ptr<sorter::FileDeleter> _fileDeleter;  // Must outlive _file
    std::ofstream _file;
    BufBuilder _buffer;
};
}

/**
 * #include "mongo/db/sorter/sorter.cpp" and call this in a single translation
 * unit once for each unique set of template parameters.
 */
#define MONGO_CREATE_SORTER(Key, Value, Comparator)                                      \
    /* public classes */                                                                 \
    template class ::mongo::Sorter<Key, Value>;                                          \
    template class ::mongo::SortIteratorInterface<Key, Value>;                           \
    template class ::mongo::SortedFileWriter<Key, Value>;                                \
    /* internal classes */                                                               \
    template class ::mongo::sorter::NoLimitSorter<Key, Value, Comparator>;               \
    template class ::mongo::sorter::LimitOneSorter<Key, Value, Comparator>;              \
    template class ::mongo::sorter::TopKSorter<Key, Value, Comparator>;                  \
    template class ::mongo::sorter::MergeIterator<Key, Value, Comparator>;               \
    template class ::mongo::sorter::InMemIterator<Key, Value>;                           \
    template class ::mongo::sorter::FileIterator<Key, Value>;                            \
    /* factory functions */                                                              \
    template ::mongo::SortIteratorInterface<Key, Value>* ::mongo::                       \
        SortIteratorInterface<Key, Value>::merge<Comparator>(                            \
            const std::vector<std::shared_ptr<SortIteratorInterface>>& iters,            \
            const SortOptions& opts,                                                     \
            const Comparator& comp);                                                     \
    template ::mongo::Sorter<Key, Value>* ::mongo::Sorter<Key, Value>::make<Comparator>( \
        const SortOptions& opts, const Comparator& comp, const Settings& settings);