summaryrefslogtreecommitdiff
path: root/src/mongo/client/index_spec.h
blob: 87a3cafd5de85416bd57b64f70d97d845e874f03 (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

/**
 *    Copyright (C) 2018-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 <string>
#include <utility>
#include <vector>

#include "mongo/db/jsobj.h"

namespace mongo {

class StringData;

class IndexSpec {
public:
    // An enumeration of symbolic names for index types.
    enum IndexType {
        kIndexTypeAscending,
        kIndexTypeDescending,
        kIndexTypeText,
        kIndexTypeGeo2D,
        kIndexTypeGeoHaystack,
        kIndexTypeGeo2DSphere,
        kIndexTypeHashed,
    };

    // The values to be encoded in BSON for each type of index.
    static const int kIndexValAscending = 1;
    static const int kIndexValDescending = -1;
    static const char kIndexValText[];
    static const char kIndexValGeo2D[];
    static const char kIndexValGeoHaystack[];
    static const char kIndexValGeo2DSphere[];
    static const char kIndexValHashed[];

    /** Create a new IndexSpec. */
    IndexSpec();

    //
    // Methods for adding keys. Methods on this class will prevent you from adding a given
    // key multiple times. Constraints on the validity of compound indexes are not enforced
    // here.
    //

    /** Add a new component, by default ascending, field to index. */
    IndexSpec& addKey(const StringData& field, IndexType type = kIndexTypeAscending);

    /** Add a component to this index. The field name of the element is used as the field
     *  name to index. The value of the element is the index type. This method exists to
     *  accomodate building future index types for which the enumeration value has not yet
     *  been extended.
     */
    IndexSpec& addKey(const BSONElement& fieldAndType);

    /** Add all components in the provided key vector to the index descriptor. */
    typedef std::vector<std::pair<std::string, IndexType>> KeyVector;
    IndexSpec& addKeys(const KeyVector& keys);

    /** Add all keys from the provided object to the index descriptor. */
    IndexSpec& addKeys(const BSONObj& keys);


    //
    // Methods for adding options. As for keys, duplicated settings are checked and will
    // raise an error.
    //

    //
    // Common index options
    //

    /** Controls whether this index should be built in the foreground or background. By
     *  default indexes are built in the foreground.
     */
    IndexSpec& background(bool value = true);

    /** Set whether or not this index should enforce uniqueness. By default, uniqueness is
     *  not enforced.
     */
    IndexSpec& unique(bool value = true);


    /** Set the name for this index. If not set, a name will be automatically generated. */
    IndexSpec& name(const StringData& name);

    /** Sets whether duplicates detected while indexing should be dropped. By default,
     *  duplicates are not dropped.
     */
    IndexSpec& dropDuplicates(bool value = true);

    /** Sets whether or not this index should be sparse. By default, indexes are not
     *  sparse.
     */
    IndexSpec& sparse(bool value = true);

    /** Enables time-to-live semantics for this index with the specified lifetime in
     *  seconds. Note that the indexed field must be of type UTC datetime for this option
     *  to work correctly.
     */
    IndexSpec& expireAfterSeconds(int value);

    /** Explicitly request an index of the given version. As of MongoDB 2.6, the only accepted
     *  values are 0 or 1. Versions 2.0 and later default to '1'. Do not set this option except
     *  in unusual circumstances.
     */
    IndexSpec& version(int value);


    //
    // Text options
    //

    /** Sets the 'weights' document for a text index. */
    IndexSpec& textWeights(const BSONObj& value);

    /** Sets the default language for a text index. */
    IndexSpec& textDefaultLanguage(const StringData& value);

    /** Sets the name of the field containing the language override in a text index. */
    IndexSpec& textLanguageOverride(const StringData& value);

    /** Sets the version of the text index to use. MongoDB 2.4 only supports version
     *  '1'. If not otherwise specified, MongoDB 2.6 defaults to version 2.
     */
    IndexSpec& textIndexVersion(int value);


    //
    // 2D Sphere Options
    //

    /** Sets the version of the 2D sphere index to use. MongoDB 2.4 only supports version
     *  '1'. If not otherwise specified, MongoDB 2.6 defaults to version 2.
     */
    IndexSpec& geo2DSphereIndexVersion(int value);


    //
    // Geo2D Options
    //

    /** Sets the number of bits of precision for geohash. */
    IndexSpec& geo2DBits(int value);

    /** Sets the minimum value for keys in a geo2d index. */
    IndexSpec& geo2DMin(double value);

    /** Sets the maximum value for keys in a geo2d index. */
    IndexSpec& geo2DMax(double value);


    //
    // Geo Haystack Options
    //

    /** Sets the bucket size for haystack indexes. */
    IndexSpec& geoHaystackBucketSize(double value);


    //
    // Support for adding generic options. This is here so that if new index options
    // become available on the server those options can be set independently of the
    // named methods above.
    //

    /** Adds another option verbatim. */
    IndexSpec& addOption(const BSONElement& option);

    /** Adds the provided options verbatim. */
    IndexSpec& addOptions(const BSONObj& options);

    /** Get a copy of the current name for this index. If a name was provided to the
     *  constructor, a copy of this name is returned. Otherwise, the current auto-generated
     *  name given the set of indexes will be returned. Note that this is a copy:
     *  subsequent changes to the indexed fields will not affect the value returned here,
     *  and you must call 'name' again to obtain the updated value.
     */
    std::string name() const;

    /** Return a BSONObj that captures the selected index keys and options. */
    BSONObj toBSON() const;

private:
    void _rename();

    std::string _name;
    bool _dynamicName;

    mutable BSONObjBuilder _keys;
    mutable BSONObjBuilder _options;
};

}  // namespace mongo