-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
MongoCollectionSettings.cs
347 lines (325 loc) · 12.2 KB
/
MongoCollectionSettings.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
/* 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 System.Collections.Generic;
using System.Text;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver
{
/// <summary>
/// The settings used to access a collection.
/// </summary>
public class MongoCollectionSettings
{
// private fields
private Setting<bool> _assignIdOnInsert;
private Setting<GuidRepresentation> _guidRepresentation;
private Setting<ReadConcern> _readConcern;
private Setting<UTF8Encoding> _readEncoding;
private Setting<ReadPreference> _readPreference;
private Setting<WriteConcern> _writeConcern;
private Setting<UTF8Encoding> _writeEncoding;
// the following fields are set when Freeze is called
private bool _isFrozen;
private int _frozenHashCode;
private string _frozenStringRepresentation;
// constructors
/// <summary>
/// Initializes a new instance of the MongoCollectionSettings class.
/// </summary>
public MongoCollectionSettings()
{
}
// public properties
/// <summary>
/// Gets or sets a value indicating whether the driver should assign Id values when missing.
/// </summary>
public bool AssignIdOnInsert
{
get { return _assignIdOnInsert.Value; }
set
{
if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
_assignIdOnInsert.Value = value;
}
}
/// <summary>
/// Gets or sets the representation used for Guids.
/// </summary>
[Obsolete("Configure serializers instead.")]
public GuidRepresentation GuidRepresentation
{
get
{
if (BsonDefaults.GuidRepresentationMode != GuidRepresentationMode.V2)
{
throw new InvalidOperationException("MongoCollectionSettings.GuidRepresentation can only be used when BsonDefaults.GuidRepresentationMode is V2.");
}
return _guidRepresentation.Value;
}
set
{
if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
if (BsonDefaults.GuidRepresentationMode != GuidRepresentationMode.V2)
{
throw new InvalidOperationException("MongoCollectionSettings.GuidRepresentation can only be used when BsonDefaults.GuidRepresentationMode is V2.");
}
_guidRepresentation.Value = value;
}
}
/// <summary>
/// Gets a value indicating whether the settings have been frozen to prevent further changes.
/// </summary>
public bool IsFrozen
{
get { return _isFrozen; }
}
/// <summary>
/// Gets or sets the read concern.
/// </summary>
public ReadConcern ReadConcern
{
get { return _readConcern.Value; }
set
{
if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
_readConcern.Value = Ensure.IsNotNull(value, nameof(value));
}
}
/// <summary>
/// Gets or sets the Read Encoding.
/// </summary>
public UTF8Encoding ReadEncoding
{
get { return _readEncoding.Value; }
set
{
if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
_readEncoding.Value = value;
}
}
/// <summary>
/// Gets or sets the read preference to use.
/// </summary>
public ReadPreference ReadPreference
{
get { return _readPreference.Value; }
set
{
if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
if (value == null)
{
throw new ArgumentNullException("value");
}
_readPreference.Value = value;
}
}
/// <summary>
/// Gets the serializer registry.
/// </summary>
public IBsonSerializerRegistry SerializerRegistry
{
get { return BsonSerializer.SerializerRegistry; }
}
/// <summary>
/// Gets or sets the WriteConcern to use.
/// </summary>
public WriteConcern WriteConcern
{
get { return _writeConcern.Value; }
set
{
if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
if (value == null)
{
throw new ArgumentNullException("value");
}
_writeConcern.Value = value;
}
}
/// <summary>
/// Gets or sets the Write Encoding.
/// </summary>
public UTF8Encoding WriteEncoding
{
get { return _writeEncoding.Value; }
set
{
if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
_writeEncoding.Value = value;
}
}
// public methods
/// <summary>
/// Creates a clone of the settings.
/// </summary>
/// <returns>A clone of the settings.</returns>
public virtual MongoCollectionSettings Clone()
{
var clone = new MongoCollectionSettings();
clone._assignIdOnInsert = _assignIdOnInsert.Clone();
clone._guidRepresentation = _guidRepresentation.Clone();
clone._readConcern = _readConcern.Clone();
clone._readEncoding = _readEncoding.Clone();
clone._readPreference = _readPreference.Clone();
clone._writeConcern = _writeConcern.Clone();
clone._writeEncoding = _writeEncoding.Clone();
return clone;
}
/// <summary>
/// Compares two MongoCollectionSettings instances.
/// </summary>
/// <param name="obj">The other instance.</param>
/// <returns>True if the two instances are equal.</returns>
public override bool Equals(object obj)
{
var rhs = obj as MongoCollectionSettings;
if (rhs == null)
{
return false;
}
else
{
if (_isFrozen && rhs._isFrozen)
{
return _frozenStringRepresentation == rhs._frozenStringRepresentation;
}
else
{
return
_assignIdOnInsert.Value == rhs._assignIdOnInsert.Value &&
_guidRepresentation.Value == rhs._guidRepresentation.Value &&
object.Equals(_readConcern.Value, rhs._readConcern.Value) &&
object.Equals(_readEncoding, rhs._readEncoding) &&
_readPreference.Value == rhs._readPreference.Value &&
_writeConcern.Value == rhs._writeConcern.Value &&
object.Equals(_writeEncoding, rhs._writeEncoding);
}
}
}
/// <summary>
/// Freezes the settings.
/// </summary>
/// <returns>The frozen settings.</returns>
public MongoCollectionSettings Freeze()
{
if (!_isFrozen)
{
_frozenHashCode = GetHashCode();
_frozenStringRepresentation = ToString();
_isFrozen = true;
}
return this;
}
/// <summary>
/// Returns a frozen copy of the settings.
/// </summary>
/// <returns>A frozen copy of the settings.</returns>
public MongoCollectionSettings FrozenCopy()
{
if (_isFrozen)
{
return this;
}
else
{
return Clone().Freeze();
}
}
/// <summary>
/// Gets the hash code.
/// </summary>
/// <returns>The hash code.</returns>
public override int GetHashCode()
{
if (_isFrozen)
{
return _frozenHashCode;
}
// see Effective Java by Joshua Bloch
int hash = 17;
hash = 37 * hash + _assignIdOnInsert.Value.GetHashCode();
hash = 37 * hash + _guidRepresentation.Value.GetHashCode();
hash = 37 * hash + ((_readConcern.Value == null) ? 0 : _readConcern.Value.GetHashCode());
hash = 37 * hash + ((_readEncoding.Value == null) ? 0 : _readEncoding.Value.GetHashCode());
hash = 37 * hash + ((_readPreference.Value == null) ? 0 : _readPreference.Value.GetHashCode());
hash = 37 * hash + ((_writeConcern.Value == null) ? 0 : _writeConcern.Value.GetHashCode());
hash = 37 * hash + ((_writeEncoding.Value == null) ? 0 : _writeEncoding.Value.GetHashCode());
return hash;
}
/// <summary>
/// Returns a string representation of the settings.
/// </summary>
/// <returns>A string representation of the settings.</returns>
public override string ToString()
{
if (_isFrozen)
{
return _frozenStringRepresentation;
}
var parts = new List<string>();
parts.Add(string.Format("AssignIdOnInsert={0}", _assignIdOnInsert));
parts.Add(string.Format("GuidRepresentation={0}", _guidRepresentation));
parts.Add(string.Format("ReadConcern={0}", _readConcern.Value));
if (_readEncoding.HasBeenSet)
{
parts.Add(string.Format("ReadEncoding={0}", (_readEncoding.Value == null) ? "null" : "UTF8Encoding"));
}
parts.Add(string.Format("ReadPreference={0}", _readPreference));
parts.Add(string.Format("WriteConcern={0}", _writeConcern));
if (_writeEncoding.HasBeenSet)
{
parts.Add(string.Format("WriteEncoding={0}", (_writeEncoding.Value == null) ? "null" : "UTF8Encoding"));
}
return string.Join(";", parts.ToArray());
}
// internal methods
internal void ApplyDefaultValues(MongoDatabaseSettings databaseSettings)
{
if (!_assignIdOnInsert.HasBeenSet)
{
AssignIdOnInsert = MongoDefaults.AssignIdOnInsert;
}
#pragma warning disable 618
if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && !_guidRepresentation.HasBeenSet)
{
GuidRepresentation = databaseSettings.GuidRepresentation;
}
#pragma warning restore 618
if (!_readConcern.HasBeenSet)
{
ReadConcern = databaseSettings.ReadConcern;
}
if (!_readEncoding.HasBeenSet)
{
ReadEncoding = databaseSettings.ReadEncoding;
}
if (!_readPreference.HasBeenSet)
{
ReadPreference = databaseSettings.ReadPreference;
}
if (!_writeConcern.HasBeenSet)
{
WriteConcern = databaseSettings.WriteConcern;
}
if (!_writeEncoding.HasBeenSet)
{
WriteEncoding = databaseSettings.WriteEncoding;
}
}
}
}