kividb 1.2.1

A fast RedisĀ®-compatible in-memory store written in Rust
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
use crate::commands::ctx::Ctx;
use crate::commands::helpers::{lazy_expire_on_read, parse_utf8};
use crate::keyspace_events::{KS_SET, notify};
use crate::resp::write::*;
use crate::store;
use crate::store::ValueRef;
use crate::types::KiviDBValue;
use crate::{read_lock, write_lock};
use ahash::AHashSet;
use parking_lot::RwLock;
use std::sync::Arc;

/// Effective protocol for the reply being built (honours `redis.setresp`).
///
/// RESP3 gives set-valued replies a dedicated type (`~`), which clients use to
/// hand back a native unordered set instead of a list. SMEMBERS / SINTER /
/// SUNION / SDIFF and `SPOP key <count>` all use it; SRANDMEMBER and SSCAN do
/// not — see their call sites for why.
#[inline]
fn reply_resp(ctx: &Ctx) -> u8 {
    crate::commands::handler::effective_resp_for_reply(ctx)
}

// ── load_set ──────────────────────────────────────────────────────────────────
// Reads the set at `key` and returns a cloned, owned AHashSet.
// Returns Some(empty set) when the key doesn't exist.
// Returns None on WRONGTYPE — callers treat None as an error signal.
//
// We clone the inner set rather than holding the RwLock across set-algebra
// operations. This keeps lock scopes short and avoids holding the shard lock
// while doing O(n) work (diff/inter/union). The clone is O(n) in the set size,
// same as the operation itself, so it doesn't change the complexity class.
fn load_set(ctx: &Ctx, key: &[u8]) -> Option<crate::member_set::MemberSet> {
    if lazy_expire_on_read(ctx.store, *ctx.selected_db, key) {
        return Some(crate::member_set::MemberSet::new());
    }
    let db_ref = &ctx.store[*ctx.selected_db];
    let shard = store::get_shard(db_ref, key);
    let map = read_lock!(shard);

    match map.get(key) {
        None => Some(crate::member_set::MemberSet::new()),
        Some(ValueRef::Set(s)) => Some(s.read().clone()),
        Some(_) => None,
    }
}

/// The members of a set, copied out of the store.
type Members = crate::member_set::MemberSet;

/// The sets named by `key_args`, in order. A key that does not exist is an
/// empty set; a key that holds something else is a WRONGTYPE error, whichever
/// position it is in.
fn load_sets(ctx: &Ctx, key_args: &[&[u8]]) -> Result<Vec<Members>, Vec<u8>> {
    key_args
        .iter()
        .map(|key_bytes| {
            let key: &[u8] = key_bytes;
            load_set(ctx, key).ok_or_else(|| {
                resp_error("WRONGTYPE Operation against a key holding the wrong kind of value")
            })
        })
        .collect()
}

// ── set_diff ──────────────────────────────────────────────────────────────────
// Returns elements in the first set that are absent from all subsequent sets.
// `key_args` is a slice of raw command bytes — each entry is a key name.
// Uses retain() for a single O(n) pass per additional set rather than nested
// loops, keeping overall complexity at O(n * m) where m is number of sets.
fn set_diff(ctx: &Ctx, key_args: &[&[u8]]) -> Result<Vec<Arc<[u8]>>, Vec<u8>> {
    let mut sets = load_sets(ctx, key_args)?.into_iter();
    let Some(mut result) = sets.next() else {
        return Ok(vec![]);
    };
    for other in sets {
        result.retain(|v| !other.contains(v));
    }
    Ok(result.into_iter().collect())
}

// ── set_inter ─────────────────────────────────────────────────────────────────
// Returns only elements present in every set.
// Starts with the first set, then retains only elements found in each
// subsequent set.
fn set_inter(ctx: &Ctx, key_args: &[&[u8]]) -> Result<Vec<Arc<[u8]>>, Vec<u8>> {
    let mut sets = load_sets(ctx, key_args)?.into_iter();
    let Some(mut result) = sets.next() else {
        return Ok(vec![]);
    };
    for other in sets {
        result.retain(|v| other.contains(v));
    }
    Ok(result.into_iter().collect())
}

// ── set_union ─────────────────────────────────────────────────────────────────
// Returns all unique elements across every set.
// Extends a single accumulator AHashSet — duplicates are deduplicated
// automatically by the set semantics.
fn set_union(ctx: &Ctx, key_args: &[&[u8]]) -> Result<Vec<Arc<[u8]>>, Vec<u8>> {
    let mut result = AHashSet::<Arc<[u8]>>::new();
    for set in load_sets(ctx, key_args)? {
        result.extend(set);
    }
    Ok(result.into_iter().collect())
}

