redis-oxide 0.2.3

High-performance async Redis client for Rust with automatic cluster support, multiplexing, and advanced features
Documentation
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//! Transaction support for Redis
//!
//! This module provides functionality for Redis transactions using MULTI/EXEC/WATCH/DISCARD.
//! Redis transactions allow you to execute a group of commands atomically.
//!
//! # Examples
//!
//! ```no_run
//! use redis_oxide::{Client, ConnectionConfig};
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = ConnectionConfig::new("redis://localhost:6379");
//! let client = Client::connect(config).await?;
//!
//! // Start a transaction
//! let mut transaction = client.transaction().await?;
//!
//! // Add commands to the transaction
//! transaction.set("key1", "value1");
//! transaction.set("key2", "value2");
//! transaction.incr("counter");
//!
//! // Execute the transaction
//! let results = transaction.exec().await?;
//! println!("Transaction results: {:?}", results);
//! # Ok(())
//! # }
//! ```

use crate::commands::Command;
use crate::core::{
    error::{RedisError, RedisResult},
    value::RespValue,
};
use std::collections::VecDeque;
use std::sync::Arc;
use tokio::sync::Mutex;

/// A Redis transaction that executes commands atomically
pub struct Transaction {
    commands: VecDeque<Box<dyn TransactionCommand>>,
    connection: Arc<Mutex<dyn TransactionExecutor + Send + Sync>>,
    watched_keys: Vec<String>,
    is_started: bool,
}

/// Trait for commands that can be used in transactions
pub trait TransactionCommand: Send + Sync {
    /// Get the command name
    fn name(&self) -> &str;

    /// Get the command arguments
    fn args(&self) -> Vec<RespValue>;

    /// Get the key(s) involved in this command (for WATCH)
    fn key(&self) -> Option<String>;
}

/// Trait for executing transactions
#[async_trait::async_trait]
pub trait TransactionExecutor {
    /// Start a transaction with MULTI
    async fn multi(&mut self) -> RedisResult<()>;

    /// Execute a command in the transaction
    async fn queue_command(&mut self, command: Box<dyn TransactionCommand>) -> RedisResult<()>;

    /// Execute the transaction with EXEC
    async fn exec(&mut self) -> RedisResult<Vec<RespValue>>;

    /// Discard the transaction with DISCARD
    async fn discard(&mut self) -> RedisResult<()>;

    /// Watch keys for changes
    async fn watch(&mut self, keys: Vec<String>) -> RedisResult<()>;

    /// Unwatch all keys
    async fn unwatch(&mut self) -> RedisResult<()>;
}

impl Transaction {
    /// Create a new transaction
    pub fn new(connection: Arc<Mutex<dyn TransactionExecutor + Send + Sync>>) -> Self {
        Self {
            commands: VecDeque::new(),
            connection,
            watched_keys: Vec::new(),
            is_started: false,
        }
    }

    /// Watch keys for changes before starting the transaction
    ///
    /// If any watched key is modified before EXEC, the transaction will be discarded.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let mut transaction = client.transaction().await?;
    ///
    /// // Watch keys before starting transaction
    /// transaction.watch(vec!["balance".to_string(), "account".to_string()]).await?;
    ///
    /// // Add commands
    /// transaction.set("balance", "100");
    /// transaction.incr("account");
    ///
    /// // Execute - will fail if watched keys were modified
    /// let results = transaction.exec().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn watch(&mut self, keys: Vec<String>) -> RedisResult<()> {
        if self.is_started {
            return Err(RedisError::Protocol("Cannot WATCH after MULTI".to_string()));
        }

