Skip to content

Commit 1273b8a

Browse files
committed
CSHARP-2188: Consolidate session handling in Core.
1 parent 10bd782 commit 1273b8a

File tree

65 files changed

+1232
-2372
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1232
-2372
lines changed

src/MongoDB.Driver.Core/Core/Bindings/CoreSession.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,27 @@ namespace MongoDB.Driver.Core.Bindings
2424
/// Represents a session.
2525
/// </summary>
2626
/// <seealso cref="MongoDB.Driver.Core.Bindings.ICoreSession" />
27-
internal sealed class CoreSession : ICoreSession
27+
public sealed class CoreSession : ICoreSession
2828
{
2929
// private fields
3030
private readonly IClusterClock _clusterClock = new ClusterClock();
3131
private bool _disposed;
32-
private readonly bool _isCausallyConsistent;
33-
private readonly bool _isImplicit;
3432
private readonly IOperationClock _operationClock = new OperationClock();
33+
private readonly CoreSessionOptions _options;
3534
private readonly ICoreServerSession _serverSession;
3635

3736
// constructors
3837
/// <summary>
3938
/// Initializes a new instance of the <see cref="CoreSession" /> class.
4039
/// </summary>
4140
/// <param name="serverSession">The server session.</param>
42-
/// <param name="isCausallyConsistent">if set to <c>true</c> [is causally consistent].</param>
43-
/// <param name="isImplicit">if set to <c>true</c> [is implicit].</param>
41+
/// <param name="options">The options.</param>
4442
public CoreSession(
4543
ICoreServerSession serverSession,
46-
bool isCausallyConsistent = false,
47-
bool isImplicit = false)
44+
CoreSessionOptions options)
4845
{
4946
_serverSession = Ensure.IsNotNull(serverSession, nameof(serverSession));
50-
_isCausallyConsistent = isCausallyConsistent;
51-
_isImplicit = isImplicit;
47+
_options = Ensure.IsNotNull(options, nameof(options));
5248
}
5349

