yang-db 0.3.1

个人使用数据库操作
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
//! Redis Streams。
//!
//! 复用 `redis` crate 的结构化回复类型(`StreamId`/`StreamReadReply` 等),
//! 在客户端上提供 XADD/XRANGE/XREAD/消费者组/Pending 恢复的一等公民入口。
//! 全部走统一的超时、重连与指标路径。

use crate::{DbError, RedisClient, Result};

/// XREADGROUP 选项。
#[derive(Debug, Clone, Copy)]
pub struct XReadGroupOptions<'a> {
    /// 消费者组名
    pub group: &'a str,
    /// 消费者名
    pub consumer: &'a str,
    /// 每个 stream 最多返回条数
    pub count: Option<u64>,
    /// 阻塞毫秒数
    pub block_ms: Option<u64>,
    /// 不写入 PEL
    pub noack: bool,
}

pub use redis::streams::{
    StreamAddOptions, StreamAutoClaimOptions, StreamAutoClaimReply, StreamClaimOptions,
    StreamClaimReply, StreamId, StreamInfoConsumersReply, StreamInfoGroupsReply,
    StreamInfoStreamReply, StreamKey, StreamPendingCountReply, StreamPendingData, StreamPendingId,
    StreamPendingReply, StreamRangeReply, StreamReadOptions, StreamReadReply,
};

impl RedisClient {
    /// XADD,返回生成的 entry ID。
    pub async fn xadd(
        &self,
        key: impl AsRef<[u8]>,
        id: &str,
        fields: &[(&str, &str)],
    ) -> Result<String> {
        self.xadd_with_options(key, id, fields, &StreamAddOptions::default())
            .await
    }

    /// XADD(带 NOMKSTREAM / MAXLEN 等选项)。
    pub async fn xadd_with_options(
        &self,
        key: impl AsRef<[u8]>,
        id: &str,
        fields: &[(&str, &str)],
        options: &StreamAddOptions,
    ) -> Result<String> {
        let mut cmd = redis::cmd("XADD");
        cmd.arg(key.as_ref()).arg(options).arg(id);
        for (field, value) in fields {
            cmd.arg(*field).arg(*value);
        }
        self.execute(&cmd)
            .await?
            .as_string()
            .ok_or_else(|| DbError::RedisTypeConversionError("XADD 未返回 entry ID".to_string()))
    }

    /// XLEN。
    pub async fn xlen(&self, key: impl AsRef<[u8]>) -> Result<u64> {
        let mut cmd = redis::cmd("XLEN");
        cmd.arg(key.as_ref());
        require_u64(&self.execute(&cmd).await?, "XLEN")
    }

    /// XRANGE。
    pub async fn xrange(
        &self,
        key: impl AsRef<[u8]>,
        start: &str,
        end: &str,
        count: Option<u64>,
    ) -> Result<StreamRangeReply> {
        let mut cmd = redis::cmd("XRANGE");
        cmd.arg(key.as_ref()).arg(start).arg(end);
        if let Some(count) = count {
            cmd.arg("COUNT").arg(count);
        }
        self.execute_typed(&cmd).await
    }

    /// XREVRANGE。
    pub async fn xrevrange(
        &self,
        key: impl AsRef<[u8]>,
        end: &str,
        start: &str,
        count: Option<u64>,
    ) -> Result<StreamRangeReply> {
        let mut cmd = redis::cmd("XREVRANGE");
        cmd.arg(key.as_ref()).arg(end).arg(start);
        if let Some(count) = count {
            cmd.arg("COUNT").arg(count);
        }
        self.execute_typed(&cmd).await
    }

    /// XREAD(非消费者组)。
    pub async fn xread(
        &self,
        keys: &[String],
        ids: &[String],
        count: Option<u64>,
        block_ms: Option<u64>,
    ) -> Result<StreamReadReply> {
        if keys.len() != ids.len() {
            return Err(DbError::InvalidArgument(
                "XREAD keys 与 ids 数量必须一致".to_string(),
            ));
        }
        let mut cmd = redis::cmd("XREAD");
        if let Some(count) = count {
            cmd.arg("COUNT").arg(count);
        }
        if let Some(block_ms) = block_ms {
            cmd.arg("BLOCK").arg(block_ms);
        }
        cmd.arg("STREAMS");
        for key in keys {
            cmd.arg(key);
        }
        for id in ids {
            cmd.arg(id);
        }
        self.execute_typed(&cmd).await
    }

    /// XREADGROUP。
    #[allow(clippy::too_many_arguments)]
    pub async fn xread_group(
        &self,
        keys: &[String],
        ids: &[String],
        group: &str,
        consumer: &str,
        count: Option<u64>,
        block_ms: Option<u64>,
        noack: bool,
    ) -> Result<StreamReadReply> {
        self.xread_group_with_options(
            keys,
            ids,
            XReadGroupOptions {
                group,
                consumer,
                count,
                block_ms,
                noack,
            },
        )
        .await
    }

