@@ -30,15 +30,15 @@ namespace MongoDB.Driver
3030 public class WriteConcern : IEquatable < WriteConcern >
3131 {
3232 // 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 ( ) ;
3535 private readonly static WriteConcern __w2 = new WriteConcern { W = 2 } . Freeze ( ) ;
3636 private readonly static WriteConcern __w3 = new WriteConcern { W = 3 } . Freeze ( ) ;
3737 private readonly static WriteConcern __w4 = new WriteConcern { W = 4 } . Freeze ( ) ;
3838 private readonly static WriteConcern __wmajority = new WriteConcern { W = "majority" } . Freeze ( ) ;
3939
4040 // private fields
41- private bool _enabled = true ;
41+ private readonly bool _enabledDefault ;
4242 private bool ? _fsync ;
4343 private bool ? _journal ;
4444 private WValue _w ;
@@ -47,6 +47,19 @@ public class WriteConcern : IEquatable<WriteConcern>
4747 private bool _isFrozen ;
4848 private int _frozenHashCode ;
4949
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+
5063 // public static properties
5164 /// <summary>
5265 /// Gets an instance of WriteConcern that checks for errors.
@@ -102,15 +115,31 @@ public static WriteConcern WMajority
102115 /// </summary>
103116 public bool Enabled
104117 {
105- get { return _enabled ; }
106- set
118+ get
107119 {
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 )
110138 {
111- throw new InvalidOperationException ( "Enabled cannot be set to false if any other WriteConcern values have been set." ) ;
139+ return true ;
112140 }
113- _enabled = value ;
141+
142+ return _enabledDefault ;
114143 }
115144 }
116145
@@ -123,7 +152,6 @@ public bool? FSync
123152 set
124153 {
125154 if ( _isFrozen ) { ThrowFrozenException ( ) ; }
126- if ( value != null ) { EnsureEnabledIsTrue ( "FSync" ) ; }
127155 _fsync = value ;
128156 }
129157 }
@@ -145,7 +173,6 @@ public bool? Journal
145173 set
146174 {
147175 if ( _isFrozen ) { ThrowFrozenException ( ) ; }
148- if ( value != null ) { EnsureEnabledIsTrue ( "Journal" ) ; }
149176 _journal = value ;
150177 }
151178 }
@@ -159,21 +186,7 @@ public WValue W
159186 set
160187 {
161188 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 ;
177190 }
178191 }
179192
@@ -186,7 +199,6 @@ public TimeSpan? WTimeout
186199 set
187200 {
188201 if ( _isFrozen ) { ThrowFrozenException ( ) ; }
189- if ( value != null ) { EnsureEnabledIsTrue ( "WTimeout" ) ; }
190202 _wTimeout = value ;
191203 }
192204 }
@@ -227,40 +239,6 @@ public static bool Equals(WriteConcern lhs, WriteConcern rhs)
227239 return lhs . Equals ( rhs ) ;
228240 }
229241
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-
264242 // public methods
265243 /// <summary>
266244 /// Creates a clone of the WriteConcern.
@@ -269,7 +247,6 @@ internal static WriteConcern Create(
269247 public WriteConcern Clone ( )
270248 {
271249 var clone = new WriteConcern ( ) ;
272- clone . _enabled = _enabled ;
273250 clone . _fsync = _fsync ;
274251 clone . _journal = _journal ;
275252 clone . _w = _w ;
@@ -297,7 +274,6 @@ public bool Equals(WriteConcern rhs)
297274 if ( ( object ) rhs == null || GetType ( ) != rhs . GetType ( ) ) { return false ; }
298275 if ( ( object ) this == ( object ) rhs ) { return true ; }
299276 return
300- _enabled == rhs . _enabled &&
301277 _fsync == rhs . _fsync &&
302278 _journal == rhs . _journal &&
303279 _w == rhs . _w &&
@@ -314,6 +290,15 @@ public WriteConcern Freeze()
314290 {
315291 _frozenHashCode = GetHashCode ( ) ;
316292 _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+ }
317302 }
318303 return this ;
319304 }
@@ -347,7 +332,6 @@ public override int GetHashCode()
347332
348333 // see Effective Java by Joshua Bloch
349334 int hash = 17 ;
350- hash = 37 * hash + _enabled . GetHashCode ( ) ;
351335 hash = 37 * hash + _fsync . GetHashCode ( ) ;
352336 hash = 37 * hash + _journal . GetHashCode ( ) ;
353337 hash = 37 * hash + ( ( _w == null ) ? 0 : _w . GetHashCode ( ) ) ;
@@ -361,43 +345,35 @@ public override int GetHashCode()
361345 /// <returns>A string representation of the WriteConcern.</returns>
362346 public override string ToString ( )
363347 {
364- var sb = new StringBuilder ( ) ;
365- sb . AppendFormat ( "enabled={0}" , XmlConvert . ToString ( _enabled ) ) ;
366- if ( _enabled )
348+ if ( ! Enabled && _enabledDefault )
367349 {
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" ;
384351 }
385- return sb . ToString ( ) ;
386- }
387352
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+ }
393370
394- private void EnsureEnabledIsTrue ( string propertyName )
395- {
396- if ( ! _enabled )
371+ if ( parts . Count == 0 && Enabled )
397372 {
398- var message = string . Format ( "{0} cannot be set when Enabled is false." , propertyName ) ;
399- throw new InvalidOperationException ( message ) ;
373+ return "w=1" ;
400374 }
375+
376+ return string . Join ( "," , parts . ToArray ( ) ) ;
401377 }
402378
403379 private void ThrowFrozenException ( )
0 commit comments