1use std::any::Any;
28use std::future::Future;
29use std::sync::Arc;
30
31use redis_tower_core::{Command, Frame, RedisConnection, RedisError};
32use tokio::sync::Mutex;
33
34use crate::client::RedisClient;
35
36pub trait PipelineExecutor {
50 fn execute_pipeline(
52 &mut self,
53 frames: Vec<Frame>,
54 ) -> impl Future<Output = Result<Vec<Frame>, RedisError>> + Send;
55}
56
57impl PipelineExecutor for RedisConnection {
58 fn execute_pipeline(
59 &mut self,
60 frames: Vec<Frame>,
61 ) -> impl Future<Output = Result<Vec<Frame>, RedisError>> + Send {
62 RedisConnection::execute_pipeline(self, frames)
63 }
64}
65
66impl<C: PipelineExecutor + Send> PipelineExecutor for Arc<Mutex<C>> {
67 fn execute_pipeline(
68 &mut self,
69 frames: Vec<Frame>,
70 ) -> impl Future<Output = Result<Vec<Frame>, RedisError>> + Send {
71 let arc = Arc::clone(self);
72 async move { arc.lock().await.execute_pipeline(frames).await }
73 }
74}
75
76impl PipelineExecutor for RedisClient {
77 fn execute_pipeline(
78 &mut self,
79 frames: Vec<Frame>,
80 ) -> impl Future<Output = Result<Vec<Frame>, RedisError>> + Send {
81 let arc = Arc::clone(&self.inner);
82 async move { arc.lock().await.execute_pipeline(frames).await }
83 }
84}
85
86pub(crate) type ResponseParser =
88 Box<dyn FnOnce(Frame) -> Result<Box<dyn Any + Send>, RedisError> + Send>;
89
90struct PipelineEntry {
92 frame: Frame,
93 parser: ResponseParser,
94}
95
96pub struct Pipeline {
127 entries: Vec<PipelineEntry>,
128}
129
130impl Pipeline {
131 pub fn new() -> Self {
133 Self {
134 entries: Vec::new(),
135 }
136 }
137
138 pub fn push<Cmd: Command + 'static>(mut self, cmd: Cmd) -> Self {
140 let frame = cmd.to_frame();
141 let parser = Box::new(
142 move |response: Frame| -> Result<Box<dyn Any + Send>, RedisError> {
143 let result = cmd.parse_response(response)?;
144 Ok(Box::new(result))
145 },
146 );
147 self.entries.push(PipelineEntry { frame, parser });
148 self
149 }
150
151 pub async fn execute<E: PipelineExecutor>(
157 self,
158 conn: &mut E,
159 ) -> Result<PipelineResults, RedisError> {
160 let frames: Vec<Frame> = self.entries.iter().map(|e| e.frame.clone()).collect();
161 let responses = conn.execute_pipeline(frames).await?;
162
163 let mut results = Vec::with_capacity(responses.len());
164 for (entry, response) in self.entries.into_iter().zip(responses) {
165 if let Frame::Error(ref e) = response {
167 results.push(Err(RedisError::Redis(
168 String::from_utf8_lossy(e).into_owned(),
169 )));
170 } else {
171 results.push((entry.parser)(response));
172 }
173 }
174
175 Ok(PipelineResults { results })
176 }
177
178 pub fn len(&self) -> usize {
180 self.entries.len()
181 }
182
183 pub fn is_empty(&self) -> bool {
185 self.entries.is_empty()
186 }
187}
188
189impl Default for Pipeline {
190 fn default() -> Self {
191 Self::new()
192 }
193}
194
195pub struct PipelineResults {
199 results: Vec<Result<Box<dyn Any + Send>, RedisError>>,
200}
201
202impl PipelineResults {
203 pub(crate) fn from_raw(results: Vec<Result<Box<dyn Any + Send>, RedisError>>) -> Self {
205 Self { results }
206 }
207
208 pub fn get<T: Send + 'static>(&self, index: usize) -> Result<&T, RedisError> {
213 match self.results.get(index) {
214 Some(Ok(boxed)) => boxed.downcast_ref::<T>().ok_or(RedisError::TypeMismatch {
215 expected: std::any::type_name::<T>(),
216 }),
217 Some(Err(e)) => Err(RedisError::Redis(e.to_string())),
218 None => Err(RedisError::IndexOutOfBounds {
219 index,
220 len: self.results.len(),
221 }),
222 }
223 }
224
225 pub fn take<T: Send + 'static>(&mut self, index: usize) -> Result<T, RedisError> {
227 match self.results.get_mut(index) {
228 Some(slot) => {
229 let result = std::mem::replace(
231 slot,
232 Err(RedisError::TypeMismatch {
233 expected: "already taken",
234 }),
235 );
236 match result {
237 Ok(boxed) => {
238 boxed
239 .downcast::<T>()
240 .map(|b| *b)
241 .map_err(|_| RedisError::TypeMismatch {
242 expected: std::any::type_name::<T>(),
243 })
244 }
245 Err(e) => Err(e),
246 }
247 }
248 None => Err(RedisError::IndexOutOfBounds {
249 index,
250 len: self.results.len(),
251 }),
252 }
253 }
254
255 pub fn len(&self) -> usize {
257 self.results.len()
258 }
259
260 pub fn is_empty(&self) -> bool {
262 self.results.is_empty()
263 }
264
265 pub fn has_errors(&self) -> bool {
267 self.results.iter().any(|r| r.is_err())
268 }
269
270 pub fn into_typed_vec<T: Send + 'static>(self) -> Result<Vec<T>, RedisError> {
275 self.results
276 .into_iter()
277 .map(|r| match r {
278 Ok(boxed) => {
279 boxed
280 .downcast::<T>()
281 .map(|b| *b)
282 .map_err(|_| RedisError::TypeMismatch {
283 expected: std::any::type_name::<T>(),
284 })
285 }
286 Err(e) => Err(e),
287 })
288 .collect()
289 }
290}
291
292#[cfg(test)]
293mod tests {
294 use super::*;
295
296 fn ok<T: Send + 'static>(value: T) -> Result<Box<dyn Any + Send>, RedisError> {
297 Ok(Box::new(value))
298 }
299
300 fn err() -> Result<Box<dyn Any + Send>, RedisError> {
301 Err(RedisError::Redis("boom".to_string()))
302 }
303
304 #[test]
305 fn has_errors_false_for_all_success() {
306 let results = PipelineResults::from_raw(vec![ok(1i64), ok(2i64)]);
307 assert!(!results.has_errors());
308 }
309
310 #[test]
311 fn has_errors_true_when_any_error() {
312 let results = PipelineResults::from_raw(vec![ok(1i64), err()]);
313 assert!(results.has_errors());
314 }
315
316 #[test]
317 fn into_typed_vec_homogeneous_succeeds() {
318 let results = PipelineResults::from_raw(vec![ok(1i64), ok(2i64), ok(3i64)]);
319 let vec = results.into_typed_vec::<i64>().unwrap();
320 assert_eq!(vec, vec![1, 2, 3]);
321 }
322
323 #[test]
324 fn into_typed_vec_errors_when_any_slot_errors() {
325 let results = PipelineResults::from_raw(vec![ok(1i64), err()]);
326 assert!(results.into_typed_vec::<i64>().is_err());
327 }
328
329 #[test]
330 fn get_out_of_bounds_returns_index_out_of_bounds() {
331 let results = PipelineResults::from_raw(vec![ok(1i64)]);
332 match results.get::<i64>(5) {
333 Err(RedisError::IndexOutOfBounds { index, len }) => {
334 assert_eq!(index, 5);
335 assert_eq!(len, 1);
336 }
337 other => panic!("expected IndexOutOfBounds, got {other:?}"),
338 }
339 }
340
341 #[test]
342 fn take_out_of_bounds_returns_index_out_of_bounds() {
343 let mut results = PipelineResults::from_raw(vec![ok(1i64)]);
344 match results.take::<i64>(5) {
345 Err(RedisError::IndexOutOfBounds { index, len }) => {
346 assert_eq!(index, 5);
347 assert_eq!(len, 1);
348 }
349 other => panic!("expected IndexOutOfBounds, got {other:?}"),
350 }
351 }
352
353 struct MockPipelineConn {
355 count: usize,
356 }
357
358 impl PipelineExecutor for MockPipelineConn {
359 fn execute_pipeline(
360 &mut self,
361 frames: Vec<Frame>,
362 ) -> impl Future<Output = Result<Vec<Frame>, RedisError>> + Send {
363 let responses: Vec<Frame> = frames
364 .iter()
365 .map(|_| Frame::SimpleString(bytes::Bytes::from("OK")))
366 .collect();
367 self.count += frames.len();
368 async move { Ok(responses) }
369 }
370 }
371
372 #[tokio::test]
373 async fn pipeline_execute_accepts_pipeline_executor() {
374 use redis_tower_commands::Set;
375
376 let mut conn = MockPipelineConn { count: 0 };
377 let results = Pipeline::new()
378 .push(Set::new("a", "1"))
379 .push(Set::new("b", "2"))
380 .execute(&mut conn)
381 .await
382 .unwrap();
383 assert_eq!(results.len(), 2);
384 assert_eq!(conn.count, 2);
385 }
386
387 #[tokio::test]
388 async fn pipeline_execute_accepts_arc_mutex_executor() {
389 use redis_tower_commands::Set;
390
391 let conn = Arc::new(Mutex::new(MockPipelineConn { count: 0 }));
392 let mut executor = Arc::clone(&conn);
393 let results = Pipeline::new()
394 .push(Set::new("a", "1"))
395 .execute(&mut executor)
396 .await
397 .unwrap();
398 assert_eq!(results.len(), 1);
399 assert_eq!(conn.lock().await.count, 1);
400 }
401}