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
use crate::{from_redis_value, FromRedisValue, RedisResult, RedisWrite, ToRedisArgs, Value};
use std::collections::HashMap;
use std::io::{Error, ErrorKind};
#[derive(PartialEq, Eq, Clone, Debug, Copy)]
pub enum StreamMaxlen {
Equals(usize),
Approx(usize),
}
impl ToRedisArgs for StreamMaxlen {
fn write_redis_args<W>(&self, out: &mut W)
where
W: ?Sized + RedisWrite,
{
let (ch, val) = match *self {
StreamMaxlen::Equals(v) => ("=", v),
StreamMaxlen::Approx(v) => ("~", v),
};
out.write_arg(b"MAXLEN");
out.write_arg(ch.as_bytes());
val.write_redis_args(out);
}
}
#[derive(Default, Debug)]
pub struct StreamClaimOptions {
idle: Option<usize>,
time: Option<usize>,
retry: Option<usize>,
force: bool,
justid: bool,
}
impl StreamClaimOptions {
pub fn idle(mut self, ms: usize) -> Self {
self.idle = Some(ms);
self
}
pub fn time(mut self, ms_time: usize) -> Self {
self.time = Some(ms_time);
self
}
pub fn retry(mut self, count: usize) -> Self {
self.retry = Some(count);
self
}
pub fn with_force(mut self) -> Self {
self.force = true;
self
}
pub fn with_justid(mut self) -> Self {
self.justid = true;
self
}
}
impl ToRedisArgs for StreamClaimOptions {
fn write_redis_args<W>(&self, out: &mut W)
where
W: ?Sized + RedisWrite,
{
if let Some(ref ms) = self.idle {
out.write_arg(b"IDLE");
out.write_arg(format!("{}", ms).as_bytes());
}
if let Some(ref ms_time) = self.time {
out.write_arg(b"TIME");
out.write_arg(format!("{}", ms_time).as_bytes());
}
if let Some(ref count) = self.retry {
out.write_arg(b"RETRYCOUNT");
out.write_arg(format!("{}", count).as_bytes());
}
if self.force {
out.write_arg(b"FORCE");
}
if self.justid {
out.write_arg(b"JUSTID");
}
}
}
type SRGroup = Option<(Vec<Vec<u8>>, Vec<Vec<u8>>)>;
#[derive(Default, Debug)]
pub struct StreamReadOptions {
block: Option<usize>,
count: Option<usize>,
noack: Option<bool>,
group: SRGroup,
}
impl StreamReadOptions {
pub fn read_only(&self) -> bool {
self.group.is_none()
}
pub fn noack(mut self) -> Self {
self.noack = Some(true);
self
}
pub fn block(mut self, ms: usize) -> Self {
self.block = Some(ms);
self
}
pub fn count(mut self, n: usize) -> Self {
self.count = Some(n);
self
}
pub fn group<GN: ToRedisArgs, CN: ToRedisArgs>(
mut self,
group_name: GN,
consumer_name: CN,
) -> Self {
self.group = Some((
ToRedisArgs::to_redis_args(&group_name),
ToRedisArgs::to_redis_args(&consumer_name),
));
self
}
}
impl ToRedisArgs for StreamReadOptions {
fn write_redis_args<W>(&self, out: &mut W)
where
W: ?Sized + RedisWrite,
{
if let Some(ref ms) = self.block {
out.write_arg(b"BLOCK");
out.write_arg(format!("{}", ms).as_bytes());
}
if let Some(ref n) = self.count {
out.write_arg(b"COUNT");
out.write_arg(format!("{}", n).as_bytes());
}
if let Some(ref group) = self.group {
if let Some(true) = self.noack {
out.write_arg(b"NOACK");
}
out.write_arg(b"GROUP");
for i in &group.0 {
out.write_arg(i);
}
for i in &group.1 {
out.write_arg(i);
}
}
}
}
#[derive(Default, Debug, Clone)]
pub struct StreamReadReply {
pub keys: Vec<StreamKey>,
}
#[derive(Default, Debug, Clone)]
pub struct StreamRangeReply {
pub ids: Vec<StreamId>,
}
#[derive(Default, Debug, Clone)]
pub struct StreamClaimReply {
pub ids: Vec<StreamId>,
}
#[derive(Debug, Clone)]
pub enum StreamPendingReply {
Empty,
Data(StreamPendingData),
}
impl Default for StreamPendingReply {
fn default() -> StreamPendingReply {
StreamPendingReply::Empty
}
}
impl StreamPendingReply {
pub fn count(&self) -> usize {
match self {
StreamPendingReply::Empty => 0,
StreamPendingReply::Data(x) => x.count,
}
}
}
#[derive(Default, Debug, Clone)]
pub struct StreamPendingData {
pub count: usize,
pub start_id: String,
pub end_id: String,
pub consumers: Vec<StreamInfoConsumer>,
}
#[derive(Default, Debug, Clone)]
pub struct StreamPendingCountReply {
pub ids: Vec<StreamPendingId>,
}
#[derive(Default, Debug, Clone)]
pub struct StreamInfoStreamReply {
pub last_generated_id: String,
pub radix_tree_keys: usize,
pub groups: usize,
pub length: usize,
pub first_entry: StreamId,
pub last_entry: StreamId,
}
#[derive(Default, Debug, Clone)]
pub struct StreamInfoConsumersReply {
pub consumers: Vec<StreamInfoConsumer>,
}
#[derive(Default, Debug, Clone)]
pub struct StreamInfoGroupsReply {
pub groups: Vec<StreamInfoGroup>,
}
#[derive(Default, Debug, Clone)]
pub struct StreamInfoConsumer {
pub name: String,
pub pending: usize,
pub idle: usize,
}
#[derive(Default, Debug, Clone)]
pub struct StreamInfoGroup {
pub name: String,
pub consumers: usize,
pub pending: usize,
pub last_delivered_id: String,
}
#[derive(Default, Debug, Clone)]
pub struct StreamPendingId {
pub id: String,
pub consumer: String,
pub last_delivered_ms: usize,
pub times_delivered: usize,
}
#[derive(Default, Debug, Clone)]
pub struct StreamKey {
pub key: String,
pub ids: Vec<StreamId>,
}
#[derive(Default, Debug, Clone)]
pub struct StreamId {
pub id: String,
pub map: HashMap<String, Value>,
}
impl StreamId {
fn from_bulk_value(v: &Value) -> RedisResult<Self> {
let mut stream_id = StreamId::default();
if let Value::Bulk(ref values) = *v {
if let Some(v) = values.get(0) {
stream_id.id = from_redis_value(&v)?;
}
if let Some(v) = values.get(1) {
stream_id.map = from_redis_value(&v)?;
}
}
Ok(stream_id)
}
pub fn get<T: FromRedisValue>(&self, key: &str) -> Option<T> {
match self.map.get(key) {
Some(ref x) => from_redis_value(*x).ok(),
None => None,
}
}
pub fn contains_key(&self, key: &&str) -> bool {
self.map.get(*key).is_some()
}
pub fn len(&self) -> usize {
self.map.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
type SRRows = Vec<HashMap<String, Vec<HashMap<String, HashMap<String, Value>>>>>;
impl FromRedisValue for StreamReadReply {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let rows: SRRows = from_redis_value(v)?;
let keys = rows
.into_iter()
.flat_map(|row| {
row.into_iter().map(|(key, entry)| {
let ids = entry
.into_iter()
.flat_map(|id_row| id_row.into_iter().map(|(id, map)| StreamId { id, map }))
.collect();
StreamKey { key, ids }
})
})
.collect();
Ok(StreamReadReply { keys })
}
}
impl FromRedisValue for StreamRangeReply {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let rows: Vec<HashMap<String, HashMap<String, Value>>> = from_redis_value(v)?;
let ids: Vec<StreamId> = rows
.into_iter()
.flat_map(|row| row.into_iter().map(|(id, map)| StreamId { id, map }))
.collect();
Ok(StreamRangeReply { ids })
}
}
impl FromRedisValue for StreamClaimReply {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let rows: Vec<HashMap<String, HashMap<String, Value>>> = from_redis_value(v)?;
let ids: Vec<StreamId> = rows
.into_iter()
.flat_map(|row| row.into_iter().map(|(id, map)| StreamId { id, map }))
.collect();
Ok(StreamClaimReply { ids })
}
}
type SPRInner = (
usize,
Option<String>,
Option<String>,
Vec<Option<(String, String)>>,
);
impl FromRedisValue for StreamPendingReply {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let (count, start, end, consumer_data): SPRInner = from_redis_value(v)?;
if count == 0 {
Ok(StreamPendingReply::Empty)
} else {
let mut result = StreamPendingData::default();
let start_id = start.ok_or_else(|| {
Error::new(
ErrorKind::Other,
"IllegalState: Non-zero pending expects start id",
)
})?;
let end_id = end.ok_or_else(|| {
Error::new(
ErrorKind::Other,
"IllegalState: Non-zero pending expects end id",
)
})?;
result.count = count;
result.start_id = start_id;
result.end_id = end_id;
for cd in consumer_data {
if let Some((name, pending)) = cd {
let mut info = StreamInfoConsumer::default();
info.name = name;
if let Ok(v) = pending.parse::<usize>() {
info.pending = v;
}
result.consumers.push(info);
}
}
Ok(StreamPendingReply::Data(result))
}
}
}
impl FromRedisValue for StreamPendingCountReply {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let mut reply = StreamPendingCountReply::default();
match v {
Value::Bulk(outer_tuple) => {
for outer in outer_tuple {
match outer {
Value::Bulk(inner_tuple) => match &inner_tuple[..] {
[Value::Data(id_bytes), Value::Data(consumer_bytes), Value::Int(last_delivered_ms_u64), Value::Int(times_delivered_u64)] =>
{
let id = String::from_utf8(id_bytes.to_vec())?;
let consumer = String::from_utf8(consumer_bytes.to_vec())?;
let last_delivered_ms = *last_delivered_ms_u64 as usize;
let times_delivered = *times_delivered_u64 as usize;
reply.ids.push(StreamPendingId {
id,
consumer,
last_delivered_ms,
times_delivered,
});
}
_ => fail!((
crate::types::ErrorKind::TypeError,
"Cannot parse redis data (3)"
)),
},
_ => fail!((
crate::types::ErrorKind::TypeError,
"Cannot parse redis data (2)"
)),
}
}
}
_ => fail!((
crate::types::ErrorKind::TypeError,
"Cannot parse redis data (1)"
)),
};
Ok(reply)
}
}
impl FromRedisValue for StreamInfoStreamReply {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let map: HashMap<String, Value> = from_redis_value(v)?;
let mut reply = StreamInfoStreamReply::default();
if let Some(v) = &map.get("last-generated-id") {
reply.last_generated_id = from_redis_value(v)?;
}
if let Some(v) = &map.get("radix-tree-nodes") {
reply.radix_tree_keys = from_redis_value(v)?;
}
if let Some(v) = &map.get("groups") {
reply.groups = from_redis_value(v)?;
}
if let Some(v) = &map.get("length") {
reply.length = from_redis_value(v)?;
}
if let Some(v) = &map.get("first-entry") {
reply.first_entry = StreamId::from_bulk_value(v)?;
}
if let Some(v) = &map.get("last-entry") {
reply.last_entry = StreamId::from_bulk_value(v)?;
}
Ok(reply)
}
}
impl FromRedisValue for StreamInfoConsumersReply {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let consumers: Vec<HashMap<String, Value>> = from_redis_value(v)?;
let mut reply = StreamInfoConsumersReply::default();
for map in consumers {
let mut c = StreamInfoConsumer::default();
if let Some(v) = &map.get("name") {
c.name = from_redis_value(v)?;
}
if let Some(v) = &map.get("pending") {
c.pending = from_redis_value(v)?;
}
if let Some(v) = &map.get("idle") {
c.idle = from_redis_value(v)?;
}
reply.consumers.push(c);
}
Ok(reply)
}
}
impl FromRedisValue for StreamInfoGroupsReply {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let groups: Vec<HashMap<String, Value>> = from_redis_value(v)?;
let mut reply = StreamInfoGroupsReply::default();
for map in groups {
let mut g = StreamInfoGroup::default();
if let Some(v) = &map.get("name") {
g.name = from_redis_value(v)?;
}
if let Some(v) = &map.get("pending") {
g.pending = from_redis_value(v)?;
}
if let Some(v) = &map.get("consumers") {
g.consumers = from_redis_value(v)?;
}
if let Some(v) = &map.get("last-delivered-id") {
g.last_delivered_id = from_redis_value(v)?;
}
reply.groups.push(g);
}
Ok(reply)
}
}