Skip to content

Commit 8ba9849

Browse files
authored
Prefix all synchronous types with Sync (#334)
1 parent aa7e66c commit 8ba9849

Some content is hidden

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

50 files changed

+645
-586
lines changed

Sources/AtlasConnectivity/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ for config in configs {
1212
}
1313

1414
do {
15-
let client = try MongoClient(uri)
15+
let client = try SyncMongoClient(uri)
1616
// run isMaster
1717
let db = client.db("test")
1818
_ = try db.runCommand(["isMaster": 1])

Sources/MongoSwift/APM.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ private func postNotification<T: MongoEvent>(type: T.Type,
456456
guard let context = contextFunc(event) else {
457457
fatalError("Missing context for \(type)")
458458
}
459-
let client = Unmanaged<MongoClient>.fromOpaque(context).takeUnretainedValue()
459+
let client = Unmanaged<SyncMongoClient>.fromOpaque(context).takeUnretainedValue()
460460
let notification = Notification(name: type.eventName, userInfo: ["event": eventStruct])
461461
client.notificationCenter.post(notification)
462462
}
@@ -493,7 +493,7 @@ extension Notification.Name {
493493
/// An extension of `ConnectionPool` to add monitoring capability for commands and server discovery and monitoring.
494494
extension ConnectionPool {
495495
/// Internal function to install monitoring callbacks for this pool.
496-
internal func initializeMonitoring(commandMonitoring: Bool, serverMonitoring: Bool, client: MongoClient) {
496+
internal func initializeMonitoring(commandMonitoring: Bool, serverMonitoring: Bool, client: SyncMongoClient) {
497497
guard let callbacks = mongoc_apm_callbacks_new() else {
498498
fatalError("failed to initialize new mongoc_apm_callbacks_t")
499499
}
@@ -517,8 +517,8 @@ extension ConnectionPool {
517517
mongoc_apm_set_server_heartbeat_failed_cb(callbacks, serverHeartbeatFailed)
518518
}
519519

520-
// we can pass the MongoClient as unretained because the callbacks are stored on clientHandle, so if the
521-
// callback is being executed, this pool and therefore its parent `MongoClient` must still be valid.
520+
// we can pass the SyncMongoClient as unretained because the callbacks are stored on clientHandle, so if the
521+
// callback is being executed, this pool and therefore its parent `SyncMongoClient` must still be valid.
522522
switch self.mode {
523523
case let .single(clientHandle):
524524
mongoc_client_set_apm_callbacks(clientHandle, callbacks, Unmanaged.passUnretained(client).toOpaque())

Sources/MongoSwift/BSON/CodingStrategies.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public struct BSONCoderOptions: CodingStrategyProvider {
3535

3636
/**
3737
* Enum representing the various encoding/decoding strategy pairs for `Date`s.
38-
* Set these on a `MongoClient`, `MongoDatabase`, or `MongoCollection` so that the strategies will be applied when
39-
* converting `Date`s between their BSON representations and their representations in (non `Document`) `Codable` types.
38+
* Set these on a `MongoClient`, `MongoDatabase`, or `MongoCollection`, or a `SyncMongoClient`, `SyncMongoDatabase`, or
39+
* `SyncMongoCollection`, so that the strategies will be applied when converting `Date`s between their BSON
40+
* representations and their representations in (non `Document`) `Codable` types.
4041
*
4142
* As per the BSON specification, the default strategy is to encode `Date`s as BSON datetime objects.
4243
*
@@ -119,9 +120,10 @@ public enum DateCodingStrategy: RawRepresentable {
119120
}
120121

121122
/**
122-
* Enum representing the various encoding/decoding strategy pairs for `UUID`s.
123-
* Set these on a `MongoClient`, `MongoDatabase`, or `MongoCollection` so that the strategies will be applied when
124-
* converting `UUID`s between their BSON representations and their representations in (non `Document`) `Codable` types.
123+
* Enum representing the various encoding/decoding strategy pairs for `Date`s.
124+
* Set these on a `MongoClient`, `MongoDatabase`, or `MongoCollection`, or a `SyncMongoClient`, `SyncMongoDatabase`, or
125+
* `SyncMongoCollection`, so that the strategies will be applied when converting `UUID`s between their BSON
126+
* representations and their representations in (non `Document`) `Codable` types.
125127
*
126128
* As per the BSON specification, the default strategy is to encode `UUID`s as BSON binary types with the UUID
127129
* subtype.
@@ -159,9 +161,10 @@ public enum UUIDCodingStrategy: RawRepresentable {
159161
}
160162

161163
/**
162-
* Enum representing the various encoding/decoding strategy pairs for `Data`s.
163-
* Set these on a `MongoClient`, `MongoDatabase`, or `MongoCollection` so that the strategies will be applied when
164-
* converting `Data`s between their BSON representations and their representations in (non `Document`) `Codable` types.
164+
* Enum representing the various encoding/decoding strategy pairs for `Date`s.
165+
* Set these on a `MongoClient`, `MongoDatabase`, or `MongoCollection`, or a `SyncMongoClient`, `SyncMongoDatabase`, or
166+
* `SyncMongoCollection`, so that the strategies will be applied when converting `Data`s between their BSON
167+
* representations and their representations in (non `Document`) `Codable` types.
165168
*
166169
* As per the BSON specification, the default strategy is to encode `Data`s as BSON binary types with the generic
167170
* binary subtype.

Sources/MongoSwift/ChangeStream.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ public struct ResumeToken: Codable, Equatable {
2424
}
2525
}
2626

27-
/// A MongoDB ChangeStream.
27+
/// A synchronous MongoDB change stream.
2828
/// - SeeAlso: https://docs.mongodb.com/manual/changeStreams/
29-
public class ChangeStream<T: Codable>: Sequence, IteratorProtocol {
29+
public class SyncChangeStream<T: Codable>: Sequence, IteratorProtocol {
3030
/// Enum for tracking the state of a change stream.
3131
internal enum State {
3232
/// Indicates that the change stream is still open. Stores a pointer to the `mongoc_change_stream_t`, along
3333
/// with the source connection, client, and possibly session to ensure they are kept alive as long
3434
/// as the change stream is.
35-
case open(changeStream: OpaquePointer, connection: Connection, client: MongoClient, session: ClientSession?)
35+
case open(changeStream: OpaquePointer,
36+
connection: Connection,
37+
client: SyncMongoClient,
38+
session: SyncClientSession?)
3639
case closed
3740
}
3841

@@ -134,15 +137,15 @@ public class ChangeStream<T: Codable>: Sequence, IteratorProtocol {
134137
}
135138

136139
/**
137-
* Initializes a `ChangeStream`.
140+
* Initializes a `SyncChangeStream`.
138141
* - Throws:
139142
* - `ServerError.commandError` if an error occurred on the server when creating the `mongoc_change_stream_t`.
140143
* - `UserError.invalidArgumentError` if the `mongoc_change_stream_t` was created with invalid options.
141144
*/
142145
internal init(stealing changeStream: OpaquePointer,
143146
connection: Connection,
144-
client: MongoClient,
145-
session: ClientSession?,
147+
client: SyncMongoClient,
148+
session: SyncClientSession?,
146149
decoder: BSONDecoder,
147150
options: ChangeStreamOptions?
148151
) throws {

Sources/MongoSwift/ChangeStreamDocument.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public enum OperationType: String, Codable {
3737
case rename
3838
}
3939

40-
/// The response document type from a `ChangeStream`.
40+
/// The response document type from a `ChangeStream` or a `SyncChangeStream`.
4141
public struct ChangeStreamEvent<T: Codable>: Codable {
4242
/// Describes the type of operation for this change.
4343
public let operationType: OperationType
@@ -64,7 +64,7 @@ public struct ChangeStreamEvent<T: Codable>: Codable {
6464
/**
6565
* Always present for operations of type `insert` and `replace`. Also present for operations of type `update` if
6666
* the user has specified `.updateLookup` for the `fullDocument` option in the `ChangeStreamOptions` used to create
67-
* the `ChangeStream` that emitted this document.
67+
* the change stream that emitted this document.
6868
*
6969
* For operations of type `insert’ and `replace’, this key will contain the document being inserted, or the new
7070
* version of the document that is replacing the existing document, respectively.

Sources/MongoSwift/ChangeStreamOptions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public enum FullDocument: RawRepresentable, Codable {
2626
}
2727
}
2828

29-
/// Options to use when creating a `ChangeStream`.
29+
/// Options to use when creating a `ChangeStream` or `SyncChangeStream`.
3030
public struct ChangeStreamOptions: Codable {
3131
/// The number of documents to return per batch. If omitted, the server will use its default batch size.
3232
/// - SeeAlso: https://docs.mongodb.com/manual/reference/command/aggregate

Sources/MongoSwift/ClientSession.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22
import mongoc
33

4-
/// Options to use when creating a ClientSession.
4+
/// Options to use when creating a `ClientSession` or `SyncClientSession`.
55
public struct ClientSessionOptions {
66
/// Whether to enable causal consistency for this session. By default, causal consistency is enabled.
77
///
@@ -31,7 +31,7 @@ private func withSessionOpts<T>(wrapping options: ClientSessionOptions?,
3131
* A MongoDB client session.
3232
* This class represents a logical session used for ordering sequential operations.
3333
*
34-
* To create a client session, use `startSession` or `withSession` on a `MongoClient`.
34+
* To create a client session, use `startSession` or `withSession` on a `MongoClient` or a `SyncMongoClient`.
3535
*
3636
* If `causalConsistency` is not set to `false` when starting a session, read and write operations that use the session
3737
* will be provided causal consistency guarantees depending on the read and write concerns used. Using "majority"
@@ -55,12 +55,12 @@ private func withSessionOpts<T>(wrapping options: ClientSessionOptions?,
5555
* - https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#sessions
5656
* - https://docs.mongodb.com/manual/core/causal-consistency-read-write-concerns/
5757
*/
58-
public final class ClientSession {
58+
public final class SyncClientSession {
5959
/// Error thrown when an inactive session is used.
6060
internal static let SessionInactiveError = UserError.logicError(message: "Tried to use an inactive session")
6161
/// Error thrown when a user attempts to use a session with a client it was not created from.
6262
internal static let ClientMismatchError = UserError.invalidArgumentError(
63-
message: "Sessions may only be used with the MongoClient used to create them")
63+
message: "Sessions may only be used with the client used to create them")
6464

6565
/// Enum for tracking the state of a session.
6666
internal enum State {
@@ -83,7 +83,7 @@ public final class ClientSession {
8383
}
8484

8585
/// The client used to start this session.
86-
public let client: MongoClient
86+
public let client: SyncMongoClient
8787

8888
/// The session ID of this session.
8989
public let id: Document
@@ -122,7 +122,7 @@ public final class ClientSession {
122122
public let options: ClientSessionOptions?
123123

124124
/// Initializes a new client session.
125-
internal init(client: MongoClient, options: ClientSessionOptions? = nil) throws {
125+
internal init(client: SyncMongoClient, options: ClientSessionOptions? = nil) throws {
126126
self.options = options
127127
self.client = client
128128

@@ -144,12 +144,12 @@ public final class ClientSession {
144144

145145
/// Retrieves this session's underlying connection. Throws an error if the provided client was not the client used
146146
/// to create this session, or if this session has been ended.
147-
internal func getConnection(forUseWith client: MongoClient) throws -> Connection {
147+
internal func getConnection(forUseWith client: SyncMongoClient) throws -> Connection {
148148
guard case let .active(_, connection) = self.state else {
149-
throw ClientSession.SessionInactiveError
149+
throw SyncClientSession.SessionInactiveError
150150
}
151151
guard self.client == client else {
152-
throw ClientSession.ClientMismatchError
152+
throw SyncClientSession.ClientMismatchError
153153
}
154154
return connection
155155
}
@@ -202,7 +202,7 @@ public final class ClientSession {
202202
/// - `UserError.logicError` if this session is inactive
203203
internal func append(to doc: inout Document) throws {
204204
guard case let .active(session, _) = self.state else {
205-
throw ClientSession.SessionInactiveError
205+
throw SyncClientSession.SessionInactiveError
206206
}
207207

208208
var error = bson_error_t()

0 commit comments

Comments
 (0)