@@ -37,29 +37,40 @@ public final class ClientSession {
3737 /// The most recent cluster time seen by this session. This value will be nil if either of the following are true:
3838 /// - No operations have been executed using this session and `advanceClusterTime` has not been called.
3939 /// - This session has been ended.
40- public var clusterTime : Document ? {
41- fatalError ( " unimplemented " )
42- }
40+ public var clusterTime : Document ? { return self . asyncSession. clusterTime }
4341
4442 /// The operation time of the most recent operation performed using this session. This value will be nil if either
4543 /// of the following are true:
4644 /// - No operations have been performed using this session and `advanceOperationTime` has not been called.
4745 /// - This session has been ended.
48- public var operationTime : Timestamp ? {
49- fatalError ( " unimplemented " )
50- }
46+ public var operationTime : Timestamp ? { return self . asyncSession. operationTime }
5147
5248 /// The options used to start this session.
53- public let options : ClientSessionOptions ?
49+ public var options : ClientSessionOptions ? { return self . asyncSession . options }
5450
5551 /// Initializes a new client session.
56- internal init ( wrapping session: MongoSwift . ClientSession ) throws {
57- fatalError ( " unimplemented " )
52+ internal init ( client: MongoClient , options: ClientSessionOptions ? ) {
53+ self . client = client
54+ self . asyncSession = client. asyncClient. startSession ( options: options)
55+ }
56+
57+ /// Ends the underlying async session.
58+ internal func end( ) {
59+ // we only call this method from places that we can't throw (deinit, defers) so we handle the error here
60+ // instead. the async method will only fail if the async client, thread pool, or event loop group have been
61+ // closed/ended. we manage the lifetimes of all of those ourselves, so if we hit the assertionFailure it's due
62+ // to a bug in our own code.
63+ do {
64+ try self . asyncSession. end ( ) . wait ( )
65+ } catch {
66+ assertionFailure ( " Error ending async session: \( error) " )
67+ }
5868 }
5969
6070 /// Cleans up internal state.
6171 deinit {
62- fatalError ( " unimplemented " )
72+ // a repeated call to `end` is a no-op so it's ok to call this even if `end()` was already called explicitly.
73+ self . end ( )
6374 }
6475
6576 /**
@@ -71,7 +82,7 @@ public final class ClientSession {
7182 * - clusterTime: The session's new cluster time, as a `Document` like `["cluster time": Timestamp(...)]`
7283 */
7384 public func advanceClusterTime( to clusterTime: Document ) {
74- fatalError ( " unimplemented " )
85+ self . asyncSession . advanceClusterTime ( to : clusterTime )
7586 }
7687
7788 /**
@@ -83,6 +94,6 @@ public final class ClientSession {
8394 * - operationTime: The session's new operationTime
8495 */
8596 public func advanceOperationTime( to operationTime: Timestamp ) {
86- fatalError ( " unimplemented " )
97+ self . asyncSession . advanceOperationTime ( to : operationTime )
8798 }
8899}
0 commit comments