Skip to content

Commit 9ab0e15

Browse files
RUST-351 Add 'allowDiskUse' option to find command (#163)
1 parent 4ebf6cb commit 9ab0e15

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/coll/options.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,12 @@ pub struct DistinctOptions {
577577
#[derive(Debug, Default, TypedBuilder, Serialize)]
578578
#[serde(rename_all = "camelCase")]
579579
pub struct FindOptions {
580+
/// Enables writing to temporary files by the server. When set to true, the find operation can
581+
/// write data to the _tmp subdirectory in the dbPath directory. Only supported in server
582+
/// versions 4.4+.
583+
#[builder(default)]
584+
pub allow_disk_use: Option<bool>,
585+
580586
/// If true, partial results will be returned from a mongos rather than an error being
581587
/// returned if one or more shards is down.
582588
#[builder(default)]
@@ -693,6 +699,7 @@ pub struct FindOptions {
693699
impl From<FindOneOptions> for FindOptions {
694700
fn from(options: FindOneOptions) -> Self {
695701
FindOptions {
702+
allow_disk_use: None,
696703
allow_partial_results: options.allow_partial_results,
697704
collation: options.collation,
698705
comment: options.comment,

src/test/coll.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,56 @@ async fn empty_insert() {
466466
e => panic!("expected argument error, got {:?}", e),
467467
};
468468
}
469+
470+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
471+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
472+
async fn find_allow_disk_use() {
473+
let find_opts = FindOptions::builder().allow_disk_use(true).build();
474+
allow_disk_use_test(find_opts, Some(true)).await;
475+
}
476+
477+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
478+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
479+
async fn find_do_not_allow_disk_use() {
480+
let find_opts = FindOptions::builder().allow_disk_use(false).build();
481+
allow_disk_use_test(find_opts, Some(false)).await;
482+
}
483+
484+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
485+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
486+
async fn find_allow_disk_use_not_specified() {
487+
let find_opts = FindOptions::builder().build();
488+
allow_disk_use_test(find_opts, None).await;
489+
}
490+
491+
#[function_name::named]
492+
async fn allow_disk_use_test(options: FindOptions, expected_value: Option<bool>) {
493+
let _guard = LOCK.run_concurrently().await;
494+
495+
let event_client = EventClient::new().await;
496+
if event_client.server_version_lt(4, 3) {
497+
return;
498+
}
499+
let coll = event_client
500+
.database(function_name!())
501+
.collection(function_name!());
502+
coll.find(None, options).await.unwrap();
503+
504+
let events = event_client.command_events.read().unwrap();
505+
let mut iter = events.iter().filter(|event| match event {
506+
CommandEvent::CommandStartedEvent(CommandStartedEvent { command_name, .. }) => {
507+
command_name == "find"
508+
}
509+
_ => false,
510+
});
511+
512+
let event = iter.next().unwrap();
513+
let allow_disk_use = match event {
514+
CommandEvent::CommandStartedEvent(CommandStartedEvent { command, .. }) => {
515+
command.get_bool("allowDiskUse").ok()
516+
}
517+
_ => None,
518+
};
519+
assert_eq!(allow_disk_use, expected_value);
520+
assert_eq!(iter.count(), 0);
521+
}

0 commit comments

Comments
 (0)