mongodb/action/
create_index.rs1use std::{marker::PhantomData, time::Duration};
2
3use crate::bson::Bson;
4
5use crate::{
6 coll::options::{CommitQuorum, CreateIndexOptions},
7 error::Result,
8 operation::CreateIndexes as Op,
9 options::WriteConcern,
10 results::{CreateIndexResult, CreateIndexesResult},
11 ClientSession,
12 Collection,
13 IndexModel,
14};
15
16use super::{
17 action_impl,
18 deeplink,
19 export_doc,
20 option_setters,
21 options_doc,
22 CollRef,
23 Multiple,
24 Single,
25};
26
27impl<T> Collection<T>
28where
29 T: Send + Sync,
30{
31 #[deeplink]
35 #[options_doc(create_index)]
36 pub fn create_index(&self, index: IndexModel) -> CreateIndex<'_> {
37 CreateIndex {
38 coll: CollRef::new(self),
39 indexes: vec![index],
40 options: None,
41 session: None,
42 _mode: PhantomData,
43 }
44 }
45
46 #[deeplink]
50 #[options_doc(create_index)]
51 pub fn create_indexes(
52 &self,
53 indexes: impl IntoIterator<Item = IndexModel>,
54 ) -> CreateIndex<'_, Multiple> {
55 CreateIndex {
56 coll: CollRef::new(self),
57 indexes: indexes.into_iter().collect(),
58 options: None,
59 session: None,
60 _mode: PhantomData,
61 }
62 }
63}
64
65#[cfg(feature = "sync")]
66impl<T> crate::sync::Collection<T>
67where
68 T: Send + Sync,
69{
70 #[deeplink]
74 #[options_doc(create_index, "run")]
75 pub fn create_index(&self, index: IndexModel) -> CreateIndex<'_> {
76 self.async_collection.create_index(index)
77 }
78
79 #[deeplink]
83 #[options_doc(create_index, "run")]
84 pub fn create_indexes(
85 &self,
86 indexes: impl IntoIterator<Item = IndexModel>,
87 ) -> CreateIndex<'_, Multiple> {
88 self.async_collection.create_indexes(indexes)
89 }
90}
91
92#[must_use]
95pub struct CreateIndex<'a, M = Single> {
96 coll: CollRef<'a>,
97 indexes: Vec<IndexModel>,
98 options: Option<CreateIndexOptions>,
99 session: Option<&'a mut ClientSession>,
100 _mode: PhantomData<M>,
101}
102
103#[option_setters(crate::coll::options::CreateIndexOptions)]
104#[export_doc(create_index)]
105impl<'a, M> CreateIndex<'a, M> {
106 pub fn session(mut self, value: impl Into<&'a mut ClientSession>) -> Self {
108 self.session = Some(value.into());
109 self
110 }
111}
112
113#[action_impl]
114impl<'a> Action for CreateIndex<'a, Single> {
115 type Future = CreateIndexFuture;
116
117 async fn execute(self) -> Result<CreateIndexResult> {
118 let inner: CreateIndex<'a, Multiple> = CreateIndex {
119 coll: self.coll,
120 indexes: self.indexes,
121 options: self.options,
122 session: self.session,
123 _mode: PhantomData,
124 };
125 let response = inner.await?;
126 Ok(response.into_create_index_result())
127 }
128}
129
130#[action_impl]
131impl<'a> Action for CreateIndex<'a, Multiple> {
132 type Future = CreateIndexesFuture;
133
134 async fn execute(mut self) -> Result<CreateIndexesResult> {
135 resolve_write_concern_with_session!(self.coll, self.options, self.session.as_ref())?;
136
137 let op = Op::new(self.coll.namespace(), self.indexes, self.options);
138 self.coll.client().execute_operation(op, self.session).await
139 }
140}