-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathdistinct.rs
96 lines (81 loc) · 2.38 KB
/
distinct.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
use serde::Deserialize;
use crate::{
bson::{doc, rawdoc, Bson, Document, RawBsonRef, RawDocumentBuf},
cmap::{Command, RawCommandResponse, StreamDescription},
coll::{options::DistinctOptions, Namespace},
error::Result,
operation::{OperationWithDefaults, Retryability},
selection_criteria::SelectionCriteria,
};
use super::{append_options_to_raw_document, ExecutionContext};
pub(crate) struct Distinct {
ns: Namespace,
field_name: String,
query: Document,
options: Option<DistinctOptions>,
}
impl Distinct {
pub fn new(
ns: Namespace,
field_name: String,
query: Document,
options: Option<DistinctOptions>,
) -> Self {
Distinct {
ns,
field_name,
query,
options,
}
}
}
impl OperationWithDefaults for Distinct {
type O = Vec<Bson>;
const NAME: &'static str = "distinct";
fn build(&mut self, _description: &StreamDescription) -> Result<Command> {
let mut body = rawdoc! {
Self::NAME: self.ns.coll.clone(),
"key": self.field_name.clone(),
"query": RawDocumentBuf::from_document(&self.query)?,
};
append_options_to_raw_document(&mut body, self.options.as_ref())?;
Ok(Command::new_read(
Self::NAME.to_string(),
self.ns.db.clone(),
self.options.as_ref().and_then(|o| o.read_concern.clone()),
body,
))
}
fn extract_at_cluster_time(
&self,
response: &bson::RawDocument,
) -> Result<Option<bson::Timestamp>> {
Ok(response
.get("atClusterTime")?
.and_then(RawBsonRef::as_timestamp))
}
fn handle_response<'a>(
&'a self,
response: RawCommandResponse,
_context: ExecutionContext<'a>,
) -> Result<Self::O> {
let response: Response = response.body()?;
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
}
fn supports_read_concern(&self, _description: &StreamDescription) -> bool {
true
}
}
#[derive(Debug, Deserialize)]
pub(crate) struct Response {
values: Vec<Bson>,
}