Skip to content

Commit 094b112

Browse files
authored
Remove OperationExecutor protocol and executeOperationAsync helper (#390)
1 parent 346e9fc commit 094b112

File tree

8 files changed

+23
-48
lines changed

8 files changed

+23
-48
lines changed

Sources/MongoSwift/MongoClient.swift

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public class MongoClient {
255255

256256
let connString = try ConnectionString(connectionString, options: options)
257257
self.connectionPool = try ConnectionPool(from: connString, options: options?.tlsOptions)
258-
self.operationExecutor = DefaultOperationExecutor(
258+
self.operationExecutor = OperationExecutor(
259259
eventLoopGroup: eventLoopGroup,
260260
threadPoolSize: options?.threadPoolSize ?? MongoClient.defaultThreadPoolSize
261261
)
@@ -350,7 +350,7 @@ public class MongoClient {
350350
session: ClientSession? = nil
351351
) -> EventLoopFuture<[DatabaseSpecification]> {
352352
let operation = ListDatabasesOperation(client: self, filter: filter, nameOnly: nil)
353-
return self.executeOperationAsync(operation, session: session).flatMapThrowing { result in
353+
return self.operationExecutor.execute(operation, client: self, session: session).flatMapThrowing { result in
354354
guard case let .specs(dbs) = result else {
355355
throw InternalError(message: "Invalid result")
356356
}
@@ -394,7 +394,7 @@ public class MongoClient {
394394
session: ClientSession? = nil
395395
) -> EventLoopFuture<[String]> {
396396
let operation = ListDatabasesOperation(client: self, filter: filter, nameOnly: true)
397-
return self.executeOperationAsync(operation, session: session).flatMapThrowing { result in
397+
return self.operationExecutor.execute(operation, client: self, session: session).flatMapThrowing { result in
398398
guard case let .names(names) = result else {
399399
throw InternalError(message: "Invalid result")
400400
}
@@ -542,15 +542,6 @@ public class MongoClient {
542542
) throws -> T.OperationResult {
543543
return try self.operationExecutor.execute(operation, using: connection, client: self, session: session).wait()
544544
}
545-
546-
/// Executes an `Operation` asynchronously using this `MongoClient` and an optionally provided session.
547-
internal func executeOperationAsync<T: Operation>(
548-
_ operation: T,
549-
using connection: Connection? = nil,
550-
session: ClientSession? = nil
551-
) -> EventLoopFuture<T.OperationResult> {
552-
return self.operationExecutor.execute(operation, using: connection, client: self, session: session)
553-
}
554545
}
555546

556547
extension MongoClient: Equatable {

Sources/MongoSwift/MongoCollection+BulkWrite.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension MongoCollection {
3131
.makeFailedFuture(InvalidArgumentError(message: "requests cannot be empty"))
3232
}
3333
let operation = BulkWriteOperation(collection: self, models: requests, options: options)
34-
return self._client.executeOperationAsync(operation, session: session)
34+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
3535
}
3636
}
3737

Sources/MongoSwift/MongoCollection+FindAndModify.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ extension MongoCollection {
110110
session: ClientSession?
111111
) -> EventLoopFuture<CollectionType?> {
112112
let operation = FindAndModifyOperation(collection: self, filter: filter, update: update, options: options)
113-
return self._client.executeOperationAsync(operation, session: session)
113+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
114114
}
115115
}
116116

Sources/MongoSwift/MongoCollection+Indexes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ extension MongoCollection {
244244
.makeFailedFuture(InvalidArgumentError(message: "models cannot be empty"))
245245
}
246246
let operation = CreateIndexesOperation(collection: self, models: models, options: options)
247-
return self._client.executeOperationAsync(operation, session: session)
247+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
248248
}
249249

250250
/**
@@ -357,7 +357,7 @@ extension MongoCollection {
357357
session: ClientSession?
358358
) -> EventLoopFuture<DropIndexesResult> {
359359
let operation = DropIndexesOperation(collection: self, index: index, options: options)
360-
return self._client.executeOperationAsync(operation, session: session)
360+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
361361
}
362362

363363
/**

Sources/MongoSwift/MongoCollection+Read.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ extension MongoCollection {
9494
session: ClientSession? = nil
9595
) -> EventLoopFuture<Int> {
9696
let operation = CountDocumentsOperation(collection: self, filter: filter, options: options)
97-
return self._client.executeOperationAsync(operation, session: session)
97+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
9898
}
9999

100100
/**
@@ -111,7 +111,7 @@ extension MongoCollection {
111111
session: ClientSession? = nil
112112
) -> EventLoopFuture<Int> {
113113
let operation = EstimatedDocumentCountOperation(collection: self, options: options)
114-
return self._client.executeOperationAsync(operation, session: session)
114+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
115115
}
116116

117117
/**
@@ -138,6 +138,6 @@ extension MongoCollection {
138138
session: ClientSession? = nil
139139
) -> EventLoopFuture<[BSON]> {
140140
let operation = DistinctOperation(collection: self, fieldName: fieldName, filter: filter, options: options)
141-
return self._client.executeOperationAsync(operation, session: session)
141+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
142142
}
143143
}

Sources/MongoSwift/MongoCollection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public struct MongoCollection<T: Codable> {
9595
*/
9696
public func drop(options: DropCollectionOptions? = nil, session: ClientSession? = nil) -> EventLoopFuture<Void> {
9797
let operation = DropCollectionOperation(collection: self, options: options)
98-
return self._client.executeOperationAsync(operation, session: session)
98+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
9999
}
100100

101101
/// Uses the provided `Connection` to get a pointer to a `mongoc_collection_t` corresponding to this

Sources/MongoSwift/MongoDatabase.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public struct MongoDatabase {
123123
*/
124124
public func drop(options: DropDatabaseOptions? = nil, session: ClientSession? = nil) -> EventLoopFuture<Void> {
125125
let operation = DropDatabaseOperation(database: self, options: options)
126-
return self._client.executeOperationAsync(operation, session: session)
126+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
127127
}
128128

129129
/**
@@ -214,7 +214,7 @@ public struct MongoDatabase {
214214
session: ClientSession? = nil
215215
) -> EventLoopFuture<MongoCollection<T>> {
216216
let operation = CreateCollectionOperation(database: self, name: name, type: type, options: options)
217-
return self._client.executeOperationAsync(operation, session: session)
217+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
218218
}
219219

220220
/**
@@ -288,12 +288,13 @@ public struct MongoDatabase {
288288
session: ClientSession? = nil
289289
) -> EventLoopFuture<[String]> {
290290
let operation = ListCollectionsOperation(database: self, nameOnly: true, filter: filter, options: options)
291-
return self._client.executeOperationAsync(operation, session: session).flatMapThrowing { result in
292-
guard case let .names(names) = result else {
293-
throw InternalError(message: "Invalid result")
291+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
292+
.flatMapThrowing { result in
293+
guard case let .names(names) = result else {
294+
throw InternalError(message: "Invalid result")
295+
}
296+
return names
294297
}
295-
return names
296-
}
297298
}
298299

299300
/**
@@ -320,7 +321,7 @@ public struct MongoDatabase {
320321
session: ClientSession? = nil
321322
) -> EventLoopFuture<Document> {
322323
let operation = RunCommandOperation(database: self, command: command, options: options)
323-
return self._client.executeOperationAsync(operation, session: session)
324+
return self._client.operationExecutor.execute(operation, client: self._client, session: session)
324325
}
325326

326327
/**

Sources/MongoSwift/Operations/Operation.swift

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,8 @@ internal protocol Operation {
1111
func execute(using connection: Connection, session: ClientSession?) throws -> OperationResult
1212
}
1313

14-
/// A protocol for types that can be used to execute `Operation`s.
15-
internal protocol OperationExecutor {
16-
/// Asynchronously executes an operation using the provided client and optionally provided session.
17-
func execute<T: Operation>(
18-
_ operation: T,
19-
using connection: Connection?,
20-
client: MongoClient,
21-
session: ClientSession?
22-
) -> EventLoopFuture<T.OperationResult>
23-
/// Asynchronously executes a block of code.
24-
func execute<T>(body: @escaping () throws -> T) -> EventLoopFuture<T>
25-
/// Closes the executor.
26-
func close() -> EventLoopFuture<Void>
27-
/// Makes a failed `EventLoopFuture` with the provided error.
28-
func makeFailedFuture<T>(_ error: Error) -> EventLoopFuture<T>
29-
}
30-
31-
/// Default executor type used by `MongoClient`s.
32-
internal class DefaultOperationExecutor: OperationExecutor {
14+
/// Operation executor used by `MongoClient`s.
15+
internal class OperationExecutor {
3316
/// A group of event loops to use for running operations in the thread pool.
3417
private let eventLoopGroup: EventLoopGroup
3518
/// The thread pool to execute operations in.
@@ -56,7 +39,7 @@ internal class DefaultOperationExecutor: OperationExecutor {
5639

5740
internal func execute<T: Operation>(
5841
_ operation: T,
59-
using connection: Connection?,
42+
using connection: Connection? = nil,
6043
client: MongoClient,
6144
session: ClientSession?
6245
) -> EventLoopFuture<T.OperationResult> {

0 commit comments

Comments
 (0)