-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
CollectionOptionsBuilder.cs
289 lines (264 loc) · 10.8 KB
/
CollectionOptionsBuilder.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
/* Copyright 2010-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.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Serializers;
namespace MongoDB.Driver.Builders
{
/// <summary>
/// A builder for the options used when creating a collection.
/// </summary>
public static class CollectionOptions
{
// public static properties
/// <summary>
/// Gets a null value with a type of IMongoCollectionOptions.
/// </summary>
public static IMongoCollectionOptions Null
{
get { return null; }
}
// public static methods
/// <summary>
/// Sets whether to automatically create an index on the _id element.
/// </summary>
/// <param name="value">Whether to automatically create an index on the _id element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
[Obsolete("AutoIndexId has been deprecated since server version 3.2.")]
public static CollectionOptionsBuilder SetAutoIndexId(bool value)
{
return new CollectionOptionsBuilder().SetAutoIndexId(value);
}
/// <summary>
/// Sets whether the collection is capped.
/// </summary>
/// <param name="value">Whether the collection is capped.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static CollectionOptionsBuilder SetCapped(bool value)
{
return new CollectionOptionsBuilder().SetCapped(value);
}
/// <summary>
/// Sets the collation.
/// </summary>
/// <param name="value">The collation.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static CollectionOptionsBuilder SetCollation(Collation value)
{
return new CollectionOptionsBuilder().SetCollation(value);
}
/// <summary>
/// Sets the index options defaults.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static CollectionOptionsBuilder SetIndexOptionDefaults(IndexOptionDefaults value)
{
return new CollectionOptionsBuilder().SetIndexOptionDefaults(value);
}
/// <summary>
/// Sets the max number of documents in a capped collection.
/// </summary>
/// <param name="value">The max number of documents.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static CollectionOptionsBuilder SetMaxDocuments(long value)
{
return new CollectionOptionsBuilder().SetMaxDocuments(value);
}
/// <summary>
/// Sets the max size of a capped collection.
/// </summary>
/// <param name="value">The max size.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static CollectionOptionsBuilder SetMaxSize(long value)
{
return new CollectionOptionsBuilder().SetMaxSize(value);
}
/// <summary>
/// Sets the storage engine options.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static CollectionOptionsBuilder SetStorageEngineOptions(BsonDocument value)
{
return new CollectionOptionsBuilder().SetStorageEngineOptions(value);
}
/// <summary>
/// Sets the validation action.
/// </summary>
/// <param name="validationAction">The validation action.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static CollectionOptionsBuilder SetValidationAction(DocumentValidationAction validationAction)
{
return new CollectionOptionsBuilder().SetValidationAction(validationAction);
}
/// <summary>
/// Sets the validation level.
/// </summary>
/// <param name="validationLevel">The validation level.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static CollectionOptionsBuilder SetValidationLevel(DocumentValidationLevel validationLevel)
{
return new CollectionOptionsBuilder().SetValidationLevel(validationLevel);
}
/// <summary>
/// Sets the validator.
/// </summary>
/// <param name="validator">The validator.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static CollectionOptionsBuilder SetValidator(IMongoQuery validator)
{
return new CollectionOptionsBuilder().SetValidator(validator);
}
}
/// <summary>
/// A builder for the options used when creating a collection.
/// </summary>
[Serializable]
[BsonSerializer(typeof(CollectionOptionsBuilder.Serializer))]
public class CollectionOptionsBuilder : BuilderBase, IMongoCollectionOptions
{
// private fields
private BsonDocument _document;
// constructors
/// <summary>
/// Initializes a new instance of the CollectionOptionsBuilder class.
/// </summary>
public CollectionOptionsBuilder()
{
_document = new BsonDocument();
}
// public methods
/// <summary>
/// Sets whether to automatically create an index on the _id element.
/// </summary>
/// <param name="value">Whether to automatically create an index on the _id element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
[Obsolete("AutoIndexId has been deprecated since server version 3.2.")]
public CollectionOptionsBuilder SetAutoIndexId(bool value)
{
_document["autoIndexId"] = value;
return this;
}
/// <summary>
/// Sets whether the collection is capped.
/// </summary>
/// <param name="value">Whether the collection is capped.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public CollectionOptionsBuilder SetCapped(bool value)
{
_document["capped"] = value;
return this;
}
/// <summary>
/// Sets the collation.
/// </summary>
/// <param name="value">The collation.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public CollectionOptionsBuilder SetCollation(Collation value)
{
_document["collation"] = value.ToBsonDocument();
return this;
}
/// <summary>
/// Sets the index options defaults.
/// </summary>
/// <param name="value">The index options defaults.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public CollectionOptionsBuilder SetIndexOptionDefaults(IndexOptionDefaults value)
{
_document["indexOptionDefaults"] = value.ToBsonDocument();
return this;
}
/// <summary>
/// Sets the max number of documents in a capped collection.
/// </summary>
/// <param name="value">The max number of documents.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public CollectionOptionsBuilder SetMaxDocuments(long value)
{
_document["max"] = value;
return this;
}
/// <summary>
/// Sets the max size of a capped collection.
/// </summary>
/// <param name="value">The max size.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public CollectionOptionsBuilder SetMaxSize(long value)
{
_document["size"] = value;
return this;
}
/// <summary>
/// Sets the storage engine options.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public CollectionOptionsBuilder SetStorageEngineOptions(BsonDocument value)
{
_document["storageEngine"] = value;
return this;
}
/// <summary>
/// Sets the validation action.
/// </summary>
/// <param name="validationAction">The validation action.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public CollectionOptionsBuilder SetValidationAction(DocumentValidationAction validationAction)
{
_document["validationAction"] = validationAction.ToString().ToLowerInvariant();
return this;
}
/// <summary>
/// Sets the validation level.
/// </summary>
/// <param name="validationLevel">The validation level.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public CollectionOptionsBuilder SetValidationLevel(DocumentValidationLevel validationLevel)
{
_document["validationLevel"] = validationLevel.ToString().ToLowerInvariant();
return this;
}
/// <summary>
/// Sets the validator.
/// </summary>
/// <param name="validator">The validator.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public CollectionOptionsBuilder SetValidator(IMongoQuery validator)
{
_document["validator"] = validator.ToBsonDocument();
return this;
}
/// <summary>
/// Returns the result of the builder as a BsonDocument.
/// </summary>
/// <returns>A BsonDocument.</returns>
public override BsonDocument ToBsonDocument()
{
return _document;
}
// nested classes
new internal class Serializer : SerializerBase<CollectionOptionsBuilder>
{
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, CollectionOptionsBuilder value)
{
BsonDocumentSerializer.Instance.Serialize(context, value._document);
}
}
}
}