Skip to content

Commit 3402ff1

Browse files
craiggwilsonrstam
authored andcommitted
test readonly enabled.
1 parent 45a070f commit 3402ff1

10 files changed

+112
-129
lines changed

Driver/Core/MongoConnectionStringBuilder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,13 @@ public override bool ContainsKey(string keyword)
838838
/// <returns>A WriteConcern.</returns>
839839
public WriteConcern GetWriteConcern(bool enabledDefault)
840840
{
841-
return WriteConcern.Create(enabledDefault, _safe, _fsync, _journal, _w, _wTimeout);
841+
return new WriteConcern(_safe.HasValue ? _safe.Value : enabledDefault)
842+
{
843+
FSync = _fsync,
844+
Journal = _journal,
845+
W = _w,
846+
WTimeout = _wTimeout
847+
};
842848
}
843849

844850
/// <summary>

Driver/Core/MongoUrl.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,13 @@ public override int GetHashCode()
500500
/// <returns>A WriteConcern.</returns>
501501
public WriteConcern GetWriteConcern(bool enabledDefault)
502502
{
503-
return WriteConcern.Create(enabledDefault, _safe, _fsync, _journal, _w, _wTimeout);
503+
return new WriteConcern(_safe.HasValue ? _safe.Value : enabledDefault)
504+
{
505+
FSync = _fsync,
506+
Journal = _journal,
507+
W = _w,
508+
WTimeout = _wTimeout
509+
};
504510
}
505511

506512
/// <summary>

Driver/Core/MongoUrlBuilder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,13 @@ private static string FormatMessage(string name, string value)
714714
/// <returns>A WriteConcern.</returns>
715715
public WriteConcern GetWriteConcern(bool enabledDefault)
716716
{
717-
return WriteConcern.Create(enabledDefault, _safe, _fsync, _journal, _w, _wTimeout);
717+
return new WriteConcern(_safe.HasValue ? _safe.Value : enabledDefault)
718+
{
719+
FSync = _fsync,
720+
Journal = _journal,
721+
W = _w,
722+
WTimeout = _wTimeout
723+
};
718724
}
719725

720726
/// <summary>

Driver/Core/SafeMode.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public SafeMode(bool enabled, bool fsync, int w, TimeSpan wtimeout)
9292
throw new ArgumentException("wtimeout cannot be non-zero when w is zero.");
9393
}
9494

95-
_writeConcern = new WriteConcern { Enabled = enabled };
95+
_writeConcern = new WriteConcern(false);
9696
if (fsync) { _writeConcern.FSync = fsync; }
97-
if (w != 0) { _writeConcern.W = w; }
97+
_writeConcern.W = w != 0 ? w : enabled ? 1 : 0;
9898
if (wtimeout != TimeSpan.Zero) { _writeConcern.WTimeout = wtimeout; }
9999
}
100100

@@ -201,9 +201,9 @@ public bool Enabled
201201
set
202202
{
203203
if (IsFrozen) { ThrowFrozenException(); }
204-
if (value)
204+
if (value && !_writeConcern.Enabled)
205205
{
206-
_writeConcern.Enabled = true;
206+
_writeConcern.W = 1;
207207
}
208208
else
209209
{
@@ -220,7 +220,6 @@ public bool FSync
220220
get { return _writeConcern.FSync ?? false; }
221221
set {
222222
if (IsFrozen) { ThrowFrozenException(); }
223-
_writeConcern.Enabled = true;
224223
_writeConcern.FSync = value;
225224
}
226225
}
@@ -251,7 +250,6 @@ public bool Journal
251250
get { return _writeConcern.Journal ?? false; }
252251
set {
253252
if (IsFrozen) { ThrowFrozenException(); }
254-
_writeConcern.Enabled = true;
255253
_writeConcern.Journal = value;
256254
}
257255
}
@@ -272,14 +270,8 @@ public int W
272270
{
273271
ResetValues();
274272
}
275-
else if (value == 1)
276-
{
277-
_writeConcern.Enabled = true;
278-
_writeConcern.W = null;
279-
}
280273
else
281274
{
282-
_writeConcern.Enabled = true;
283275
_writeConcern.W = value;
284276
}
285277
}
@@ -298,7 +290,6 @@ public string WMode
298290
set
299291
{
300292
if (IsFrozen) { ThrowFrozenException(); }
301-
_writeConcern.Enabled = true;
302293
_writeConcern.W = value;
303294
}
304295
}
@@ -311,7 +302,6 @@ public TimeSpan WTimeout
311302
get { return _writeConcern.WTimeout ?? TimeSpan.Zero; }
312303
set {
313304
if (IsFrozen) { ThrowFrozenException(); }
314-
_writeConcern.Enabled = true;
315305
_writeConcern.WTimeout = value;
316306
}
317307
}
@@ -568,10 +558,9 @@ public override string ToString()
568558
// private methods
569559
private void ResetValues()
570560
{
571-
_writeConcern.Enabled = false; // defaults to false because this is the obsolete SafeMode class
572561
_writeConcern.FSync = null;
573562
_writeConcern.Journal = null;
574-
_writeConcern.W = null;
563+
_writeConcern.W = 0; // defaults to 0 because this is the obsolete SafeMode class
575564
_writeConcern.WTimeout = null;
576565
}
577566

Driver/Core/WriteConcern.cs

Lines changed: 71 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)