Skip to content

Commit c300351

Browse files
CSHARP-2351: Add documentation sample for causal consistency.
1 parent bead6b3 commit c300351

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* Copyright 2018-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using FluentAssertions;
17+
using MongoDB.Bson;
18+
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
19+
using System;
20+
using System.Collections.Generic;
21+
using Xunit;
22+
23+
namespace MongoDB.Driver.Examples
24+
{
25+
public class CausalConsistencyExamples
26+
{
27+
[Fact]
28+
public void Causal_Consistency_Example_1()
29+
{
30+
RequireServer.Check().SupportsCausalConsistency();
31+
32+
string testDatabaseName = "test";
33+
string itemsCollectionName = "items";
34+
var client = CreateClient();
35+
DropCollection(client, testDatabaseName, itemsCollectionName);
36+
37+
// Start Causal Consistency Example 1
38+
using (var session1 = client.StartSession(new ClientSessionOptions { CausalConsistency = true }))
39+
{
40+
var currentDate = DateTime.UtcNow.Date;
41+
var items = client.GetDatabase("test", new MongoDatabaseSettings
42+
{
43+
ReadConcern = ReadConcern.Majority,
44+
WriteConcern = new WriteConcern(
45+
WriteConcern.WMode.Majority,
46+
TimeSpan.FromMilliseconds(1000))
47+
})
48+
.GetCollection<BsonDocument>("items");
49+
50+
items.UpdateOne(session1,
51+
Builders<BsonDocument>.Filter.And(
52+
Builders<BsonDocument>.Filter.Eq("sku", "111"),
53+
Builders<BsonDocument>.Filter.Eq("end", BsonNull.Value)),
54+
Builders<BsonDocument>.Update.Set("end", currentDate));
55+
56+
items.InsertOne(session1, new BsonDocument
57+
{
58+
{"sku", "nuts-111"},
59+
{"name", "Pecans"},
60+
{"start", currentDate}
61+
});
62+
}
63+
// End Causal Consistency Example 1
64+
65+
var result = client.GetDatabase(testDatabaseName).GetCollection<BsonDocument>(itemsCollectionName).Find("{}").FirstOrDefault();
66+
RemoveIds(new[] { result });
67+
result["start"].Should().NotBeNull();
68+
result.Remove("start");
69+
result.Should().Be("{ \"sku\" : \"nuts-111\", \"name\" : \"Pecans\" }");
70+
}
71+
72+
[Fact]
73+
public void Causal_Consistency_Example_2()
74+
{
75+
RequireServer.Check().SupportsCausalConsistency();
76+
77+
string testDatabaseName = "test";
78+
string itemsCollectionName = "items";
79+
var client = CreateClient();
80+
DropCollection(client, testDatabaseName, itemsCollectionName);
81+
82+
using (var session1 = client.StartSession(new ClientSessionOptions { CausalConsistency = true }))
83+
{
84+
client.GetDatabase(testDatabaseName).RunCommand<BsonDocument>(session1, "{ ping : 1 }");
85+
86+
// Start Causal Consistency Example 2
87+
using (var session2 = client.StartSession(new ClientSessionOptions { CausalConsistency = true }))
88+
{
89+
session2.AdvanceClusterTime(session1.ClusterTime);
90+
session2.AdvanceOperationTime(session1.OperationTime);
91+
92+
var items = client.GetDatabase("test", new MongoDatabaseSettings
93+
{
94+
ReadPreference = ReadPreference.Secondary,
95+
ReadConcern = ReadConcern.Majority,
96+
WriteConcern = new WriteConcern(WriteConcern.WMode.Majority, TimeSpan.FromMilliseconds(1000))
97+
})
98+
.GetCollection<BsonDocument>("items");
99+
100+
var filter = Builders<BsonDocument>.Filter.Eq("end", BsonNull.Value);
101+
foreach (var item in items.Find(session2, filter).ToEnumerable())
102+
{
103+
// process item
104+
}
105+
}
106+
// End Causal Consistency Example 2
107+
}
108+
}
109+
110+
private IMongoClient CreateClient()
111+
{
112+
var connectionString = CoreTestConfiguration.ConnectionString.ToString();
113+
return new MongoClient(connectionString);
114+
}
115+
116+
private void DropCollection(IMongoClient client, string databaseName, string collectionName)
117+
{
118+
client.GetDatabase(databaseName).DropCollection(collectionName);
119+
}
120+
121+
private void RemoveIds(IEnumerable<BsonDocument> documents)
122+
{
123+
foreach (var document in documents)
124+
{
125+
document.Remove("_id");
126+
}
127+
}
128+
}
129+
}

tests/MongoDB.Driver.Examples/MongoDB.Driver.Examples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
</ItemGroup>
6666
<ItemGroup>
6767
<Compile Include="AggregatePrimer.cs" />
68+
<Compile Include="CausalConsistencyExamples.cs" />
6869
<Compile Include="ChangeStreamExamples.cs" />
6970
<Compile Include="DocumentationExamples.cs" />
7071
<Compile Include="IndexesPrimer.cs" />

0 commit comments

Comments
 (0)