Skip to content

Commit 845a5d0

Browse files
committed
various cleanup
1 parent b2afcb1 commit 845a5d0

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

src/client/executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ impl Client {
400400
bytes: serialized,
401401
};
402402

403-
let start_time = Instant::now();
404403
self.emit_command_event(|handler| {
405404
let command_body = if should_redact {
406405
Document::new()
@@ -419,6 +418,7 @@ impl Client {
419418
handler.handle_command_started_event(command_started_event);
420419
});
421420

421+
let start_time = Instant::now();
422422
let command_result = match connection.send_raw_command(raw_cmd, request_id).await {
423423
Ok(response) => {
424424
match T::Response::deserialize_response(&response) {

src/operation/insert/mod.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(crate) struct Insert<'a, T> {
2525
ns: Namespace,
2626
documents: Vec<&'a T>,
2727
inserted_ids: Vec<Bson>,
28-
options: InsertManyOptions,
28+
options: Option<InsertManyOptions>,
2929
}
3030

3131
impl<'a, T> Insert<'a, T> {
@@ -34,9 +34,6 @@ impl<'a, T> Insert<'a, T> {
3434
documents: Vec<&'a T>,
3535
options: Option<InsertManyOptions>,
3636
) -> Self {
37-
let mut options =
38-
options.unwrap_or_else(|| InsertManyOptions::builder().ordered(true).build());
39-
options.ordered = Some(options.ordered.unwrap_or(true));
4037
Self {
4138
ns,
4239
options,
@@ -46,7 +43,10 @@ impl<'a, T> Insert<'a, T> {
4643
}
4744

4845
fn is_ordered(&self) -> bool {
49-
self.options.ordered.unwrap_or(true)
46+
self.options
47+
.as_ref()
48+
.and_then(|o| o.ordered)
49+
.unwrap_or(true)
5050
}
5151
}
5252

@@ -58,13 +58,6 @@ impl<'a, T: Serialize> Operation for Insert<'a, T> {
5858
const NAME: &'static str = "insert";
5959

6060
fn build(&mut self, description: &StreamDescription) -> Result<Command<InsertCommand>> {
61-
if self.documents.is_empty() {
62-
return Err(ErrorKind::InvalidArgument {
63-
message: "must specify at least one document to insert".to_string(),
64-
}
65-
.into());
66-
}
67-
6861
let mut docs: Vec<Vec<u8>> = Vec::new();
6962
let mut size = 0;
7063

@@ -79,14 +72,19 @@ impl<'a, T: Serialize> Operation for Insert<'a, T> {
7972
Some(b) => b,
8073
None => {
8174
let oid = ObjectId::new();
82-
let new_len = doc.len() as i32 + 1 + 4 + 12;
83-
doc.splice(0..4, new_len.to_le_bytes().iter().cloned());
8475

85-
let mut new_doc = Vec::new();
86-
new_doc.write_u8(ElementType::ObjectId as u8)?;
87-
new_doc.write_all(b"_id\0")?;
88-
new_doc.extend(oid.bytes().iter());
89-
doc.splice(4..4, new_doc.into_iter());
76+
// write element to temporary buffer
77+
let mut new_id = Vec::new();
78+
new_id.write_u8(ElementType::ObjectId as u8)?;
79+
new_id.write_all(b"_id\0")?;
80+
new_id.extend(oid.bytes().iter());
81+
82+
// insert element to beginning of existing doc after length
83+
doc.splice(4..4, new_id.into_iter());
84+
85+
// update length of doc
86+
let new_len = doc.len() as i32;
87+
doc.splice(0..4, new_len.to_le_bytes().iter().cloned());
9088

9189
Bson::ObjectId(oid)
9290
}
@@ -112,13 +110,16 @@ impl<'a, T: Serialize> Operation for Insert<'a, T> {
112110
.into());
113111
}
114112

113+
let mut options = self.options.clone().unwrap_or_default();
114+
options.ordered = Some(self.is_ordered());
115+
115116
let body = InsertCommand {
116117
insert: self.ns.coll.clone(),
117118
documents: DocumentArraySpec {
118119
documents: docs,
119120
length: size as i32,
120121
},
121-
options: self.options.clone(),
122+
options,
122123
};
123124

124125
Ok(Command::new("insert".to_string(), self.ns.db.clone(), body))
@@ -161,7 +162,7 @@ impl<'a, T: Serialize> Operation for Insert<'a, T> {
161162

162163
// update length of original doc
163164
let final_length = serialized.len() as i32;
164-
(&mut serialized[0..4]).write_all(&final_length.to_le_bytes())?;
165+
serialized.splice(0..4, final_length.to_le_bytes().iter().cloned());
165166

166167
Ok(serialized)
167168
}
@@ -211,7 +212,7 @@ impl<'a, T: Serialize> Operation for Insert<'a, T> {
211212
}
212213

213214
fn write_concern(&self) -> Option<&WriteConcern> {
214-
self.options.write_concern.as_ref()
215+
self.options.as_ref().and_then(|o| o.write_concern.as_ref())
215216
}
216217

217218
fn retryability(&self) -> Retryability {

src/operation/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub(crate) trait Operation {
6969
/// The output type of this operation.
7070
type O;
7171

72+
/// The format of the command body constructed in `build`.
7273
type Command: CommandBody;
7374

7475
/// The format of the command response from the server.
@@ -81,6 +82,8 @@ pub(crate) trait Operation {
8182
/// The operation may store some additional state that is required for handling the response.
8283
fn build(&mut self, description: &StreamDescription) -> Result<Command<Self::Command>>;
8384

85+
/// Perform custom serialization of the built command.
86+
/// By default, this will just call through to the `Serialize` implementation of the command.
8487
fn serialize_command(&mut self, cmd: Command<Self::Command>) -> Result<Vec<u8>> {
8588
Ok(bson::to_vec(&cmd)?)
8689
}
@@ -141,15 +144,19 @@ pub(crate) trait CommandBody: Serialize {
141144

142145
impl CommandBody for Document {
143146
fn should_redact(&self) -> bool {
144-
self.contains_key("speculativeAuthenticate")
147+
if let Some(command_name) = bson_util::first_key(self) {
148+
HELLO_COMMAND_NAMES.contains(command_name.to_lowercase().as_str())
149+
&& self.contains_key("speculativeAuthenticate")
150+
} else {
151+
false
152+
}
145153
}
146154
}
147155

148156
impl<T: CommandBody> Command<T> {
149157
pub(crate) fn should_redact(&self) -> bool {
150158
let name = self.name.to_lowercase();
151-
REDACTED_COMMANDS.contains(name.as_str())
152-
|| HELLO_COMMAND_NAMES.contains(name.as_str()) && self.body.should_redact()
159+
REDACTED_COMMANDS.contains(name.as_str()) || self.body.should_redact()
153160
}
154161
}
155162

0 commit comments

Comments
 (0)