preview-glide-rust 0.2.0

Valkey GLIDE — native Rust client for Valkey and Redis OSS, built on glide-core.
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
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
//! Sorted-set commands. Mirrors Python's sorted-set command surface.
#![allow(clippy::type_complexity)]

use crate::commands::options::Limit;
use crate::error::Result;
use crate::executor::CommandExecutor;
use crate::value;
use async_trait::async_trait;
use bytes::Bytes;
use redis::{Cmd, ToRedisArgs};

/// A score boundary for `ZRANGEBYSCORE`/`ZCOUNT` etc.
///
/// Mirrors Python `ScoreBoundary` + `InfBound`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ScoreBound {
    /// Negative infinity (`-inf`).
    NegativeInfinity,
    /// Positive infinity (`+inf`).
    PositiveInfinity,
    /// Inclusive bound at `value`.
    Inclusive(f64),
    /// Exclusive bound at `value` (`(value`).
    Exclusive(f64),
}

impl ScoreBound {
    fn to_arg(self) -> String {
        match self {
            ScoreBound::NegativeInfinity => "-inf".to_string(),
            ScoreBound::PositiveInfinity => "+inf".to_string(),
            ScoreBound::Inclusive(v) => v.to_string(),
            ScoreBound::Exclusive(v) => format!("({v}"),
        }
    }
}

/// A lexicographical boundary for `ZRANGEBYLEX`/`ZLEXCOUNT`.
///
/// Mirrors Python `LexBoundary` + `InfBound`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LexBound {
    /// Smallest possible value (`-`).
    NegativeInfinity,
    /// Largest possible value (`+`).
    PositiveInfinity,
    /// Inclusive bound (`[value`).
    Inclusive(Vec<u8>),
    /// Exclusive bound (`(value`).
    Exclusive(Vec<u8>),
}

impl LexBound {
    fn to_arg(&self) -> Vec<u8> {
        match self {
            LexBound::NegativeInfinity => b"-".to_vec(),
            LexBound::PositiveInfinity => b"+".to_vec(),
            LexBound::Inclusive(v) => {
                let mut out = vec![b'['];
                out.extend_from_slice(v);
                out
            }
            LexBound::Exclusive(v) => {
                let mut out = vec![b'('];
                out.extend_from_slice(v);
                out
            }
        }
    }
}

/// Aggregation mode for `ZUNIONSTORE`/`ZINTERSTORE`.
///
/// Mirrors Python `AggregationType`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AggregationType {
    /// Sum the scores.
    Sum,
    /// Take the minimum score.
    Min,
    /// Take the maximum score.
    Max,
}

impl AggregationType {
    fn as_arg(&self) -> &'static str {
        match self {
            AggregationType::Sum => "SUM",
            AggregationType::Min => "MIN",
            AggregationType::Max => "MAX",
        }
    }
}

/// Sorted-set commands (`ZADD`, `ZRANGE`, `ZSCORE`, ...).
#[async_trait]
pub trait SortedSetCommands: CommandExecutor {
    /// Increment the score of `member` by `increment` (`ZADD ... INCR`).
    ///
    /// This is the plain (unconditional) `INCR` form, so the result is always
    /// `Some(new_score)`. The `Option` return is kept for the conditional
    /// forms of the command: combined with `NX`/`XX`/`GT`/`LT` (reachable via
    /// [`crate::CustomCommand::custom_command`]) the server replies nil when
    /// the condition suppresses the update.
    async fn zadd_incr<K: ToRedisArgs + Send, M: ToRedisArgs + Send>(
        &self,
        key: K,
        member: M,
        increment: f64,
    ) -> Result<Option<f64>> {
        let mut cmd = Cmd::new();
        cmd.arg("ZADD")
            .arg(key)
            .arg("INCR")
            .arg(increment)
            .arg(member);
        value::to_opt_f64(self.execute_command(cmd, None).await?)
    }

    /// Get the rank of `member` with its score, low to high (`ZRANK ... WITHSCORE`).
    async fn zrank_withscore<K: ToRedisArgs + Send, M: ToRedisArgs + Send>(
        &self,
        key: K,
        member: M,
    ) -> Result<Option<(i64, f64)>> {
        let mut cmd = Cmd::new();
        cmd.arg("ZRANK").arg(key).arg(member).arg("WITHSCORE");
        parse_rank_withscore(self.execute_command(cmd, None).await?)
    }

