Skip to content

Commit 53c8c8e

Browse files
author
rstam
committed
CSHARP-627: Remove use of safe=true (still accepted on connection string for backward compatibility). Standardize on Acknowledged and Unacknowledged as the names of the WriteConcern constants for w=1 and w=0. Get unit tests to pass again.
1 parent 3402ff1 commit 53c8c8e

24 files changed

+473
-577
lines changed

BsonUnitTests/DefaultSerializer/Serializers/NetPrimitiveSerializerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,10 +1815,10 @@ public void TestMongoDB()
18151815
{
18161816
var obj = new TestClass
18171817
{
1818-
V = new Uri("mongodb://localhost/?safe=true")
1818+
V = new Uri("mongodb://localhost/?w=1")
18191819
};
18201820
var json = obj.ToJson();
1821-
var expected = "{ 'V' : 'mongodb://localhost/?safe=true' }".Replace("'", "\"");
1821+
var expected = "{ 'V' : 'mongodb://localhost/?w=1' }".Replace("'", "\"");
18221822
Assert.AreEqual(expected, json);
18231823

18241824
var bson = obj.ToBson();

Driver/Core/MongoClientSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public MongoClientSettings()
8282
_verifySslCertificate = true;
8383
_waitQueueSize = MongoDefaults.ComputedWaitQueueSize;
8484
_waitQueueTimeout = MongoDefaults.WaitQueueTimeout;
85-
_writeConcern = WriteConcern.Errors;
85+
_writeConcern = WriteConcern.Acknowledged;
8686
}
8787

8888
// public properties

Driver/Core/MongoCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public virtual WriteConcernResult CreateIndex(IMongoIndexKeys keys, IMongoIndexO
194194
var insertOptions = new MongoInsertOptions
195195
{
196196
CheckElementNames = false,
197-
WriteConcern = WriteConcern.Errors
197+
WriteConcern = WriteConcern.Acknowledged
198198
};
199199
var result = indexes.Insert(index, insertOptions);
200200
return result;

Driver/Core/MongoConnectionStringBuilder.cs

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public class MongoConnectionStringBuilder : DbConnectionStringBuilder
5454
{ "readpreference", "readPreference" },
5555
{ "readpreferencetags", "readPreferenceTags" },
5656
{ "replicaset", "replicaSet" },
57-
{ "safe", "safe" },
5857
{ "secondaryacceptablelatency", "secondaryAcceptableLatency" },
5958
{ "secondaryacceptablelatencyms", "secondaryAcceptableLatency" },
6059
{ "server", "server" },
@@ -91,7 +90,6 @@ public class MongoConnectionStringBuilder : DbConnectionStringBuilder
9190
private string _password;
9291
private ReadPreference _readPreference;
9392
private string _replicaSetName;
94-
private bool? _safe;
9593
private TimeSpan _secondaryAcceptableLatency;
9694
private IEnumerable<MongoServerAddress> _servers;
9795
private bool? _slaveOk;
@@ -376,27 +374,6 @@ public string ReplicaSetName
376374
}
377375
}
378376

