Skip to content

Commit e130fc0

Browse files
authored
SWIFT-772: Add 'allowDiskUse' option to find command
1 parent 21dbc93 commit e130fc0

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

Sources/MongoSwift/Operations/AggregateOperation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33

44
/// Options to use when executing an `aggregate` command on a `MongoCollection`.
55
public struct AggregateOptions: Codable {
6-
/// Enables writing to temporary files. When set to true, aggregation stages
6+
/// Enables the server to write to temporary files. When set to true, the aggregate operation
77
/// can write data to the _tmp subdirectory in the dbPath directory.
88
public var allowDiskUse: Bool?
99

Sources/MongoSwift/Operations/FindOperation.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public enum CursorType {
3232

3333
/// Options to use when executing a `find` command on a `MongoCollection`.
3434
public struct FindOptions: Codable {
35+
/// Enables the server to write to temporary files. When set to true, the find operation
36+
/// can write data to the _tmp subdirectory in the dbPath directory.
37+
public var allowDiskUse: Bool?
38+
3539
/// Get partial results from a mongos if some shards are down (instead of throwing an error).
3640
public var allowPartialResults: Bool?
3741

@@ -130,6 +134,7 @@ public struct FindOptions: Codable {
130134

131135
/// Convenience initializer allowing any/all parameters to be omitted or optional.
132136
public init(
137+
allowDiskUse: Bool? = nil,
133138
allowPartialResults: Bool? = nil,
134139
batchSize: Int32? = nil,
135140
collation: Document? = nil,
@@ -150,6 +155,7 @@ public struct FindOptions: Codable {
150155
skip: Int64? = nil,
151156
sort: Document? = nil
152157
) {
158+
self.allowDiskUse = allowDiskUse
153159
self.allowPartialResults = allowPartialResults
154160
self.batchSize = batchSize
155161
self.collation = collation
@@ -191,9 +197,9 @@ public struct FindOptions: Codable {
191197

192198
// Encode everything except `self.readPreference`, because this is sent to libmongoc separately
193199
private enum CodingKeys: String, CodingKey {
194-
case allowPartialResults, awaitData, batchSize, collation, comment, hint, limit, max, maxAwaitTimeMS,
195-
maxTimeMS, min, noCursorTimeout, projection, readConcern, returnKey, showRecordId, tailable, skip,
196-
sort
200+
case allowDiskUse, allowPartialResults, awaitData, batchSize, collation, comment, hint, limit, max,
201+
maxAwaitTimeMS, maxTimeMS, min, noCursorTimeout, projection, readConcern, returnKey, showRecordId,
202+
tailable, skip, sort
197203
}
198204
}
199205

Tests/LinuxMain.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ extension MongoCollection_IndexTests {
227227
]
228228
}
229229

230+
extension MongoCrudV2Tests {
231+
static var allTests = [
232+
("testFindOptionsAllowDiskUse", testFindOptionsAllowDiskUse),
233+
]
234+
}
235+
230236
extension MongoCursorTests {
231237
static var allTests = [
232238
("testNonTailableCursor", testNonTailableCursor),
@@ -399,6 +405,7 @@ XCTMain([
399405
testCase(MongoCollectionTests.allTests),
400406
testCase(MongoCollection_BulkWriteTests.allTests),
401407
testCase(MongoCollection_IndexTests.allTests),
408+
testCase(MongoCrudV2Tests.allTests),
402409
testCase(MongoCursorTests.allTests),
403410
testCase(MongoDatabaseTests.allTests),
404411
testCase(OptionsTests.allTests),
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Foundation
2+
import MongoSwiftSync
3+
import Nimble
4+
import TestsCommon
5+
import XCTest
6+
7+
// TODO: remove with SWIFT-780
8+
/// A place for CrudV2 Tests until the swift crud v2 runner is shipped
9+
final class MongoCrudV2Tests: MongoSwiftTestCase {
10+
func testFindOptionsAllowDiskUse() throws {
11+
try self.withTestNamespace { client, _, coll in
12+
guard try client.serverVersion() >= ServerVersion(major: 4, minor: 3, patch: 5) else {
13+
print("Skipping test; MongoDB 4.3.5+ feature")
14+
return
15+
}
16+
17+
let monitor = client.addCommandMonitor()
18+
try coll.insertOne(["dog": "notCat"])
19+
20+
try monitor.captureEvents {
21+
let optionAllowDiskUseNil = FindOptions()
22+
expect(try coll.find(["dog": "notCat"], options: optionAllowDiskUseNil)).toNot(throwError())
23+
24+
let optionAllowDiskUseFalse = FindOptions(allowDiskUse: false)
25+
expect(try coll.find(["dog": "notCat"], options: optionAllowDiskUseFalse)).toNot(throwError())
26+
27+
let optionAllowDiskUseTrue = FindOptions(allowDiskUse: true)
28+
expect(try coll.find(["dog": "notCat"], options: optionAllowDiskUseTrue)).toNot(throwError())
29+
}
30+
31+
let events = monitor.commandStartedEvents()
32+
expect(events).to(haveCount(3))
33+
34+
let eventAllowDiskUseNil = events[0]
35+
expect(eventAllowDiskUseNil.command["find"]).toNot(beNil())
36+
expect(eventAllowDiskUseNil.command["allowDiskUse"]).to(beNil())
37+
38+
let eventAllowDiskUseFalse = events[1]
39+
expect(eventAllowDiskUseFalse.command["find"]).toNot(beNil())
40+
expect(eventAllowDiskUseFalse.command["allowDiskUse"]?.boolValue).to(beFalse())
41+
42+
let eventAllowDiskUseTrue = events[2]
43+
expect(eventAllowDiskUseTrue.command["find"]).toNot(beNil())
44+
expect(eventAllowDiskUseTrue.command["allowDiskUse"]?.boolValue).to(beTrue())
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)