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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use std::{collections::HashMap, sync::Arc, time::Duration};
use derivative::Derivative;
use serde::Deserialize;
use typed_builder::TypedBuilder;
use crate::{
bson::{doc, Bson, Document},
error::{ErrorKind, Result},
options::StreamAddress,
sdam::public::ServerInfo,
};
#[derive(Clone, Derivative, Deserialize)]
#[derivative(Debug)]
#[serde(untagged)]
#[non_exhaustive]
pub enum SelectionCriteria {
#[serde(skip)]
ReadPreference(ReadPreference),
#[serde(skip)]
Predicate(#[derivative(Debug = "ignore")] Predicate),
}
impl PartialEq for SelectionCriteria {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::ReadPreference(r1), Self::ReadPreference(r2)) => r1 == r2,
_ => false,
}
}
}
impl From<ReadPreference> for SelectionCriteria {
fn from(read_pref: ReadPreference) -> Self {
Self::ReadPreference(read_pref)
}
}
impl SelectionCriteria {
pub(crate) fn as_read_pref(&self) -> Option<&ReadPreference> {
match self {
Self::ReadPreference(ref read_pref) => Some(read_pref),
Self::Predicate(..) => None,
}
}
#[cfg(test)]
pub(crate) fn as_predicate(&self) -> Option<&Predicate> {
match self {
Self::Predicate(ref p) => Some(p),
_ => None,
}
}
pub(crate) fn is_read_pref_primary(&self) -> bool {
match self {
Self::ReadPreference(ReadPreference::Primary) => true,
_ => false,
}
}
pub(crate) fn max_staleness(&self) -> Option<Duration> {
self.as_read_pref().and_then(|pref| pref.max_staleness())
}
pub(crate) fn from_address(address: StreamAddress) -> Self {
SelectionCriteria::Predicate(Arc::new(move |server| server.address() == &address))
}
}
pub type Predicate = Arc<dyn Send + Sync + Fn(&ServerInfo) -> bool>;
#[derive(Clone, Debug, PartialEq)]
pub enum ReadPreference {
Primary,
Secondary { options: ReadPreferenceOptions },
PrimaryPreferred { options: ReadPreferenceOptions },
SecondaryPreferred { options: ReadPreferenceOptions },
Nearest { options: ReadPreferenceOptions },
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq, TypedBuilder)]
#[non_exhaustive]
pub struct ReadPreferenceOptions {
#[builder(default)]
pub tag_sets: Option<Vec<TagSet>>,
#[builder(default)]
pub max_staleness: Option<Duration>,
}
impl ReadPreference {
pub(crate) fn max_staleness(&self) -> Option<Duration> {
match self {
ReadPreference::Primary => None,
ReadPreference::Secondary { ref options }
| ReadPreference::PrimaryPreferred { ref options }
| ReadPreference::SecondaryPreferred { ref options }
| ReadPreference::Nearest { ref options } => options.max_staleness,
}
}
pub(crate) fn with_tags(mut self, tag_sets: Vec<TagSet>) -> Result<Self> {
let options = match self {
ReadPreference::Primary => {
return Err(ErrorKind::ArgumentError {
message: "read preference tags can only be specified when a non-primary mode \
is specified"
.to_string(),
}
.into());
}
ReadPreference::Secondary { ref mut options } => options,
ReadPreference::PrimaryPreferred { ref mut options } => options,
ReadPreference::SecondaryPreferred { ref mut options } => options,
ReadPreference::Nearest { ref mut options } => options,
};
options.tag_sets = Some(tag_sets);
Ok(self)
}
pub(crate) fn with_max_staleness(mut self, max_staleness: Duration) -> Result<Self> {
let options = match self {
ReadPreference::Primary => {
return Err(ErrorKind::ArgumentError {
message: "max staleness can only be specified when a non-primary mode is \
specified"
.to_string(),
}
.into());
}
ReadPreference::Secondary { ref mut options } => options,
ReadPreference::PrimaryPreferred { ref mut options } => options,
ReadPreference::SecondaryPreferred { ref mut options } => options,
ReadPreference::Nearest { ref mut options } => options,
};
options.max_staleness = Some(max_staleness);
Ok(self)
}
pub(crate) fn into_document(self) -> Document {
let (mode, tag_sets, max_staleness) = match self {
ReadPreference::Primary => ("primary", None, None),
ReadPreference::PrimaryPreferred { options } => {
("primaryPreferred", options.tag_sets, options.max_staleness)
}
ReadPreference::Secondary { options } => {
("secondary", options.tag_sets, options.max_staleness)
}
ReadPreference::SecondaryPreferred { options } => (
"secondaryPreferred",
options.tag_sets,
options.max_staleness,
),
ReadPreference::Nearest { options } => {
("nearest", options.tag_sets, options.max_staleness)
}
};
let mut doc = doc! { "mode": mode };
if let Some(max_stale) = max_staleness {
doc.insert("maxStalenessSeconds", max_stale.as_secs());
}
if let Some(tag_sets) = tag_sets {
let tags: Vec<Bson> = tag_sets
.into_iter()
.map(|tag_set| {
Bson::Document(tag_set.into_iter().map(|(k, v)| (k, v.into())).collect())
})
.collect();
doc.insert("tags", tags);
}
doc
}
}
pub type TagSet = HashMap<String, String>;