5450
// public properties
@@ -59,14 +55,20 @@ public CoreSession(
5955
public BsonDocument Id => _serverSession.Id;
6056

6157
/// <inheritdoc />
62-
public bool IsCausallyConsistent => _isCausallyConsistent;
58+
public bool IsCausallyConsistent => _options.IsCausallyConsistent;
6359

6460
/// <inheritdoc />
65-
public bool IsImplicit => _isImplicit;
61+
public bool IsImplicit => _options.IsImplicit;
6662

6763
/// <inheritdoc />
6864
public BsonTimestamp OperationTime => _operationClock.OperationTime;
6965

66+
/// <inheritdoc />
67+
public CoreSessionOptions Options => _options;
68+
69+
/// <inheritdoc />
70+
public ICoreServerSession ServerSession => _serverSession;
71+
7072
// public methods
7173
/// <inheritdoc />
7274
public void AdvanceClusterTime(BsonDocument newClusterTime)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
namespace MongoDB.Driver.Core.Bindings
17+
{
18+
/// <summary>
19+
/// Core session options.
20+
/// </summary>
21+
public class CoreSessionOptions
22+
{
23+
// private fields
24+
private readonly bool _isCausallyConsistent;
25+
private readonly bool _isImplicit;
26+
27+
// constructors
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="CoreSessionOptions"/> class.
30+
/// </summary>
31+
/// <param name="isCausallyConsistent">if set to <c>true</c> this session is causally consistent]</param>
32+
/// <param name="isImplicit">if set to <c>true</c> this session is an implicit session.</param>
33+
public CoreSessionOptions(
34+
bool isCausallyConsistent = false,
35+
bool isImplicit = false)
36+
{
37+
_isCausallyConsistent = isCausallyConsistent;
38+
_isImplicit = isImplicit;
39+
}
40+
41+
// public properties
42+
/// <summary>
43+
/// Gets a value indicating whether this session is causally consistent.
44+
/// </summary>
45+
/// <value>
46+
/// <c>true</c> if this session is causally consistent; otherwise, <c>false</c>.
47+
/// </value>
48+
public bool IsCausallyConsistent => _isCausallyConsistent;
49+
50+
/// <summary>
51+
/// Gets a value indicating whether this session is an implicit session.
52+
/// </summary>
53+
/// <value>
54+
/// <c>true</c> if this session is an implicit session; otherwise, <c>false</c>.
55+
/// </value>
56+
public bool IsImplicit => _isImplicit;
57+
}
58+
}

src/MongoDB.Driver.Core/Core/Bindings/ICoreSession.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using MongoDB.Bson;
18+
using MongoDB.Driver.Core.Clusters;
1819

1920
namespace MongoDB.Driver.Core.Bindings
2021
{
@@ -64,6 +65,22 @@ public interface ICoreSession : IDisposable
6465
/// </value>
6566
BsonTimestamp OperationTime { get; }
6667

68+
/// <summary>
69+
/// Gets the session options.
70+
/// </summary>
71+
/// <value>
72+
/// The session options.
73+
/// </value>
74+
CoreSessionOptions Options { get; }
75+
76+
/// <summary>
77+
/// Gets the server session.
78+
/// </summary>
79+
/// <value>
80+
/// The server session.
81+
/// </value>
82+
ICoreServerSession ServerSession { get; }
83+
6784
// methods
6885
/// <summary>
6986
/// Advances the cluster time.

src/MongoDB.Driver/NoServerSession.cs renamed to src/MongoDB.Driver.Core/Core/Bindings/NoCoreServerSession.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017-present MongoDB Inc.
1+
/* Copyright 2018-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -16,46 +16,31 @@
1616
using System;
1717
using MongoDB.Bson;
1818

19-
namespace MongoDB.Driver
19+
namespace MongoDB.Driver.Core.Bindings
2020
{
21-
/// <summary>
22-
/// A class that represents no server session.
23-
/// </summary>
24-
/// <seealso cref="MongoDB.Driver.IServerSession" />
25-
internal sealed class NoServerSession : IServerSession
21+
internal class NoCoreServerSession : ICoreServerSession
2622
{
2723
#region static
2824
// private static fields
29-
private static readonly IServerSession __instance = new NoServerSession();
30-
31-
// public static fields
32-
/// <summary>
33-
/// Gets the pre-created instance.
34-
/// </summary>
35-
/// <value>
36-
/// The instance.
37-
/// </value>
38-
public static IServerSession Instance => __instance;
25+
private static readonly ICoreServerSession __instance = new NoCoreServerSession();
26+
27+
// public static properties
28+
public static ICoreServerSession Instance => __instance;
3929
#endregion
4030

41-
/// <inheritdoc />
4231
public BsonDocument Id => null;
4332

44-
/// <inheritdoc />
4533
public DateTime? LastUsedAt => null;
4634

47-
/// <inheritdoc />
4835
public long AdvanceTransactionNumber()
4936
{
5037
return -1;
5138
}
5239

53-
/// <inheritdoc />
5440
public void Dispose()
5541
{
5642
}
5743

58-
/// <inheritdoc />
5944
public void WasUsed()
6045
{
6146
}

src/MongoDB.Driver.Core/Core/Bindings/NoCoreSession.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using MongoDB.Bson;
17+
using MongoDB.Driver.Core.Clusters;
1718

1819
namespace MongoDB.Driver.Core.Bindings
1920
{
@@ -63,6 +64,12 @@ public static ICoreSessionHandle NewHandle()
6364
/// <inheritdoc />
6465
public BsonTimestamp OperationTime => null;
6566

67+
/// <inheritdoc />
68+
public CoreSessionOptions Options => null;
69+
70+
/// <inheritdoc />
71+
public ICoreServerSession ServerSession => NoCoreServerSession.Instance;
72+
6673
/// <inheritdoc />
6774
public void AdvanceClusterTime(BsonDocument newClusterTime)
6875
{

src/MongoDB.Driver.Core/Core/Bindings/WrappingCoreSession.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using MongoDB.Bson;
18+
using MongoDB.Driver.Core.Clusters;
1819
using MongoDB.Driver.Core.Misc;
1920

2021
namespace MongoDB.Driver.Core.Bindings
@@ -93,6 +94,26 @@ public virtual BsonTimestamp OperationTime
9394
}
9495
}
9596

97+
/// <inheritdoc />
98+
public virtual CoreSessionOptions Options
99+
{
100+
get
101+
{
102+
ThrowIfDisposed();
103+
return _wrapped.Options;
104+
}
105+
}
106+
107+
/// <inheritdoc />
108+
public virtual ICoreServerSession ServerSession
109+
{
110+
get
111+
{
112+
ThrowIfDisposed();
113+
return _wrapped.ServerSession;
114+
}
115+
}
116+
96117
/// <summary>
97118
/// Gets the wrapped session.
98119
/// </summary>

src/MongoDB.Driver.Core/Core/Clusters/Cluster.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
using System.Text;
2222
using System.Threading;
2323
using System.Threading.Tasks;
24+
using MongoDB.Bson;
2425
using MongoDB.Driver.Core.Async;
26+
using MongoDB.Driver.Core.Bindings;
2527
using MongoDB.Driver.Core.Clusters.ServerSelectors;
2628
using MongoDB.Driver.Core.Configuration;
2729
using MongoDB.Driver.Core.Events;
@@ -281,6 +283,14 @@ public async Task<IServer> SelectServerAsync(IServerSelector selector, Cancellat
281283
}
282284
}
283285

286+
public ICoreSessionHandle StartSession(CoreSessionOptions options)
287+
{
288+
options = options ?? new CoreSessionOptions();
289+
var serverSession = AcquireServerSession();
290+
var session = new CoreSession(serverSession, options);
291+
return new CoreSessionHandle(session);
292+
}
293+
284294
protected abstract bool TryGetServer(EndPoint endPoint, out IClusterableServer server);
285295

286296
protected void UpdateClusterDescription(ClusterDescription newClusterDescription)

src/MongoDB.Driver.Core/Core/Clusters/ClusterClock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace MongoDB.Driver.Core.Clusters
2222
/// A cluster clock.
2323
/// </summary>
2424
/// <seealso cref="MongoDB.Driver.Core.Clusters.IClusterClock" />
25-
public class ClusterClock : IClusterClock
25+
internal class ClusterClock : IClusterClock
2626
{
2727
#region static
2828
// public static methods

src/MongoDB.Driver.Core/Core/Clusters/ICluster.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
*/
1515

1616
using System;
17-
using System.Collections.Generic;
18-
using System.Linq;
19-
using System.Net;
20-
using System.Text;
2117
using System.Threading;
2218
using System.Threading.Tasks;
19+
using MongoDB.Driver.Core.Bindings;
2320
using MongoDB.Driver.Core.Clusters.ServerSelectors;
2421
using MongoDB.Driver.Core.Configuration;
25-
using MongoDB.Driver.Core.Events;
2622
using MongoDB.Driver.Core.Servers;
2723

2824
namespace MongoDB.Driver.Core.Clusters
@@ -90,5 +86,14 @@ public interface ICluster : IDisposable
9086
/// <param name="cancellationToken">The cancellation token.</param>
9187
/// <returns>A Task representing the operation. The result of the Task is the selected server.</returns>
9288
Task<IServer> SelectServerAsync(IServerSelector selector, CancellationToken cancellationToken);
89+
90+
/// <summary>
91+
/// Starts a session.
92+
/// </summary>
93+
/// <param name="options">The options.</param>
94+
/// <returns>
95+
/// A session.
96+
/// </returns>
97+
ICoreSessionHandle StartSession(CoreSessionOptions options = null);
9398
}
9499
}

src/MongoDB.Driver.Core/Core/Operations/OperationClock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace MongoDB.Driver.Core.Operations
2222
/// An operation clock.
2323
/// </summary>
2424
/// <seealso cref="MongoDB.Driver.Core.Operations.IOperationClock" />
25-
public class OperationClock : IOperationClock
25+
internal class OperationClock : IOperationClock
2626
{
2727
#region static
2828
// public static methods

0 commit comments

Comments
 (0)