Skip to content

Commit 6d4348a

Browse files
authored
SWIFT-656 Remove embedded driver (#349)
1 parent d983ead commit 6d4348a

File tree

6 files changed

+25
-139
lines changed

6 files changed

+25
-139
lines changed

Sources/MongoSwift/APM.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,10 @@ extension ConnectionPool {
544544

545545
// we can pass the MongoClient as unretained because the callbacks are stored on clientHandle, so if the
546546
// callback is being executed, this pool and therefore its parent `MongoClient` must still be valid.
547-
switch self.mode {
548-
case let .single(clientHandle):
549-
mongoc_client_set_apm_callbacks(clientHandle, callbacks, Unmanaged.passUnretained(client).toOpaque())
550-
case let .pooled(pool):
547+
switch self.state {
548+
case let .open(pool):
551549
mongoc_client_pool_set_apm_callbacks(pool, callbacks, Unmanaged.passUnretained(client).toOpaque())
552-
case .none:
550+
case .closed:
553551
fatalError("ConnectionPool was already closed")
554552
}
555553
}

Sources/MongoSwift/ConnectionPool.swift

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,18 @@ internal struct Connection {
1212

1313
/// A pool of one or more connections.
1414
internal class ConnectionPool {
15-
/// Represents the mode of a `ConnectionPool`.
16-
internal enum Mode {
17-
/// Indicates that we are in single-client mode using the associated pointer to a `mongoc_client_t`.
18-
case single(client: OpaquePointer)
19-
/// Indicates that we are in pooled mode using the associated pointer to a `mongoc_client_pool_t`.
20-
case pooled(pool: OpaquePointer)
15+
/// Represents the state of a `ConnectionPool`.
16+
internal enum State {
17+
/// Indicates that the `ConnectionPool` is open and using the associated pointer to a `mongoc_client_pool_t`.
18+
case open(pool: OpaquePointer)
2119
/// Indicates that the `ConnectionPool` has been closed and contains no connections.
22-
case none
20+
case closed
2321
}
2422

25-
/// The mode of this `ConnectionPool`.
26-
internal private(set) var mode: Mode
23+
/// The state of this `ConnectionPool`.
24+
internal private(set) var state: State
2725

28-
/// Initializes the pool in single mode using the provided pointer to a `mongoc_client_t`.
29-
internal init(stealing pointer: OpaquePointer) {
30-
self.mode = .single(client: pointer)
31-
32-
// This call may fail, and if it does, either the error api version was already set or the client was derived
33-
// from a pool. In either case, the error handling in MongoSwift will be incorrect unless the correct api
34-
// version was set by the caller.
35-
mongoc_client_set_error_api(pointer, MONGOC_ERROR_API_VERSION_2)
36-
}
37-
38-
/// Initializes the pool in pooled mode using the provided `ConnectionString`.
26+
/// Initializes the pool using the provided `ConnectionString`.
3927
internal init(from connString: ConnectionString, options: TLSOptions? = nil) throws {
4028
guard let pool = mongoc_client_pool_new(connString._uri) else {
4129
throw UserError.invalidArgumentError(message: "libmongoc not built with TLS support")
@@ -45,7 +33,7 @@ internal class ConnectionPool {
4533
throw RuntimeError.internalError(message: "Could not configure error handling on client pool")
4634
}
4735

48-
self.mode = .pooled(pool: pool)
36+
self.state = .open(pool: pool)
4937
if let options = options {
5038
try self.setTLSOptions(options)
5139
}
@@ -58,37 +46,31 @@ internal class ConnectionPool {
5846

5947
/// Closes the pool, cleaning up underlying resources.
6048
internal func close() {
61-
switch self.mode {
62-
case let .single(clientHandle):
63-
mongoc_client_destroy(clientHandle)
64-
case let .pooled(pool):
49+
switch self.state {
50+
case let .open(pool):
6551
mongoc_client_pool_destroy(pool)
66-
case .none:
52+
case .closed:
6753
return
6854
}
69-
self.mode = .none
55+
self.state = .closed
7056
}
7157

7258
/// Checks out a connection. This connection must be returned to the pool via `checkIn`.
7359
internal func checkOut() throws -> Connection {
74-
switch self.mode {
75-
case let .single(clientHandle):
76-
return Connection(clientHandle)
77-
case let .pooled(pool):
60+
switch self.state {
61+
case let .open(pool):
7862
return Connection(mongoc_client_pool_pop(pool))
79-
case .none:
63+
case .closed:
8064
throw RuntimeError.internalError(message: "ConnectionPool was already closed")
8165
}
8266
}
8367

8468
/// Returns a connection to the pool.
8569
internal func checkIn(_ connection: Connection) {
86-
switch self.mode {
87-
case .single:
88-
return
89-
case let .pooled(pool):
70+
switch self.state {
71+
case let .open(pool):
9072
mongoc_client_pool_push(pool, connection.clientHandle)
91-
case .none:
73+
case .closed:
9274
fatalError("ConnectionPool was already closed")
9375
}
9476
}
@@ -128,12 +110,10 @@ internal class ConnectionPool {
128110
if let invalidHosts = options.allowInvalidHostnames {
129111
opts.allow_invalid_hostname = invalidHosts
130112
}
131-
switch self.mode {
132-
case let .single(clientHandle):
133-
mongoc_client_set_ssl_opts(clientHandle, &opts)
134-
case let .pooled(pool):
113+
switch self.state {
114+
case let .open(pool):
135115
mongoc_client_pool_set_ssl_opts(pool, &opts)
136-
case .none:
116+
case .closed:
137117
throw RuntimeError.internalError(message: "ConnectionPool was already closed")
138118
}
139119
}

Sources/MongoSwift/MongoClient.swift

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -252,36 +252,6 @@ public class MongoClient {
252252
)
253253
}
254254

255-
/**
256-
* :nodoc:
257-
*/
258-
@available(*, deprecated, message: "Use MongoClient(stealing:) instead.")
259-
public convenience init(fromPointer pointer: OpaquePointer) {
260-
self.init(stealing: pointer)
261-
}
262-
263-
/**
264-
* :nodoc:
265-
* Create a new client from an existing `mongoc_client_t`. The new client will destroy the `mongoc_client_t` upon
266-
* deinitialization.
267-
* Do not use this initializer unless you know what you are doing. You *must* call libmongoc_init *before* using
268-
* this initializer for the first time.
269-
*
270-
* If this client was derived from a pool, ensure that the error api version was set to 2 on the pool.
271-
*
272-
* - Parameters:
273-
* - pointer: the `mongoc_client_t` to store and use internally
274-
*/
275-
public init(stealing pointer: OpaquePointer) {
276-
self.connectionPool = ConnectionPool(stealing: pointer)
277-
self.encoder = BSONEncoder()
278-
self.decoder = BSONDecoder()
279-
self.readConcern = nil
280-
self.readPreference = ReadPreference()
281-
self.writeConcern = nil
282-
self.notificationCenter = NotificationCenter.default
283-
}
284-
285255
/**
286256
* Starts a new `ClientSession` with the provided options.
287257
*

Tests/LinuxMain.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ extension Document_SequenceTests {
161161
extension MongoClientTests {
162162
static var allTests = [
163163
("testListDatabases", testListDatabases),
164-
("testOpaqueInitialization", testOpaqueInitialization),
165164
("testFailedClientInitialization", testFailedClientInitialization),
166165
("testServerVersion", testServerVersion),
167166
("testCodingStrategies", testCodingStrategies),

Tests/MongoSwiftTests/MongoClientTests.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,6 @@ final class MongoClientTests: MongoSwiftTestCase {
5050
}
5151
}
5252

53-
func testOpaqueInitialization() throws {
54-
if MongoSwiftTestCase.ssl {
55-
print("Skipping test, bypasses SSL setup")
56-
return
57-
}
58-
let connectionString = MongoSwiftTestCase.connStr
59-
var error = bson_error_t()
60-
guard let uri = mongoc_uri_new_with_error(connectionString, &error) else {
61-
throw extractMongoError(error: error)
62-
}
63-
64-
guard let client_t = mongoc_client_new_from_uri(uri) else {
65-
throw UserError.invalidArgumentError(message: "libmongoc not built with TLS support.")
66-
}
67-
68-
let client = MongoClient(stealing: client_t)
69-
let db = client.db(type(of: self).testDatabase)
70-
let coll = db.collection(self.getCollectionName())
71-
let insertResult = try coll.insertOne(["test": 42])
72-
let findResult = try coll.find(["_id": insertResult!.insertedId])
73-
let docs = Array(findResult)
74-
expect(docs[0]["test"]).to(equal(42))
75-
try db.drop()
76-
}
77-
7853
func testFailedClientInitialization() {
7954
// check that we fail gracefully with an error if passing in an invalid URI
8055
expect(try MongoClient("abcd")).to(throwError(UserError.invalidArgumentError(message: "")))

release/release.sh

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,5 @@ git tag "v${1}"
2323
git push
2424
git push --tags
2525

26-
# update podspec
27-
cat > ${PWD}/MongoSwift.podspec <<- EOF
28-
Pod::Spec.new do |spec|
29-
spec.name = "MongoSwift"
30-
spec.version = "${1}"
31-
spec.summary = "The Swift driver for MongoDB"
32-
spec.homepage = "https://github.com/mongodb/mongo-swift-driver"
33-
spec.license = 'Apache License, Version 2.0'
34-
spec.authors = {
35-
"Matt Broadstone" => "mbroadst@mongodb.com",
36-
"Kaitlin Mahar" => "kaitlin.mahar@mongodb.com",
37-
"Patrick Freed" => "patrick.freed@mongodb.com"
38-
}
39-
40-
spec.source = {
41-
:git => "https://github.com/mongodb/mongo-swift-driver.git",
42-
:tag => 'v${1}'
43-
}
44-
45-
spec.ios.deployment_target = "11.0"
46-
spec.tvos.deployment_target = "10.2"
47-
spec.watchos.deployment_target = "4.3"
48-
49-
spec.requires_arc = true
50-
spec.source_files = "Sources/MongoSwift/**/*.swift"
51-
52-
spec.dependency 'mongo-embedded-c-driver', '~> 1.13.0-4.0.0'
53-
end
54-
EOF
55-
56-
# publish new podspec
57-
pod trunk push ${PWD}/MongoSwift.podspec --allow-warnings
58-
59-
# cleanup podspec
60-
rm ${PWD}/MongoSwift.podspec
61-
6226
# go to GitHub to publish release notes
6327
open "https://github.com/mongodb/mongo-swift-driver/releases/tag/v${1}"

0 commit comments

Comments
 (0)