@@ -13,6 +13,7 @@ use uuid::Uuid;
13
13
14
14
use crate :: {
15
15
bson:: { doc, spec:: BinarySubtype , Binary , Bson , Document } ,
16
+ options:: SessionOptions ,
16
17
Client ,
17
18
RUNTIME ,
18
19
} ;
@@ -28,29 +29,44 @@ lazy_static! {
28
29
} ;
29
30
}
30
31
31
- /// Session to be used with client operations. This acts as a handle to a server session.
32
- /// This keeps the details of how server sessions are pooled opaque to users.
32
+ /// A MongoDB client session. This struct represents a logical session used for ordering sequential
33
+ /// operations. To create a `ClientSession`, call `start_session` on a `Client`.
34
+ ///
35
+ /// `ClientSession` instances are not thread safe or fork safe. They can only be used by one thread
36
+ /// or process at a time.
33
37
#[ derive( Debug ) ]
34
- pub ( crate ) struct ClientSession {
38
+ pub struct ClientSession {
35
39
cluster_time : Option < ClusterTime > ,
36
40
server_session : ServerSession ,
37
41
client : Client ,
38
42
is_implicit : bool ,
43
+ options : Option < SessionOptions > ,
39
44
}
40
45
41
46
impl ClientSession {
42
47
/// Creates a new `ClientSession` wrapping the provided server session.
43
- pub ( crate ) fn new_implicit ( server_session : ServerSession , client : Client ) -> Self {
48
+ pub ( crate ) fn new (
49
+ server_session : ServerSession ,
50
+ client : Client ,
51
+ options : Option < SessionOptions > ,
52
+ is_implicit : bool ,
53
+ ) -> Self {
44
54
Self {
45
55
client,
46
56
server_session,
47
57
cluster_time : None ,
48
- is_implicit : true ,
58
+ is_implicit,
59
+ options,
49
60
}
50
61
}
51
62
63
+ /// The client used to create this session.
64
+ pub fn client ( & self ) -> Client {
65
+ self . client . clone ( )
66
+ }
67
+
52
68
/// The id of this session.
53
- pub ( crate ) fn id ( & self ) -> & Document {
69
+ pub fn id ( & self ) -> & Document {
54
70
& self . server_session . id
55
71
}
56
72
@@ -61,13 +77,18 @@ impl ClientSession {
61
77
62
78
/// The highest seen cluster time this session has seen so far.
63
79
/// This will be `None` if this session has not been used in an operation yet.
64
- pub ( crate ) fn cluster_time ( & self ) -> Option < & ClusterTime > {
80
+ pub fn cluster_time ( & self ) -> Option < & ClusterTime > {
65
81
self . cluster_time . as_ref ( )
66
82
}
67
83
84
+ /// The options used to create this session.
85
+ pub fn options ( & self ) -> Option < & SessionOptions > {
86
+ self . options . as_ref ( )
87
+ }
88
+
68
89
/// Set the cluster time to the provided one if it is greater than this session's highest seen
69
90
/// cluster time or if this session's cluster time is `None`.
70
- pub ( crate ) fn advance_cluster_time ( & mut self , to : & ClusterTime ) {
91
+ pub fn advance_cluster_time ( & mut self , to : & ClusterTime ) {
71
92
if self . cluster_time ( ) . map ( |ct| ct < to) . unwrap_or ( true ) {
72
93
self . cluster_time = Some ( to. clone ( ) ) ;
73
94
}
@@ -89,6 +110,12 @@ impl ClientSession {
89
110
self . server_session . txn_number += 1 ;
90
111
self . server_session . txn_number
91
112
}
113
+
114
+ /// Whether this session is dirty.
115
+ #[ cfg( test) ]
116
+ pub ( crate ) fn is_dirty ( & self ) -> bool {
117
+ self . server_session . dirty
118
+ }
92
119
}
93
120
94
121
impl Drop for ClientSession {
@@ -109,7 +136,7 @@ impl Drop for ClientSession {
109
136
110
137
/// Client side abstraction of a server session. These are pooled and may be associated with
111
138
/// multiple `ClientSession`s over the course of their lifetime.
112
- #[ derive( Debug ) ]
139
+ #[ derive( Clone , Debug ) ]
113
140
pub ( crate ) struct ServerSession {
114
141
/// The id of the server session to which this corresponds.
115
142
id : Document ,
0 commit comments