Skip to content

Commit 8697d77

Browse files
RUST-728 Inherit collection options in find and count_documents (#313)
1 parent 6494093 commit 8697d77

File tree

25 files changed

+91
-118
lines changed

25 files changed

+91
-118
lines changed

src/cmap/conn/command.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,15 @@ use crate::{
1717
pub(crate) struct Command {
1818
pub(crate) name: String,
1919
pub(crate) target_db: String,
20-
pub(crate) read_pref: Option<ReadPreference>,
2120
pub(crate) body: Document,
2221
}
2322

2423
impl Command {
25-
/// Constructs a read command.
26-
pub(crate) fn new_read(
27-
name: String,
28-
target_db: String,
29-
read_pref: Option<ReadPreference>,
30-
body: Document,
31-
) -> Self {
32-
Self {
33-
name,
34-
target_db,
35-
read_pref,
36-
body,
37-
}
38-
}
39-
4024
/// Constructs a new command.
4125
pub(crate) fn new(name: String, target_db: String, body: Document) -> Self {
4226
Self {
4327
name,
4428
target_db,
45-
read_pref: None,
4629
body,
4730
}
4831
}
@@ -79,6 +62,11 @@ impl Command {
7962
self.body.insert("apiDeprecationErrors", deprecation_errors);
8063
}
8164
}
65+
66+
pub(crate) fn set_read_preference(&mut self, read_preference: ReadPreference) {
67+
self.body
68+
.insert("$readPreference", read_preference.into_document());
69+
}
8270
}
8371

8472
#[derive(Debug, Clone)]

src/cmap/conn/wire/message.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ impl Message {
3030
pub(crate) fn with_command(mut command: Command, request_id: Option<i32>) -> Self {
3131
command.body.insert("$db", command.target_db);
3232

33-
if let Some(read_pref) = command.read_pref {
34-
command
35-
.body
36-
.insert("$readPreference", read_pref.into_document());
37-
};
38-
3933
Self {
4034
response_to: 0,
4135
flags: MessageFlags::empty(),

src/cmap/establish/handshake/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,9 @@ impl Handshaker {
188188

189189
body.insert("client", metadata);
190190

191-
let mut command = Command::new_read(
191+
let mut command = Command::new(
192192
"isMaster".to_string(),
193193
db.unwrap_or_else(|| "admin".to_string()),
194-
None,
195194
body,
196195
);
197196

src/cmap/establish/test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ async fn speculative_auth_test(
5858
.await
5959
.unwrap();
6060

61-
let command = Command::new_read(
61+
let mut command = Command::new(
6262
"find".into(),
6363
authorized_db_name.into(),
64-
Some(ReadPreference::PrimaryPreferred {
65-
options: Default::default(),
66-
}),
6764
doc! { "find": "foo", "limit": 1 },
6865
);
66+
command.set_read_preference(ReadPreference::PrimaryPreferred {
67+
options: Default::default(),
68+
});
6969

7070
let response = conn.send_command(command, None).await.unwrap();
7171

src/cmap/test/integration.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,8 @@ async fn acquire_connection_and_send_command() {
4848
let read_pref = ReadPreference::PrimaryPreferred {
4949
options: Default::default(),
5050
};
51-
let mut cmd = Command::new_read(
52-
"listDatabases".to_string(),
53-
"admin".to_string(),
54-
Some(read_pref),
55-
body,
56-
);
51+
let mut cmd = Command::new("listDatabases".to_string(), "admin".to_string(), body);
52+
cmd.set_read_preference(read_pref);
5753
if let Some(server_api) = client_options.server_api.as_ref() {
5854
cmd.set_server_api(server_api);
5955
}

src/coll/mod.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,10 @@ where
275275
options: impl Into<Option<CountOptions>>,
276276
session: impl Into<Option<&mut ClientSession>>,
277277
) -> Result<i64> {
278-
let options = options.into();
279-
let filter = filter.into();
280-
let op = CountDocuments::new(self.namespace(), filter, options);
278+
let mut options = options.into();
279+
resolve_options!(self, options, [read_concern, selection_criteria]);
280+
281+
let op = CountDocuments::new(self.namespace(), filter.into(), options);
281282
self.client().execute_operation(op, session).await
282283
}
283284

@@ -430,7 +431,10 @@ where
430431
filter: impl Into<Option<Document>>,
431432
options: impl Into<Option<FindOptions>>,
432433
) -> Result<Cursor<T>> {
433-
let find = Find::new(self.namespace(), filter.into(), options.into());
434+
let mut options = options.into();
435+
resolve_options!(self, options, [read_concern, selection_criteria]);
436+
437+
let find = Find::new(self.namespace(), filter.into(), options);
434438
let client = self.client();
435439

436440
client
@@ -461,10 +465,10 @@ where
461465
filter: impl Into<Option<Document>>,
462466
options: impl Into<Option<FindOneOptions>>,
463467
) -> Result<Option<T>> {
464-
let options: FindOptions = options
465-
.into()
466-
.map(Into::into)
467-
.unwrap_or_else(Default::default);
468+
let mut options = options.into();
469+
resolve_options!(self, options, [read_concern, selection_criteria]);
470+
471+
let options: FindOptions = options.map(Into::into).unwrap_or_else(Default::default);
468472
let mut cursor = self.find(filter, Some(options)).await?;
469473
cursor.next().await.transpose()
470474
}
@@ -477,10 +481,10 @@ where
477481
options: impl Into<Option<FindOneOptions>>,
478482
session: &mut ClientSession,
479483
) -> Result<Option<T>> {
480-
let options: FindOptions = options
481-
.into()
482-
.map(Into::into)
483-
.unwrap_or_else(Default::default);
484+
let mut options = options.into();
485+
resolve_options!(self, options, [read_concern, selection_criteria]);
486+
487+
let options: FindOptions = options.map(Into::into).unwrap_or_else(Default::default);
484488
let mut cursor = self
485489
.find_with_session(filter, Some(options), session)
486490
.await?;

src/operation/count/test.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ async fn build() {
2828
}
2929
);
3030
assert_eq!(count_command.target_db, "test_db");
31-
assert_eq!(count_command.read_pref, None);
3231
}
3332

3433
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
@@ -58,7 +57,6 @@ async fn build_with_options() {
5857
}
5958
);
6059
assert_eq!(count_command.target_db, "test_db");
61-
assert_eq!(count_command.read_pref, None);
6260
}
6361

6462
#[cfg_attr(feature = "tokio-runtime", tokio::test)]

src/operation/count_documents/test.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ async fn build() {
3636

3737
assert_eq!(count_command.body, expected_body);
3838
assert_eq!(count_command.target_db, "test_db");
39-
assert_eq!(count_command.read_pref, None);
4039
}
4140

4241
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
@@ -77,7 +76,6 @@ async fn build_with_options() {
7776

7877
assert_eq!(count_command.body, expected_body);
7978
assert_eq!(count_command.target_db, "test_db");
80-
assert_eq!(count_command.read_pref, None);
8179
}
8280

8381
#[cfg_attr(feature = "tokio-runtime", tokio::test)]

src/operation/create/test.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ async fn build() {
3232

3333
assert_eq!(cmd.name.as_str(), "create");
3434
assert_eq!(cmd.target_db.as_str(), "test_db");
35-
assert_eq!(cmd.read_pref.as_ref(), None);
3635
assert_eq!(
3736
cmd.body,
3837
doc! {
@@ -64,7 +63,6 @@ async fn build_validator() {
6463

6564
assert_eq!(cmd.name.as_str(), "create");
6665
assert_eq!(cmd.target_db.as_str(), "test_db");
67-
assert_eq!(cmd.read_pref.as_ref(), None);
6866
assert_eq!(
6967
cmd.body,
7068
doc! {

src/operation/delete/test.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ async fn build_many() {
3333

3434
assert_eq!(cmd.name.as_str(), "delete");
3535
assert_eq!(cmd.target_db.as_str(), "test_db");
36-
assert_eq!(cmd.read_pref.as_ref(), None);
3736

3837
let mut expected_body = doc! {
3938
"delete": "test_coll",
@@ -77,7 +76,6 @@ async fn build_one() {
7776

7877
assert_eq!(cmd.name.as_str(), "delete");
7978
assert_eq!(cmd.target_db.as_str(), "test_db");
80-
assert_eq!(cmd.read_pref.as_ref(), None);
8179

8280
let mut expected_body = doc! {
8381
"delete": "test_coll",

0 commit comments

Comments
 (0)