-
Notifications
You must be signed in to change notification settings - Fork 176
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-1553 Add support for document sequences (OP_MSG
payload type 1)
#1009
Merged
isabelatkinson
merged 3 commits into
mongodb:main
from
isabelatkinson:document-sequences
Jan 19, 2024
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
use bson::{RawDocument, RawDocumentBuf}; | ||
use serde::{de::DeserializeOwned, Deserialize, Serialize}; | ||
|
||
use super::wire::Message; | ||
use super::wire::{message::DocumentSequence, Message}; | ||
use crate::{ | ||
bson::{rawdoc, Document}, | ||
bson_util::extend_raw_document_buf, | ||
client::{options::ServerApi, ClusterTime, HELLO_COMMAND_NAMES, REDACTED_COMMANDS}, | ||
bson::Document, | ||
client::{options::ServerApi, ClusterTime}, | ||
error::{Error, ErrorKind, Result}, | ||
hello::{HelloCommandResponse, HelloReply}, | ||
operation::{CommandErrorBody, CommandResponse}, | ||
|
@@ -14,37 +13,26 @@ use crate::{ | |
ClientSession, | ||
}; | ||
|
||
/// A command that has been serialized to BSON. | ||
#[derive(Debug)] | ||
pub(crate) struct RawCommand { | ||
pub(crate) name: String, | ||
pub(crate) target_db: String, | ||
/// Whether or not the server may respond to this command multiple times via the moreToComeBit. | ||
pub(crate) exhaust_allowed: bool, | ||
pub(crate) bytes: Vec<u8>, | ||
} | ||
|
||
impl RawCommand { | ||
pub(crate) fn should_compress(&self) -> bool { | ||
let name = self.name.to_lowercase(); | ||
!REDACTED_COMMANDS.contains(name.as_str()) && !HELLO_COMMAND_NAMES.contains(name.as_str()) | ||
} | ||
} | ||
|
||
/// Driver-side model of a database command. | ||
#[serde_with::skip_serializing_none] | ||
#[derive(Clone, Debug, Serialize, Default)] | ||
#[serde(rename_all = "camelCase")] | ||
pub(crate) struct Command<T = Document> { | ||
pub(crate) struct Command<T = Document> | ||
where | ||
T: Serialize, | ||
{ | ||
#[serde(skip)] | ||
pub(crate) name: String, | ||
|
||
#[serde(skip)] | ||
pub(crate) exhaust_allowed: bool, | ||
|
||
#[serde(flatten)] | ||
#[serde(skip)] | ||
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. AFAICT the new behavior ( 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. Oh yeah you're right, reverted |
||
pub(crate) body: T, | ||
|
||
#[serde(skip)] | ||
pub(crate) document_sequences: Vec<DocumentSequence>, | ||
|
||
#[serde(rename = "$db")] | ||
pub(crate) target_db: String, | ||
|
||
|
@@ -70,13 +58,17 @@ pub(crate) struct Command<T = Document> { | |
recovery_token: Option<Document>, | ||
} | ||
|
||
impl<T> Command<T> { | ||
pub(crate) fn new(name: String, target_db: String, body: T) -> Self { | ||
impl<T> Command<T> | ||
where | ||
T: Serialize, | ||
{ | ||
pub(crate) fn new(name: impl ToString, target_db: impl ToString, body: T) -> Self { | ||
Self { | ||
name, | ||
target_db, | ||
name: name.to_string(), | ||
target_db: target_db.to_string(), | ||
exhaust_allowed: false, | ||
body, | ||
document_sequences: Vec::new(), | ||
lsid: None, | ||
cluster_time: None, | ||
server_api: None, | ||
|
@@ -100,6 +92,7 @@ impl<T> Command<T> { | |
target_db, | ||
exhaust_allowed: false, | ||
body, | ||
document_sequences: Vec::new(), | ||
lsid: None, | ||
cluster_time: None, | ||
server_api: None, | ||
|
@@ -112,6 +105,17 @@ impl<T> Command<T> { | |
} | ||
} | ||
|
||
pub(crate) fn add_document_sequence( | ||
&mut self, | ||
identifier: impl ToString, | ||
documents: Vec<RawDocumentBuf>, | ||
) { | ||
self.document_sequences.push(DocumentSequence { | ||
identifier: identifier.to_string(), | ||
documents, | ||
}); | ||
} | ||
|
||
pub(crate) fn set_session(&mut self, session: &ClientSession) { | ||
self.lsid = Some(session.id().clone()) | ||
} | ||
|
@@ -178,19 +182,6 @@ impl<T> Command<T> { | |
} | ||
} | ||
|
||
impl Command<RawDocumentBuf> { | ||
pub(crate) fn into_bson_bytes(mut self) -> Result<Vec<u8>> { | ||
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, | ||
|
@@ -220,9 +211,8 @@ impl RawCommandResponse { | |
) | ||
} | ||
|
||
pub(crate) fn new(source: ServerAddress, message: Message) -> Result<Self> { | ||
let raw = message.single_document_response()?; | ||
Ok(Self::new_raw(source, RawDocumentBuf::from_bytes(raw)?)) | ||
pub(crate) fn new(source: ServerAddress, message: Message) -> Self { | ||
Self::new_raw(source, message.document_payload) | ||
} | ||
|
||
pub(crate) fn new_raw(source: ServerAddress, raw: RawDocumentBuf) -> Self { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
mod header; | ||
mod message; | ||
pub(crate) mod message; | ||
mod util; | ||
|
||
pub(crate) use self::{ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 eliminated the
RawCommand
layer betweenCommand
andMessage
here.RawCommand
contained a vec of bytes created byOperation::serialize_command
with the assumption that those bytes should be sent as a single payload type 0 section when converting toMessage
: this no longer makes sense when commands could require multiple sections.