Skip to content

Commit f5b0c92

Browse files
RUST-728 Inherit collection options in find and count_documents (1.2.x) (#319)
1 parent e015359 commit f5b0c92

File tree

31 files changed

+146
-145
lines changed

31 files changed

+146
-145
lines changed

src/cmap/conn/command.rs

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

2322
impl Command {
24-
/// Constructs a read command.
25-
pub(crate) fn new_read(
26-
name: String,
27-
target_db: String,
28-
read_pref: Option<ReadPreference>,
29-
body: Document,
30-
) -> Self {
31-
Self {
32-
name,
33-
target_db,
34-
read_pref,
35-
body,
36-
}
37-
}
38-
3923
/// Constructs a new command.
4024
pub(crate) fn new(name: String, target_db: String, body: Document) -> Self {
4125
Self {
4226
name,
4327
target_db,
44-
read_pref: None,
4528
body,
4629
}
4730
}
@@ -78,6 +61,11 @@ impl Command {
7861
self.body.insert("apiDeprecationErrors", deprecation_errors);
7962
}
8063
}
64+
65+
pub(crate) fn set_read_preference(&mut self, read_preference: ReadPreference) {
66+
self.body
67+
.insert("$readPreference", read_preference.into_document());
68+
}
8169
}
8270

8371
#[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: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,10 @@ where
232232
filter: impl Into<Option<Document>>,
233233
options: impl Into<Option<CountOptions>>,
234234
) -> Result<i64> {
235-
let options = options.into();
236-
let filter = filter.into();
237-
let op = CountDocuments::new(self.namespace(), filter, options);
235+
let mut options = options.into();
236+
resolve_options!(self, options, [read_concern, selection_criteria]);
237+
238+
let op = CountDocuments::new(self.namespace(), filter.into(), options);
238239
self.client().execute_operation(op).await
239240
}
240241

@@ -294,7 +295,10 @@ where
294295
filter: impl Into<Option<Document>>,
295296
options: impl Into<Option<FindOptions>>,
296297
) -> Result<Cursor<T>> {
297-
let find = Find::new(self.namespace(), filter.into(), options.into());
298+
let mut options = options.into();
299+
resolve_options!(self, options, [read_concern, selection_criteria]);
300+
301+
let find = Find::new(self.namespace(), filter.into(), options);
298302
let client = self.client();
299303

300304
client
@@ -309,11 +313,10 @@ where
309313
filter: impl Into<Option<Document>>,
310314
options: impl Into<Option<FindOneOptions>>,
311315
) -> Result<Option<T>> {
312-
let mut options: FindOptions = options
313-
.into()
314-
.map(Into::into)
315-
.unwrap_or_else(Default::default);
316-
options.limit = Some(-1);
316+
let mut options = options.into();
317+
resolve_options!(self, options, [read_concern, selection_criteria]);
318+
319+
let options: FindOptions = options.map(Into::into).unwrap_or_else(Default::default);
317320
let mut cursor = self.find(filter, Some(options)).await?;
318321
cursor.next().await.transpose()
319322
}

src/error.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ impl Error {
4444

4545
pub(crate) fn pool_cleared_error(address: &StreamAddress) -> Self {
4646
ErrorKind::ConnectionPoolClearedError {
47-
message: format!("Conneciton pool for {} cleared during operation execution", address)
47+
message: format!(
48+
"Conneciton pool for {} cleared during operation execution",
49+
address
50+
),
4851
}
4952
.into()
5053
}
@@ -129,10 +132,13 @@ impl Error {
129132

130133
/// Whether an error originated from the server.
131134
pub(crate) fn is_server_error(&self) -> bool {
132-
matches!(self.kind.as_ref(), ErrorKind::AuthenticationError { .. }
133-
| ErrorKind::BulkWriteError(_)
134-
| ErrorKind::CommandError(_)
135-
| ErrorKind::WriteError(_))
135+
matches!(
136+
self.kind.as_ref(),
137+
ErrorKind::AuthenticationError { .. }
138+
| ErrorKind::BulkWriteError(_)
139+
| ErrorKind::CommandError(_)
140+
| ErrorKind::WriteError(_)
141+
)
136142
}
137143

138144
/// Returns the labels for this error.
@@ -153,7 +159,9 @@ impl Error {
153159

154160
/// Whether this error contains the specified label.
155161
pub fn contains_label<T: AsRef<str>>(&self, label: T) -> bool {
156-
self.labels().iter().any(|actual_label| actual_label.as_str() == label.as_ref())
162+
self.labels()
163+
.iter()
164+
.any(|actual_label| actual_label.as_str() == label.as_ref())
157165
}
158166

159167
/// Returns a copy of this Error with the specified label added.
@@ -373,7 +381,10 @@ impl ErrorKind {
373381
}
374382

375383
pub(crate) fn is_network_error(&self) -> bool {
376-
matches!(self, ErrorKind::Io(..) | ErrorKind::ConnectionPoolClearedError { .. })
384+
matches!(
385+
self,
386+
ErrorKind::Io(..) | ErrorKind::ConnectionPoolClearedError { .. }
387+
)
377388
}
378389

379390
/// Gets the code/message tuple from this error, if applicable. In the case of write errors, the
@@ -397,13 +408,14 @@ impl ErrorKind {
397408
pub(crate) fn code_name(&self) -> Option<&str> {
398409
match self {
399410
ErrorKind::CommandError(ref cmd_err) => Some(cmd_err.code_name.as_str()),
400-
ErrorKind::WriteError(ref failure) => {
401-
match failure {
402-
WriteFailure::WriteConcernError(ref wce) => Some(wce.code_name.as_str()),
403-
WriteFailure::WriteError(ref we) => we.code_name.as_deref(),
404-
}
405-
}
406-
ErrorKind::BulkWriteError(ref bwe) => bwe.write_concern_error.as_ref().map(|wce| wce.code_name.as_str()),
411+
ErrorKind::WriteError(ref failure) => match failure {
412+
WriteFailure::WriteConcernError(ref wce) => Some(wce.code_name.as_str()),
413+
WriteFailure::WriteError(ref we) => we.code_name.as_deref(),
414+
},
415+
ErrorKind::BulkWriteError(ref bwe) => bwe
416+
.write_concern_error
417+
.as_ref()
418+
.map(|wce| wce.code_name.as_str()),
407419
_ => None,
408420
}
409421
}
@@ -564,8 +576,8 @@ pub enum WriteFailure {
564576
/// An error that occurred due to not being able to satisfy a write concern.
565577
WriteConcernError(WriteConcernError),
566578

567-
/// An error that occurred during a write operation that wasn't due to being unable to satisfy a
568-
/// write concern.
579+
/// An error that occurred during a write operation that wasn't due to being unable to satisfy
580+
/// a write concern.
569581
WriteError(WriteError),
570582
}
571583

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
7373
#![warn(missing_docs)]
7474
#![warn(missing_crate_level_docs)]
75-
7675
#![cfg_attr(
7776
feature = "cargo-clippy",
7877
allow(

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)]

0 commit comments

Comments
 (0)