        let mut connection = self.connection.lock().await;
        connection.watch(keys.clone()).await?;
        self.watched_keys.extend(keys);
        Ok(())
    }

    /// Unwatch all previously watched keys
    pub async fn unwatch(&mut self) -> RedisResult<()> {
        let mut connection = self.connection.lock().await;
        connection.unwatch().await?;
        self.watched_keys.clear();
        Ok(())
    }

    /// Add a command to the transaction
    pub fn add_command(&mut self, command: Box<dyn TransactionCommand>) -> &mut Self {
        self.commands.push_back(command);
        self
    }

    /// Add a SET command to the transaction
    pub fn set(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
        use crate::commands::SetCommand;
        let cmd = SetCommand::new(key.into(), value.into());
        self.add_command(Box::new(cmd))
    }

    /// Add a GET command to the transaction
    pub fn get(&mut self, key: impl Into<String>) -> &mut Self {
        use crate::commands::GetCommand;
        let cmd = GetCommand::new(key.into());
        self.add_command(Box::new(cmd))
    }

    /// Add a DEL command to the transaction
    pub fn del(&mut self, keys: Vec<String>) -> &mut Self {
        use crate::commands::DelCommand;
        let cmd = DelCommand::new(keys);
        self.add_command(Box::new(cmd))
    }

    /// Add an INCR command to the transaction
    pub fn incr(&mut self, key: impl Into<String>) -> &mut Self {
        use crate::commands::IncrCommand;
        let cmd = IncrCommand::new(key.into());
        self.add_command(Box::new(cmd))
    }

    /// Add a DECR command to the transaction
    pub fn decr(&mut self, key: impl Into<String>) -> &mut Self {
        use crate::commands::DecrCommand;
        let cmd = DecrCommand::new(key.into());
        self.add_command(Box::new(cmd))
    }

    /// Add an INCRBY command to the transaction
    pub fn incr_by(&mut self, key: impl Into<String>, increment: i64) -> &mut Self {
        use crate::commands::IncrByCommand;
        let cmd = IncrByCommand::new(key.into(), increment);
        self.add_command(Box::new(cmd))
    }

    /// Add a DECRBY command to the transaction
    pub fn decr_by(&mut self, key: impl Into<String>, decrement: i64) -> &mut Self {
        use crate::commands::DecrByCommand;
        let cmd = DecrByCommand::new(key.into(), decrement);
        self.add_command(Box::new(cmd))
    }

    /// Add an EXISTS command to the transaction
    pub fn exists(&mut self, keys: Vec<String>) -> &mut Self {
        use crate::commands::ExistsCommand;
        let cmd = ExistsCommand::new(keys);
        self.add_command(Box::new(cmd))
    }

    /// Add an EXPIRE command to the transaction
    pub fn expire(&mut self, key: impl Into<String>, seconds: std::time::Duration) -> &mut Self {
        use crate::commands::ExpireCommand;
        let cmd = ExpireCommand::new(key.into(), seconds);
        self.add_command(Box::new(cmd))
    }

    /// Add a TTL command to the transaction
    pub fn ttl(&mut self, key: impl Into<String>) -> &mut Self {
        use crate::commands::TtlCommand;
        let cmd = TtlCommand::new(key.into());
        self.add_command(Box::new(cmd))
    }

    // Hash commands

    /// Add an HGET command to the transaction
    pub fn hget(&mut self, key: impl Into<String>, field: impl Into<String>) -> &mut Self {
        use crate::commands::HGetCommand;
        let cmd = HGetCommand::new(key.into(), field.into());
        self.add_command(Box::new(cmd))
    }

    /// Add an HSET command to the transaction
    pub fn hset(
        &mut self,
        key: impl Into<String>,
        field: impl Into<String>,
        value: impl Into<String>,
    ) -> &mut Self {
        use crate::commands::HSetCommand;
        let cmd = HSetCommand::new(key.into(), field.into(), value.into());
        self.add_command(Box::new(cmd))
    }

    /// Get the number of commands in the transaction
    #[must_use]
    pub fn len(&self) -> usize {
        self.commands.len()
    }

    /// Check if the transaction is empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.commands.is_empty()
    }

    /// Clear all commands from the transaction
    pub fn clear(&mut self) {
        self.commands.clear();
    }

    /// Execute the transaction
    ///
    /// This sends MULTI, queues all commands, and then executes them with EXEC.
    /// Returns the results of all commands in the same order they were added.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The transaction is empty
    /// - Network communication fails
    /// - Any watched key was modified (returns empty result)
    /// - Any command in the transaction fails
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let mut transaction = client.transaction().await?;
    /// transaction.set("key1", "value1");
    /// transaction.get("key1");
    ///
    /// let results = transaction.exec().await?;
    /// assert_eq!(results.len(), 2);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn exec(&mut self) -> RedisResult<Vec<RespValue>> {
        if self.commands.is_empty() {
            return Err(RedisError::Protocol("Transaction is empty".to_string()));
        }

        let mut connection = self.connection.lock().await;

        // Start transaction if not already started
        if !self.is_started {
            connection.multi().await?;
            self.is_started = true;
        }

        // Queue all commands
        let commands: Vec<Box<dyn TransactionCommand>> = self.commands.drain(..).collect();
        for command in commands {
            connection.queue_command(command).await?;
        }

        // Execute the transaction
        let results = connection.exec().await?;
        self.is_started = false;

        Ok(results)
    }

    /// Discard the transaction
    ///
    /// This cancels the transaction and discards all queued commands.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let mut transaction = client.transaction().await?;
    /// transaction.set("key1", "value1");
    /// transaction.set("key2", "value2");
    ///
    /// // Cancel the transaction
    /// transaction.discard().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn discard(&mut self) -> RedisResult<()> {
        let mut connection = self.connection.lock().await;
        connection.discard().await?;
        self.commands.clear();
        self.is_started = false;
        Ok(())
    }
}

