-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathDropIndexesOperation.swift
47 lines (40 loc) · 1.76 KB
/
DropIndexesOperation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import CLibMongoC
/// Options to use when dropping an index from a `MongoCollection`.
public struct DropIndexOptions: Encodable {
/// The maximum amount of time to allow the query to run - enforced server-side.
public var maxTimeMS: Int?
/// An optional `WriteConcern` to use for the command.
public var writeConcern: WriteConcern?
/// Initializer allowing any/all parameters to be omitted.
public init(maxTimeMS: Int? = nil, writeConcern: WriteConcern? = nil) {
self.maxTimeMS = maxTimeMS
self.writeConcern = writeConcern
}
}
/// An operation corresponding to a "dropIndexes" command.
internal struct DropIndexesOperation<T: Codable>: Operation {
private let collection: MongoCollection<T>
private let index: BSON
private let options: DropIndexOptions?
internal init(collection: MongoCollection<T>, index: BSON, options: DropIndexOptions?) {
self.collection = collection
self.index = index
self.options = options
}
internal func execute(using connection: Connection, session: ClientSession?) throws {
let command: BSONDocument = ["dropIndexes": .string(self.collection.name), "index": self.index]
let opts = try encodeOptions(options: self.options, session: session)
do {
try self.collection.withMongocCollection(from: connection) { collPtr in
try runMongocCommand(command: command, options: opts) { cmdPtr, optsPtr, replyPtr, error in
mongoc_collection_write_command_with_opts(collPtr, cmdPtr, optsPtr, replyPtr, &error)
}
}
} catch let error as MongoErrorProtocol {
guard !error.isNsNotFound else {
return
}
throw error
}
}
}