// ── store_set ─────────────────────────────────────────────────────────────────
// Writes a Vec<Arc<[u8]>> back into the store as a KiviDBValue::Set.
// Overwrites whatever was at dst_key — this is the correct behaviour for
// SDIFFSTORE / SINTERSTORE / SUNIONSTORE which always replace the destination:
// the value, and with it any TTL the old one had. (An empty result leaves no
// key at all; the dispatcher removes it — see `settle_written_keys`.)
fn store_set(ctx: &mut Ctx, dst_key: &[u8], members: Vec<Arc<[u8]>>) {
    let set: crate::member_set::MemberSet = members.into_iter().collect();
    let db = *ctx.selected_db;
    let db_ref = &ctx.store[db];
    let shard = store::get_shard(db_ref, dst_key);
    let replaced = write_lock!(shard).insert_expiring(
        dst_key,
        KiviDBValue::Set(Arc::new(RwLock::new(set))),
        None,
    );
    drop(replaced);
}

pub fn handle(ctx: &mut Ctx, name: &str) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;

    match name {
        "SADD" => {
            if cmd.len() < 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'sadd' command",
                ));
            }

            // The key and the members as they stand in the command. The key
            // was copied to a string of its own, and every member to a shared
            // copy in a vector, whether or not the set had it already.
            let key: &[u8] = cmd[1];

            // The set is written with its shard held: for reading when it is
            // there, which keeps a command that empties it from taking it out
            // of the keyspace meanwhile; for writing when it is made, and
            // then made with its members, never empty. Found, then let go of
            // and written, the members went into a set a `SREM` had just
            // removed (#271).
            let insert = |set: &mut crate::member_set::MemberSet| {
                cmd[2..]
                    .iter()
                    .filter(|member| set.insert_bytes(member))
                    .count() as i64
            };
            let shard = store::get_shard(&ctx.store[*ctx.selected_db], key);
            let found = match read_lock!(shard).get(key) {
                Some(ValueRef::Set(s)) => Some(insert(&mut s.write())),
                Some(_) => {
                    return Some(resp_error(
                        "WRONGTYPE Operation against a key holding the wrong kind of value",
                    ));
                }
                None => None,
            };
            let added = match found {
                Some(added) => added,
                None => {
                    let mut map = write_lock!(shard);
                    // Looked for again: it may have been made since.
                    match map.get(key) {
                        Some(ValueRef::Set(s)) => insert(&mut s.write()),
                        Some(_) => {
                            return Some(resp_error(
                                "WRONGTYPE Operation against a key holding the wrong kind of value",
                            ));
                        }
                        None => {
                            let mut set = crate::member_set::MemberSet::new();
                            let added = insert(&mut set);
                            map.insert(key, KiviDBValue::Set(Arc::new(RwLock::new(set))));
                            added
                        }
                    }
                }
            };
            if added > 0 {
                notify(ctx.pubsub, *ctx.selected_db, KS_SET, "sadd", key);
            }
            Some(resp_integer(added))
        }

        "SCARD" => {
            if cmd.len() != 2 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'scard' command",
                ));
            }
            let key: &[u8] = cmd[1];

            if lazy_expire_on_read(ctx.store, *ctx.selected_db, key) {
                return Some(resp_integer(0));
            }

            let db_ref = &ctx.store[*ctx.selected_db];
            let shard = store::get_shard(db_ref, key);
            let map = read_lock!(shard);

            match map.get(key) {
                None => Some(resp_integer(0)),
                Some(ValueRef::Set(s)) => Some(resp_integer(s.read().len() as i64)),
                Some(_) => Some(resp_error(
                    "WRONGTYPE Operation against a key holding the wrong kind of value",
                )),
            }
        }

        "SISMEMBER" => {
            if cmd.len() != 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'sismember' command",
                ));
            }
            let key: &[u8] = cmd[1];
            let member = &cmd[2];

            let db_ref = &ctx.store[*ctx.selected_db];
            let shard = store::get_shard(db_ref, key);
            let map = read_lock!(shard);

            match map.get(key) {
                None => {
                    crate::stats::record_miss();
                    Some(resp_integer(0))
                }
                Some(ValueRef::Set(s)) => {
                    let inner = s.read();
                    let found = inner.contains(member);
                    if found {
                        crate::stats::record_hit();
                    } else {
                        crate::stats::record_miss();
                    }
                    Some(resp_integer(found as i64))
                }
                Some(_) => {
                    crate::stats::record_miss();
                    Some(resp_error(
                        "WRONGTYPE Operation against a key holding the wrong kind of value",
                    ))
                }
            }
        }

        "SMISMEMBER" => {
            // SMISMEMBER key member [member ...]  — returns array of 1/0
            if cmd.len() < 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'smismember' command",
                ));
            }
            let key: &[u8] = cmd[1];

            let db_ref = &ctx.store[*ctx.selected_db];
            let shard = store::get_shard(db_ref, key);
            let map = read_lock!(shard);

            match map.get(key) {
                None => {
                    crate::stats::record_miss();
                    // key missing: all zeroes
                    let zeros = vec![resp_integer(0); cmd.len() - 2];
                    Some(resp_array(zeros))
                }
                Some(ValueRef::Set(s)) => {
                    let inner = s.read();
                    let results: Vec<Vec<u8>> = cmd[2..]
                        .iter()
                        .map(|m| resp_integer(inner.contains(m) as i64))
                        .collect();
                    crate::stats::record_hit();
                    Some(resp_array(results))
                }
                Some(_) => {
                    crate::stats::record_miss();
                    Some(resp_error(
                        "WRONGTYPE Operation against a key holding the wrong kind of value",
                    ))
                }
            }
        }
        "SMEMBERS" => {
            if cmd.len() != 2 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'smembers' command",
                ));
            }
            let key: &[u8] = cmd[1];

            let db_ref = &ctx.store[*ctx.selected_db];
            let shard = store::get_shard(db_ref, key);
            let map = read_lock!(shard);

            match map.get(key) {
                None => {
                    crate::stats::record_miss();
                    // Missing key is an empty *set* under RESP3
                    // (`shared.emptyset[c->resp]`), not an empty array.
                    Some(resp_set_for(reply_resp(ctx), vec![]))
                }
                Some(ValueRef::Set(s)) => {
                    let inner = s.read();
                    let mut buf = Vec::with_capacity(16 + inner.len() * 24);
                    // RESP3 has a real set type (`~`); RESP2 only has arrays.
                    // Identical payload, one byte of header difference — this
                    // keeps the single-pass build instead of collecting into a
                    // Vec just to re-encode it.
                    buf.push(if reply_resp(ctx) >= 3 { b'~' } else { b'*' });
                    buf.extend_from_slice(inner.len().to_string().as_bytes());
                    buf.extend_from_slice(b"\r\n");
                    for v in inner.iter() {
                        write_bulk(&mut buf, v);
                    }
                    crate::stats::record_hit();
                    Some(buf)
                }
                Some(_) => {
                    crate::stats::record_miss();
                    Some(resp_error(
                        "WRONGTYPE Operation against a key holding the wrong kind of value",
                    ))
                }
            }
        }
        "SREM" => {
            if cmd.len() < 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'srem' command",
                ));
            }
            let key: &[u8] = cmd[1];

            let db_ref = &ctx.store[*ctx.selected_db];
            let shard = store::get_shard(db_ref, key);
            let map = read_lock!(shard);

            match map.get(key) {
                None => {
                    crate::stats::record_miss();
                    Some(resp_integer(0))
                }
                Some(ValueRef::Set(s)) => {
                    let mut inner = s.write();
                    let mut removed = 0i64;
                    for m in cmd.iter().skip(2) {
                        // remove() on crate::member_set::MemberSet accepts &[u8] via Borrow
                        if inner.remove(m) {
                            removed += 1;
                        }
                    }
                    drop(inner);
                    crate::stats::record_hit();
                    if removed > 0 {
                        notify(ctx.pubsub, *ctx.selected_db, KS_SET, "srem", key);
                    }
                    Some(resp_integer(removed))
                }
                Some(_) => {
                    crate::stats::record_miss();
                    Some(resp_error(
                        "WRONGTYPE Operation against a key holding the wrong kind of value",
                    ))
                }
            }
        }
        "SPOP" => {
            if cmd.len() < 2 || cmd.len() > 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'spop' command",
                ));
            }
            let key: &[u8] = cmd[1];
            let count: Option<usize> = match cmd.get(2) {
                None => None,
                Some(raw) => match parse_utf8(raw).and_then(|s| s.parse::<i64>().ok()) {
                    Some(n) if n >= 0 => Some(n as usize),
                    Some(_) => {
                        return Some(resp_error("ERR value is out of range, must be positive"));
                    }
                    None => return Some(resp_error("ERR value is not an integer or out of range")),
                },
            };

            let resp = reply_resp(ctx);
            let db_ref = &ctx.store[*ctx.selected_db];
            let shard = store::get_shard(db_ref, key);
            let map = read_lock!(shard);

            match map.get(key) {
                None => match count {
                    None => {
                        crate::stats::record_miss();
                        Some(resp_null_bulk())
                    }
                    // With a count, a missing key is an empty set under RESP3.
                    Some(_) => Some(resp_set_for(resp, vec![])),
                },
                Some(ValueRef::Set(s)) => {
                    let mut inner = s.write();
                    // Which members go is random, as the command's name
                    // says, and each is found without looking at the rest:
                    // see `MemberSet`.
                    let mut rng = rand::thread_rng();
                    let wanted = count.unwrap_or(1).min(inner.len());
                    let mut popped: Vec<Arc<[u8]>> = Vec::with_capacity(wanted);
                    while popped.len() < wanted {
                        match inner.pop_random(&mut rng) {
                            Some(member) => popped.push(member),
                            None => break,
                        }
                    }
                    drop(inner);
                    if popped.is_empty() {
                        crate::stats::record_miss();
                    } else {
                        crate::stats::record_hit();
                        notify(ctx.pubsub, *ctx.selected_db, KS_SET, "spop", key);
                    }
                    match count {
                        None => Some(match popped.first() {
                            Some(member) => resp_bulk(member),
                            None => resp_null_bulk(),
                        }),
                        // SPOP with a count replies with the RESP3 set type
                        // (`addReplySetLen` in spopWithCountCommand). Note
                        // SRANDMEMBER below deliberately stays an array: a
                        // negative count there may repeat members, which a
                        // set cannot represent.
                        Some(_) => Some(resp_set_for(
                            resp,
                            popped.iter().map(|v| resp_bulk(v)).collect(),
                        )),
                    }
                }
                Some(_) => {
                    crate::stats::record_miss();
                    Some(resp_error(
                        "WRONGTYPE Operation against a key holding the wrong kind of value",
                    ))
                }
            }
        }

        "SRANDMEMBER" => {
            if cmd.len() < 2 || cmd.len() > 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'srandmember' command",
                ));
            }
            let key: &[u8] = cmd[1];
            let count: Option<i64> = match cmd.get(2) {
                None => None,
                Some(raw) => match parse_utf8(raw).and_then(|s| s.parse::<i64>().ok()) {
                    // The magnitude has to be a count.
                    Some(i64::MIN) => return Some(resp_error("ERR value is out of range")),
                    Some(n) => Some(n),
                    None => return Some(resp_error("ERR value is not an integer or out of range")),
                },
            };

            let db_ref = &ctx.store[*ctx.selected_db];
            let shard = store::get_shard(db_ref, key);
            let map = read_lock!(shard);

            match map.get(key) {
                None => match count {
                    None => {
                        crate::stats::record_miss();
                        Some(resp_null_bulk())
                    }
                    Some(_) => Some(resp_array(vec![])),
                },
                Some(ValueRef::Set(s)) => {
                    let inner = s.read(); // read-only — no removal
                    let mut rng = rand::thread_rng();
                    crate::stats::record_hit();
                    match count {
                        None => Some(match inner.random(&mut rng) {
                            Some(member) => resp_bulk(member),
                            None => resp_null_bulk(),
                        }),
                        // A count: that many distinct members, or all of them.
                        Some(n) if n >= 0 => Some(resp_array(
                            inner
                                .random_distinct(n as usize, &mut rng)
                                .iter()
                                .map(|member| resp_bulk(member))
                                .collect(),
                        )),
                        // A negative count: that many picks, each from the
                        // whole set, so a member may come up more than once.
                        Some(n) => {
                            let mut items: Vec<Vec<u8>> = Vec::new();
                            for _ in 0..n.unsigned_abs() {
                                match inner.random(&mut rng) {
                                    Some(member) => items.push(resp_bulk(member)),
                                    None => break,
                                }
                            }
                            Some(resp_array(items))
                        }
                    }
                }
                Some(_) => {
                    crate::stats::record_miss();
                    Some(resp_error(
                        "WRONGTYPE Operation against a key holding the wrong kind of value",
                    ))
                }
            }
        }

        "SMOVE" => {
            // SMOVE source destination member
            if cmd.len() != 4 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'smove' command",
                ));
            }
            let src_key: Vec<u8> = cmd[1].to_vec();
            let dst_key: Vec<u8> = cmd[2].to_vec();
            let member: Arc<[u8]> = Arc::from(cmd[3]);
            let db = *ctx.selected_db;
            let db_ref = &ctx.store[db];
            const WRONGTYPE: &str =
                "WRONGTYPE Operation against a key holding the wrong kind of value";

            // Both keys are looked at before either is changed: a
            // destination of another type is an error that moves nothing.
            let source =
                match read_lock!(store::get_shard(db_ref, &src_key)).get(src_key.as_slice()) {
                    None => None,
                    Some(ValueRef::Set(s)) => Some(Arc::clone(s)),
                    Some(_) => return Some(resp_error(WRONGTYPE)),
                };
            match read_lock!(store::get_shard(db_ref, &dst_key)).get(dst_key.as_slice()) {
                None | Some(ValueRef::Set(_)) => {}
                Some(_) => return Some(resp_error(WRONGTYPE)),
            }
            let Some(source) = source else {
                crate::stats::record_miss();
                return Some(resp_integer(0));
            };
            if src_key == dst_key {
                // Moving it to where it is: it is there or it is not.
                return Some(resp_integer(source.read().contains(member.as_ref()) as i64));
            }
            if !source.write().remove(member.as_ref()) {
                crate::stats::record_miss();
                return Some(resp_integer(0));
            }

            let added = {
                let shard = store::get_shard(db_ref, &dst_key);
                let mut map = write_lock!(shard);
                match map.get(dst_key.as_slice()) {
                    Some(ValueRef::Set(s)) => s.write().insert(Arc::clone(&member)),
                    _ => {
                        let mut new_set = crate::member_set::MemberSet::new();
                        new_set.insert(Arc::clone(&member));
                        map.insert(&*dst_key, KiviDBValue::Set(Arc::new(RwLock::new(new_set))));
                        true
                    }
                }
            };

            crate::stats::record_hit();
            notify(ctx.pubsub, db, KS_SET, "srem", &src_key);
            if added {
                notify(ctx.pubsub, db, KS_SET, "sadd", &dst_key);
            } else {
                // It was in the destination already, which is as it was: a
                // transaction watching it has nothing to be aborted for.
                crate::watch::unchanged(db, &dst_key);
            }
            Some(resp_integer(1))
        }
        "SDIFF" => {
            if cmd.len() < 2 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'sdiff' command",
                ));
            }
            crate::stats::record_hit();
            let resp = reply_resp(ctx);
            let members = match set_diff(ctx, &cmd[1..]) {
                Ok(members) => members,
                Err(e) => return Some(e),
            };
            Some(resp_set_for(
                resp,
                members.into_iter().map(|v| resp_bulk(&v)).collect(),
            ))
        }

        "SDIFFSTORE" => {
            if cmd.len() < 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'sdiffstore' command",
                ));
            }
            let dst_key: Vec<u8> = cmd[1].to_vec();
            let result = match set_diff(ctx, &cmd[2..]) {
                Ok(result) => result,
                Err(e) => return Some(e),
            };
            let len = result.len() as i64;
            store_set(ctx, &dst_key, result);
            crate::stats::record_hit();
            notify(ctx.pubsub, *ctx.selected_db, KS_SET, "sdiffstore", &dst_key);
            Some(resp_integer(len))
        }

        "SINTER" => {
            if cmd.len() < 2 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'sinter' command",
                ));
            }
            crate::stats::record_hit();
            let resp = reply_resp(ctx);
            let members = match set_inter(ctx, &cmd[1..]) {
                Ok(members) => members,
                Err(e) => return Some(e),
            };
            Some(resp_set_for(
                resp,
                members.into_iter().map(|v| resp_bulk(&v)).collect(),
            ))
        }

        "SINTERSTORE" => {
            if cmd.len() < 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'sinterstore' command",
                ));
            }
            let dst_key: Vec<u8> = cmd[1].to_vec();
            let result = match set_inter(ctx, &cmd[2..]) {
                Ok(result) => result,
                Err(e) => return Some(e),
            };
            let len = result.len() as i64;
            store_set(ctx, &dst_key, result);
            crate::stats::record_hit();
            notify(
                ctx.pubsub,
                *ctx.selected_db,
                KS_SET,
                "sinterstore",
                &dst_key,
            );
            Some(resp_integer(len))
        }

        "SINTERCARD" => {
            // SINTERCARD numkeys key [key ...] [LIMIT limit]
            let numkeys = match parse_utf8(cmd[1]).and_then(|s| s.parse::<i64>().ok()) {
                Some(n) if n > 0 => n as usize,
                _ => return Some(resp_error("ERR numkeys should be greater than 0")),
            };
            let keys_end = 2 + numkeys;
            if cmd.len() < keys_end {
                return Some(resp_error(
                    "ERR Number of keys can't be greater than number of args",
                ));
            }
            // 0: no limit.
            let limit: usize = match &cmd[keys_end..] {
                [] => 0,
                [word, n] if word.eq_ignore_ascii_case(b"LIMIT") => {
                    match parse_utf8(n).and_then(|s| s.parse::<i64>().ok()) {
                        Some(n) if n >= 0 => n as usize,
                        _ => return Some(resp_error("ERR LIMIT can't be negative")),
                    }
                }
                _ => return Some(resp_error("ERR syntax error")),
            };

            let result = match set_inter(ctx, &cmd[2..keys_end]) {
                Ok(result) => result,
                Err(e) => return Some(e),
            };
            let count = if limit > 0 {
                result.len().min(limit)
            } else {
                result.len()
            };
            crate::stats::record_hit();
            Some(resp_integer(count as i64))
        }

        "SUNION" => {
            if cmd.len() < 2 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'sunion' command",
                ));
            }
            crate::stats::record_hit();
            let resp = reply_resp(ctx);
            let members = match set_union(ctx, &cmd[1..]) {
                Ok(members) => members,
                Err(e) => return Some(e),
            };
            Some(resp_set_for(
                resp,
                members.into_iter().map(|v| resp_bulk(&v)).collect(),
            ))
        }

        "SUNIONSTORE" => {
            if cmd.len() < 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'sunionstore' command",
                ));
            }
            let dst_key: Vec<u8> = cmd[1].to_vec();
            let result = match set_union(ctx, &cmd[2..]) {
                Ok(result) => result,
                Err(e) => return Some(e),
            };
            let len = result.len() as i64;
            store_set(ctx, &dst_key, result);
            crate::stats::record_hit();
            notify(
                ctx.pubsub,
                *ctx.selected_db,
                KS_SET,
                "sunionstore",
                &dst_key,
            );
            Some(resp_integer(len))
        }

        "SSCAN" => {
            // SSCAN key cursor [MATCH pattern] [COUNT count]
            if cmd.len() < 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'sscan' command",
                ));
            }
            let key: &[u8] = cmd[1];
            let (cursor, options) = match crate::commands::helpers::scan_arguments(cmd, 2, &[]) {
                Ok(parsed) => parsed,
                Err(e) => return Some(e),
            };

            let db_ref = &ctx.store[*ctx.selected_db];
            let shard = store::get_shard(db_ref, key);
            let map = read_lock!(shard);

            match map.get(key) {
                None => {
                    crate::stats::record_miss();
                    Some(resp_array(vec![resp_bulk_str("0"), resp_array(vec![])]))
                }
                Some(value @ ValueRef::Set(s)) => {
                    // A set Redis would encode compactly comes back whole.
                    let (cursor, count) = if crate::commands::generics::scans_whole(value) {
                        (0, usize::MAX)
                    } else {
                        (cursor, options.count)
                    };
                    let inner = s.read();
                    // In the order of the members' positions: see
                    // `helpers::scan_position`.
                    let (page, next_cursor) = crate::commands::helpers::scan_page(
                        inner
                            .iter()
                            .map(|m| (crate::commands::helpers::scan_position(m), m)),
                        cursor,
                        count,
                        inner.len(),
                        1024,
                    );
                    let items: Vec<Vec<u8>> = page
                        .into_iter()
                        .filter(|m| {
                            options.pattern.as_deref().is_none_or(|pat| {
                                crate::commands::helpers::glob_match_bytes(pat, m)
                            })
                        })
                        .map(|m| resp_bulk(m))
                        .collect();

                    crate::stats::record_hit();
                    Some(resp_array(vec![
                        resp_bulk_str(&next_cursor.to_string()),
                        resp_array(items),
                    ]))
                }
                Some(_) => {
                    crate::stats::record_miss();
                    Some(resp_error(
                        "WRONGTYPE Operation against a key holding the wrong kind of value",
                    ))
                }
            }
        }

        // SUNIONCARD / SDIFFCARD
        _ => crate::commands::set_card::handle(ctx, name),
    }
}