-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathblocking_commands.rs
296 lines (281 loc) · 9.42 KB
/
blocking_commands.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use crate::{
client::{prepare_command, MonitorStream, PreparedCommand},
commands::{LMoveWhere, ZMPopResult, ZWhere},
resp::{cmd, deserialize_vec_of_triplets, PrimitiveResponse, SingleArg, SingleArgCollection},
Result,
};
use serde::{
de::{DeserializeOwned, Visitor},
Deserialize, Deserializer,
};
use std::{fmt, marker::PhantomData};
/// Result for the [`bzpopmin`](BlockingCommands::bzpopmin)
/// and [`bzpopmax`](BlockingCommands::bzpopmax) commands
#[derive(Deserialize)]
pub struct BZpopMinMaxResult<K, E>(
#[serde(deserialize_with = "deserialize_bzop_min_max_result")] pub Option<Vec<(K, E, f64)>>,
)
where
K: DeserializeOwned,
E: DeserializeOwned;
#[allow(clippy::complexity)]
pub fn deserialize_bzop_min_max_result<'de, D, K, V>(
deserializer: D,
) -> std::result::Result<Option<Vec<(K, V, f64)>>, D::Error>
where
D: Deserializer<'de>,
K: DeserializeOwned,
V: DeserializeOwned,
{
struct OptionVisitor<K, V> {
phantom: PhantomData<(K, V)>,
}
impl<'de, K, V> Visitor<'de> for OptionVisitor<K, V>
where
K: DeserializeOwned,
V: DeserializeOwned,
{
type Value = Option<Vec<(K, V, f64)>>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Option<Vec<(K, V, f64)>>")
}
fn visit_none<E>(self) -> std::result::Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(None)
}
fn visit_some<D>(self, deserializer: D) -> std::result::Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
deserialize_vec_of_triplets(deserializer).map(Some)
}
}
deserializer.deserialize_option(OptionVisitor {
phantom: PhantomData,
})
}
/// A group of blocking commands
pub trait BlockingCommands<'a> {
/// This command is the blocking variant of [`lmove`](crate::commands::ListCommands::lmove).
///
/// # Return
/// the element being popped from `source` and pushed to `destination`.
/// If timeout is reached, a None reply is returned.
///
/// # See Also
/// [<https://redis.io/commands/blmove/>](https://redis.io/commands/blmove/)
#[must_use]
fn blmove<S, D, E>(
self,
source: S,
destination: D,
where_from: LMoveWhere,
where_to: LMoveWhere,
timeout: f64,
) -> PreparedCommand<'a, Self, E>
where
Self: Sized,
S: SingleArg,
D: SingleArg,
E: PrimitiveResponse,
{
prepare_command(
self,
cmd("BLMOVE")
.arg(source)
.arg(destination)
.arg(where_from)
.arg(where_to)
.arg(timeout),
)
}
/// This command is the blocking variant of [`lmpop`](crate::commands::ListCommands::lmpop).
///
/// # Return
/// - None when no element could be popped, and timeout is reached.
/// - Tuple composed by the name of the key from which elements were popped and the list of popped element
///
/// # See Also
/// [<https://redis.io/commands/blmpop/>](https://redis.io/commands/blmpop/)
#[must_use]
fn blmpop<K, KK, E>(
self,
timeout: f64,
keys: KK,
where_: LMoveWhere,
count: usize,
) -> PreparedCommand<'a, Self, Option<(String, Vec<E>)>>
where
Self: Sized,
K: SingleArg,
KK: SingleArgCollection<K>,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(
self,
cmd("BLMPOP")
.arg(timeout)
.arg(keys.num_args())
.arg(keys)
.arg(where_)
.arg("COUNT")
.arg(count),
)
}
/// This command is a blocking list pop primitive.
///
/// It is the blocking version of [`lpop`](crate::commands::ListCommands::lpop) because it
/// blocks the connection when there are no elements to pop from any of the given lists.
///
/// An element is popped from the head of the first list that is non-empty,
/// with the given keys being checked in the order that they are given.
///
/// # Return
/// - `None` when no element could be popped and the timeout expired
/// - a tuple with the first element being the name of the key where an element was popped
/// and the second element being the value of the popped element.
///
/// # See Also
/// [<https://redis.io/commands/blpop/>](https://redis.io/commands/blpop/)
#[must_use]
fn blpop<K, KK, K1, V>(
self,
keys: KK,
timeout: f64,
) -> PreparedCommand<'a, Self, Option<(K1, V)>>
where
Self: Sized,
K: SingleArg,
KK: SingleArgCollection<K>,
K1: PrimitiveResponse + DeserializeOwned,
V: PrimitiveResponse + DeserializeOwned,
{
prepare_command(self, cmd("BLPOP").arg(keys).arg(timeout))
}
/// This command is a blocking list pop primitive.
///
/// It is the blocking version of [`rpop`](crate::commands::ListCommands::rpop) because it
/// blocks the connection when there are no elements to pop from any of the given lists.
///
/// An element is popped from the tail of the first list that is non-empty,
/// with the given keys being checked in the order that they are given.
///
/// # Return
/// - `None` when no element could be popped and the timeout expired
/// - a tuple with the first element being the name of the key where an element was popped
/// and the second element being the value of the popped element.
///
/// # See Also
/// [<https://redis.io/commands/brpop/>](https://redis.io/commands/brpop/)
#[must_use]
fn brpop<K, KK, K1, V>(
self,
keys: KK,
timeout: f64,
) -> PreparedCommand<'a, Self, Option<(K1, V)>>
where
Self: Sized,
K: SingleArg,
KK: SingleArgCollection<K>,
K1: PrimitiveResponse + DeserializeOwned,
V: PrimitiveResponse + DeserializeOwned,
{
prepare_command(self, cmd("BRPOP").arg(keys).arg(timeout))
}
/// This command is the blocking variant of [`zmpop`](crate::commands::SortedSetCommands::zmpop).
///
/// # Return
/// * `None` if no element could be popped
/// * A tuple made up of
/// * The name of the key from which elements were popped
/// * An array of tuples with all the popped members and their scores
///
/// # See Also
/// [<https://redis.io/commands/bzmpop/>](https://redis.io/commands/bzmpop/)
#[must_use]
fn bzmpop<K, KK, E>(
self,
timeout: f64,
keys: KK,
where_: ZWhere,
count: usize,
) -> PreparedCommand<'a, Self, Option<ZMPopResult<E>>>
where
Self: Sized,
K: SingleArg,
KK: SingleArgCollection<K>,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(
self,
cmd("BZMPOP")
.arg(timeout)
.arg(keys.num_args())
.arg(keys)
.arg(where_)
.arg("COUNT")
.arg(count),
)
}
/// This command is the blocking variant of [`zpopmax`](crate::commands::SortedSetCommands::zpopmax).
///
/// # Return
/// * `None` when no element could be popped and the timeout expired.
/// * The list of tuple with
/// * the first element being the name of the key where a member was popped,
/// * the second element is the popped member itself,
/// * and the third element is the score of the popped element.
///
/// # See Also
/// [<https://redis.io/commands/bzpopmax/>](https://redis.io/commands/bzpopmax/)
#[must_use]
fn bzpopmax<K, KK, E, K1>(
self,
keys: KK,
timeout: f64,
) -> PreparedCommand<'a, Self, BZpopMinMaxResult<K1, E>>
where
Self: Sized,
K: SingleArg,
KK: SingleArgCollection<K>,
K1: PrimitiveResponse + DeserializeOwned,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(self, cmd("BZPOPMAX").arg(keys).arg(timeout))
}
/// This command is the blocking variant of [`zpopmin`](crate::commands::SortedSetCommands::zpopmin).
///
/// # Return
/// * `None` when no element could be popped and the timeout expired.
/// * The list of tuple with
/// * the first element being the name of the key where a member was popped,
/// * the second element is the popped member itself,
/// * and the third element is the score of the popped element.
///
/// # See Also
/// [<https://redis.io/commands/bzpopmin/>](https://redis.io/commands/bzpopmin/)
#[must_use]
fn bzpopmin<K, KK, E, K1>(
self,
keys: KK,
timeout: f64,
) -> PreparedCommand<'a, Self, BZpopMinMaxResult<K1, E>>
where
Self: Sized,
K: SingleArg,
KK: SingleArgCollection<K>,
K1: PrimitiveResponse + DeserializeOwned,
E: PrimitiveResponse + DeserializeOwned,
{
prepare_command(self, cmd("BZPOPMIN").arg(keys).arg(timeout))
}
/// Debugging command that streams back every command processed by the Redis server.
///
/// # See Also
/// [<https://redis.io/commands/monitor/>](https://redis.io/commands/monitor/)
#[must_use]
#[allow(async_fn_in_trait)]
async fn monitor(self) -> Result<MonitorStream>;
}