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
use crate::{
Result,
client::{Client, PreparedCommand, command_traits::*},
resp::{Command, RespBatchDeserializer},
};
use bytes::Bytes;
use serde::de::DeserializeOwned;
use smallvec::SmallVec;
/// Represents a Redis command pipeline.
pub struct Pipeline<'a> {
client: &'a Client,
commands: Vec<Command>,
forget_flags: SmallVec<[bool; 10]>,
retry_on_error: Option<bool>,
}
impl Pipeline<'_> {
pub(crate) fn new<'a>(client: &'a Client) -> Pipeline<'a> {
Pipeline {
client,
commands: Vec::new(),
forget_flags: SmallVec::new(),
retry_on_error: None,
}
}
pub fn reserve(&mut self, additional: usize) {
self.commands.reserve(additional);
self.forget_flags.reserve(additional);
}
/// Set a flag to override default `retry_on_error` behavior.
///
/// See [Config::retry_on_error](crate::client::Config::retry_on_error)
pub fn retry_on_error(&mut self, retry_on_error: bool) {
self.retry_on_error = Some(retry_on_error);
}
/// Queue a command built with the generic API.
///
/// Built-in commands use [`BatchPreparedCommand::queue`] instead:
/// `pipeline.get::<()>("k").queue()`. The names differ because the calls do:
/// this one takes a command, that one consumes a prepared command.
pub fn queue_command(&mut self, command: impl Into<Command>) {
self.commands.push(command.into());
self.forget_flags.push(false);
}
/// Queue a command built with the generic API and forget its response.
///
/// See [`Self::queue_command`] for why the name differs from
/// [`BatchPreparedCommand::forget`].
pub fn forget_command(&mut self, command: impl Into<Command>) {
self.commands.push(command.into());
self.forget_flags.push(true);
}
/// The name of the one command whose response the caller awaits, when there
/// is exactly one.
///
/// That is the only case where a name reaches the caller: with several
/// responses [`Self::execute`] deserializes the reply as a whole, which
/// belongs to no single command. So one name is taken here, from the command
/// the flags say is awaited, instead of one per reply — the difference being
/// what a pipeline of a thousand commands pays to read none of them.
fn single_awaited_command(commands: &[Command], forget_flags: &[bool]) -> Option<Bytes> {
let mut awaited = forget_flags
.iter()
.enumerate()
.filter(|(_, forget)| !**forget);
match (awaited.next(), awaited.next()) {
(Some((index, _)), None) => commands.get(index).map(Command::name_bytes),
_ => None,
}
}
/// Execute the pipeline by the sending the queued command
/// as a whole batch to the Redis server.
///
/// # Return
/// It is the caller's responsibility to use the right type to cast the server response
/// to the right tuple or collection depending on which command has been
/// [queued](BatchPreparedCommand::queue) or [forgotten](BatchPreparedCommand::forget).
///
/// The most generic type that can be requested as a result is `Vec<resp::Value>`
///
/// # Example
/// ```
/// use rustis::{
/// client::{Client, Pipeline, BatchPreparedCommand},
/// commands::StringCommands,
/// resp::{cmd, Value}, Result,
/// };
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// let client = Client::connect("127.0.0.1:6379").await?;
///
/// let mut pipeline = client.create_pipeline();
/// pipeline.set("key1", "value1").forget();
/// pipeline.set("key2", "value2").forget();
/// pipeline.get::<()>("key1").queue();
/// pipeline.get::<()>("key2").queue();
///
/// let (value1, value2): (String, String) = pipeline.execute().await?;
/// assert_eq!("value1", value1);
/// assert_eq!("value2", value2);
///
/// Ok(())
/// }
/// ```
#[expect(
clippy::arithmetic_side_effects,
reason = "the index advances once per retained result, so it is bounded by the \
flag list it indexes."
)]
pub async fn execute<T: DeserializeOwned>(self) -> Result<T> {
// An empty pipeline never reaches the network layer (no `MessageToReceive`
// is created), so awaiting a result would surface an opaque channel-canceled
// error. Resolve it as an empty batch instead.
if self.commands.is_empty() {
let deserializer = RespBatchDeserializer::new(&[]);
return T::deserialize(&deserializer);
}
let awaited_command = Self::single_awaited_command(&self.commands, &self.forget_flags);
let mut results = self
.client
.internal_send_batch(self.commands, self.retry_on_error)
.await?;
// Forget-flag filtering runs whenever at least one command is forgotten,
// regardless of the batch size: a single forgotten command must have its
// response dropped just like it would in a multi-command batch. When
// nothing is forgotten the whole `retain` pass is skipped.
if self.forget_flags.iter().any(|&forget| forget) {
let mut idx = 0;
results.retain(|_| {
let keep = !self.forget_flags[idx];
idx += 1;
keep
});
}
// A single response deserializes directly as `T` rather than as a
// one-element batch. Peeling it off with `pop` inside the condition
// rather than after it keeps the emptiness of `results` the only thing
// this branch depends on, with no length invariant left to assert.
if results.len() == 1
&& let Some(result) = results.pop()
{
return match (result.to(), awaited_command) {
(Err(e), Some(command)) => Err(e.with_command(command)),
(named, _) => named,
};
}
let deserializer = RespBatchDeserializer::new(&results);
T::deserialize(&deserializer)
}
}
/// Extension trait dedicated to [`PreparedCommand`](crate::client::PreparedCommand)
/// to add specific methods for the [`Pipeline`](crate::client::Pipeline) &
/// the [`Transaction`](crate::client::Transaction) executors
///
/// # The response type is ignored here
///
/// Queuing discards a [`PreparedCommand`](crate::client::PreparedCommand)'s
/// response type. The type on
/// [`Pipeline::execute`](crate::client::Pipeline::execute) decides the decoding.
/// Write `::<()>` on a queued command. Any other type compiles and means nothing.
pub trait BatchPreparedCommand<R = ()> {
/// Queue a command. Its response type is ignored — see the trait docs.
fn queue(self);
/// Queue a command and forget its response.
fn forget(self);
}
impl<'a, R: DeserializeOwned> BatchPreparedCommand
for PreparedCommand<'a, &'a mut Pipeline<'_>, R>
{
/// Queue a command.
#[inline]
fn queue(self) {
self.executor.queue_command(self.command)
}
/// Queue a command and forget its response.
#[inline]
fn forget(self) {
self.executor.forget_command(self.command)
}
}
impl_pipeline_command_traits!(Pipeline<'_>);