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
#[cfg(test)]
mod test;
use serde::Deserialize;
use crate::{
bson::{doc, Bson, Document},
cmap::{Command, StreamDescription},
coll::{options::DistinctOptions, Namespace},
error::Result,
operation::{append_options, Operation, Retryability},
selection_criteria::SelectionCriteria,
};
use super::CommandResponse;
pub(crate) struct Distinct {
ns: Namespace,
field_name: String,
query: Option<Document>,
options: Option<DistinctOptions>,
}
impl Distinct {
pub fn new(
ns: Namespace,
field_name: String,
query: Option<Document>,
options: Option<DistinctOptions>,
) -> Self {
Distinct {
ns,
field_name,
query,
options,
}
}
#[cfg(test)]
pub(crate) fn empty() -> Self {
Distinct {
ns: Namespace {
db: String::new(),
coll: String::new(),
},
field_name: String::new(),
query: None,
options: None,
}
}
}
impl Operation for Distinct {
type O = Vec<Bson>;
type Command = Document;
type Response = CommandResponse<Response>;
const NAME: &'static str = "distinct";
fn build(&mut self, _description: &StreamDescription) -> Result<Command> {
let mut body: Document = doc! {
Self::NAME: self.ns.coll.clone(),
"key": self.field_name.clone(),
};
if let Some(ref query) = self.query {
body.insert("query", query.clone());
}
append_options(&mut body, self.options.as_ref())?;
Ok(Command::new(
Self::NAME.to_string(),
self.ns.db.clone(),
body,
))
}
fn handle_response(
&self,
response: Response,
_description: &StreamDescription,
) -> Result<Self::O> {
Ok(response.values)
}
fn selection_criteria(&self) -> Option<&SelectionCriteria> {
if let Some(ref options) = self.options {
return options.selection_criteria.as_ref();
}
None
}
fn retryability(&self) -> Retryability {
Retryability::Read
}
}
#[derive(Debug, Deserialize)]
pub(crate) struct Response {
values: Vec<Bson>,
}