mongodb/action/
drop_index.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::time::Duration;

use bson::Bson;

use super::{action_impl, deeplink, option_setters, CollRef};
use crate::{
    coll::options::DropIndexOptions,
    error::{ErrorKind, Result},
    operation::DropIndexes as Op,
    options::WriteConcern,
    ClientSession,
    Collection,
};

impl<T> Collection<T>
where
    T: Send + Sync,
{
    /// Drops the index specified by `name` from this collection.
    ///
    /// `await` will return d[`Result<()>`].
    #[deeplink]
    pub fn drop_index(&self, name: impl AsRef<str>) -> DropIndex {
        DropIndex {
            coll: CollRef::new(self),
            name: Some(name.as_ref().to_string()),
            options: None,
            session: None,
        }
    }

    /// Drops all indexes associated with this collection.
    ///
    /// `await` will return d[`Result<()>`].
    #[deeplink]
    pub fn drop_indexes(&self) -> DropIndex {
        DropIndex {
            coll: CollRef::new(self),
            name: None,
            options: None,
            session: None,
        }
    }
}

#[cfg(feature = "sync")]
impl<T> crate::sync::Collection<T>
where
    T: Send + Sync,
{
    /// Drops the index specified by `name` from this collection.
    ///
    /// [`run`](DropIndex::run) will return d[`Result<()>`].
    #[deeplink]
    pub fn drop_index(&self, name: impl AsRef<str>) -> DropIndex {
        self.async_collection.drop_index(name)
    }

    /// Drops all indexes associated with this collection.
    ///
    /// [`run`](DropIndex::run) will return d[`Result<()>`].
    #[deeplink]
    pub fn drop_indexes(&self) -> DropIndex {
        self.async_collection.drop_indexes()
    }
}

/// Drop an index or indexes.  Construct with [`Collection::drop_index`] or
/// [`Collection::drop_indexes`].
#[must_use]
pub struct DropIndex<'a> {
    coll: CollRef<'a>,
    name: Option<String>,
    options: Option<DropIndexOptions>,
    session: Option<&'a mut ClientSession>,
}

impl<'a> DropIndex<'a> {
    option_setters!(options: DropIndexOptions;
        max_time: Duration,
        write_concern: WriteConcern,
        comment: Bson,
    );

    /// Use the provided session when running the operation.
    pub fn session(mut self, value: impl Into<&'a mut ClientSession>) -> Self {
        self.session = Some(value.into());
        self
    }
}

#[action_impl]
impl<'a> Action for DropIndex<'a> {
    type Future = DropIndexFuture;

    async fn execute(mut self) -> Result<()> {
        if matches!(self.name.as_deref(), Some("*")) {
            return Err(ErrorKind::InvalidArgument {
                message: "Cannot pass name \"*\" to drop_index since more than one index would be \
                          dropped."
                    .to_string(),
            }
            .into());
        }
        resolve_write_concern_with_session!(self.coll, self.options, self.session.as_ref())?;

        // If there is no provided name, that means we should drop all indexes.
        let index_name = self.name.unwrap_or_else(|| "*".to_string());

        let op = Op::new(self.coll.namespace(), index_name, self.options);
        self.coll.client().execute_operation(op, self.session).await
    }
}