Skip to content

Commit ff154b2

Browse files
Nathan Blinnisabelatkinson
authored andcommitted
RUST-958 Add explicit sessions to index management API
1 parent 63d89e4 commit ff154b2

File tree

1 file changed

+88
-13
lines changed

1 file changed

+88
-13
lines changed

src/coll/mod.rs

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,18 +346,37 @@ impl<T> Collection<T> {
346346
.await
347347
}
348348

349-
/// Creates the given index on this collection.
350-
pub async fn create_index(
349+
async fn create_index_common(
351350
&self,
352351
index: IndexModel,
353352
options: impl Into<Option<CreateIndexOptions>>,
353+
session: impl Into<Option<&mut ClientSession>>,
354354
) -> Result<CreateIndexResult> {
355355
let response = self
356-
.create_indexes_common(vec![index], options, None)
356+
.create_indexes_common(vec![index], options, session)
357357
.await?;
358358
Ok(response.into())
359359
}
360360

361+
/// Creates the given index on this collection.
362+
pub async fn create_index(
363+
&self,
364+
index: IndexModel,
365+
options: impl Into<Option<CreateIndexOptions>>,
366+
) -> Result<CreateIndexResult> {
367+
self.create_index_common(index, options, None).await
368+
}
369+
370+
/// Convenience method for creating a single index on an explicit session.
371+
pub async fn create_index_with_session(
372+
&self,
373+
index: IndexModel,
374+
options: impl Into<Option<CreateIndexOptions>>,
375+
session: &mut ClientSession,
376+
) -> Result<CreateIndexResult> {
377+
self.create_index_common(index, options, session).await
378+
}
379+
361380
/// Creates the given indexes on this collection.
362381
pub async fn create_indexes(
363382
&self,
@@ -367,6 +386,16 @@ impl<T> Collection<T> {
367386
self.create_indexes_common(indexes, options, None).await
368387
}
369388

389+
/// Create several indexes on an explicit session.
390+
pub async fn create_indexes_with_session(
391+
&self,
392+
indexes: impl IntoIterator<Item = IndexModel>,
393+
options: impl Into<Option<CreateIndexOptions>>,
394+
session: &mut ClientSession,
395+
) -> Result<CreateIndexesResult> {
396+
self.create_indexes_common(indexes, options, session).await
397+
}
398+
370399
/// Deletes all documents stored in the collection matching `query`.
371400
pub async fn delete_many(
372401
&self,
@@ -477,7 +506,7 @@ impl<T> Collection<T> {
477506
.await
478507
}
479508

480-
async fn drop_index_common(
509+
async fn drop_indexes_common(
481510
&self,
482511
name: impl Into<Option<&str>>,
483512
options: impl Into<Option<DropIndexOptions>>,
@@ -495,11 +524,11 @@ impl<T> Collection<T> {
495524
self.client().execute_operation(drop_index, session).await
496525
}
497526

498-
/// Drops the index specified by `name` from this collection.
499-
pub async fn drop_index(
527+
async fn drop_index_common(
500528
&self,
501529
name: impl AsRef<str>,
502530
options: impl Into<Option<DropIndexOptions>>,
531+
session: impl Into<Option<&mut ClientSession>>,
503532
) -> Result<()> {
504533
let name = name.as_ref();
505534
if name == "*" {
@@ -510,12 +539,40 @@ impl<T> Collection<T> {
510539
}
511540
.into());
512541
}
542+
self.drop_indexes_common(name, options, session).await
543+
}
544+
545+
/// Drops the index specified by `name` from this collection.
546+
pub async fn drop_index(
547+
&self,
548+
name: &str,
549+
options: impl Into<Option<DropIndexOptions>>,
550+
) -> Result<()> {
513551
self.drop_index_common(name, options, None).await
514552
}
515553

554+
/// Drop the index specified by `name` from the collection using an explicit session.
555+
pub async fn drop_index_with_session(
556+
&self,
557+
name: &str,
558+
options: impl Into<Option<DropIndexOptions>>,
559+
session: &mut ClientSession,
560+
) -> Result<()> {
561+
self.drop_index_common(name, options, session).await
562+
}
563+
516564
/// Drops all indexes associated with this collection.
517565
pub async fn drop_indexes(&self, options: impl Into<Option<DropIndexOptions>>) -> Result<()> {
518-
self.drop_index_common(None, options, None).await
566+
self.drop_indexes_common(None, options, None).await
567+
}
568+
569+
/// Drop all indexes associated with the collection using an explicit session.
570+
pub async fn drop_indexes_with_session(
571+
&self,
572+
options: impl Into<Option<DropIndexOptions>>,
573+
session: &mut ClientSession,
574+
) -> Result<()> {
575+
self.drop_indexes_common(None, options, session).await
519576
}
520577

521578
/// Lists all indexes on this collection.
@@ -531,6 +588,20 @@ impl<T> Collection<T> {
531588
.map(|(spec, session)| Cursor::new(client.clone(), spec, session))
532589
}
533590

591+
/// List all indexes on the collection using an explicit session.
592+
pub async fn list_indexes_with_session(
593+
&self,
594+
options: impl Into<Option<ListIndexOptions>>,
595+
session: &mut ClientSession,
596+
) -> Result<SessionCursor<IndexModel>> {
597+
let list_indexes = ListIndexes::new(self.namespace(), options.into());
598+
let client = self.client();
599+
client
600+
.execute_operation(list_indexes, session)
601+
.await
602+
.map(|spec| SessionCursor::new(client.clone(), spec))
603+
}
604+
534605
async fn list_index_names_common(
535606
&self,
536607
cursor: impl TryStreamExt<Ok = IndexModel, Error = Error>,
@@ -543,15 +614,19 @@ impl<T> Collection<T> {
543614

544615
/// Gets the names of all indexes on the collection.
545616
pub async fn list_index_names(&self) -> Result<Vec<String>> {
546-
let list_indexes = ListIndexes::new(self.namespace(), None);
547-
let client = self.client();
548-
let cursor: Cursor<IndexModel> = client
549-
.execute_cursor_operation(list_indexes)
550-
.await
551-
.map(|(spec, session)| Cursor::new(client.clone(), spec, session))?;
617+
let cursor = self.list_indexes(None).await?;
552618
self.list_index_names_common(cursor).await
553619
}
554620

621+
/// Gets the names of all indexes on the collection using the provided `ClientSession`.
622+
pub async fn list_index_names_with_session(
623+
&self,
624+
session: &mut ClientSession,
625+
) -> Result<Vec<String>> {
626+
let mut cursor = self.list_indexes_with_session(None, session).await?;
627+
self.list_index_names_common(cursor.stream(session)).await
628+
}
629+
555630
async fn update_many_common(
556631
&self,
557632
query: Document,

0 commit comments

Comments
 (0)