@@ -30,15 +30,15 @@ namespace MongoDB.Driver
30
30
public class WriteConcern : IEquatable < WriteConcern >
31
31
{
32
32
// private static fields
33
- private readonly static WriteConcern __errors = new WriteConcern { Enabled = true } . Freeze ( ) ;
34
- private readonly static WriteConcern __none = new WriteConcern { Enabled = false } . Freeze ( ) ;
33
+ private readonly static WriteConcern __errors = new WriteConcern { W = 1 } . Freeze ( ) ;
34
+ private readonly static WriteConcern __none = new WriteConcern ( ) . Freeze ( ) ;
35
35
private readonly static WriteConcern __w2 = new WriteConcern { W = 2 } . Freeze ( ) ;
36
36
private readonly static WriteConcern __w3 = new WriteConcern { W = 3 } . Freeze ( ) ;
37
37
private readonly static WriteConcern __w4 = new WriteConcern { W = 4 } . Freeze ( ) ;
38
38
private readonly static WriteConcern __wmajority = new WriteConcern { W = "majority" } . Freeze ( ) ;
39
39
40
40
// private fields
41
- private bool _enabled = true ;
41
+ private readonly bool _enabledDefault ;
42
42
private bool ? _fsync ;
43
43
private bool ? _journal ;
44
44
private WValue _w ;
@@ -47,6 +47,19 @@ public class WriteConcern : IEquatable<WriteConcern>
47
47
private bool _isFrozen ;
48
48
private int _frozenHashCode ;
49
49
50
+ // constructors
51
+ /// <summary>
52
+ /// Initializes a new instance of the <see cref="WriteConcern" /> class.
53
+ /// </summary>
54
+ public WriteConcern ( )
55
+ : this ( true )
56
+ { }
57
+
58
+ internal WriteConcern ( bool enabledDefault )
59
+ {
60
+ _enabledDefault = enabledDefault ;
61
+ }
62
+
50
63
// public static properties
51
64
/// <summary>
52
65
/// Gets an instance of WriteConcern that checks for errors.
@@ -102,15 +115,31 @@ public static WriteConcern WMajority
102
115
/// </summary>
103
116
public bool Enabled
104
117
{
105
- get { return _enabled ; }
106
- set
118
+ get
107
119
{
108
- if ( _isFrozen ) { ThrowFrozenException ( ) ; }
109
- if ( ! value && AnyWriteConcernSettingsAreSet ( ) )
120
+ // order here doesn't matter because
121
+ // after we are frozen there will be
122
+ // no conflicting values
123
+ if ( _w != null )
124
+ {
125
+ var wCount = _w as WCount ;
126
+ if ( wCount != null )
127
+ {
128
+ return wCount . Value > 0 ;
129
+ }
130
+
131
+ return true ;
132
+ }
133
+ if ( _fsync . HasValue )
134
+ {
135
+ return true ;
136
+ }
137
+ if ( _journal . HasValue )
110
138
{
111
- throw new InvalidOperationException ( "Enabled cannot be set to false if any other WriteConcern values have been set." ) ;
139
+ return true ;
112
140
}
113
- _enabled = value ;
141
+
142
+ return _enabledDefault ;
114
143
}
115
144
}
116
145
@@ -123,7 +152,6 @@ public bool? FSync
123
152
set
124
153
{
125
154
if ( _isFrozen ) { ThrowFrozenException ( ) ; }
126
- if ( value != null ) { EnsureEnabledIsTrue ( "FSync" ) ; }
127
155
_fsync = value ;
128
156
}
129
157
}
@@ -145,7 +173,6 @@ public bool? Journal
145
173
set
146
174
{
147
175
if ( _isFrozen ) { ThrowFrozenException ( ) ; }
148
- if ( value != null ) { EnsureEnabledIsTrue ( "Journal" ) ; }
149
176
_journal = value ;
150
177
}
151
178
}
@@ -159,21 +186,7 @@ public WValue W
159
186
set
160
187
{
161
188
if ( _isFrozen ) { ThrowFrozenException ( ) ; }
162
- if ( value is WCount && ( ( WCount ) value ) . Value == 0 )
163
- {
164
- _w = null ;
165
- Enabled = false ;
166
- }
167
- else if ( value is WCount && ( ( WCount ) value ) . Value == 1 )
168
- {
169
- Enabled = true ;
170
- _w = null ;
171
- }
172
- else
173
- {
174
- if ( value != null ) { EnsureEnabledIsTrue ( "W" ) ; }
175
- _w = value ;
176
- }
189
+ _w = value ;
177
190
}
178
191
}
179
192
@@ -186,7 +199,6 @@ public TimeSpan? WTimeout
186
199
set
187
200
{
188
201
if ( _isFrozen ) { ThrowFrozenException ( ) ; }
189
- if ( value != null ) { EnsureEnabledIsTrue ( "WTimeout" ) ; }
190
202
_wTimeout = value ;
191
203
}
192
204
}
@@ -227,40 +239,6 @@ public static bool Equals(WriteConcern lhs, WriteConcern rhs)
227
239
return lhs . Equals ( rhs ) ;
228
240
}
229
241
230
- // internal static methods
231
- internal static WriteConcern Create (
232
- bool enabledDefault ,
233
- bool ? safe ,
234
- bool ? fsync ,
235
- bool ? journal ,
236
- WValue w ,
237
- TimeSpan ? wTimeout )
238
- {
239
- var wIsMagicZero = false ;
240
- var wIsMagicOne = false ;
241
- if ( safe == null )
242
- {
243
- wIsMagicZero = w is WCount && ( ( WCount ) w ) . Value == 0 ;
244
- wIsMagicOne = w is WCount && ( ( WCount ) w ) . Value == 1 ;
245
- }
246
- var wIsMagic = wIsMagicZero || wIsMagicOne ;
247
-
248
- var enabled = enabledDefault ;
249
- if ( safe != null ) { enabled = safe . Value ; }
250
- else if ( wIsMagicZero ) { enabled = false ; }
251
- else if ( wIsMagicOne ) { enabled = true ; }
252
- else if ( fsync != null || journal != null || w != null || wTimeout != null ) { enabled = true ; }
253
-
254
- return new WriteConcern
255
- {
256
- Enabled = enabled ,
257
- FSync = fsync ,
258
- Journal = journal ,
259
- W = wIsMagic ? null : w ,
260
- WTimeout = wTimeout
261
- } ;
262
- }
263
-
264
242
// public methods
265
243
/// <summary>
266
244
/// Creates a clone of the WriteConcern.
@@ -269,7 +247,6 @@ internal static WriteConcern Create(
269
247
public WriteConcern Clone ( )
270
248
{
271
249
var clone = new WriteConcern ( ) ;
272
- clone . _enabled = _enabled ;
273
250
clone . _fsync = _fsync ;
274
251
clone . _journal = _journal ;
275
252
clone . _w = _w ;
@@ -297,7 +274,6 @@ public bool Equals(WriteConcern rhs)
297
274
if ( ( object ) rhs == null || GetType ( ) != rhs . GetType ( ) ) { return false ; }
298
275
if ( ( object ) this == ( object ) rhs ) { return true ; }
299
276
return
300
- _enabled == rhs . _enabled &&
301
277
_fsync == rhs . _fsync &&
302
278
_journal == rhs . _journal &&
303
279
_w == rhs . _w &&
@@ -314,6 +290,15 @@ public WriteConcern Freeze()
314
290
{
315
291
_frozenHashCode = GetHashCode ( ) ;
316
292
_isFrozen = true ;
293
+
294
+ if ( _fsync . HasValue || _journal . HasValue )
295
+ {
296
+ if ( _w != null && _w is WCount && ( ( WCount ) _w ) . Value == 0 )
297
+ {
298
+ var message = string . Format ( "There are conflicting values in WriteConcern({0}). When W=0, no other values may be set." , ToString ( ) ) ;
299
+ throw new MongoException ( message ) ;
300
+ }
301
+ }
317
302
}
318
303
return this ;
319
304
}
@@ -347,7 +332,6 @@ public override int GetHashCode()
347
332
348
333
// see Effective Java by Joshua Bloch
349
334
int hash = 17 ;
350
- hash = 37 * hash + _enabled . GetHashCode ( ) ;
351
335
hash = 37 * hash + _fsync . GetHashCode ( ) ;
352
336
hash = 37 * hash + _journal . GetHashCode ( ) ;
353
337
hash = 37 * hash + ( ( _w == null ) ? 0 : _w . GetHashCode ( ) ) ;
@@ -361,43 +345,35 @@ public override int GetHashCode()
361
345
/// <returns>A string representation of the WriteConcern.</returns>
362
346
public override string ToString ( )
363
347
{
364
- var sb = new StringBuilder ( ) ;
365
- sb . AppendFormat ( "enabled={0}" , XmlConvert . ToString ( _enabled ) ) ;
366
- if ( _enabled )
348
+ if ( ! Enabled && _enabledDefault )
367
349
{
368
- if ( _fsync != null )
369
- {
370
- sb . AppendFormat ( ",fsync={0}" , _fsync . Value ) ;
371
- }
372
- if ( _journal != null )
373
- {
374
- sb . AppendFormat ( ",journal={0}" , _journal . Value ) ;
375
- }
376
- if ( _w != null )
377
- {
378
- sb . AppendFormat ( ",w={0}" , _w ) ;
379
- }
380
- if ( _wTimeout != null )
381
- {
382
- sb . AppendFormat ( ",wtimeout={0}" , MongoUrlBuilder . FormatTimeSpan ( _wTimeout . Value ) ) ;
383
- }
350
+ return "w=0" ;
384
351
}
385
- return sb . ToString ( ) ;
386
- }
387
352
388
- // private methods
389
- private bool AnyWriteConcernSettingsAreSet ( )
390
- {
391
- return _fsync != null || _journal != null || _w != null || _wTimeout != null ;
392
- }
353
+ List < string > parts = new List < string > ( ) ;
354
+ if ( _fsync != null )
355
+ {
356
+ parts . Add ( string . Format ( "fsync={0}" , _fsync . Value ) ) ;
357
+ }
358
+ if ( _journal != null )
359
+ {
360
+ parts . Add ( string . Format ( ",journal={0}" , _journal . Value ) ) ;
361
+ }
362
+ if ( _w != null )
363
+ {
364
+ parts . Add ( string . Format ( ",w={0}" , _w ) ) ;
365
+ }
366
+ if ( _wTimeout != null )
367
+ {
368
+ parts . Add ( string . Format ( ",wtimeout={0}" , MongoUrlBuilder . FormatTimeSpan ( _wTimeout . Value ) ) ) ;
369
+ }
393
370
394
- private void EnsureEnabledIsTrue ( string propertyName )
395
- {
396
- if ( ! _enabled )
371
+ if ( parts . Count == 0 && Enabled )
397
372
{
398
- var message = string . Format ( "{0} cannot be set when Enabled is false." , propertyName ) ;
399
- throw new InvalidOperationException ( message ) ;
373
+ return "w=1" ;
400
374
}
375
+
376
+ return string . Join ( "," , parts . ToArray ( ) ) ;
401
377
}
402
378
403
379
private void ThrowFrozenException ( )
0 commit comments