forked from mongodb/mongo-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_and_modify.rs
164 lines (148 loc) · 4.34 KB
/
find_and_modify.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
mod options;
#[cfg(test)]
mod test;
use std::fmt::Debug;
use bson::{from_slice, RawBson};
use serde::{de::DeserializeOwned, Deserialize};
use self::options::FindAndModifyOptions;
use crate::{
bson::{doc, Document},
bson_util,
cmap::{Command, RawCommandResponse, StreamDescription},
coll::{
options::{
FindOneAndDeleteOptions,
FindOneAndReplaceOptions,
FindOneAndUpdateOptions,
UpdateModifications,
},
Namespace,
},
error::{ErrorKind, Result},
operation::{append_options, remove_empty_write_concern, OperationWithDefaults, Retryability},
options::WriteConcern,
};
pub(crate) struct FindAndModify<T = Document>
where
T: DeserializeOwned,
{
ns: Namespace,
query: Document,
options: FindAndModifyOptions,
_phantom: std::marker::PhantomData<T>,
}
impl<T> FindAndModify<T>
where
T: DeserializeOwned,
{
pub fn with_delete(
ns: Namespace,
query: Document,
options: Option<FindOneAndDeleteOptions>,
) -> Self {
let options =
FindAndModifyOptions::from_find_one_and_delete_options(options.unwrap_or_default());
FindAndModify {
ns,
query,
options,
_phantom: Default::default(),
}
}
pub fn with_replace(
ns: Namespace,
query: Document,
replacement: Document,
options: Option<FindOneAndReplaceOptions>,
) -> Result<Self> {
bson_util::replacement_document_check(&replacement)?;
let options = FindAndModifyOptions::from_find_one_and_replace_options(
replacement,
options.unwrap_or_default(),
);
Ok(FindAndModify {
ns,
query,
options,
_phantom: Default::default(),
})
}
pub fn with_update(
ns: Namespace,
query: Document,
update: UpdateModifications,
options: Option<FindOneAndUpdateOptions>,
) -> Result<Self> {
if let UpdateModifications::Document(ref d) = update {
bson_util::update_document_check(d)?;
};
let options = FindAndModifyOptions::from_find_one_and_update_options(
update,
options.unwrap_or_default(),
);
Ok(FindAndModify {
ns,
query,
options,
_phantom: Default::default(),
})
}
}
impl<T> OperationWithDefaults for FindAndModify<T>
where
T: DeserializeOwned,
{
type O = Option<T>;
type Command = Document;
const NAME: &'static str = "findAndModify";
fn build(&mut self, description: &StreamDescription) -> Result<Command> {
if self.options.hint.is_some() && description.max_wire_version.unwrap_or(0) < 8 {
return Err(ErrorKind::InvalidArgument {
message: "Specifying a hint to find_one_and_x is not supported on server versions \
< 4.4"
.to_string(),
}
.into());
}
let mut body: Document = doc! {
Self::NAME: self.ns.coll.clone(),
"query": self.query.clone(),
};
remove_empty_write_concern!(Some(&mut self.options));
append_options(&mut body, Some(&self.options).as_ref())?;
Ok(Command::new(
Self::NAME.to_string(),
self.ns.db.clone(),
body,
))
}
fn handle_response(
&self,
response: RawCommandResponse,
_description: &StreamDescription,
) -> Result<Self::O> {
#[derive(Debug, Deserialize)]
pub(crate) struct Response {
value: RawBson,
}
let response: Response = response.body()?;
match response.value {
RawBson::Document(doc) => Ok(Some(from_slice(doc.as_bytes())?)),
RawBson::Null => Ok(None),
other => Err(ErrorKind::InvalidResponse {
message: format!(
"expected document for value field of findAndModify response, but instead got \
{:?}",
other
),
}
.into()),
}
}
fn write_concern(&self) -> Option<&WriteConcern> {
self.options.write_concern.as_ref()
}
fn retryability(&self) -> Retryability {
Retryability::Write
}
}