Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dist: trusty

env:
global:
- MONGODB_VERSION=3.6.5
- MONGODB_VERSION=3.6.14
- LIBMONGOC_VERSION=r1.15
- LIBMONGOC_CACHE_DIR=${HOME}/libmongoc-${LIBMONGOC_VERSION}

Expand Down
28 changes: 8 additions & 20 deletions Sources/MongoSwift/ConnectionString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,33 @@ internal class ConnectionString {
/// Pointer to the underlying `mongoc_uri_t`.
internal let _uri: OpaquePointer

/// Initializes a new `ConnectionString` with the provided options. Updates `options` to correctly reflect the
/// final options set on the `ConnectionString`.
internal init(_ connectionString: String, options: inout ClientOptions) throws {
/// Initializes a new `ConnectionString` with the provided options.
internal init(_ connectionString: String, options: ClientOptions? = nil) throws {
Copy link
Contributor Author

@kmahar kmahar Oct 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some reason I wrote this clunky inout API where the connection string would update the client options struct to reflect the final values on the URI so that the MongoClient could have correct values for read concern, write concern, and read preference.
however, it's way simpler to just have MongoClient read back those values by calling the appropriate getters on the connection string after it creates it (see MongoClient.init.)

var error = bson_error_t()
guard let uri = mongoc_uri_new_with_error(connectionString, &error) else {
throw extractMongoError(error: error)
}
self._uri = uri

if let rc = options.readConcern {
if let rc = options?.readConcern {
self.readConcern = rc
}
options.readConcern = self.readConcern

if let wc = options.writeConcern {
if let wc = options?.writeConcern {
self.writeConcern = wc
}
options.writeConcern = self.writeConcern

if let rp = options.readPreference {
if let rp = options?.readPreference {
self.readPreference = rp
}
options.readPreference = self.readPreference

if let rw = options.retryWrites {
if let rw = options?.retryWrites {
mongoc_uri_set_option_as_bool(self._uri, MONGOC_URI_RETRYWRITES, rw)
}
if let rr = options.retryReads {
mongoc_uri_set_option_as_bool(self._uri, MONGOC_URI_RETRYREADS, rr)
}

// we can't get values for retryReads and retryWrites individually, so we will read
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the client doesn't get the options struct back anymore, and doesn't actually care what the values of retryReads and retryWrites are, so we don't need to worry about retrieving those values anymore to send back.

// them out from the full doc here instead.
guard let opts = mongoc_uri_get_options(self._uri) else {
return
if let rr = options?.retryReads {
mongoc_uri_set_option_as_bool(self._uri, MONGOC_URI_RETRYREADS, rr)
}
let optsDoc = Document(copying: opts)
options.retryReads = optsDoc["retryreads"] as? Bool
options.retryWrites = optsDoc["retrywrites"] as? Bool
}

/// Cleans up the underlying `mongoc_uri_t`.
Expand Down
19 changes: 10 additions & 9 deletions Sources/MongoSwift/MongoClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,29 +217,30 @@ public class SyncMongoClient {
// Initialize mongoc. Repeated calls have no effect so this is safe to do every time.
initializeMongoc()

var options = options ?? ClientOptions()
let connString = try ConnectionString(connectionString, options: &options)
self.connectionPool = try ConnectionPool(from: connString, options: options.tlsOptions)
let connString = try ConnectionString(connectionString, options: options)
self.connectionPool = try ConnectionPool(from: connString, options: options?.tlsOptions)

if let rc = options.readConcern, !rc.isDefault {
let rc = connString.readConcern
if !rc.isDefault {
self.readConcern = rc
} else {
self.readConcern = nil
}

if let wc = options.writeConcern, !wc.isDefault {
let wc = connString.writeConcern
if !wc.isDefault {
self.writeConcern = wc
} else {
self.writeConcern = nil
}

self.readPreference = options.readPreference ?? ReadPreference()
self.readPreference = connString.readPreference
self.encoder = BSONEncoder(options: options)
self.decoder = BSONDecoder(options: options)
self.notificationCenter = options.notificationCenter ?? NotificationCenter.default
self.notificationCenter = options?.notificationCenter ?? NotificationCenter.default

self.connectionPool.initializeMonitoring(commandMonitoring: options.commandMonitoring,
serverMonitoring: options.serverMonitoring,
self.connectionPool.initializeMonitoring(commandMonitoring: options?.commandMonitoring ?? false,
serverMonitoring: options?.serverMonitoring ?? false,
client: self)
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
@testable import MongoSwiftTests
import XCTest

extension AuthTests {
static var allTests = [
("testAuthConnectionStrings", testAuthConnectionStrings),
("testAuthProseTests", testAuthProseTests),
]
}

extension BSONValueTests {
static var allTests = [
("testInvalidDecimal128", testInvalidDecimal128),
Expand Down Expand Up @@ -283,6 +290,7 @@ extension SDAMTests {
}

XCTMain([
testCase(AuthTests.allTests),
testCase(BSONValueTests.allTests),
testCase(ChangeStreamSpecTests.allTests),
testCase(ChangeStreamTests.allTests),
Expand Down
Loading