Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 73 additions & 16 deletions src/action/run_command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use bson::{Bson, Document};
use bson::{Bson, Document, RawDocumentBuf};

use crate::{
client::session::TransactionState,
Expand Down Expand Up @@ -40,7 +40,27 @@ impl Database {
pub fn run_command(&self, command: Document) -> RunCommand {
RunCommand {
db: self,
command,
command: RawDocumentBuf::from_document(&command),
options: None,
session: None,
}
}

/// Runs a database-level command.
///
/// Note that no inspection is done on `doc`, so the command will not use the database's default
/// read concern or write concern. If specific read concern or write concern is desired, it must
/// be specified manually.
/// Please note that run_command doesn't validate WriteConcerns passed into the body of the
/// command document.
///
/// `await` will return d[`Result<Document>`].
#[deeplink]
#[options_doc(run_command)]
pub fn run_raw_command(&self, command: RawDocumentBuf) -> RunCommand {
RunCommand {
db: self,
command: Ok(command),
options: None,
session: None,
}
Expand All @@ -55,7 +75,22 @@ impl Database {
pub fn run_cursor_command(&self, command: Document) -> RunCursorCommand {
RunCursorCommand {
db: self,
command,
command: RawDocumentBuf::from_document(&command),
options: None,
session: ImplicitSession,
}
}

/// Runs a database-level command and returns a cursor to the response.
///
/// `await` will return d[`Result<Cursor<Document>>`] or a
/// d[`Result<SessionCursor<Document>>`] if a [`ClientSession`] is provided.
#[deeplink]
#[options_doc(run_cursor_command)]
pub fn run_raw_cursor_command(&self, command: RawDocumentBuf) -> RunCursorCommand {
RunCursorCommand {
db: self,
command: Ok(command),
options: None,
session: ImplicitSession,
}
Expand All @@ -79,6 +114,21 @@ impl crate::sync::Database {
self.async_database.run_command(command)
}

/// Runs a database-level command.
///
/// Note that no inspection is done on `doc`, so the command will not use the database's default
/// read concern or write concern. If specific read concern or write concern is desired, it must
/// be specified manually.
/// Please note that run_command doesn't validate WriteConcerns passed into the body of the
/// command document.
///
/// [`run`](RunCommand::run) will return d[`Result<Document>`].
#[deeplink]
#[options_doc(run_command, sync)]
pub fn run_raw_command(&self, command: RawDocumentBuf) -> RunCommand {
self.async_database.run_raw_command(command)
}

/// Runs a database-level command and returns a cursor to the response.
///
/// [`run`](RunCursorCommand::run) will return d[`Result<crate::sync::Cursor<Document>>`] or a
Expand All @@ -88,13 +138,23 @@ impl crate::sync::Database {
pub fn run_cursor_command(&self, command: Document) -> RunCursorCommand {
self.async_database.run_cursor_command(command)
}

/// Runs a database-level command and returns a cursor to the response.
///
/// [`run`](RunCursorCommand::run) will return d[`Result<crate::sync::Cursor<Document>>`] or a
/// d[`Result<crate::sync::SessionCursor<Document>>`] if a [`ClientSession`] is provided.
#[deeplink]
#[options_doc(run_cursor_command, sync)]
pub fn run_raw_cursor_command(&self, command: RawDocumentBuf) -> RunCursorCommand {
self.async_database.run_raw_cursor_command(command)
}
}

/// Run a database-level command. Create with [`Database::run_command`].
#[must_use]
pub struct RunCommand<'a> {
db: &'a Database,
command: Document,
command: bson::raw::Result<RawDocumentBuf>,
options: Option<RunCommandOptions>,
session: Option<&'a mut ClientSession>,
}
Expand All @@ -115,10 +175,11 @@ impl<'a> Action for RunCommand<'a> {

async fn execute(self) -> Result<Document> {
let mut selection_criteria = self.options.and_then(|o| o.selection_criteria);
let command = self.command?;
if let Some(session) = &self.session {
match session.transaction.state {
TransactionState::Starting | TransactionState::InProgress => {
if self.command.contains_key("readConcern") {
if command.get("readConcern").ok().is_some() {
Copy link
Contributor

@isabelatkinson isabelatkinson Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if command.get("readConcern").ok().is_some() {
if command.get("readConcern").is_ok_and(|rc| rc.is_some()) {

get returns an error if the document is malformed, so this as currently written will evaluate to true for any valid document

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, made this change 👍

return Err(ErrorKind::InvalidArgument {
message: "Cannot set read concern after starting a transaction".into(),
}
Expand All @@ -139,12 +200,8 @@ impl<'a> Action for RunCommand<'a> {
}
}

let operation = run_command::RunCommand::new(
self.db.name().into(),
self.command,
selection_criteria,
None,
)?;
let operation =
run_command::RunCommand::new(self.db.name().into(), command, selection_criteria, None);
self.db
.client()
.execute_operation(operation, self.session)
Expand All @@ -157,7 +214,7 @@ impl<'a> Action for RunCommand<'a> {
#[must_use]
pub struct RunCursorCommand<'a, Session = ImplicitSession> {
db: &'a Database,
command: Document,
command: bson::raw::Result<RawDocumentBuf>,
options: Option<RunCursorCommandOptions>,
session: Session,
}
Expand Down Expand Up @@ -192,10 +249,10 @@ impl<'a> Action for RunCursorCommand<'a, ImplicitSession> {
.and_then(|options| options.selection_criteria.clone());
let rcc = run_command::RunCommand::new(
self.db.name().to_string(),
self.command,
self.command?,
selection_criteria,
None,
)?;
);
let rc_command = run_cursor_command::RunCursorCommand::new(rcc, self.options)?;
let client = self.db.client();
client.execute_cursor_operation(rc_command).await
Expand All @@ -218,10 +275,10 @@ impl<'a> Action for RunCursorCommand<'a, ExplicitSession<'a>> {
.and_then(|options| options.selection_criteria.clone());
let rcc = run_command::RunCommand::new(
self.db.name().to_string(),
self.command,
self.command?,
selection_criteria,
None,
)?;
);
let rc_command = run_cursor_command::RunCursorCommand::new(rcc, self.options)?;
let client = self.db.client();
client
Expand Down
2 changes: 1 addition & 1 deletion src/client/csfle/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl CryptExecutor {
let db = db.as_ref().ok_or_else(|| {
Error::internal("db required for NeedMongoMarkings state")
})?;
let op = RawOutput(RunCommand::new_raw(db.to_string(), command, None, None)?);
let op = RawOutput(RunCommand::new(db.to_string(), command, None, None));
let mongocryptd_client = self.mongocryptd_client.as_ref().ok_or_else(|| {
Error::invalid_argument("this operation requires mongocryptd")
})?;
Expand Down
6 changes: 3 additions & 3 deletions src/coll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ pub mod options;

use std::{fmt, fmt::Debug, str::FromStr, sync::Arc};

use bson::rawdoc;
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize};

use self::options::*;
use crate::{
bson::doc,
client::options::ServerAddress,
cmap::conn::PinnedConnectionHandle,
concern::{ReadConcern, WriteConcern},
Expand Down Expand Up @@ -199,13 +199,13 @@ where

let op = crate::operation::run_command::RunCommand::new(
ns.db,
doc! {
rawdoc! {
"killCursors": ns.coll.as_str(),
"cursors": [cursor_id]
},
drop_address.map(SelectionCriteria::from_address),
pinned_connection,
)?;
);
self.client().execute_operation(op, None).await?;
Ok(())
}
Expand Down
21 changes: 3 additions & 18 deletions src/operation/run_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,17 @@ pub(crate) struct RunCommand<'conn> {

impl<'conn> RunCommand<'conn> {
pub(crate) fn new(
db: String,
command: Document,
selection_criteria: Option<SelectionCriteria>,
pinned_connection: Option<&'conn PinnedConnectionHandle>,
) -> Result<Self> {
Ok(Self {
db,
command: RawDocumentBuf::from_document(&command)?,
selection_criteria,
pinned_connection,
})
}

#[cfg(feature = "in-use-encryption")]
pub(crate) fn new_raw(
db: String,
command: RawDocumentBuf,
selection_criteria: Option<SelectionCriteria>,
pinned_connection: Option<&'conn PinnedConnectionHandle>,
) -> Result<Self> {
Ok(Self {
) -> Self {
Self {
db,
command,
selection_criteria,
pinned_connection,
})
}
}

fn command_name(&self) -> Option<&str> {
Expand Down