    /// XREADGROUP(选项结构体版本)。
    pub async fn xread_group_with_options(
        &self,
        keys: &[String],
        ids: &[String],
        options: XReadGroupOptions<'_>,
    ) -> Result<StreamReadReply> {
        if keys.len() != ids.len() {
            return Err(DbError::InvalidArgument(
                "XREADGROUP keys 与 ids 数量必须一致".to_string(),
            ));
        }
        let mut cmd = redis::cmd("XREADGROUP");
        cmd.arg("GROUP").arg(options.group).arg(options.consumer);
        if let Some(count) = options.count {
            cmd.arg("COUNT").arg(count);
        }
        if let Some(block_ms) = options.block_ms {
            cmd.arg("BLOCK").arg(block_ms);
        }
        if options.noack {
            cmd.arg("NOACK");
        }
        cmd.arg("STREAMS");
        for key in keys {
            cmd.arg(key);
        }
        for id in ids {
            cmd.arg(id);
        }
        self.execute_typed(&cmd).await
    }

    /// XDEL。
    pub async fn xdel(&self, key: impl AsRef<[u8]>, ids: &[String]) -> Result<u64> {
        if ids.is_empty() {
            return Ok(0);
        }

        let mut cmd = redis::cmd("XDEL");
        cmd.arg(key.as_ref());
        for id in ids {
            cmd.arg(id);
        }
        require_u64(&self.execute(&cmd).await?, "XDEL")
    }

    /// XTRIM MAXLEN。
    pub async fn xtrim_maxlen(
        &self,
        key: impl AsRef<[u8]>,
        maxlen: u64,
        approx: bool,
    ) -> Result<u64> {
        let mut cmd = redis::cmd("XTRIM");
        cmd.arg(key.as_ref()).arg("MAXLEN");
        if approx {
            cmd.arg("~");
        }
        cmd.arg(maxlen);
        require_u64(&self.execute(&cmd).await?, "XTRIM")
    }

    /// XGROUP CREATE。
    pub async fn xgroup_create(
        &self,
        key: impl AsRef<[u8]>,
        group: &str,
        id: &str,
        mkstream: bool,
    ) -> Result<()> {
        let mut cmd = redis::cmd("XGROUP");
        cmd.arg("CREATE").arg(key.as_ref()).arg(group).arg(id);
        if mkstream {
            cmd.arg("MKSTREAM");
        }
        self.execute_typed::<String>(&cmd).await?;
        Ok(())
    }

    /// XGROUP DESTROY。
    pub async fn xgroup_destroy(&self, key: impl AsRef<[u8]>, group: &str) -> Result<u64> {
        let mut cmd = redis::cmd("XGROUP");
        cmd.arg("DESTROY").arg(key.as_ref()).arg(group);
        require_u64(&self.execute(&cmd).await?, "XGROUP DESTROY")
    }

    /// XGROUP CREATECONSUMER。
    pub async fn xgroup_createconsumer(
        &self,
        key: impl AsRef<[u8]>,
        group: &str,
        consumer: &str,
    ) -> Result<u64> {
        let mut cmd = redis::cmd("XGROUP");
        cmd.arg("CREATECONSUMER")
            .arg(key.as_ref())
            .arg(group)
            .arg(consumer);
        require_u64(&self.execute(&cmd).await?, "XGROUP CREATECONSUMER")
    }

    /// XGROUP DELCONSUMER。
    pub async fn xgroup_delconsumer(
        &self,
        key: impl AsRef<[u8]>,
        group: &str,
        consumer: &str,
    ) -> Result<u64> {
        let mut cmd = redis::cmd("XGROUP");
        cmd.arg("DELCONSUMER")
            .arg(key.as_ref())
            .arg(group)
            .arg(consumer);
        require_u64(&self.execute(&cmd).await?, "XGROUP DELCONSUMER")
    }

    /// XGROUP SETID。
    pub async fn xgroup_setid(&self, key: impl AsRef<[u8]>, group: &str, id: &str) -> Result<()> {
        let mut cmd = redis::cmd("XGROUP");
        cmd.arg("SETID").arg(key.as_ref()).arg(group).arg(id);
        self.execute_typed::<String>(&cmd).await?;
        Ok(())
    }

    /// XACK。
    pub async fn xack(&self, key: impl AsRef<[u8]>, group: &str, ids: &[String]) -> Result<u64> {
        if ids.is_empty() {
            return Ok(0);
        }

        let mut cmd = redis::cmd("XACK");
        cmd.arg(key.as_ref()).arg(group);
        for id in ids {
            cmd.arg(id);
        }
        require_u64(&self.execute(&cmd).await?, "XACK")
    }

