Skip to content

Commit c4562c2

Browse files
RUST-13 Add indexOptionDefaults to createCollections (#189)
1 parent 6d37f7c commit c4562c2

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/db/options.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ pub struct CreateCollectionOptions {
9090
/// The write concern for the operation.
9191
#[builder(default)]
9292
pub write_concern: Option<WriteConcern>,
93+
94+
/// The default configuration for indexes created on this collection, including the _id index.
95+
#[builder(default)]
96+
pub index_option_defaults: Option<IndexOptionDefaults>,
9397
}
9498

9599
/// Specifies how strictly the database should apply validation rules to existing documents during
@@ -117,6 +121,16 @@ pub enum ValidationAction {
117121
Warn,
118122
}
119123

124+
/// Specifies default configuration for indexes created on a collection, including the _id index.
125+
#[derive(Clone, Debug, PartialEq, Serialize)]
126+
#[serde(rename_all = "camelCase")]
127+
pub struct IndexOptionDefaults {
128+
/// The `storageEngine` document should be in the following form:
129+
///
130+
/// `{ <storage-engine-name>: <options> }`
131+
pub storage_engine: Document,
132+
}
133+
120134
/// Specifies the options to a [`Database::drop`](../struct.Database.html#method.drop) operation.
121135
#[derive(Debug, Default, TypedBuilder, Serialize)]
122136
#[serde(rename_all = "camelCase")]

src/test/db.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ use serde::Deserialize;
77
use crate::{
88
bson::{doc, Bson, Document},
99
error::Result,
10-
options::{AggregateOptions, CreateCollectionOptions},
11-
test::{util::TestClient, LOCK},
10+
options::{AggregateOptions, CreateCollectionOptions, IndexOptionDefaults},
11+
test::{
12+
util::{CommandEvent, EventClient, TestClient},
13+
LOCK,
14+
},
1215
Database,
1316
};
1417

@@ -318,3 +321,55 @@ async fn db_aggregate_disk_use() {
318321
.await
319322
.expect("aggregate with disk use should succeed");
320323
}
324+
325+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
326+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
327+
#[function_name::named]
328+
async fn create_index_options_defaults() {
329+
let defaults = IndexOptionDefaults {
330+
storage_engine: doc! { "wiredTiger": doc! {} },
331+
};
332+
index_option_defaults_test(Some(defaults), function_name!()).await;
333+
}
334+
335+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
336+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
337+
#[function_name::named]
338+
async fn create_index_options_defaults_not_specified() {
339+
index_option_defaults_test(None, function_name!()).await;
340+
}
341+
342+
async fn index_option_defaults_test(defaults: Option<IndexOptionDefaults>, name: &str) {
343+
let _guard = LOCK.run_concurrently().await;
344+
345+
let client = EventClient::new().await;
346+
let db = client.database(name);
347+
348+
let options = CreateCollectionOptions::builder()
349+
.index_option_defaults(defaults.clone())
350+
.build();
351+
db.create_collection(name, options).await.unwrap();
352+
db.drop(None).await.unwrap();
353+
354+
let events = client.command_events.read().unwrap();
355+
let mut iter = events.iter().filter_map(|event| match event {
356+
CommandEvent::CommandStartedEvent(event) => {
357+
if event.command_name == "create" {
358+
Some(event)
359+
} else {
360+
None
361+
}
362+
}
363+
_ => None,
364+
});
365+
366+
let event = iter.next().unwrap();
367+
let event_defaults = match event.command.get_document("indexOptionDefaults") {
368+
Ok(defaults) => Some(IndexOptionDefaults {
369+
storage_engine: defaults.get_document("storageEngine").unwrap().clone(),
370+
}),
371+
Err(_) => None,
372+
};
373+
assert_eq!(event_defaults, defaults);
374+
assert!(iter.next().is_none());
375+
}

0 commit comments

Comments
 (0)