-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathEstimatedDocumentCountOperation.swift
57 lines (47 loc) · 2.11 KB
/
EstimatedDocumentCountOperation.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
48
49
50
51
52
53
54
55
56
57
import CLibMongoC
/// Options to use when executing an `estimatedDocumentCount` command on a `MongoCollection`.
public struct EstimatedDocumentCountOptions: Codable {
/// The maximum amount of time to allow the query to run.
public var maxTimeMS: Int?
/// A ReadConcern to use for this operation.
public var readConcern: ReadConcern?
// swiftlint:disable redundant_optional_initialization
/// A ReadPreference to use for this operation.
public var readPreference: ReadPreference? = nil
// swiftlint:enable redundant_optional_initialization
/// Convenience initializer allowing any/all parameters to be optional
public init(
maxTimeMS: Int? = nil,
readConcern: ReadConcern? = nil,
readPreference: ReadPreference? = nil
) {
self.maxTimeMS = maxTimeMS
self.readConcern = readConcern
self.readPreference = readPreference
}
internal enum CodingKeys: String, CodingKey, CaseIterable {
case maxTimeMS, readConcern
}
}
/// An operation corresponding to a "count" command on a collection.
internal struct EstimatedDocumentCountOperation<T: Codable>: Operation {
private let collection: MongoCollection<T>
private let options: EstimatedDocumentCountOptions?
internal init(collection: MongoCollection<T>, options: EstimatedDocumentCountOptions?) {
self.collection = collection
self.options = options
}
internal func execute(using connection: Connection, session: ClientSession?) throws -> Int {
let opts = try encodeOptions(options: options, session: session)
var error = bson_error_t()
let count = self.collection.withMongocCollection(from: connection) { collPtr in
withOptionalBSONPointer(to: opts) { optsPtr in
ReadPreference.withOptionalMongocReadPreference(from: self.options?.readPreference) { rpPtr in
mongoc_collection_estimated_document_count(collPtr, optsPtr, rpPtr, nil, &error)
}
}
}
guard count != -1 else { throw extractMongoError(error: error) }
return Int(count)
}
}