-
Notifications
You must be signed in to change notification settings - Fork 178
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
RUST-877 Delay replacement document serialization until Operation::build
#942
Conversation
Operation::build
Operation::build
a7e4c93
to
8fe50c3
Compare
@@ -119,6 +125,17 @@ pub(crate) fn read_document_bytes<R: Read>(mut reader: R) -> Result<Vec<u8>> { | |||
Ok(bytes) | |||
} | |||
|
|||
pub(crate) fn extend_raw_document_buf( |
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
to this
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.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
This method (when it's called in Operation::serialize_command
) bypasses running the body of the command back through the serializer (i.e. the default implementation for serialize_command
) and instead appends the rest of the command to the body. The tradeoff here is that the raw BSON created on 187 is immediately copied on line 189; we could manually serialize and append each field directly to command
instead, but I think the size of rest_of_command
is small enough that it shouldn't make much of a difference in the vast majority of cases.
Most Command
s for other operations currently use the default Document
type for T
, but in the future (maybe in tandem with RUST-1747) I think we should change them to use RawDocumentBuf
and then standardize this approach for serializing commands.
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 on all points.
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.
We've talked in the past about deleting these low-value operation tests as they become outdated. These changes broke most of them so I just removed the whole file.
src/operation/update/test.rs
Outdated
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.
ditto above
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed on all points.
@@ -119,6 +125,17 @@ pub(crate) fn read_document_bytes<R: Read>(mut reader: R) -> Result<Vec<u8>> { | |||
Ok(bytes) | |||
} | |||
|
|||
pub(crate) fn extend_raw_document_buf( |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this could just be to_bson().try_into()?
?
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.
I added this as a minor optimization to avoid an intermediary allocation of a regular Bson
via to_bson
. It did seem unnecessary to have both to_bson
and to_raw_bson
though so I just refactored the few uses of to_bson
and removed it.
Ideally we'd have a to_raw_bson(value: impl Serialize)
method in bson, but I'm not sure how that would actually work; the raw serializer spits out raw document bytes as opposed to the non-raw serializer, which generates BSON values.
ns: Namespace, | ||
query: Document, | ||
modification: Modification<'a, R>, | ||
human_readable_serialization: Option<bool>, |
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.
Could this be an un-serialized field in FindAndModifyOptions
rather than adding it as an explicit parameter?
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.
this was slightly more ergonomic because it allows implementing From
for the various options rather than writing custom methods. this is maybe just personal preference too but it made sense to me to have only fields that will be serialized and sent to the server within the options. this did make me realize that there's no need to unwrap_or_default
the options anymore so I removed that logic
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.
LGTM!
No description provided.