Skip to content

Commit 37a1556

Browse files
craiggwilsonrstam
authored andcommitted
CSHARP-1694: Add support for linearizable read concern.
1 parent 1573017 commit 37a1556

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/MongoDB.Driver.Core/ReadConcern.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace MongoDB.Driver
2626
public sealed class ReadConcern : IEquatable<ReadConcern>, IConvertibleToBsonDocument
2727
{
2828
private static readonly ReadConcern __default = new ReadConcern();
29+
private static readonly ReadConcern __linearizable = new ReadConcern(ReadConcernLevel.Linearizable);
2930
private static readonly ReadConcern __local = new ReadConcern(ReadConcernLevel.Local);
3031
private static readonly ReadConcern __majority = new ReadConcern(ReadConcernLevel.Majority);
3132

@@ -34,13 +35,18 @@ public sealed class ReadConcern : IEquatable<ReadConcern>, IConvertibleToBsonDoc
3435
/// </summary>
3536
public static ReadConcern Default => __default;
3637

38+
/// <summary>
39+
/// Gets a linearizable read concern.
40+
/// </summary>
41+
public static ReadConcern Linearizable => __linearizable;
42+
3743
/// <summary>
3844
/// Gets a local read concern.
3945
/// </summary>
4046
public static ReadConcern Local => __local;
4147

4248
/// <summary>
43-
/// Gets a majority read concern
49+
/// Gets a majority read concern.
4450
/// </summary>
4551
public static ReadConcern Majority => __majority;
4652

@@ -59,6 +65,8 @@ public static ReadConcern FromBsonDocument(BsonDocument document)
5965
var level = (ReadConcernLevel)Enum.Parse(typeof(ReadConcernLevel), (string)levelValue, true);
6066
switch (level)
6167
{
68+
case ReadConcernLevel.Linearizable:
69+
return ReadConcern.Linearizable;
6270
case ReadConcernLevel.Local:
6371
return ReadConcern.Local;
6472
case ReadConcernLevel.Majority:
@@ -140,6 +148,9 @@ public BsonDocument ToBsonDocument()
140148
{
141149
switch (_level.Value)
142150
{
151+
case ReadConcernLevel.Linearizable:
152+
level = "linearizable";
153+
break;
143154
case ReadConcernLevel.Local:
144155
level = "local";
145156
break;

src/MongoDB.Driver.Core/ReadConcernLevel.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public enum ReadConcernLevel
3333
/// <summary>
3434
/// Reads data committed to a majority of nodes.
3535
/// </summary>
36-
Majority
36+
Majority,
37+
/// <summary>
38+
/// Avoids returning data from a "stale" primary
39+
/// (one that has already been superseded by a new primary but doesn't know it yet).
40+
/// It is important to note that readConcern level linearizable does not by itself
41+
/// produce linearizable reads; they must be issued in conjunction with w:majority
42+
/// writes to the same document(s) in order to be linearizable.
43+
/// </summary>
44+
Linearizable
3745
}
3846
}

tests/MongoDB.Driver.Core.Tests/ReadConcernTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,17 @@ public void Equals_should_return_false_when_level_is_not_equal()
5454
ReadConcern.Default.Should().NotBe(ReadConcern.Local);
5555
ReadConcern.Default.Should().NotBe(ReadConcern.Majority);
5656
ReadConcern.Local.Should().NotBe(ReadConcern.Majority);
57+
ReadConcern.Linearizable.Should().NotBe(ReadConcern.Local);
58+
ReadConcern.Linearizable.Should().NotBe(ReadConcern.Majority);
5759
}
5860

5961
[Fact]
6062
public void Equals_should_return_true_when_level_is_equal()
6163
{
6264
ReadConcern.Default.Should().Be(ReadConcern.Default);
6365
new ReadConcern().Should().Be(ReadConcern.Default);
66+
ReadConcern.Linearizable.Should().Be(ReadConcern.Linearizable);
67+
new ReadConcern(ReadConcernLevel.Linearizable).Should().Be(ReadConcern.Linearizable);
6468
ReadConcern.Local.Should().Be(ReadConcern.Local);
6569
new ReadConcern(ReadConcernLevel.Local).Should().Be(ReadConcern.Local);
6670
ReadConcern.Majority.Should().Be(ReadConcern.Majority);
@@ -101,6 +105,7 @@ public void ThrowIfNotSupported_should_throw_when_the_serverVersion_is_less_than
101105
}
102106

103107
[Theory]
108+
[InlineData(ReadConcernLevel.Linearizable, "{ level: 'linearizable' }")]
104109
[InlineData(ReadConcernLevel.Local, "{ level: 'local' }")]
105110
[InlineData(ReadConcernLevel.Majority, "{ level: 'majority' }")]
106111
[InlineData(null, "{ }")]

0 commit comments

Comments
 (0)