Skip to content

Commit de98529

Browse files
RUST-1071 Add comment field to all options structs
1 parent 223cee3 commit de98529

File tree

16 files changed

+317
-182
lines changed

16 files changed

+317
-182
lines changed

src/change_stream/options.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::time::Duration;
55
use typed_builder::TypedBuilder;
66

77
use crate::{
8-
bson::Timestamp,
8+
bson::{Bson, Timestamp},
99
change_stream::event::ResumeToken,
1010
collation::Collation,
1111
concern::ReadConcern,
@@ -94,6 +94,14 @@ pub struct ChangeStreamOptions {
9494
#[builder(default)]
9595
#[serde(skip_serializing)]
9696
pub selection_criteria: Option<SelectionCriteria>,
97+
98+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
99+
/// database profiler, currentOp and logs.
100+
///
101+
/// The comment can be any [`Bson`] value on server versions 4.4+. On lower server versions,
102+
/// the comment must be a [`Bson::String`] value.
103+
#[builder(default)]
104+
pub comment: Option<Bson>,
97105
}
98106

99107
impl ChangeStreamOptions {
@@ -104,6 +112,7 @@ impl ChangeStreamOptions {
104112
.max_await_time(self.max_await_time)
105113
.read_concern(self.read_concern.clone())
106114
.selection_criteria(self.selection_criteria.clone())
115+
.comment_bson(self.comment.clone())
107116
.build()
108117
}
109118
}

src/coll/options.rs

Lines changed: 135 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub enum CursorType {
9595

9696
/// Specifies the options to a
9797
/// [`Collection::insert_one`](../struct.Collection.html#method.insert_one) operation.
98+
#[skip_serializing_none]
9899
#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
99100
#[serde(rename_all = "camelCase")]
100101
#[builder(field_defaults(default, setter(into)))]
@@ -105,6 +106,12 @@ pub struct InsertOneOptions {
105106

106107
/// The write concern for the operation.
107108
pub write_concern: Option<WriteConcern>,
109+
110+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
111+
/// database profiler, currentOp and logs.
112+
///
113+
/// This option is only available on server versions 4.4+.
114+
pub comment: Option<Bson>,
108115
}
109116

110117
/// Specifies the options to a
@@ -127,6 +134,12 @@ pub struct InsertManyOptions {
127134
/// The write concern for the operation.
128135
#[serde(skip_deserializing)]
129136
pub write_concern: Option<WriteConcern>,
137+
138+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
139+
/// database profiler, currentOp and logs.
140+
///
141+
/// This option is only available on server versions 4.4+.
142+
pub comment: Option<Bson>,
130143
}
131144

132145
impl InsertManyOptions {
@@ -135,6 +148,7 @@ impl InsertManyOptions {
135148
bypass_document_validation: options.bypass_document_validation,
136149
ordered: None,
137150
write_concern: options.write_concern,
151+
comment: options.comment,
138152
}
139153
}
140154
}
@@ -180,6 +194,7 @@ impl From<Vec<Document>> for UpdateModifications {
180194
/// Specifies the options to a
181195
/// [`Collection::update_one`](../struct.Collection.html#method.update_one) or
182196
/// [`Collection::update_many`](../struct.Collection.html#method.update_many) operation.
197+
#[skip_serializing_none]
183198
#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
184199
#[serde(rename_all = "camelCase")]
185200
#[builder(field_defaults(default, setter(into)))]
@@ -219,6 +234,12 @@ pub struct UpdateOptions {
219234
/// Only available in MongoDB 5.0+.
220235
#[serde(rename = "let")]
221236
pub let_vars: Option<Document>,
237+
238+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
239+
/// database profiler, currentOp and logs.
240+
///
241+
/// This option is only available on server versions 4.4+.
242+
pub comment: Option<Bson>,
222243
}
223244

224245
impl UpdateOptions {
@@ -230,13 +251,15 @@ impl UpdateOptions {
230251
write_concern: options.write_concern,
231252
collation: options.collation,
232253
let_vars: options.let_vars,
254+
comment: options.comment,
233255
..Default::default()
234256
}
235257
}
236258
}
237259

238260
/// Specifies the options to a
239261
/// [`Collection::replace_one`](../struct.Collection.html#method.replace_one) operation.
262+
#[skip_serializing_none]
240263
#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
241264
#[serde(rename_all = "camelCase")]
242265
#[builder(field_defaults(default, setter(into)))]
@@ -270,6 +293,12 @@ pub struct ReplaceOptions {
270293
/// Only available in MongoDB 5.0+.
271294
#[serde(rename = "let")]
272295
pub let_vars: Option<Document>,
296+
297+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
298+
/// database profiler, currentOp and logs.
299+
///
300+
/// This option is only available on server versions 4.4+.
301+
pub comment: Option<Bson>,
273302
}
274303

275304
/// Specifies the options to a
@@ -301,11 +330,18 @@ pub struct DeleteOptions {
301330
/// Only available in MongoDB 5.0+.
302331
#[serde(rename = "let")]
303332
pub let_vars: Option<Document>,
333+
334+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
335+
/// database profiler, currentOp and logs.
336+
///
337+
/// This option is only available on server versions 4.4+.
338+
pub comment: Option<Bson>,
304339
}
305340

306341
/// Specifies the options to a
307342
/// [`Collection::find_one_and_delete`](../struct.Collection.html#method.find_one_and_delete)
308343
/// operation.
344+
#[skip_serializing_none]
309345
#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
310346
#[serde(rename_all = "camelCase")]
311347
#[builder(field_defaults(default, setter(into)))]
@@ -343,11 +379,18 @@ pub struct FindOneAndDeleteOptions {
343379
/// Only available in MongoDB 5.0+.
344380
#[serde(rename = "let")]
345381
pub let_vars: Option<Document>,
382+
383+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
384+
/// database profiler, currentOp and logs.
385+
///
386+
/// This option is only available on server versions 4.4+.
387+
pub comment: Option<Bson>,
346388
}
347389

348390
/// Specifies the options to a
349391
/// [`Collection::find_one_and_replace`](../struct.Collection.html#method.find_one_and_replace)
350392
/// operation.
393+
#[skip_serializing_none]
351394
#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
352395
#[builder(field_defaults(default, setter(into)))]
353396
#[serde(rename_all = "camelCase")]
@@ -394,11 +437,18 @@ pub struct FindOneAndReplaceOptions {
394437
/// Only available in MongoDB 5.0+.
395438
#[serde(rename = "let")]
396439
pub let_vars: Option<Document>,
440+
441+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
442+
/// database profiler, currentOp and logs.
443+
///
444+
/// This option is only available on server versions 4.4+.
445+
pub comment: Option<Bson>,
397446
}
398447

399448
/// Specifies the options to a
400449
/// [`Collection::find_one_and_update`](../struct.Collection.html#method.find_one_and_update)
401450
/// operation.
451+
#[skip_serializing_none]
402452
#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
403453
#[serde(rename_all = "camelCase")]
404454
#[builder(field_defaults(default, setter(into)))]
@@ -451,6 +501,12 @@ pub struct FindOneAndUpdateOptions {
451501
/// Only available in MongoDB 5.0+.
452502
#[serde(rename = "let")]
453503
pub let_vars: Option<Document>,
504+
505+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
506+
/// database profiler, currentOp and logs.
507+
///
508+
/// This option is only available on server versions 4.4+.
509+
pub comment: Option<Bson>,
454510
}
455511

456512
/// Specifies the options to a [`Collection::aggregate`](../struct.Collection.html#method.aggregate)
@@ -486,10 +542,23 @@ pub struct AggregateOptions {
486542
/// information on how to use this option.
487543
pub collation: Option<Collation>,
488544

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

553+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
554+
/// database profiler, currentOp and logs.
555+
///
556+
/// This option is only supported on server versions 4.4+. Use the `comment` option on
557+
/// older server versions.
558+
// TODO RUST-1364: Remove this field
559+
#[serde(rename(serialize = "comment"))]
560+
pub comment_bson: Option<Bson>,
561+
493562
/// The index to use for the operation.
494563
pub hint: Option<Hint>,
495564

@@ -549,6 +618,7 @@ pub struct AggregateOptions {
549618

550619
/// Specifies the options to a
551620
/// [`Collection::count_documents`](../struct.Collection.html#method.count_documents) operation.
621+
#[skip_serializing_none]
552622
#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
553623
#[serde(rename_all = "camelCase")]
554624
#[builder(field_defaults(default, setter(into)))]
@@ -587,6 +657,12 @@ pub struct CountOptions {
587657
/// The level of the read concern.
588658
#[serde(skip_serializing)]
589659
pub read_concern: Option<ReadConcern>,
660+
661+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
662+
/// database profiler, currentOp and logs.
663+
///
664+
/// This option is only available on server versions 4.4+.
665+
pub comment: Option<Bson>,
590666
}
591667

592668
// rustfmt tries to split the link up when it's all on one line, which breaks the link, so we wrap
@@ -624,10 +700,12 @@ pub struct EstimatedDocumentCountOptions {
624700
#[serde(skip_serializing)]
625701
pub read_concern: Option<ReadConcern>,
626702

627-
/// Tags the query with an arbitrary BSON object to help trace the operation through the
703+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
628704
/// database profiler, currentOp and logs.
629705
///
630-
/// This option is only available on server versions 4.4+.
706+
/// This option is only supported on server versions 4.4+. The comment can be any [`Bson`]
707+
/// value on server versions 4.4.14+. On server versions between 4.4.0 and 4.4.14, only
708+
/// [`Bson::String`] values are supported.
631709
pub comment: Option<Bson>,
632710
}
633711

@@ -666,6 +744,12 @@ pub struct DistinctOptions {
666744
/// See the [documentation](https://www.mongodb.com/docs/manual/reference/collation/) for more
667745
/// information on how to use this option.
668746
pub collation: Option<Collation>,
747+
748+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
749+
/// database profiler, currentOp and logs.
750+
///
751+
/// This option is only available on server versions 4.4+.
752+
pub comment: Option<Bson>,
669753
}
670754

671755
/// Specifies the options to a [`Collection::find`](../struct.Collection.html#method.find)
@@ -694,10 +778,23 @@ pub struct FindOptions {
694778
#[serde(serialize_with = "bson_util::serialize_u32_option_as_i32")]
695779
pub batch_size: Option<u32>,
696780

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

789+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
790+
/// database profiler, currentOp and logs.
791+
///
792+
/// This option is only supported on server versions 4.4+. Use the `comment` option on
793+
/// older server versions.
794+
// TODO RUST-1364: Remove this field
795+
#[serde(rename(serialize = "comment"))]
796+
pub comment_bson: Option<Bson>,
797+
701798
/// The type of cursor to return.
702799
#[serde(skip)]
703800
pub cursor_type: Option<CursorType>,
@@ -792,6 +889,7 @@ impl From<FindOneOptions> for FindOptions {
792889
allow_partial_results: options.allow_partial_results,
793890
collation: options.collation,
794891
comment: options.comment,
892+
comment_bson: options.comment_bson,
795893
hint: options.hint,
796894
max: options.max,
797895
max_scan: options.max_scan,
@@ -845,10 +943,21 @@ pub struct FindOneOptions {
845943
/// information on how to use this option.
846944
pub collation: Option<Collation>,
847945

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

953+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
954+
/// database profiler, currentOp and logs.
955+
///
956+
/// This option is only supported on server versions 4.4+. Use the `comment` option on
957+
/// older server versions.
958+
// TODO RUST-1364: Remove this field
959+
pub comment_bson: Option<Bson>,
960+
852961
/// The index to use for the operation.
853962
pub hint: Option<Hint>,
854963

@@ -936,6 +1045,12 @@ pub struct CreateIndexOptions {
9361045

9371046
/// The write concern for the operation.
9381047
pub write_concern: Option<WriteConcern>,
1048+
1049+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
1050+
/// database profiler, currentOp and logs.
1051+
///
1052+
/// This option is only available on server versions 4.4+.
1053+
pub comment: Option<Bson>,
9391054
}
9401055

9411056
/// Specifies the options to a [`Collection::drop`](../struct.Collection.html#method.drop)
@@ -978,6 +1093,12 @@ pub struct DropIndexOptions {
9781093

9791094
/// The write concern for the operation.
9801095
pub write_concern: Option<WriteConcern>,
1096+
1097+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
1098+
/// database profiler, currentOp and logs.
1099+
///
1100+
/// This option is only available on server versions 4.4+.
1101+
pub comment: Option<Bson>,
9811102
}
9821103

9831104
/// Specifies the options to a
@@ -1003,6 +1124,12 @@ pub struct ListIndexesOptions {
10031124
/// The number of indexes the server should return per cursor batch.
10041125
#[serde(default, skip_serializing)]
10051126
pub batch_size: Option<u32>,
1127+
1128+
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
1129+
/// database profiler, currentOp and logs.
1130+
///
1131+
/// This option is only available on server versions 4.4+.
1132+
pub comment: Option<Bson>,
10061133
}
10071134

10081135
/// The minimum number of data-bearing voting replica set members (i.e. commit quorum), including

src/cursor/common.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use serde::{de::DeserializeOwned, Deserialize};
1414
use tokio::sync::oneshot;
1515

1616
use crate::{
17-
bson::Document,
17+
bson::{Bson, Document},
1818
change_stream::event::ResumeToken,
1919
cmap::conn::PinnedConnectionHandle,
2020
error::{Error, ErrorKind, Result},
@@ -374,6 +374,7 @@ impl CursorSpecification {
374374
address: ServerAddress,
375375
batch_size: impl Into<Option<u32>>,
376376
max_time: impl Into<Option<Duration>>,
377+
comment: impl Into<Option<Bson>>,
377378
) -> Self {
378379
Self {
379380
info: CursorInformation {
@@ -382,6 +383,7 @@ impl CursorSpecification {
382383
address,
383384
batch_size: batch_size.into(),
384385
max_time: max_time.into(),
386+
comment: comment.into(),
385387
},
386388
initial_buffer: info.first_batch,
387389
post_batch_resume_token: ResumeToken::from_raw(info.post_batch_resume_token),
@@ -416,6 +418,7 @@ pub(crate) struct CursorInformation {
416418
pub(crate) id: i64,
417419
pub(crate) batch_size: Option<u32>,
418420
pub(crate) max_time: Option<Duration>,
421+
pub(crate) comment: Option<Bson>,
419422
}
420423

421424
#[derive(Debug)]

0 commit comments

Comments
 (0)