Skip to content

RUST-1810 Clean up concern helpers #1011

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
Jan 18, 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
2 changes: 1 addition & 1 deletion manual/src/encryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ async fn main() -> Result<()> {
db.create_collection(
&encrypted_namespace.coll,
CreateCollectionOptions::builder()
.write_concern(WriteConcern::MAJORITY)
.write_concern(WriteConcern::majority())
.validator(doc! { "$jsonSchema": schema })
.build(),
).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/client/csfle/client_encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ impl ClientEncryption {
.collection_with_options(
&key_vault_namespace.coll,
CollectionOptions::builder()
.write_concern(WriteConcern::MAJORITY)
.read_concern(ReadConcern::MAJORITY)
.write_concern(WriteConcern::majority())
.read_concern(ReadConcern::majority())
.build(),
);
Ok(ClientEncryption {
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 @@ -167,7 +167,7 @@ impl CryptExecutor {
.find(
filter,
FindOptions::builder()
.read_concern(ReadConcern::MAJORITY)
.read_concern(ReadConcern::majority())
.build(),
)
.await?;
Expand Down
76 changes: 37 additions & 39 deletions src/concern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,6 @@ pub struct ReadConcern {
pub level: ReadConcernLevel,
}

impl ReadConcern {
/// A `ReadConcern` with level `ReadConcernLevel::Local`.
pub const LOCAL: ReadConcern = ReadConcern {
level: ReadConcernLevel::Local,
};
/// A `ReadConcern` with level `ReadConcernLevel::Majority`.
pub const MAJORITY: ReadConcern = ReadConcern {
level: ReadConcernLevel::Majority,
};
/// A `ReadConcern` with level `ReadConcernLevel::Linearizable`.
pub const LINEARIZABLE: ReadConcern = ReadConcern {
level: ReadConcernLevel::Linearizable,
};
/// A `ReadConcern` with level `ReadConcernLevel::Available`.
pub const AVAILABLE: ReadConcern = ReadConcern {
level: ReadConcernLevel::Available,
};
/// A `ReadConcern` with level `ReadConcernLevel::Snapshot`.
pub const SNAPSHOT: ReadConcern = ReadConcern {
level: ReadConcernLevel::Snapshot,
};
}

/// An internal-only read concern type that allows the omission of a "level" as well as
/// specification of "atClusterTime" and "afterClusterTime".
#[skip_serializing_none]
Expand Down Expand Up @@ -104,8 +81,8 @@ impl ReadConcern {
/// Creates a read concern with a custom read concern level. This is present to provide forwards
/// compatibility with any future read concerns which may be added to new versions of
/// MongoDB.
pub fn custom(level: String) -> Self {
ReadConcernLevel::from_str(level.as_str()).into()
pub fn custom(level: impl AsRef<str>) -> Self {
ReadConcernLevel::from_str(level.as_ref()).into()
}

#[cfg(test)]
Expand Down Expand Up @@ -242,20 +219,6 @@ pub struct WriteConcern {
pub journal: Option<bool>,
}

impl WriteConcern {
// Can't use `Default::default()` in const contexts yet :(
const DEFAULT: WriteConcern = WriteConcern {
w: None,
w_timeout: None,
journal: None,
};
/// A `WriteConcern` requesting `Acknowledgment::Majority`.
pub const MAJORITY: WriteConcern = WriteConcern {
w: Some(Acknowledgment::Majority),
..WriteConcern::DEFAULT
};
}

/// The type of the `w` field in a [`WriteConcern`](struct.WriteConcern.html).
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
Expand Down Expand Up @@ -311,6 +274,16 @@ impl From<u32> for Acknowledgment {
}
}

impl From<&str> for Acknowledgment {
fn from(s: &str) -> Self {
if s == "majority" {
Acknowledgment::Majority
} else {
Acknowledgment::Custom(s.to_string())
}
}
}

impl From<String> for Acknowledgment {
fn from(s: String) -> Self {
if s == "majority" {
Expand All @@ -322,6 +295,21 @@ impl From<String> for Acknowledgment {
}

impl WriteConcern {
/// A 'WriteConcern' requesting [`Acknowledgment::Nodes`].
pub fn nodes(v: u32) -> Self {
Acknowledgment::Nodes(v).into()
}

/// A `WriteConcern` requesting [`Acknowledgment::Majority`].
pub fn majority() -> Self {
Acknowledgment::Majority.into()
}

/// A `WriteConcern` with a custom acknowledgment.
pub fn custom(s: impl AsRef<str>) -> Self {
Acknowledgment::from(s.as_ref()).into()
}

pub(crate) fn is_acknowledged(&self) -> bool {
self.w != Some(Acknowledgment::Nodes(0)) || self.journal == Some(true)
}
Expand Down Expand Up @@ -381,3 +369,13 @@ impl WriteConcern {
state.serialize(serializer)
}
}

impl From<Acknowledgment> for WriteConcern {
fn from(w: Acknowledgment) -> Self {
WriteConcern {
w: Some(w),
w_timeout: None,
journal: None,
}
}
}
12 changes: 6 additions & 6 deletions src/test/csfle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ async fn init_client() -> Result<(EventClient, Collection<Document>)> {
.collection_with_options::<Document>(
"datakeys",
CollectionOptions::builder()
.read_concern(ReadConcern::MAJORITY)
.write_concern(WriteConcern::MAJORITY)
.read_concern(ReadConcern::majority())
.write_concern(WriteConcern::majority())
.build(),
);
datakeys.drop(None).await?;
Expand Down Expand Up @@ -1593,7 +1593,7 @@ impl DeadlockTestCase {
.insert_one(
load_testdata("external/external-key.json")?,
InsertOneOptions::builder()
.write_concern(WriteConcern::MAJORITY)
.write_concern(WriteConcern::majority())
.build(),
)
.await?;
Expand Down Expand Up @@ -2362,7 +2362,7 @@ async fn explicit_encryption_setup() -> Result<Option<ExplicitEncryptionTestData
.insert_one(
key1_document,
InsertOneOptions::builder()
.write_concern(WriteConcern::MAJORITY)
.write_concern(WriteConcern::majority())
.build(),
)
.await?;
Expand Down Expand Up @@ -2509,7 +2509,7 @@ async fn unique_index_keyaltnames_setup() -> Result<(ClientEncryption, Binary)>
),
},
CreateIndexOptions::builder()
.write_concern(WriteConcern::MAJORITY)
.write_concern(WriteConcern::majority())
.build(),
)
.await?;
Expand Down Expand Up @@ -3264,7 +3264,7 @@ async fn range_explicit_encryption_test(
.insert_one(
key1_document,
InsertOneOptions::builder()
.write_concern(WriteConcern::MAJORITY)
.write_concern(WriteConcern::majority())
.build(),
)
.await?;
Expand Down
6 changes: 3 additions & 3 deletions src/test/spec/v2_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl TestContext {
// Reset the test collection as needed
#[allow(unused_mut)]
let mut options = DropCollectionOptions::builder()
.write_concern(WriteConcern::MAJORITY)
.write_concern(WriteConcern::majority())
.build();
#[cfg(feature = "in-use-encryption-unstable")]
if let Some(enc_fields) = &test_file.encrypted_fields {
Expand All @@ -158,7 +158,7 @@ impl TestContext {

#[allow(unused_mut)]
let mut options = CreateCollectionOptions::builder()
.write_concern(WriteConcern::MAJORITY)
.write_concern(WriteConcern::majority())
.build();
#[cfg(feature = "in-use-encryption-unstable")]
{
Expand All @@ -181,7 +181,7 @@ impl TestContext {
TestData::Single(data) => {
if !data.is_empty() {
let options = InsertManyOptions::builder()
.write_concern(WriteConcern::MAJORITY)
.write_concern(WriteConcern::majority())
.build();
coll.insert_many(data.clone(), options).await.unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/spec/v2_runner/csfle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) async fn populate_key_vault(client: &Client, kv_data: Option<&Vec<Doc
.collection_with_options::<Document>(
"datakeys",
CollectionOptions::builder()
.write_concern(WriteConcern::MAJORITY)
.write_concern(WriteConcern::majority())
.build(),
);
datakeys.drop(None).await.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/test/spec/v2_runner/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ impl TestOperation for AssertCollectionExists {
.database_with_options(
&self.database,
DatabaseOptions::builder()
.read_concern(ReadConcern::MAJORITY)
.read_concern(ReadConcern::majority())
.build(),
)
.list_collection_names(None)
Expand Down
2 changes: 1 addition & 1 deletion src/test/spec/v2_runner/test_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Outcome {
let coll_opts = CollectionOptions::default();
#[cfg(feature = "in-use-encryption-unstable")]
let coll_opts = CollectionOptions::builder()
.read_concern(crate::options::ReadConcern::LOCAL)
.read_concern(crate::options::ReadConcern::local())
.build();
let coll = client
.database(db_name)
Expand Down