Skip to content

Commit a7e4c93

Browse files
improve command serialization
1 parent ff90fc2 commit a7e4c93

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

src/bson_util.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ pub(crate) fn read_document_bytes<R: Read>(mut reader: R) -> Result<Vec<u8>> {
125125
Ok(bytes)
126126
}
127127

128+
pub(crate) fn extend_raw_document_buf(
129+
this: &mut RawDocumentBuf,
130+
other: RawDocumentBuf,
131+
) -> Result<()> {
132+
for result in other.iter() {
133+
let (k, v) = result?;
134+
this.append(k, v.to_raw_bson());
135+
}
136+
Ok(())
137+
}
138+
128139
#[cfg(test)]
129140
mod test {
130141
use crate::bson_util::num_decimal_digits;

src/cmap/conn/command.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
33

44
use super::wire::Message;
55
use crate::{
6-
bson::Document,
6+
bson::{rawdoc, Document},
7+
bson_util::extend_raw_document_buf,
78
client::{options::ServerApi, ClusterTime, HELLO_COMMAND_NAMES, REDACTED_COMMANDS},
89
error::{Error, ErrorKind, Result},
910
hello::{HelloCommandResponse, HelloReply},
@@ -177,6 +178,19 @@ impl<T> Command<T> {
177178
}
178179
}
179180

181+
impl Command<RawDocumentBuf> {
182+
pub(crate) fn into_bson_bytes(mut self) -> Result<Vec<u8>> {
183+
let mut command = self.body;
184+
185+
// Clear the body of the command to avoid re-serializing.
186+
self.body = rawdoc! {};
187+
let rest_of_command = bson::to_raw_document_buf(&self)?;
188+
189+
extend_raw_document_buf(&mut command, rest_of_command)?;
190+
Ok(command.into_bytes())
191+
}
192+
}
193+
180194
#[derive(Debug, Clone)]
181195
pub(crate) struct RawCommandResponse {
182196
pub(crate) source: ServerAddress,

src/operation.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
3232

3333
use crate::{
3434
bson::{self, Bson, Document},
35-
bson_util,
35+
bson_util::{self, extend_raw_document_buf},
3636
client::{ClusterTime, HELLO_COMMAND_NAMES, REDACTED_COMMANDS},
3737
cmap::{conn::PinnedConnectionHandle, Command, RawCommandResponse, StreamDescription},
3838
error::{
@@ -243,10 +243,7 @@ pub(crate) fn append_options_to_raw_document<T: Serialize>(
243243
) -> Result<()> {
244244
if let Some(options) = options {
245245
let options_raw_doc = bson::to_raw_document_buf(options)?;
246-
for result in options_raw_doc.into_iter() {
247-
let (key, value) = result?;
248-
doc.append(key, value.to_raw_bson());
249-
}
246+
extend_raw_document_buf(doc, options_raw_doc)?;
250247
}
251248
Ok(())
252249
}

src/operation/find_and_modify.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ impl<'a, R: Serialize, T: DeserializeOwned> OperationWithDefaults for FindAndMod
133133
))
134134
}
135135

136+
fn serialize_command(&mut self, cmd: Command<Self::Command>) -> Result<Vec<u8>> {
137+
cmd.into_bson_bytes()
138+
}
139+
136140
fn handle_response(
137141
&self,
138142
response: RawCommandResponse,

src/operation/update.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ impl<'a, T: Serialize> OperationWithDefaults for Update<'a, T> {
182182
))
183183
}
184184

185+
fn serialize_command(&mut self, cmd: Command<Self::Command>) -> Result<Vec<u8>> {
186+
cmd.into_bson_bytes()
187+
}
188+
185189
fn handle_response(
186190
&self,
187191
raw_response: RawCommandResponse,

0 commit comments

Comments
 (0)