summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/index_bounds.h
blob: 58ca8f0570c9ecd59f1d68a25157e9d44cea1af9 (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
/**
 *    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 <string>
#include <vector>

#include "mongo/db/jsobj.h"
#include "mongo/db/query/interval.h"

namespace mongo {

    /**
     * An ordered list of intervals for one field.
     */
    struct OrderedIntervalList {
        OrderedIntervalList() { }
        OrderedIntervalList(const string& n) : name(n) { }

        // Must be ordered according to the index order.
        vector<Interval> intervals;

        // TODO: We could drop this.  Only used in IndexBounds::isValidFor.
        string name;
    };

    /**
     * Tied to an index.  Permissible values for all fields in the index.  Requires the index to
     * interpret.  Previously known as FieldRangeVector.
     */
    struct IndexBounds {
        IndexBounds() : isSimpleRange(false) { }

        // For each indexed field, the values that the field is allowed to take on.
        vector<OrderedIntervalList> fields;

        // Debugging check.
        // We must have as many fields the key pattern does.
        // The fields must be oriented in the direction we'd encounter them given the indexing
        // direction (the value of the field in keyPattern) and index traversal direction provided
        // by 'direction'.
        //
        // An example: [7, 20]
        // We can traverse this forward if indexed ascending
        // We can traverse this backwards if indexed descending.
        bool isValidFor(const BSONObj& keyPattern, int direction);

        // Methods below used for debugging purpose only. Do not use outside testing code.
        size_t size() const;
        std::string getFieldName(size_t i) const;
        size_t getNumIntervals(size_t i) const;
        Interval getInterval(size_t i, size_t j) const;
        std::string toString() const;
        BSONObj toBSON() const;

        // TODO: KILL THIS?
        // We need this for legacy non-index indices (2d/2dsphere) that take a BSONObj and don't
        // deal with the kind of absurd Btree-only behavior of IndexBoundsChecker.
        bool isSimpleRange;
        BSONObj startKey;
        BSONObj endKey;
        bool endKeyInclusive;
    };

    /**
     * A helper used by IndexScan to navigate an index.
     */
    class IndexBoundsChecker {
    public:
        /**
         * keyPattern is the index that we're iterating over.
         * bounds are the bounds we're allowed to iterate over.
         * direction is the direction we're moving over the index, 1 or -1.
         *
         * Bounds not owned by us.
         */
        IndexBoundsChecker(const IndexBounds* bounds, const BSONObj& keyPattern, int direction);

        /**
         * Get the key that we should with.
         */
        void getStartKey(vector<const BSONElement*>* valueOut, vector<bool>* inclusiveOut);

        /**
         * The states of a key from an index scan.  See checkKey below.
         */
        enum KeyState {
            VALID,
            MUST_ADVANCE,
            DONE,
        };

        /**
         * This function checks if the key is within the bounds we're iterating over and updates any
         * internal state required to efficiently determine if the key is within our bounds.
         *
         * Possible outcomes:
         *
         * 1. The key is in our bounds.  Returns VALID.  Caller can use the data associated with the
         * key.
         *
         * 2. The key is not in our bounds but has not exceeded the maximum value in our bounds.
         * Returns MUST_ADVANCE.  Caller must advance to the key provided in the out parameters and
         * call checkKey again.
         *
         * 3. The key is past our bounds.  Returns DONE.  No further keys will satisfy the bounds
         * and the caller should stop.
         *
         * keyEltsToUse, movePastKeyElts, out, and incOut must all be non-NULL.
         * out and incOut must already be resized to have as many elements as the key has fields.
         *
         * In parameters:
         * key is the index key.
         *
         * Out parameters, only valid if we return MUST_ADVANCE:
         *
         * keyEltsToUse: The key that the caller should advance to is made up of the first
         *               'keyEltsToUse' of the key that was provided.
         *
         * movePastKeyElts: If true, the caller must only use the first 'keyEltsToUse' of the
         *                  provided key to form its key.  It moves to the first key that is after
         *                  the key formed by only using those elements.
         *
         * out: If keyEltsToUse is less than the number of indexed fields in the key, the remaining
         *      fields are taken from here.  out is not filled from the start but from the position
         *      that the key corresponds to.  An example:  If keyEltsToUse is 1, movePastKeyElts is
         *      false, and the index we're iterating over has two fields, out[1] will have the value
         *      for the second field.
         *
         * incOut: If the i-th element is false, seek to the key *after* the i-th element of out.
         *         If the i-th element is true, seek to the i-th element of out.
         */
        KeyState checkKey(const BSONObj& key, int* keyEltsToUse, bool* movePastKeyElts,
                          vector<const BSONElement*>* out, vector<bool>* incOut);

    private:
        enum Location {
            BEHIND = -1,
            WITHIN = 0,
            AHEAD = 1,
        };

        /**
         * Find the first field in the key that isn't within the interval we think it is.  Returns
         * false if every field is in the interval we think it is.  Returns true and populates out
         * parameters if a field isn't in the interval we think it is.
         *
         * Out parameters set if we return true:
         * 'where' is the leftmost field that isn't in the interval we think it is.
         * 'what' is the orientation of the field with respect to that interval.
         */
        bool findLeftmostProblem(const vector<BSONElement>& keyValues, size_t* where,
                                 Location* what);

        /**
         * Returns true if it's possible to advance any of the first 'fieldsToCheck' fields of the
         * index key and still be within valid index bounds.
         *
         * keyValues are the elements of the index key in order.
         */
        bool spaceLeftToAdvance(size_t fieldsToCheck, const vector<BSONElement>& keyValues);

        /**
         * Returns BEHIND if the key is behind the interval.
         * Returns WITHIN if the key is within the interval.
         * Returns AHEAD if the key is ahead the interval.
         *
         * All directions are oriented along 'direction'.
         */
        static Location intervalCmp(const Interval& interval, const BSONElement& key,
                                    const int expectedDirection);

        /**
         * If 'elt' is in any interval, return WITHIN and set 'newIntervalIndex' to the index of the
         * interval in the ordered interval list.
         *
         * If 'elt' is not in any interval but could be advanced to be in one, return BEHIND and set
         * 'newIntervalIndex' to the index of the interval that 'elt' could be advanced to.
         *
         * If 'elt' cannot be advanced to any interval, return AHEAD.
         *
         * TODO(efficiency): Start search from a given index.
         * TODO(efficiency): Binary search for the answer.
         */
        static Location findIntervalForField(const BSONElement &elt, const OrderedIntervalList& oil,
                                             const int expectedDirection, size_t* newIntervalIndex);

        // The actual bounds.  Must outlive this object.  Not owned by us.
        const IndexBounds* _bounds;

        // For each field, which interval are we currently in?
        vector<size_t> _curInterval;

        // Direction of scan * direction of indexing.
        vector<int> _expectedDirection;
    };

}  // namespace mongo