    /// XPENDING 摘要。
    pub async fn xpending(&self, key: impl AsRef<[u8]>, group: &str) -> Result<StreamPendingReply> {
        let mut cmd = redis::cmd("XPENDING");
        cmd.arg(key.as_ref()).arg(group);
        self.execute_typed(&cmd).await
    }

    /// XPENDING 范围查询(可选 consumer 过滤)。
    pub async fn xpending_range(
        &self,
        key: impl AsRef<[u8]>,
        group: &str,
        start: &str,
        end: &str,
        count: u64,
        consumer: Option<&str>,
    ) -> Result<StreamPendingCountReply> {
        let mut cmd = redis::cmd("XPENDING");
        cmd.arg(key.as_ref())
            .arg(group)
            .arg(start)
            .arg(end)
            .arg(count);
        if let Some(consumer) = consumer {
            cmd.arg(consumer);
        }
        self.execute_typed(&cmd).await
    }

    /// XCLAIM(不含 IDLE/TIME/RETRYCOUNT 等高级选项)。
    pub async fn xclaim(
        &self,
        key: impl AsRef<[u8]>,
        group: &str,
        consumer: &str,
        min_idle_ms: u64,
        ids: &[String],
    ) -> Result<StreamClaimReply> {
        self.xclaim_with_options(
            key,
            group,
            consumer,
            min_idle_ms,
            ids,
            &StreamClaimOptions::default(),
        )
        .await
    }

    /// XCLAIM(带选项)。
    pub async fn xclaim_with_options(
        &self,
        key: impl AsRef<[u8]>,
        group: &str,
        consumer: &str,
        min_idle_ms: u64,
        ids: &[String],
        options: &StreamClaimOptions,
    ) -> Result<StreamClaimReply> {
        let mut cmd = redis::cmd("XCLAIM");
        cmd.arg(key.as_ref())
            .arg(group)
            .arg(consumer)
            .arg(min_idle_ms);
        for id in ids {
            cmd.arg(id);
        }
        cmd.arg(options);
        self.execute_typed(&cmd).await
    }

    /// XAUTOCLAIM。
    pub async fn xautoclaim(
        &self,
        key: impl AsRef<[u8]>,
        group: &str,
        consumer: &str,
        min_idle_ms: u64,
        start: &str,
        count: Option<u64>,
    ) -> Result<StreamAutoClaimReply> {
        let mut options = StreamAutoClaimOptions::default();
        if let Some(count) = count {
            options = options.count(count as usize);
        }
        self.xautoclaim_with_options(key, group, consumer, min_idle_ms, start, &options)
            .await
    }

    /// XAUTOCLAIM(带选项)。
    pub async fn xautoclaim_with_options(
        &self,
        key: impl AsRef<[u8]>,
        group: &str,
        consumer: &str,
        min_idle_ms: u64,
        start: &str,
        options: &StreamAutoClaimOptions,
    ) -> Result<StreamAutoClaimReply> {
        let mut cmd = redis::cmd("XAUTOCLAIM");
        cmd.arg(key.as_ref())
            .arg(group)
            .arg(consumer)
            .arg(min_idle_ms)
            .arg(start)
            .arg(options);
        self.execute_typed(&cmd).await
    }

    /// XINFO STREAM。
    pub async fn xinfo_stream(&self, key: impl AsRef<[u8]>) -> Result<StreamInfoStreamReply> {
        let mut cmd = redis::cmd("XINFO");
        cmd.arg("STREAM").arg(key.as_ref());
        self.execute_typed(&cmd).await
    }

    /// XINFO GROUPS。
    pub async fn xinfo_groups(&self, key: impl AsRef<[u8]>) -> Result<StreamInfoGroupsReply> {
        let mut cmd = redis::cmd("XINFO");
        cmd.arg("GROUPS").arg(key.as_ref());
        self.execute_typed(&cmd).await
    }

    /// XINFO CONSUMERS。
    pub async fn xinfo_consumers(
        &self,
        key: impl AsRef<[u8]>,
        group: &str,
    ) -> Result<StreamInfoConsumersReply> {
        let mut cmd = redis::cmd("XINFO");
        cmd.arg("CONSUMERS").arg(key.as_ref()).arg(group);
        self.execute_typed(&cmd).await
    }
}

fn require_u64(value: &crate::RedisValue, operation: &str) -> Result<u64> {
    let int = value
        .as_i64()
        .ok_or_else(|| DbError::RedisTypeConversionError(format!("{operation} 未返回整数")))?;
    u64::try_from(int)
        .map_err(|_| DbError::RedisTypeConversionError(format!("{operation} 返回负数")))
}