forked from mongodb/mongo-csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIClientSessionExtensions.cs
More file actions
103 lines (91 loc) · 4.35 KB
/
IClientSessionExtensions.cs
File metadata and controls
103 lines (91 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
namespace MongoDB.Driver
{
/// <summary>
/// Extension methods for <see cref="IClientSession"/>.
/// </summary>
public static class IClientSessionExtensions
{
/// <summary>
/// Gets the snapshot time for a snapshot session.
/// </summary>
/// <param name="session">The client session handle.</param>
/// <returns>The snapshot time as a <see cref="BsonTimestamp"/>.</returns>
/// <exception cref="InvalidOperationException">Thrown when the session is not a snapshot session.</exception>
public static BsonTimestamp GetSnapshotTime(this IClientSession session)
{
var clientSessionHandle = (ClientSessionHandle)session;
return clientSessionHandle.WrappedCoreSession.IsSnapshot ?
clientSessionHandle.SnapshotTime
: throw new InvalidOperationException("Cannot retrieve snapshot time from a non-snapshot session.");
}
}
// TODO: CSOT: Make it public when CSOT will be ready for GA
internal static class IInternalClientSessionExtensions
{
// TODO: Merge these extension methods in IClientSession interface on major release
public static void AbortTransaction(this IClientSession session, AbortTransactionOptions options, CancellationToken cancellationToken = default)
{
if (options?.Timeout == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout)
{
session.AbortTransaction(cancellationToken);
return;
}
((IClientSessionInternal)session).AbortTransaction(options, cancellationToken);
}
public static Task AbortTransactionAsync(this IClientSession session, AbortTransactionOptions options, CancellationToken cancellationToken = default)
{
if (options?.Timeout == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout)
{
return session.AbortTransactionAsync(cancellationToken);
}
return ((IClientSessionInternal)session).AbortTransactionAsync(options, cancellationToken);
}
public static void CommitTransaction(this IClientSession session, CommitTransactionOptions options, CancellationToken cancellationToken = default)
{
if (options?.Timeout == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout)
{
session.CommitTransaction(cancellationToken);
return;
}
((IClientSessionInternal)session).CommitTransaction(options, cancellationToken);
}
public static Task CommitTransactionAsync(this IClientSession session, CommitTransactionOptions options, CancellationToken cancellationToken = default)
{
if (options?.Timeout == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout)
{
return session.CommitTransactionAsync(cancellationToken);
}
return ((IClientSessionInternal)session).CommitTransactionAsync(options, cancellationToken);
}
internal static ReadPreference GetEffectiveReadPreference(this IClientSession session, ReadPreference defaultReadPreference)
{
if (session.IsInTransaction)
{
var transactionReadPreference = session.WrappedCoreSession.CurrentTransaction.TransactionOptions?.ReadPreference;
if (transactionReadPreference != null)
{
return transactionReadPreference;
}
}
return defaultReadPreference ?? ReadPreference.Primary;
}
}
}