-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
CreateIndexRequest.cs
379 lines (349 loc) · 11.6 KB
/
CreateIndexRequest.cs
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* Copyright 2013-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using MongoDB.Bson;
using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver.Core.Operations
{
/// <summary>
/// Represents a create index request.
/// </summary>
public class CreateIndexRequest
{
// fields
private BsonDocument _additionalOptions;
private bool? _background;
private int? _bits;
private double? _bucketSize;
private Collation _collation;
private string _defaultLanguage;
private TimeSpan? _expireAfter;
private bool? _hidden;
private string _languageOverride;
private readonly BsonDocument _keys;
private double? _max;
private double? _min;
private string _name;
private BsonDocument _partialFilterExpression;
private bool? _sparse;
private int? _sphereIndexVersion;
private BsonDocument _storageEngine;
private int? _textIndexVersion;
private bool? _unique;
private int? _version;
private BsonDocument _weights;
private BsonDocument _wildcardProjection;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="CreateIndexRequest"/> class.
/// </summary>
/// <param name="keys">The keys.</param>
public CreateIndexRequest(BsonDocument keys)
{
_keys = Ensure.IsNotNull(keys, nameof(keys));
}
// properties
/// <summary>
/// Gets or sets the additional options.
/// </summary>
/// <value>
/// The additional options.
/// </value>
public BsonDocument AdditionalOptions
{
get { return _additionalOptions; }
set { _additionalOptions = value; }
}
/// <summary>
/// Gets or sets a value indicating whether the index should be created in the background.
/// </summary>
/// <value>
/// A value indicating whether the index should be created in the background.
/// </value>
public bool? Background
{
get { return _background; }
set { _background = value; }
}
/// <summary>
/// Gets or sets the bits of precision of the geohash values for 2d geo indexes.
/// </summary>
/// <value>
/// The bits of precision of the geohash values for 2d geo indexes.
/// </value>
public int? Bits
{
get { return _bits; }
set { _bits = value; }
}
/// <summary>
/// Gets or sets the size of the bucket for geo haystack indexes.
/// </summary>
/// <value>
/// The size of the bucket for geo haystack indexes.
/// </value>
[Obsolete("GeoHaystack indexes were deprecated in server version 4.4.")]
public double? BucketSize
{
get { return _bucketSize; }
set { _bucketSize = value; }
}
/// <summary>
/// Gets or sets the collation.
/// </summary>
public Collation Collation
{
get { return _collation; }
set { _collation = value; }
}
/// <summary>
/// Gets or sets the default language for text indexes.
/// </summary>
/// <value>
/// The default language for text indexes.
/// </value>
public string DefaultLanguage
{
get { return _defaultLanguage; }
set { _defaultLanguage = value; }
}
/// <summary>
/// Gets or sets when documents in a TTL collection expire.
/// </summary>
/// <value>
/// When documents in a TTL collection expire.
/// </value>
public TimeSpan? ExpireAfter
{
get { return _expireAfter; }
set { _expireAfter = value; }
}
/// <summary>
/// Gets or sets a value indicating whether the index is hidden.
/// </summary>
public bool? Hidden
{
get { return _hidden; }
set { _hidden = value; }
}
/// <summary>
/// Gets or sets the language override for text indexes.
/// </summary>
/// <value>
/// The language override for text indexes.
/// </value>
public string LanguageOverride
{
get { return _languageOverride; }
set { _languageOverride = value; }
}
/// <summary>
/// Gets the keys.
/// </summary>
/// <value>
/// The keys.
/// </value>
public BsonDocument Keys
{
get { return _keys; }
}
/// <summary>
/// Gets or sets the maximum coordinate value for 2d indexes.
/// </summary>
/// <value>
/// The maximum coordinate value for 2d indexesThe maximum.
/// </value>
public double? Max
{
get { return _max; }
set { _max = value; }
}
/// <summary>
/// Gets or sets the minimum coordinate value for 2d indexes.
/// </summary>
/// <value>
/// The minimum coordinate value for 2d indexes.
/// </value>
public double? Min
{
get { return _min; }
set { _min = value; }
}
/// <summary>
/// Gets or sets the index name.
/// </summary>
/// <value>
/// The index name.
/// </value>
public string Name
{
get { return _name; }
set { _name = value; }
}
/// <summary>
/// Gets or sets the partial filter expression.
/// </summary>
/// <value>
/// The partial filter expression.
/// </value>
public BsonDocument PartialFilterExpression
{
get { return _partialFilterExpression; }
set { _partialFilterExpression = value; }
}
/// <summary>
/// Gets or sets a value indicating whether the index is a sparse index.
/// </summary>
/// <value>
/// A value indicating whether the index is a sparse index.
/// </value>
public bool? Sparse
{
get { return _sparse; }
set { _sparse = value; }
}
/// <summary>
/// Gets or sets the 2dsphere index version.
/// </summary>
/// <value>
/// The 2dsphere index version.
/// </value>
public int? SphereIndexVersion
{
get { return _sphereIndexVersion; }
set { _sphereIndexVersion = value; }
}
/// <summary>
/// Gets or sets the storage engine options.
/// </summary>
/// <value>
/// The storage engine options.
/// </value>
public BsonDocument StorageEngine
{
get { return _storageEngine; }
set { _storageEngine = value; }
}
/// <summary>
/// Gets or sets the text index version.
/// </summary>
/// <value>
/// The text index version.
/// </value>
public int? TextIndexVersion
{
get { return _textIndexVersion; }
set { _textIndexVersion = value; }
}
/// <summary>
/// Gets or sets a value indicating whether the index enforces the uniqueness of the key values.
/// </summary>
/// <value>
/// A value indicating whether the index enforces the uniqueness of the key values.
/// </value>
public bool? Unique
{
get { return _unique; }
set { _unique = value; }
}
/// <summary>
/// Gets or sets the index version.
/// </summary>
/// <value>
/// The index version.
/// </value>
public int? Version
{
get { return _version; }
set { _version = value; }
}
/// <summary>
/// Gets or sets the weights for text indexes.
/// </summary>
/// <value>
/// The weights for text indexes.
/// </value>
public BsonDocument Weights
{
get { return _weights; }
set { _weights = value; }
}
/// <summary>
/// Gets or sets the wildcard projection.
/// </summary>
/// <value>
/// The wildcardProjection for indexes.
/// </value>
public BsonDocument WildcardProjection
{
get { return _wildcardProjection; }
set { _wildcardProjection = value; }
}
// publuc methods
/// <summary>
/// Gets the name of the index.
/// </summary>
/// <returns>The name of the index.</returns>
public string GetIndexName()
{
if (_name != null)
{
return _name;
}
if (_additionalOptions != null)
{
BsonValue name;
if (_additionalOptions.TryGetValue("name", out name))
{
return name.AsString;
}
}
return IndexNameHelper.GetIndexName(_keys);
}
// methods
internal BsonDocument CreateIndexDocument()
{
var document = new BsonDocument
{
{ "key", _keys },
{ "name", GetIndexName() },
{ "background", () => _background.Value, _background.HasValue },
{ "bits", () => _bits.Value, _bits.HasValue },
{ "bucketSize", () => _bucketSize.Value, _bucketSize.HasValue },
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
{ "default_language", () => _defaultLanguage, _defaultLanguage != null },
{ "expireAfterSeconds", () => _expireAfter.Value.TotalSeconds, _expireAfter.HasValue },
{ "hidden", () => _hidden.Value, _hidden.HasValue },
{ "language_override", () => _languageOverride, _languageOverride != null },
{ "max", () => _max.Value, _max.HasValue },
{ "min", () => _min.Value, _min.HasValue },
{ "partialFilterExpression", _partialFilterExpression, _partialFilterExpression != null },
{ "sparse", () => _sparse.Value, _sparse.HasValue },
{ "2dsphereIndexVersion", () => _sphereIndexVersion.Value, _sphereIndexVersion.HasValue },
{ "storageEngine", () => _storageEngine, _storageEngine != null },
{ "textIndexVersion", () => _textIndexVersion.Value, _textIndexVersion.HasValue },
{ "unique", () => _unique.Value, _unique.HasValue },
{ "v", () => _version.Value, _version.HasValue },
{ "weights", () => _weights, _weights != null },
{ "wildcardProjection", _wildcardProjection, _wildcardProjection != null }
};
if (_additionalOptions != null)
{
document.Merge(_additionalOptions, overwriteExistingElements: false);
}
return document;
}
}
}