379-
/// <summary>
380-
/// Gets or sets the safe value.
381-
/// </summary>
382-
[Obsolete("Use W=0 or W=1 instead.")]
383-
public bool? Safe
384-
{
385-
get { return _safe; }
386-
set
387-
{
388-
if (value != null)
389-
{
390-
if (!value.Value && AnyWriteConcernSettingsAreSet())
391-
{
392-
throw new InvalidOperationException("Safe cannot be set to false if any other write concern values have been set.");
393-
}
394-
}
395-
_safe = value;
396-
base["safe"] = (value == null) ? null : XmlConvert.ToString(value.Value);
397-
}
398-
}
399-
400377
/// <summary>
401378
/// Gets or sets the SafeMode to use.
402379
/// </summary>
@@ -405,7 +382,7 @@ public SafeMode SafeMode
405382
{
406383
get
407384
{
408-
if (_safe != null || AnyWriteConcernSettingsAreSet())
385+
if (AnyWriteConcernSettingsAreSet())
409386
{
410387
#pragma warning disable 618
411388
return new SafeMode(GetWriteConcern(false));
@@ -418,23 +395,20 @@ public SafeMode SafeMode
418395
}
419396
set
420397
{
421-
Safe = null;
422-
FSync = null;
423-
Journal = null;
424-
W = null;
425-
WTimeout = null;
426-
427-
if (value != null)
398+
if (value == null)
428399
{
429-
Safe = value.Enabled;
430-
if (value.Enabled)
431-
{
432-
var writeConcern = value.WriteConcern;
433-
if (writeConcern.FSync != null) { FSync = writeConcern.FSync.Value; }
434-
if (writeConcern.Journal != null) { Journal = writeConcern.Journal.Value; }
435-
if (writeConcern.W != null) { W = writeConcern.W; }
436-
if (writeConcern.WTimeout != null) { WTimeout = writeConcern.WTimeout.Value; }
437-
}
400+
FSync = null;
401+
Journal = null;
402+
W = null;
403+
WTimeout = null;
404+
}
405+
else
406+
{
407+
var writeConcern = value.WriteConcern;
408+
FSync = writeConcern.FSync;
409+
Journal = writeConcern.Journal;
410+
W = writeConcern.W ?? (writeConcern.Enabled ? 1 : 0);
411+
WTimeout = writeConcern.WTimeout;
438412
}
439413
}
440414
}
@@ -746,9 +720,27 @@ public override object this[string keyword]
746720
ReplicaSetName = (string)value;
747721
break;
748722
case "safe":
749-
#pragma warning disable 618
750-
Safe = Convert.ToBoolean(value);
751-
#pragma warning restore
723+
var safe = Convert.ToBoolean(value);
724+
if (_w == null)
725+
{
726+
W = safe ? 1 : 0;
727+
}
728+
else
729+
{
730+
if (safe)
731+
{
732+
// don't overwrite existing W value unless it's 0
733+
var wCount = _w as WriteConcern.WCount;
734+
if (wCount != null && wCount.Value == 0)
735+
{
736+
W = 1;
737+
}
738+
}
739+
else
740+
{
741+
W = 0;
742+
}
743+
}
752744
break;
753745
case "secondaryacceptablelatency":
754746
case "secondaryacceptablelatencyms":
@@ -838,7 +830,7 @@ public override bool ContainsKey(string keyword)
838830
/// <returns>A WriteConcern.</returns>
839831
public WriteConcern GetWriteConcern(bool enabledDefault)
840832
{
841-
return new WriteConcern(_safe.HasValue ? _safe.Value : enabledDefault)
833+
return new WriteConcern(enabledDefault)
842834
{
843835
FSync = _fsync,
844836
Journal = _journal,
@@ -945,7 +937,6 @@ private void ResetValues()
945937
_password = null;
946938
_readPreference = null;
947939
_replicaSetName = null;
948-
_safe = null;
949940
_secondaryAcceptableLatency = MongoDefaults.SecondaryAcceptableLatency;
950941
_servers = null;
951942
_slaveOk = null;

Driver/Core/MongoCursorEnumerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ private void KillCursor()
401401
{
402402
using (var message = new MongoKillCursorsMessage(_openCursorId))
403403
{
404-
connection.SendMessage(message, WriteConcern.None, _cursor.Database.Name);
404+
connection.SendMessage(message, WriteConcern.Unacknowledged, _cursor.Database.Name);
405405
}
406406
}
407407
finally

Driver/Core/MongoUrl.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public class MongoUrl : IEquatable<MongoUrl>
7373
private readonly int _minConnectionPoolSize;
7474
private readonly ReadPreference _readPreference;
7575
private readonly string _replicaSetName;
76-
private readonly bool? _safe;
7776
private readonly TimeSpan _secondaryAcceptableLatency;
7877
private readonly IEnumerable<MongoServerAddress> _servers;
7978
private readonly bool _slaveOk;
@@ -109,9 +108,6 @@ public MongoUrl(string url)
109108
_minConnectionPoolSize = builder.MinConnectionPoolSize;
110109
_readPreference = builder.ReadPreference;
111110
_replicaSetName = builder.ReplicaSetName;
112-
#pragma warning disable 618
113-
_safe = builder.Safe;
114-
#pragma warning restore
115111
_secondaryAcceptableLatency = builder.SecondaryAcceptableLatency;
116112
_servers = builder.Servers;
117113
#pragma warning disable 618
@@ -259,15 +255,6 @@ public string ReplicaSetName
259255
get { return _replicaSetName; }
260256
}
261257

262-
/// <summary>
263-
/// Gets the safe value.
264-
/// </summary>
265-
[Obsolete("Use W=0 or W=1 instead.")]
266-
public bool? Safe
267-
{
268-
get { return _safe; }
269-
}
270-
271258
/// <summary>
272259
/// Gets the SafeMode to use.
273260
/// </summary>
@@ -276,7 +263,7 @@ public SafeMode SafeMode
276263
{
277264
get
278265
{
279-
if (_safe != null || AnyWriteConcernSettingsAreSet())
266+
if (AnyWriteConcernSettingsAreSet())
280267
{
281268
#pragma warning disable 618
282269
return new SafeMode(GetWriteConcern(false));
@@ -500,7 +487,7 @@ public override int GetHashCode()
500487
/// <returns>A WriteConcern.</returns>
501488
public WriteConcern GetWriteConcern(bool enabledDefault)
502489
{
503-
return new WriteConcern(_safe.HasValue ? _safe.Value : enabledDefault)
490+
return new WriteConcern(enabledDefault)
504491
{
505492
FSync = _fsync,
506493
Journal = _journal,

Driver/Core/MongoUrlBuilder.cs

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public class MongoUrlBuilder
4848
private int _minConnectionPoolSize;
4949
private ReadPreference _readPreference;
5050
private string _replicaSetName;
51-
private bool? _safe;
5251
private TimeSpan _secondaryAcceptableLatency;
5352
private IEnumerable<MongoServerAddress> _servers;
5453
private bool? _slaveOk;
@@ -286,26 +285,6 @@ public string ReplicaSetName
286285
set { _replicaSetName = value; }
287286
}
288287

289-
/// <summary>
290-
/// Gets or sets the safe value.
291-
/// </summary>
292-
[Obsolete("Use W=0 or W=1 instead.")]
293-
public bool? Safe
294-
{
295-
get { return _safe; }
296-
set
297-
{
298-
if (value != null)
299-
{
300-
if (!value.Value && AnyWriteConcernSettingsAreSet())
301-
{
302-
throw new InvalidOperationException("Safe cannot be set to false if any other write concern values have been set.");
303-
}
304-
}
305-
_safe = value;
306-
}
307-
}
308-
309288
/// <summary>
310289
/// Gets or sets the SafeMode to use.
311290
/// </summary>
@@ -314,7 +293,7 @@ public SafeMode SafeMode
314293
{
315294
get
316295
{
317-
if (_safe != null || AnyWriteConcernSettingsAreSet())
296+
if (AnyWriteConcernSettingsAreSet())
318297
{
319298
#pragma warning disable 618
320299
return new SafeMode(GetWriteConcern(false));
@@ -327,23 +306,20 @@ public SafeMode SafeMode
327306
}
328307
set
329308
{
330-
Safe = null;
331-
FSync = null;
332-
Journal = null;
333-
W = null;
334-
WTimeout = null;
335-
336-
if (value != null)
309+
if (value == null)
337310
{
338-
Safe = value.Enabled;
339-
if (value.Enabled)
340-
{
341-
var writeConcern = value.WriteConcern;
342-
if (writeConcern.FSync != null) { FSync = writeConcern.FSync.Value; }
343-
if (writeConcern.Journal != null) { Journal = writeConcern.Journal.Value; }
344-
if (writeConcern.W != null) { W = writeConcern.W; }
345-
if (writeConcern.WTimeout != null) { WTimeout = writeConcern.WTimeout.Value; }
346-
}
311+
FSync = null;
312+
Journal = null;
313+
W = null;
314+
WTimeout = null;
315+
}
316+
else
317+
{
318+
var writeConcern = value.WriteConcern;
319+
FSync = writeConcern.FSync;
320+
Journal = writeConcern.Journal;
321+
W = writeConcern.W ?? (writeConcern.Enabled ? 1 : 0);
322+
WTimeout = writeConcern.WTimeout;
347323
}
348324
}
349325
}
@@ -714,7 +690,7 @@ private static string FormatMessage(string name, string value)
714690
/// <returns>A WriteConcern.</returns>
715691
public WriteConcern GetWriteConcern(bool enabledDefault)
716692
{
717-
return new WriteConcern(_safe.HasValue ? _safe.Value : enabledDefault)
693+
return new WriteConcern(enabledDefault)
718694
{
719695
FSync = _fsync,
720696
Journal = _journal,
@@ -828,9 +804,27 @@ public void Parse(string url)
828804
ReplicaSetName = value;
829805
break;
830806
case "safe":
831-
#pragma warning disable 618
832-
Safe = ParseBoolean(name, value);
833-
#pragma warning restore
807+
var safe = Convert.ToBoolean(value);
808+
if (_w == null)
809+
{
810+
W = safe ? 1 : 0;
811+
}
812+
else
813+
{
814+
if (safe)
815+
{
816+
// don't overwrite existing W value unless it's 0
817+
var wCount = _w as WriteConcern.WCount;
818+
if (wCount != null && wCount.Value == 0)
819+
{
820+
W = 1;
821+
}
822+
}
823+
else
824+
{
825+
W = 0;
826+
}
827+
}
834828
break;
835829
case "secondaryacceptablelatency":
836830
case "secondaryacceptablelatencyms":
@@ -970,10 +964,6 @@ public override string ToString()
970964
}
971965
}
972966
}
973-
if (_safe != null)
974-
{
975-
query.AppendFormat("safe={0};", XmlConvert.ToString(_safe.Value));
976-
}
977967
if (_fsync != null)
978968
{
979969
query.AppendFormat("fsync={0};", XmlConvert.ToString(_fsync.Value));
@@ -1069,7 +1059,6 @@ private void ResetValues()
10691059
_minConnectionPoolSize = MongoDefaults.MinConnectionPoolSize;
10701060
_readPreference = null;
10711061
_replicaSetName = null;
1072-
_safe = null;
10731062
_secondaryAcceptableLatency = MongoDefaults.SecondaryAcceptableLatency;
10741063
_servers = null;
10751064
_slaveOk = null;

0 commit comments

Comments
 (0)