    /// Get the rank of `member` with its score, high to low
    /// (`ZREVRANK ... WITHSCORE`).
    async fn zrevrank_withscore<K: ToRedisArgs + Send, M: ToRedisArgs + Send>(
        &self,
        key: K,
        member: M,
    ) -> Result<Option<(i64, f64)>> {
        let mut cmd = Cmd::new();
        cmd.arg("ZREVRANK").arg(key).arg(member).arg("WITHSCORE");
        parse_rank_withscore(self.execute_command(cmd, None).await?)
    }

    #[doc(hidden)]
    async fn bzpop<K: ToRedisArgs + Send + Sync>(
        &self,
        op: &'static str,
        keys: &[K],
        timeout: f64,
    ) -> Result<Option<(Bytes, Bytes, f64)>> {
        let mut cmd = Cmd::new();
        cmd.arg(op);
        for k in keys {
            cmd.arg(k);
        }
        cmd.arg(timeout);
        match self.execute_command(cmd, None).await? {
            redis::Value::Nil => Ok(None),
            redis::Value::Array(mut items) if items.len() == 3 => {
                let score = value::to_f64(items.pop().unwrap())?;
                let member = value::to_bytes(items.pop().unwrap())?;
                let key = value::to_bytes(items.pop().unwrap())?;
                Ok(Some((key, member, score)))
            }
            other => Err(crate::error::GlideError::Request(format!(
                "unexpected blocking zpop reply: {other:?}"
            ))),
        }
    }

    /// Store a range of a sorted set into `destination` (`ZRANGESTORE` by index).
    /// Returns the number of elements stored.
    async fn zrangestore_by_index<D: ToRedisArgs + Send, S: ToRedisArgs + Send>(
        &self,
        destination: D,
        source: S,
        start: i64,
        stop: i64,
        rev: bool,
    ) -> Result<i64> {
        let mut cmd = Cmd::new();
        cmd.arg("ZRANGESTORE")
            .arg(destination)
            .arg(source)
            .arg(start)
            .arg(stop);
        if rev {
            cmd.arg("REV");
        }
        value::to_i64(self.execute_command(cmd, None).await?)
    }

    /// Store into `destination` the members of the sorted set `source` whose
    /// scores are within `[min, max]` (`ZRANGESTORE ... BYSCORE`), returning the
    /// number of elements stored.
    ///
    /// When `rev` is `true` the range is interpreted in reverse (highest scores
    /// first); the bounds are emitted in the order the server requires for `REV`.
    /// `limit` applies an optional `LIMIT offset count`.
    async fn zrangestore_by_score<D: ToRedisArgs + Send, S: ToRedisArgs + Send>(
        &self,
        destination: D,
        source: S,
        min: ScoreBound,
        max: ScoreBound,
        rev: bool,
        limit: Option<Limit>,
    ) -> Result<i64> {
        // For a reverse range the server expects the high bound first.
        let (first, second) = if rev { (max, min) } else { (min, max) };
        let mut cmd = Cmd::new();
        cmd.arg("ZRANGESTORE")
            .arg(destination)
            .arg(source)
            .arg(first.to_arg())
            .arg(second.to_arg())
            .arg("BYSCORE");
        if rev {
            cmd.arg("REV");
        }
        if let Some(limit) = limit {
            cmd.arg("LIMIT").arg(limit.offset).arg(limit.count);
        }
        value::to_i64(self.execute_command(cmd, None).await?)
    }

    /// Store into `destination` the members of the sorted set `source` whose
    /// lexicographical values are within `[min, max]`
    /// (`ZRANGESTORE ... BYLEX`), returning the number of elements stored.
    ///
    /// When `rev` is `true` the range is interpreted in reverse; the bounds are
    /// emitted in the order the server requires for `REV`. `limit` applies an
    /// optional `LIMIT offset count`.
    async fn zrangestore_by_lex<D: ToRedisArgs + Send, S: ToRedisArgs + Send>(
        &self,
        destination: D,
        source: S,
        min: &LexBound,
        max: &LexBound,
        rev: bool,
        limit: Option<Limit>,
    ) -> Result<i64> {
        let (first, second) = if rev { (max, min) } else { (min, max) };
        let mut cmd = Cmd::new();
        cmd.arg("ZRANGESTORE")
            .arg(destination)
            .arg(source)
            .arg(first.to_arg())
            .arg(second.to_arg())
            .arg("BYLEX");
        if rev {
            cmd.arg("REV");
        }
        if let Some(limit) = limit {
            cmd.arg("LIMIT").arg(limit.offset).arg(limit.count);
        }
        value::to_i64(self.execute_command(cmd, None).await?)
    }

