Skip to content

RUST-1364 Collapse comment and comment_bson #1070

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

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 2 additions & 4 deletions src/action/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ impl<'a, T: Send + Sync, Session> Find<'a, T, Session> {
allow_disk_use: bool,
allow_partial_results: bool,
batch_size: u32,
comment: String,
comment_bson: Bson,
comment: Bson,
cursor_type: CursorType,
hint: Hint,
limit: i64,
Expand Down Expand Up @@ -168,8 +167,7 @@ impl<'a, T: Send + Sync> FindOne<'a, T> {
option_setters! { options: FindOneOptions;
allow_partial_results: bool,
collation: Collation,
comment: String,
comment_bson: Bson,
comment: Bson,
hint: Hint,
max: Document,
max_scan: u64,
Expand Down
32 changes: 5 additions & 27 deletions src/coll/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,7 @@ pub struct AggregateOptions {
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
/// database profiler, currentOp and logs.
///
/// This option is only supported on server versions 4.4+. Use the `comment` option on
/// older server versions.
/// For server versions less than 4.4, only a string value may be provided.
pub comment: Option<Bson>,

/// The index to use for the operation.
Expand Down Expand Up @@ -776,22 +775,11 @@ pub struct FindOptions {
#[serde(serialize_with = "serde_util::serialize_u32_option_as_i32")]
pub batch_size: Option<u32>,

/// Tags the query with an arbitrary string to help trace the operation through the
/// database profiler, currentOp and logs.
///
/// If both this option and `comment_bson` are specified, `comment_bson` will take precedence.
// TODO RUST-1364: Update this field to be of type Option<Bson>
#[serde(skip_serializing)]
pub comment: Option<String>,

/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
/// database profiler, currentOp and logs.
///
/// This option is only supported on server versions 4.4+. Use the `comment` option on
/// older server versions.
// TODO RUST-1364: Remove this field
#[serde(rename(serialize = "comment"))]
pub comment_bson: Option<Bson>,
/// For server versions less than 4.4, only a string value may be provided.
pub comment: Option<Bson>,

/// The type of cursor to return.
#[serde(skip)]
Expand Down Expand Up @@ -887,7 +875,6 @@ impl From<FindOneOptions> for FindOptions {
allow_partial_results: options.allow_partial_results,
collation: options.collation,
comment: options.comment,
comment_bson: options.comment_bson,
hint: options.hint,
max: options.max,
max_scan: options.max_scan,
Expand Down Expand Up @@ -941,20 +928,11 @@ pub struct FindOneOptions {
/// information on how to use this option.
pub collation: Option<Collation>,

/// Tags the query with an arbitrary string value to help trace the operation through the
/// database profiler, currentOp and logs.
///
/// If both this option and `comment_bson` are specified, `comment_bson` will take precedence.
// TODO RUST-1364: Update this field to be of type Option<Bson>
pub comment: Option<String>,

/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
/// database profiler, currentOp and logs.
///
/// This option is only supported on server versions 4.4+. Use the `comment` option on
/// older server versions.
// TODO RUST-1364: Remove this field
pub comment_bson: Option<Bson>,
/// For server versions less than 4.4, only a string value may be provided.
pub comment: Option<Bson>,

/// The index to use for the operation.
pub hint: Option<Hint>,
Expand Down
14 changes: 2 additions & 12 deletions src/operation/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ pub(crate) struct Find {
}

impl Find {
pub(crate) fn new(ns: Namespace, filter: Document, mut options: Option<FindOptions>) -> Self {
if let Some(ref mut options) = options {
if let Some(ref comment) = options.comment {
if options.comment_bson.is_none() {
options.comment_bson = Some(comment.clone().into());
}
}
}

pub(crate) fn new(ns: Namespace, filter: Document, options: Option<FindOptions>) -> Self {
Self {
ns,
filter,
Expand Down Expand Up @@ -108,9 +100,7 @@ impl OperationWithDefaults for Find {
let comment = if description.max_wire_version.unwrap_or(0) < SERVER_4_4_0_WIRE_VERSION {
None
} else {
self.options
.as_ref()
.and_then(|opts| opts.comment_bson.clone())
self.options.as_ref().and_then(|opts| opts.comment.clone())
};

Ok(CursorSpecification::new(
Expand Down
21 changes: 2 additions & 19 deletions src/test/spec/unified_runner/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,20 +553,13 @@ impl Find {
) -> Result<TestCursor> {
let collection = test_runner.get_collection(id).await;

let (comment, comment_bson) = match &self.comment {
Some(Bson::String(string)) => (Some(string.clone()), None),
Some(bson) => (None, Some(bson.clone())),
None => (None, None),
};

// `FindOptions` is constructed without the use of `..Default::default()` to enforce at
// compile-time that any new fields added there need to be considered here.
let options = FindOptions {
allow_disk_use: self.allow_disk_use,
allow_partial_results: self.allow_partial_results,
batch_size: self.batch_size,
comment,
comment_bson,
comment: self.comment.clone(),
hint: self.hint.clone(),
limit: self.limit,
max: self.max.clone(),
Expand Down Expand Up @@ -1040,21 +1033,11 @@ impl<'de> Deserialize<'de> for FindOne {
#[derive(Deserialize)]
struct Helper {
filter: Option<Document>,
comment: Option<Bson>,
#[serde(flatten)]
options: FindOneOptions,
}

let mut helper = Helper::deserialize(deserializer)?;
match helper.comment {
Some(Bson::String(string)) => {
helper.options.comment = Some(string);
}
Some(bson) => {
helper.options.comment_bson = Some(bson);
}
_ => {}
}
let helper = Helper::deserialize(deserializer)?;

Ok(Self {
filter: helper.filter,
Expand Down