-
Notifications
You must be signed in to change notification settings - Fork 177
RUST-877 Delay replacement document serialization until Operation::build
#942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6707e5f
d347ace
d22989a
e18233b
8fe50c3
410c217
b9849a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,8 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; | |
|
||
use super::wire::Message; | ||
use crate::{ | ||
bson::Document, | ||
bson::{rawdoc, Document}, | ||
bson_util::extend_raw_document_buf, | ||
client::{options::ServerApi, ClusterTime, HELLO_COMMAND_NAMES, REDACTED_COMMANDS}, | ||
error::{Error, ErrorKind, Result}, | ||
hello::{HelloCommandResponse, HelloReply}, | ||
|
@@ -177,6 +178,19 @@ impl<T> Command<T> { | |
} | ||
} | ||
|
||
impl Command<RawDocumentBuf> { | ||
pub(crate) fn into_bson_bytes(mut self) -> Result<Vec<u8>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method (when it's called in Most There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed on all points. |
||
let mut command = self.body; | ||
|
||
// Clear the body of the command to avoid re-serializing. | ||
self.body = rawdoc! {}; | ||
let rest_of_command = bson::to_raw_document_buf(&self)?; | ||
|
||
extend_raw_document_buf(&mut command, rest_of_command)?; | ||
Ok(command.into_bytes()) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub(crate) struct RawCommandResponse { | ||
pub(crate) source: ServerAddress, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
use std::time::Duration; | ||
|
||
use bson::serde_helpers; | ||
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; | ||
use serde_with::skip_serializing_none; | ||
use typed_builder::TypedBuilder; | ||
|
||
use crate::{ | ||
bson::{doc, Bson, Document}, | ||
bson::{doc, serde_helpers, Bson, Document, RawBson, RawDocumentBuf}, | ||
concern::{ReadConcern, WriteConcern}, | ||
error::Result, | ||
options::Collation, | ||
selection_criteria::SelectionCriteria, | ||
serde_util, | ||
|
@@ -63,7 +63,7 @@ impl<'de> Deserialize<'de> for ReturnDocument { | |
} | ||
|
||
/// Specifies the index to use for an operation. | ||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] | ||
#[serde(untagged)] | ||
#[non_exhaustive] | ||
pub enum Hint { | ||
|
@@ -74,11 +74,11 @@ pub enum Hint { | |
} | ||
|
||
impl Hint { | ||
pub(crate) fn to_bson(&self) -> Bson { | ||
match self { | ||
Hint::Keys(ref d) => Bson::Document(d.clone()), | ||
Hint::Name(ref s) => Bson::String(s.clone()), | ||
} | ||
pub(crate) fn to_raw_bson(&self) -> Result<RawBson> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like this could just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this as a minor optimization to avoid an intermediary allocation of a regular Ideally we'd have a |
||
Ok(match self { | ||
Hint::Keys(ref d) => RawBson::Document(RawDocumentBuf::from_document(d)?), | ||
Hint::Name(ref s) => RawBson::String(s.clone()), | ||
}) | ||
} | ||
} | ||
|
||
|
@@ -174,17 +174,6 @@ pub enum UpdateModifications { | |
Pipeline(Vec<Document>), | ||
} | ||
|
||
impl UpdateModifications { | ||
pub(crate) fn to_bson(&self) -> Bson { | ||
match self { | ||
UpdateModifications::Document(ref d) => Bson::Document(d.clone()), | ||
UpdateModifications::Pipeline(ref p) => { | ||
Bson::Array(p.iter().map(|d| Bson::Document(d.clone())).collect()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl From<Document> for UpdateModifications { | ||
fn from(item: Document) -> Self { | ||
UpdateModifications::Document(item) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A slightly optimized version of this could just append all of the non-size bytes from
other
tothis
and then re-calculate the size, might be something worth adding to the bson API.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, filed RUST-1754 for this.