summaryrefslogtreecommitdiff
path: root/src/mongo/bson/bsonobj_comparator_interface.h
blob: a063f638391688f2d62d6701ce13dd50a8816ec7 (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
/**
 *    Copyright (C) 2016 MongoDB 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 <initializer_list>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include "mongo/base/disallow_copying.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/util/assert_util.h"

namespace mongo {

/**
 * A BSONObj::ComparatorInterface is an abstract class for comparing BSONObj objects. Usage for
 * comparing two BSON objects, 'lhs' and 'rhs', where 'comparator' is an instance of a class
 * implementing this interface, is as shown below:
 *
 *  bool lessThan = comparator.evaluate(lhs < rhs);
 *  bool lessThanOrEqual = comparator.evaluate(lhs <= rhs);
 *  bool equal = comparator.evaluate(lhs == rhs);
 *  bool greaterThanOrEqual = comparator.evaluate(lhs >= rhs);
 *  bool greaterThan = comparator.evaluate(lhs > rhs);
 *  bool notEqual = comparator.evaluate(lhs != rhs);
 *
 * Can also be used to obtain function objects compatible for use with standard library algorithms
 * such as std::sort, and to construct STL sets and maps which respect this comparator.
 *
 * All methods are thread-safe.
 */
class BSONObj::ComparatorInterface {
    MONGO_DISALLOW_COPYING(ComparatorInterface);

public:
    /**
     * Functor compatible for use with ordered STL containers.
     */
    class LessThan {
    public:
        explicit LessThan(const ComparatorInterface* comparator) : _comparator(comparator) {}

        bool operator()(const BSONObj& lhs, const BSONObj& rhs) const {
            return _comparator->compare(lhs, rhs) < 0;
        }

    private:
        const ComparatorInterface* _comparator;
    };

    /**
     * Functor compatible for use with unordered STL containers.
     */
    class EqualTo {
    public:
        explicit EqualTo(const ComparatorInterface* comparator) : _comparator(comparator) {}

        bool operator()(const BSONObj& lhs, const BSONObj& rhs) const {
            return _comparator->compare(lhs, rhs) == 0;
        }

    private:
        const ComparatorInterface* _comparator;
    };

    using BSONObjSet = std::set<BSONObj, BSONObj::ComparatorInterface::LessThan>;

    // TODO SERVER-23990: Make the BSONObj hash collation-aware.
    using BSONObjUnorderedSet =
        std::unordered_set<BSONObj, BSONObj::Hasher, BSONObj::ComparatorInterface::EqualTo>;

    template <typename T>
    using BSONObjIndexedMap = std::map<BSONObj, T, BSONObj::ComparatorInterface::LessThan>;

    // TODO SERVER-23990: Make the BSONObj hash collation-aware.
    template <typename T>
    using BSONObjIndexedUnorderedMap =
        std::unordered_map<BSONObj, T, BSONObj::Hasher, BSONObj::ComparatorInterface::EqualTo>;

    virtual ~ComparatorInterface() = default;

    /**
     * Compares two BSONObj objects. Returns <0, 0, >0 if 'lhs' < 'rhs', 'lhs' == 'rhs', or 'lhs' >
     * 'rhs' respectively.
     */
    virtual int compare(const BSONObj& lhs, const BSONObj& rhs) const = 0;

    /**
     * Evaluates a deferred comparison object generated by invocation of one of the BSONObj operator
     * overloads for relops.
     */
    bool evaluate(BSONObj::DeferredComparison deferredComparison) const {
        int cmp = compare(deferredComparison.lhs, deferredComparison.rhs);
        switch (deferredComparison.type) {
            case BSONObj::DeferredComparison::Type::kLT:
                return cmp < 0;
            case BSONObj::DeferredComparison::Type::kLTE:
                return cmp <= 0;
            case BSONObj::DeferredComparison::Type::kEQ:
                return cmp == 0;
            case BSONObj::DeferredComparison::Type::kGT:
                return cmp > 0;
            case BSONObj::DeferredComparison::Type::kGTE:
                return cmp >= 0;
            case BSONObj::DeferredComparison::Type::kNE:
                return cmp != 0;
        }

        MONGO_UNREACHABLE;
    }

    /**
     * Returns a function object which computes whether one BSONObj is less than another under this
     * comparator. This comparator must outlive the returned function object.
     */
    LessThan makeLessThan() const {
        return LessThan(this);
    }

    /**
     * Returns a function object which computes whether one BSONObj is equal to another under this
     * comparator. This comparator must outlive the returned function object.
     */
    EqualTo makeEqualTo() const {
        return EqualTo(this);
    }

    /**
     * Constructs a BSONObjSet whose ordering is given by this comparator. This comparator must
     * outlive the returned set.
     */
    BSONObjSet makeBSONObjSet(std::initializer_list<BSONObj> init = {}) const {
        return BSONObjSet(init, LessThan(this));
    }

    /**
     * Constructs a BSONObjUnorderedSet whose equivalence classes are given by this
     * comparator. This comparator must outlive the returned set.
     */
    BSONObjUnorderedSet makeBSONObjUnorderedSet(std::initializer_list<BSONObj> init = {}) const {
        // TODO SERVER-23990: Make the BSONObj hash collation-aware.
        return BSONObjUnorderedSet(init, 0, BSONObj::Hasher(), EqualTo(this));
    }

    /**
     * Constructs an ordered map from BSONObj to type T whose ordering is given by this comparator.
     * This comparator must outlive the returned map.
     */
    template <typename T>
    BSONObjIndexedMap<T> makeBSONObjIndexedMap(
        std::initializer_list<std::pair<const BSONObj, T>> init = {}) const {
        return BSONObjIndexedMap<T>(init, LessThan(this));
    }

    /**
     * Constructs an unordered map from BSONObj to type T whose ordering is given by this
     * comparator. This comparator must outlive the returned map.
     */
    template <typename T>
    BSONObjIndexedUnorderedMap<T> makeBSONObjIndexedUnorderedMap(
        std::initializer_list<std::pair<const BSONObj, T>> init = {}) const {
        // TODO SERVER-23990: Make the BSONObj hash collation-aware.
        return BSONObjIndexedUnorderedMap<T>(init, 0, BSONObj::Hasher(), EqualTo(this));
    }

protected:
    constexpr ComparatorInterface() = default;
};

using BSONObjSet = BSONObj::ComparatorInterface::BSONObjSet;

using BSONObjUnorderedSet = BSONObj::ComparatorInterface::BSONObjUnorderedSet;

template <typename T>
using BSONObjIndexedMap = BSONObj::ComparatorInterface::BSONObjIndexedMap<T>;

template <typename T>
using BSONObjIndexedUnorderedMap = BSONObj::ComparatorInterface::BSONObjIndexedUnorderedMap<T>;

}  // namespace mongo