/// Transaction result wrapper for easier handling
#[derive(Debug, Clone)]
pub struct TransactionResult {
    results: Vec<RespValue>,
    index: usize,
}

impl TransactionResult {
    /// Create a new transaction result
    #[must_use]
    pub fn new(results: Vec<RespValue>) -> Self {
        Self { results, index: 0 }
    }

    /// Get the next result from the transaction
    ///
    /// # Errors
    ///
    /// Returns an error if there are no more results or type conversion fails.
    pub fn next<T>(&mut self) -> RedisResult<T>
    where
        T: TryFrom<RespValue>,
        T::Error: Into<RedisError>,
    {
        if self.index >= self.results.len() {
            return Err(RedisError::Protocol(
                "No more results in transaction".to_string(),
            ));
        }

        let result = self.results[self.index].clone();
        self.index += 1;

        T::try_from(result).map_err(Into::into)
    }

    /// Get a result at a specific index
    ///
    /// # Errors
    ///
    /// Returns an error if the index is out of bounds or type conversion fails.
    pub fn get<T>(&self, index: usize) -> RedisResult<T>
    where
        T: TryFrom<RespValue>,
        T::Error: Into<RedisError>,
    {
        if index >= self.results.len() {
            return Err(RedisError::Protocol(format!(
                "Index {} out of bounds",
                index
            )));
        }

        let result = self.results[index].clone();
        T::try_from(result).map_err(Into::into)
    }

    /// Get the number of results
    #[must_use]
    pub fn len(&self) -> usize {
        self.results.len()
    }

    /// Check if there are no results
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.results.is_empty()
    }

    /// Get all results as a vector
    #[must_use]
    pub fn into_results(self) -> Vec<RespValue> {
        self.results
    }
}

// Implement TransactionCommand for all command types
impl TransactionCommand for crate::commands::GetCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        Some(self.keys()[0].iter().map(|&b| b as char).collect())
    }
}

impl TransactionCommand for crate::commands::SetCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        Some(self.keys()[0].iter().map(|&b| b as char).collect())
    }
}

impl TransactionCommand for crate::commands::DelCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        if let Some(first_key) = self.keys().first() {
            Some(first_key.iter().map(|&b| b as char).collect())
        } else {
            None
        }
    }
}

impl TransactionCommand for crate::commands::IncrCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        Some(self.keys()[0].iter().map(|&b| b as char).collect())
    }
}

impl TransactionCommand for crate::commands::DecrCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        Some(self.keys()[0].iter().map(|&b| b as char).collect())
    }
}

impl TransactionCommand for crate::commands::IncrByCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        Some(self.keys()[0].iter().map(|&b| b as char).collect())
    }
}

impl TransactionCommand for crate::commands::DecrByCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        Some(self.keys()[0].iter().map(|&b| b as char).collect())
    }
}

impl TransactionCommand for crate::commands::ExistsCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        if let Some(first_key) = self.keys().first() {
            Some(first_key.iter().map(|&b| b as char).collect())
        } else {
            None
        }
    }
}

