Skip to content

Commit f7186ce

Browse files
Nathan Blinnisabelatkinson
authored andcommitted
RUST-957 Implement sync index management API
1 parent ff154b2 commit f7186ce

File tree

3 files changed

+159
-23
lines changed

3 files changed

+159
-23
lines changed

src/coll/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ impl<T> Collection<T> {
355355
let response = self
356356
.create_indexes_common(vec![index], options, session)
357357
.await?;
358-
Ok(response.into())
358+
Ok(response.into_create_index_result())
359359
}
360360

361361
/// Creates the given index on this collection.
@@ -367,7 +367,7 @@ impl<T> Collection<T> {
367367
self.create_index_common(index, options, None).await
368368
}
369369

370-
/// Convenience method for creating a single index on an explicit session.
370+
/// Creates the given index on this collection using the provided `ClientSession`.
371371
pub async fn create_index_with_session(
372372
&self,
373373
index: IndexModel,
@@ -386,7 +386,7 @@ impl<T> Collection<T> {
386386
self.create_indexes_common(indexes, options, None).await
387387
}
388388

389-
/// Create several indexes on an explicit session.
389+
/// Creates the given indexes on this collection using the provided `ClientSession`.
390390
pub async fn create_indexes_with_session(
391391
&self,
392392
indexes: impl IntoIterator<Item = IndexModel>,
@@ -545,16 +545,16 @@ impl<T> Collection<T> {
545545
/// Drops the index specified by `name` from this collection.
546546
pub async fn drop_index(
547547
&self,
548-
name: &str,
548+
name: impl AsRef<str>,
549549
options: impl Into<Option<DropIndexOptions>>,
550550
) -> Result<()> {
551551
self.drop_index_common(name, options, None).await
552552
}
553553

554-
/// Drop the index specified by `name` from the collection using an explicit session.
554+
/// Drops the index specified by `name` from this collection using the provided `ClientSession`.
555555
pub async fn drop_index_with_session(
556556
&self,
557-
name: &str,
557+
name: impl AsRef<str>,
558558
options: impl Into<Option<DropIndexOptions>>,
559559
session: &mut ClientSession,
560560
) -> Result<()> {
@@ -566,7 +566,7 @@ impl<T> Collection<T> {
566566
self.drop_indexes_common(None, options, None).await
567567
}
568568

569-
/// Drop all indexes associated with the collection using an explicit session.
569+
/// Drops all indexes associated with this collection using the provided `ClientSession`.
570570
pub async fn drop_indexes_with_session(
571571
&self,
572572
options: impl Into<Option<DropIndexOptions>>,
@@ -588,7 +588,7 @@ impl<T> Collection<T> {
588588
.map(|(spec, session)| Cursor::new(client.clone(), spec, session))
589589
}
590590

591-
/// List all indexes on the collection using an explicit session.
591+
/// Lists all indexes on this collection using the provided `ClientSession`.
592592
pub async fn list_indexes_with_session(
593593
&self,
594594
options: impl Into<Option<ListIndexOptions>>,

src/results.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,6 @@ pub struct CreateIndexResult {
101101
pub commit_quorum: Option<CommitQuorum>,
102102
}
103103

104-
impl From<CreateIndexesResult> for CreateIndexResult {
105-
fn from(result: CreateIndexesResult) -> Self {
106-
Self {
107-
// Safe unwrap because a successful `createIndex` command will always have names.
108-
index_name: result.index_names.into_iter().next().unwrap(),
109-
created_collection_automatically: result.created_collection_automatically,
110-
num_indexes_before: result.num_indexes_before,
111-
num_indexes_after: result.num_indexes_after,
112-
note: result.note,
113-
commit_quorum: result.commit_quorum,
114-
}
115-
}
116-
}
117-
118104
/// Information about the indexes created as a result of a
119105
/// [`Collection::create_indexes`](../struct.Collection.html#method.create_indexes).
120106
#[derive(Debug, Clone, PartialEq)]
@@ -139,6 +125,19 @@ pub struct CreateIndexesResult {
139125
pub commit_quorum: Option<CommitQuorum>,
140126
}
141127

128+
impl CreateIndexesResult {
129+
pub(crate) fn into_create_index_result(self) -> CreateIndexResult {
130+
CreateIndexResult {
131+
index_name: self.index_names.into_iter().next().unwrap(),
132+
created_collection_automatically: self.created_collection_automatically,
133+
num_indexes_before: self.num_indexes_before,
134+
num_indexes_after: self.num_indexes_after,
135+
note: self.note,
136+
commit_quorum: self.commit_quorum,
137+
}
138+
}
139+
}
140+
142141
#[derive(Debug, Clone)]
143142
pub(crate) struct GetMoreResult<T> {
144143
pub(crate) batch: VecDeque<T>,

src/sync/coll.rs

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ use super::{ClientSession, Cursor, SessionCursor};
66
use crate::{
77
bson::{Bson, Document},
88
error::Result,
9+
index::IndexModel,
910
options::{
1011
AggregateOptions,
1112
CountOptions,
13+
CreateIndexOptions,
1214
DeleteOptions,
1315
DistinctOptions,
1416
DropCollectionOptions,
17+
DropIndexOptions,
1518
EstimatedDocumentCountOptions,
1619
FindOneAndDeleteOptions,
1720
FindOneAndReplaceOptions,
@@ -20,14 +23,22 @@ use crate::{
2023
FindOptions,
2124
InsertManyOptions,
2225
InsertOneOptions,
26+
ListIndexOptions,
2327
ReadConcern,
2428
ReplaceOptions,
2529
SelectionCriteria,
2630
UpdateModifications,
2731
UpdateOptions,
2832
WriteConcern,
2933
},
30-
results::{DeleteResult, InsertManyResult, InsertOneResult, UpdateResult},
34+
results::{
35+
CreateIndexResult,
36+
CreateIndexesResult,
37+
DeleteResult,
38+
InsertManyResult,
39+
InsertOneResult,
40+
UpdateResult,
41+
},
3142
Collection as AsyncCollection,
3243
Namespace,
3344
RUNTIME,
@@ -210,6 +221,52 @@ impl<T> Collection<T> {
210221
))
211222
}
212223

224+
/// Creates the given index on this collection.
225+
pub fn create_index(
226+
&self,
227+
index: IndexModel,
228+
options: impl Into<Option<CreateIndexOptions>>,
229+
) -> Result<CreateIndexResult> {
230+
RUNTIME.block_on(self.async_collection.create_index(index, options))
231+
}
232+
233+
/// Creates the given index on this collection using the provided `ClientSession`.
234+
pub fn create_index_with_session(
235+
&self,
236+
index: IndexModel,
237+
options: impl Into<Option<CreateIndexOptions>>,
238+
session: &mut ClientSession,
239+
) -> Result<CreateIndexResult> {
240+
RUNTIME.block_on(self.async_collection.create_index_with_session(
241+
index,
242+
options,
243+
&mut session.async_client_session,
244+
))
245+
}
246+
247+
/// Creates the given indexes on this collection.
248+
pub fn create_indexes(
249+
&self,
250+
indexes: impl IntoIterator<Item = IndexModel>,
251+
options: impl Into<Option<CreateIndexOptions>>,
252+
) -> Result<CreateIndexesResult> {
253+
RUNTIME.block_on(self.async_collection.create_indexes(indexes, options))
254+
}
255+
256+
/// Creates the given indexes on this collection using the provided `ClientSession`.
257+
pub fn create_indexes_with_session(
258+
&self,
259+
indexes: impl IntoIterator<Item = IndexModel>,
260+
options: impl Into<Option<CreateIndexOptions>>,
261+
session: &mut ClientSession,
262+
) -> Result<CreateIndexesResult> {
263+
RUNTIME.block_on(self.async_collection.create_indexes_with_session(
264+
indexes,
265+
options,
266+
&mut session.async_client_session,
267+
))
268+
}
269+
213270
/// Deletes all documents stored in the collection matching `query`.
214271
pub fn delete_many(
215272
&self,
@@ -316,6 +373,86 @@ impl<T> Collection<T> {
316373
)
317374
}
318375

376+
/// Drops the index specified by `name` from this collection.
377+
pub fn drop_index(
378+
&self,
379+
name: impl AsRef<str>,
380+
options: impl Into<Option<DropIndexOptions>>,
381+
) -> Result<()> {
382+
RUNTIME.block_on(self.async_collection.drop_index(name, options))
383+
}
384+
385+
/// Drops the index specified by `name` from this collection using the provided `ClientSession`.
386+
pub fn drop_index_with_session(
387+
&self,
388+
name: impl AsRef<str>,
389+
options: impl Into<Option<DropIndexOptions>>,
390+
session: &mut ClientSession,
391+
) -> Result<()> {
392+
RUNTIME.block_on(self.async_collection.drop_index_with_session(
393+
name,
394+
options,
395+
&mut session.async_client_session,
396+
))
397+
}
398+
399+
/// Drops all indexes associated with this collection.
400+
pub fn drop_indexes(&self, options: impl Into<Option<DropIndexOptions>>) -> Result<()> {
401+
RUNTIME.block_on(self.async_collection.drop_indexes(options))
402+
}
403+
404+
/// Drops all indexes associated with this collection using the provided `ClientSession`.
405+
pub fn drop_indexes_with_session(
406+
&self,
407+
options: impl Into<Option<DropIndexOptions>>,
408+
session: &mut ClientSession,
409+
) -> Result<()> {
410+
RUNTIME.block_on(
411+
self.async_collection
412+
.drop_indexes_with_session(options, &mut session.async_client_session),
413+
)
414+
}
415+
416+
/// Lists all indexes on this collection.
417+
pub fn list_indexes(
418+
&self,
419+
options: impl Into<Option<ListIndexOptions>>,
420+
) -> Result<Cursor<IndexModel>> {
421+
RUNTIME
422+
.block_on(self.async_collection.list_indexes(options))
423+
.map(Cursor::new)
424+
}
425+
426+
/// Lists all indexes on this collection using the provided `ClientSession`.
427+
pub fn list_indexes_with_session(
428+
&self,
429+
options: impl Into<Option<ListIndexOptions>>,
430+
session: &mut ClientSession,
431+
) -> Result<SessionCursor<IndexModel>> {
432+
RUNTIME
433+
.block_on(
434+
self.async_collection
435+
.list_indexes_with_session(options, &mut session.async_client_session),
436+
)
437+
.map(SessionCursor::new)
438+
}
439+
440+
/// Gets the names of all indexes on the collection.
441+
pub fn list_index_names(&self) -> Result<Vec<String>> {
442+
RUNTIME.block_on(self.async_collection.list_index_names())
443+
}
444+
445+
/// Gets the names of all indexes on the collection using the provided `ClientSession`.
446+
pub fn list_index_names_with_session(
447+
&self,
448+
session: &mut ClientSession,
449+
) -> Result<Vec<String>> {
450+
RUNTIME.block_on(
451+
self.async_collection
452+
.list_index_names_with_session(&mut session.async_client_session),
453+
)
454+
}
455+
319456
/// Updates all documents matching `query` in the collection using the provided `ClientSession`.
320457
///
321458
/// Both `Document` and `Vec<Document>` implement `Into<UpdateModifications>`, so either can be

0 commit comments

Comments
 (0)