summaryrefslogtreecommitdiff
path: root/src/mongo/db/index/index_cursor.h
blob: 6d50f243f7b7891c13f88696f98cc1f58b73c17f (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
/**
*    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 <vector>
#include "mongo/db/diskloc.h"
#include "mongo/db/jsobj.h"

namespace mongo {

    struct CursorOptions;

    /**
     * An IndexCursor is the interface through which one traverses the entries of a given
     * index. The internal structure of an index is kept isolated.
     *
     * The cursor must be initiailized by seek()ing to a given entry in the index.  The index is
     * traversed by calling next() or skip()-ping ahead.
     *
     * The set of predicates a given index can understand is known a priori.  These predicates may
     * be simple (a key location for a Btree index) or rich ($within for a geo index).
     *
     * Locking is the responsibility of the caller.  The IndexCursor keeps state.  If the caller
     * wishes to yield or unlock, it must call savePosition() first.  When it decides to unyield it
     * must call restorePosition().  The cursor may be EOF after a restorePosition().
     */
    class IndexCursor {
    public:
        virtual ~IndexCursor() { }

        /**
         * Set options on the cursor (direction).  See CursorOptions below.
         */
        virtual Status setOptions(const CursorOptions& options) = 0;

        /**
         * A cursor doesn't point anywhere by default.  You must seek to the start position.
         * The provided position must be a predicate that the index understands.  The
         * predicate must describe one value, though there may be several instances
         *
         * Possible return values:
         * 1. Success: seeked to the position.
         * 2. Success: seeked to 'closest' key oriented according to the cursor's direction.
         * 3. Error: can't seek to the position.
         */
        virtual Status seek(const BSONObj& position) = 0;

        //
        // Iteration support
        //

        // Are we out of documents?
        virtual bool isEOF() const = 0;

        // Move to the next key/value pair.  Assumes !isEOF().
        virtual void next() = 0;
        
        //
        // Accessors
        //

        // Current key we point at.  Assumes !isEOF().
        virtual BSONObj getKey() const = 0;

        // Current value we point at.  Assumes !isEOF().
        virtual DiskLoc getValue() const = 0;

        //
        // Yielding support
        //

        /**
         * Yielding semantics:
         * If the entry that a cursor points at is not deleted during a yield, the cursor will
         * point at that entry after a restore.
         * An entry inserted during a yield may or may not be returned by an in-progress scan.
         * An entry deleted during a yield may or may not be returned by an in-progress scan.
         * An entry modified during a yield may or may not be returned by an in-progress scan.
         * An entry that is not inserted or deleted during a yield will be returned, and only once.
         * If the index returns entries in a given order (Btree), this order will be mantained even
         * if the entry corresponding to a saved position is deleted during a yield.
         */

        /**
         * Save our current position in the index.  Assumes that we are currently pointing to a
         * valid position in the index.
         * If not, we error.  Otherwise, succeed.
         */
        virtual Status savePosition() = 0;

        /**
         * Restore the saved position.  Errors if there is no saved position.
         * The cursor may be EOF after a restore.
         */
        virtual Status restorePosition() = 0;

        // Return a string describing the cursor.
        virtual string toString() = 0;

        /**
         *  Add debugging info to the provided builder.
         * TODO(hk): We can do this better, perhaps with a more structured format.
         */
        virtual void explainDetails(BSONObjBuilder* b) { }
    };

    // All the options we might want to set on a cursor.
    struct CursorOptions {
        // Set the direction of the scan.  Ignored if the cursor doesn't have directions (geo).
        enum Direction {
            DECREASING = -1,
            INCREASING = 1,
        };

        Direction direction;

        // 2d indices need to know exactly how many results you want beforehand.
        // Ignored by every other index.
        int numWanted;
    };

}  // namespace mongo