    /// Compute the difference of the given sorted sets (`ZDIFF`).
    async fn zdiff<K: ToRedisArgs + Send + Sync>(&self, keys: &[K]) -> Result<Vec<Bytes>> {
        let mut cmd = Cmd::new();
        cmd.arg("ZDIFF").arg(keys.len());
        for k in keys {
            cmd.arg(k);
        }
        collect_bytes(self.execute_command(cmd, None).await?)
    }

    /// Compute the difference of the given sorted sets with scores
    /// (`ZDIFF ... WITHSCORES`).
    async fn zdiff_withscores<K: ToRedisArgs + Send + Sync>(
        &self,
        keys: &[K],
    ) -> Result<Vec<(Bytes, f64)>> {
        let mut cmd = Cmd::new();
        cmd.arg("ZDIFF").arg(keys.len());
        for k in keys {
            cmd.arg(k);
        }
        cmd.arg("WITHSCORES");
        collect_member_scores(self.execute_command(cmd, None).await?)
    }

    /// Store the difference of the given sorted sets into `destination`
    /// (`ZDIFFSTORE`).
    async fn zdiffstore<D: ToRedisArgs + Send, K: ToRedisArgs + Send + Sync>(
        &self,
        destination: D,
        keys: &[K],
    ) -> Result<i64> {
        let mut cmd = Cmd::new();
        cmd.arg("ZDIFFSTORE").arg(destination).arg(keys.len());
        for k in keys {
            cmd.arg(k);
        }
        value::to_i64(self.execute_command(cmd, None).await?)
    }

    /// Compute the union of the given sorted sets (`ZUNION`).
    async fn zunion<K: ToRedisArgs + Send + Sync>(
        &self,
        keys: &[K],
        aggregate: Option<AggregationType>,
    ) -> Result<Vec<Bytes>> {
        let mut cmd = Cmd::new();
        cmd.arg("ZUNION").arg(keys.len());
        for k in keys {
            cmd.arg(k);
        }
        if let Some(agg) = aggregate {
            cmd.arg("AGGREGATE").arg(agg.as_arg());
        }
        collect_bytes(self.execute_command(cmd, None).await?)
    }

    /// Compute the union of the given sorted sets with scores
    /// (`ZUNION ... WITHSCORES`).
    async fn zunion_withscores<K: ToRedisArgs + Send + Sync>(
        &self,
        keys: &[K],
        aggregate: Option<AggregationType>,
    ) -> Result<Vec<(Bytes, f64)>> {
        let mut cmd = Cmd::new();
        cmd.arg("ZUNION").arg(keys.len());
        for k in keys {
            cmd.arg(k);
        }
        if let Some(agg) = aggregate {
            cmd.arg("AGGREGATE").arg(agg.as_arg());
        }
        cmd.arg("WITHSCORES");
        collect_member_scores(self.execute_command(cmd, None).await?)
    }

    /// Compute the intersection of the given sorted sets (`ZINTER`).
    async fn zinter<K: ToRedisArgs + Send + Sync>(
        &self,
        keys: &[K],
        aggregate: Option<AggregationType>,
    ) -> Result<Vec<Bytes>> {
        let mut cmd = Cmd::new();
        cmd.arg("ZINTER").arg(keys.len());
        for k in keys {
            cmd.arg(k);
        }
        if let Some(agg) = aggregate {
            cmd.arg("AGGREGATE").arg(agg.as_arg());
        }
        collect_bytes(self.execute_command(cmd, None).await?)
    }

    /// Compute the intersection of the given sorted sets with scores
    /// (`ZINTER ... WITHSCORES`).
    async fn zinter_withscores<K: ToRedisArgs + Send + Sync>(
        &self,
        keys: &[K],
        aggregate: Option<AggregationType>,
    ) -> Result<Vec<(Bytes, f64)>> {
        let mut cmd = Cmd::new();
        cmd.arg("ZINTER").arg(keys.len());
        for k in keys {
            cmd.arg(k);
        }
        if let Some(agg) = aggregate {
            cmd.arg("AGGREGATE").arg(agg.as_arg());
        }
        cmd.arg("WITHSCORES");
        collect_member_scores(self.execute_command(cmd, None).await?)
    }