impl TransactionCommand for crate::commands::ExpireCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        Some(self.keys()[0].iter().map(|&b| b as char).collect())
    }
}

impl TransactionCommand for crate::commands::TtlCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        Some(self.keys()[0].iter().map(|&b| b as char).collect())
    }
}

impl TransactionCommand for crate::commands::HGetCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        Some(self.keys()[0].iter().map(|&b| b as char).collect())
    }
}

impl TransactionCommand for crate::commands::HSetCommand {
    fn name(&self) -> &str {
        self.command_name()
    }

    fn args(&self) -> Vec<RespValue> {
        <Self as Command>::args(self)
    }

    fn key(&self) -> Option<String> {
        Some(self.keys()[0].iter().map(|&b| b as char).collect())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use tokio::sync::Mutex;

    struct MockTransactionExecutor {
        commands: Vec<String>,
        multi_called: bool,
        exec_called: bool,
    }

    impl MockTransactionExecutor {
        fn new() -> Self {
            Self {
                commands: Vec::new(),
                multi_called: false,
                exec_called: false,
            }
        }
    }

    #[async_trait::async_trait]
    impl TransactionExecutor for MockTransactionExecutor {
        async fn multi(&mut self) -> RedisResult<()> {
            self.multi_called = true;
            Ok(())
        }

        async fn queue_command(&mut self, command: Box<dyn TransactionCommand>) -> RedisResult<()> {
            self.commands.push(command.name().to_string());
            Ok(())
        }

        async fn exec(&mut self) -> RedisResult<Vec<RespValue>> {
            self.exec_called = true;
            let mut results = Vec::new();
            for _ in 0..self.commands.len() {
                results.push(RespValue::SimpleString("OK".to_string()));
            }
            Ok(results)
        }

        async fn discard(&mut self) -> RedisResult<()> {
            self.commands.clear();
            self.multi_called = false;
            Ok(())
        }

        async fn watch(&mut self, _keys: Vec<String>) -> RedisResult<()> {
            Ok(())
        }

        async fn unwatch(&mut self) -> RedisResult<()> {
            Ok(())
        }
    }

    #[tokio::test]
    async fn test_transaction_creation() {
        let executor = MockTransactionExecutor::new();
        let transaction = Transaction::new(Arc::new(Mutex::new(executor)));

        assert!(transaction.is_empty());
        assert_eq!(transaction.len(), 0);
    }

    #[tokio::test]
    async fn test_transaction_add_commands() {
        let executor = MockTransactionExecutor::new();
        let mut transaction = Transaction::new(Arc::new(Mutex::new(executor)));

        transaction.set("key1", "value1");
        transaction.get("key1");

        assert_eq!(transaction.len(), 2);
        assert!(!transaction.is_empty());
    }

    #[tokio::test]
    async fn test_transaction_exec() {
        let executor = MockTransactionExecutor::new();
        let mut transaction = Transaction::new(Arc::new(Mutex::new(executor)));

        transaction.set("key1", "value1");
        transaction.get("key1");

        let results = transaction.exec().await.unwrap();
        assert_eq!(results.len(), 2);
        assert!(transaction.is_empty()); // Commands should be consumed
    }

    #[tokio::test]
    async fn test_transaction_discard() {
        let executor = MockTransactionExecutor::new();
        let mut transaction = Transaction::new(Arc::new(Mutex::new(executor)));

        transaction.set("key1", "value1");
        transaction.get("key1");
        assert_eq!(transaction.len(), 2);

        transaction.discard().await.unwrap();
        assert!(transaction.is_empty());
    }

    #[tokio::test]
    async fn test_transaction_result() {
        let results = vec![
            RespValue::SimpleString("OK".to_string()),
            RespValue::BulkString(b"value1".to_vec().into()),
            RespValue::Integer(42),
        ];

        let mut transaction_result = TransactionResult::new(results);

        assert_eq!(transaction_result.len(), 3);
        assert!(!transaction_result.is_empty());

        let first: String = transaction_result.next().unwrap();
        assert_eq!(first, "OK");

        let second: String = transaction_result.get(1).unwrap();
        assert_eq!(second, "value1");
    }
}