    /// Cardinality of the intersection of the given sorted sets (`ZINTERCARD`),
    /// with an optional `LIMIT`.
    async fn zintercard<K: ToRedisArgs + Send + Sync>(
        &self,
        keys: &[K],
        limit: Option<i64>,
    ) -> Result<i64> {
        let mut cmd = Cmd::new();
        cmd.arg("ZINTERCARD").arg(keys.len());
        for k in keys {
            cmd.arg(k);
        }
        if let Some(l) = limit {
            cmd.arg("LIMIT").arg(l);
        }
        value::to_i64(self.execute_command(cmd, None).await?)
    }

    #[doc(hidden)]
    async fn zsetop_store<D: ToRedisArgs + Send, K: ToRedisArgs + Send + Sync>(
        &self,
        op: &'static str,
        destination: D,
        keys: &[K],
        aggregate: Option<AggregationType>,
    ) -> Result<i64> {
        let mut cmd = Cmd::new();
        cmd.arg(op).arg(destination).arg(keys.len());
        for k in keys {
            cmd.arg(k);
        }
        if let Some(agg) = aggregate {
            cmd.arg("AGGREGATE").arg(agg.as_arg());
        }
        value::to_i64(self.execute_command(cmd, None).await?)
    }
}

fn collect_bytes(v: redis::Value) -> Result<Vec<Bytes>> {
    match v {
        redis::Value::Array(items) => items.into_iter().map(value::to_bytes).collect(),
        redis::Value::Nil => Ok(Vec::new()),
        other => Ok(vec![value::to_bytes(other)?]),
    }
}

/// Parse a `WITHSCORES`/`ZPOPMIN`-style reply into `(member, score)` pairs,
/// handling both RESP2 flat arrays and RESP3 nested pairs.
fn collect_member_scores(v: redis::Value) -> Result<Vec<(Bytes, f64)>> {
    match v {
        redis::Value::Nil => Ok(Vec::new()),
        // RESP3 returns a map of member -> score.
        redis::Value::Map(pairs) => pairs
            .into_iter()
            .map(|(m, s)| Ok((value::to_bytes(m)?, value::to_f64(s)?)))
            .collect(),
        redis::Value::Array(items) => {
            // RESP3: array of [member, score] pairs.
            if items
                .iter()
                .all(|it| matches!(it, redis::Value::Array(inner) if inner.len() == 2))
            {
                let mut out = Vec::with_capacity(items.len());
                for it in items {
                    if let redis::Value::Array(mut pair) = it {
                        let score = value::to_f64(pair.pop().unwrap())?;
                        let member = value::to_bytes(pair.pop().unwrap())?;
                        out.push((member, score));
                    }
                }
                Ok(out)
            } else {
                // RESP2: flat [member, score, member, score, ...].
                let mut out = Vec::with_capacity(items.len() / 2);
                let mut iter = items.into_iter();
                while let (Some(m), Some(s)) = (iter.next(), iter.next()) {
                    out.push((value::to_bytes(m)?, value::to_f64(s)?));
                }
                Ok(out)
            }
        }
        other => Err(crate::error::GlideError::Request(format!(
            "unexpected sorted-set reply: {other:?}"
        ))),
    }
}

impl<T: CommandExecutor + ?Sized> SortedSetCommands for T {}

/// Parse a `ZRANK ... WITHSCORE` reply (`[rank, score]` or nil).
fn parse_rank_withscore(v: redis::Value) -> Result<Option<(i64, f64)>> {
    match v {
        redis::Value::Nil => Ok(None),
        redis::Value::Array(mut items) if items.len() == 2 => {
            let score = value::to_f64(items.pop().unwrap())?;
            let rank = value::to_i64(items.pop().unwrap())?;
            Ok(Some((rank, score)))
        }
        other => Err(crate::error::GlideError::Request(format!(
            "unexpected ZRANK WITHSCORE reply: {other:?}"
        ))),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn score_bound_formatting() {
        assert_eq!(ScoreBound::NegativeInfinity.to_arg(), "-inf");
        assert_eq!(ScoreBound::PositiveInfinity.to_arg(), "+inf");
        assert_eq!(ScoreBound::Exclusive(1.0).to_arg(), "(1");
    }
}