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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
use crate::commands::ctx::Ctx;
use crate::commands::helpers::*;
use crate::resp::write::*;
use crate::store;
use crate::store::ValueRef;
use crate::types::SharedBytes;
use crate::{read_lock, write_lock};

// ── Bitmap helpers ────────────────────────────────────────────────────────────
//
// Redis bitmaps are just strings. Bit `offset` lives in byte `offset / 8`,
// at bit position `7 - (offset % 8)` within that byte (big-endian bit order).
//
// IMPORTANT Rust lesson: when you do `string[byte_idx] |= mask`, you need a
// mutable reference. Since KiviDBValue::String wraps an `Arc<[u8]>`, which is
// immutable, we need to copy-on-write: get the existing bytes, clone into a
// Vec<u8>, mutate, then store back as a new Arc<[u8]>.

/// Returns (byte_index, bit_mask) for a given bit offset.
/// Redis uses big-endian bit ordering within each byte.
#[inline]
fn bit_byte_and_mask(offset: u64) -> (usize, u8) {
    let byte_idx = (offset / 8) as usize;
    let bit_pos = 7 - (offset % 8); // MSB first
    (byte_idx, 1u8 << bit_pos)
}

/// Count set bits in a byte slice using Rust's built-in popcount.
/// This compiles to a single `popcnt` CPU instruction on x86-64.
#[inline]
fn popcount(data: &[u8]) -> u64 {
    data.iter().map(|b| b.count_ones() as u64).sum()
}

#[inline]
fn parse_i64_arg(arg: &[u8]) -> Result<i64, ()> {
    std::str::from_utf8(arg)
        .ok()
        .and_then(|s| s.parse().ok())
        .ok_or(())
}

// ── GETBIT ────────────────────────────────────────────────────────────────────
fn cmd_getbit(ctx: &mut Ctx) -> Vec<u8> {
    let cmd = &ctx.cmd;
    if cmd.len() != 3 {
        return resp_error("ERR wrong number of arguments for 'getbit' command");
    }
    let key: &[u8] = cmd[1];
    let offset: u64 = match std::str::from_utf8(cmd[2])
        .ok()
        .and_then(|s| s.parse().ok())
    {
        Some(n) => n,
        None => return resp_error("ERR bit offset is not an integer or out of range"),
    };

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

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

    match map.get(key) {
        Some(ValueRef::String(bytes)) => {
            let (byte_idx, mask) = bit_byte_and_mask(offset);
            if byte_idx >= bytes.len() {
                resp_integer(0)
            } else {
                resp_integer(if bytes[byte_idx] & mask != 0 { 1 } else { 0 })
            }
        }
        None => resp_integer(0),
        _ => resp_error("WRONGTYPE Operation against a key holding the wrong kind of value"),
    }
}

// ── SETBIT ────────────────────────────────────────────────────────────────────
// Rust lesson: Arc<[u8]> is immutable. To mutate, we must:
//   1. Read the existing bytes (or empty)
//   2. Clone into a Vec<u8>
//   3. Extend if needed (Redis auto-extends with zeros)
//   4. Mutate the specific byte
//   5. Store back as a new Arc<[u8]>
fn cmd_setbit(ctx: &mut Ctx) -> Vec<u8> {
    let cmd = &ctx.cmd;
    if cmd.len() != 4 {
        return resp_error("ERR wrong number of arguments for 'setbit' command");
    }
    let key: &[u8] = cmd[1];
    let offset: u64 = match std::str::from_utf8(cmd[2])
        .ok()
        .and_then(|s| s.parse().ok())
    {
        Some(n) if n < 2u64.pow(32) => n, // Redis limit: 2^32 - 1
        _ => return resp_error("ERR bit offset is not an integer or out of range"),
    };
    let bit_val: u8 = match std::str::from_utf8(cmd[3])
        .ok()
        .and_then(|s| s.parse().ok())
    {
        Some(v @ 0) | Some(v @ 1) => v,
        _ => return resp_error("ERR bit is not an integer or out of range"),
    };

    let key_owned = key.to_vec();
    let (byte_idx, mask) = bit_byte_and_mask(offset);

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

    // Get existing bytes or start empty
    let existing: Vec<u8> = match map.get(key) {
        Some(ValueRef::String(b)) => b.to_vec(), // Arc<[u8]> → Vec<u8> (clone)
        None => vec![],
        _ => {
            return resp_error("WRONGTYPE Operation against a key holding the wrong kind of value");
        }
    };

    // Remember the old bit value before we mutate
    let old_bit = if byte_idx < existing.len() && existing[byte_idx] & mask != 0 {
        1i64
    } else {
        0i64
    };

    // No-op: bit already matches — skip touching the DB / replication dirty counters (Redis).
    if byte_idx < existing.len() {
        let cur_set = existing[byte_idx] & mask != 0;
        let want_set = bit_val == 1;
        if cur_set == want_set {
            ctx.skip_write_side_effects = true;
            return resp_integer(old_bit);
        }
    }

    // Extend with zeros if needed, then set/clear the bit
    let mut new_bytes = existing;
    if byte_idx >= new_bytes.len() {
        new_bytes.resize(byte_idx + 1, 0u8);
    }
    if bit_val == 1 {
        new_bytes[byte_idx] |= mask;
    } else {
        new_bytes[byte_idx] &= !mask;
    }

    map.insert_str(key_owned, new_bytes.as_slice());

    resp_integer(old_bit)
}

// ── BITCOUNT ──────────────────────────────────────────────────────────────────
// Syntax: BITCOUNT key [start end [BYTE | BIT]]
fn cmd_bitcount(ctx: &mut Ctx) -> Vec<u8> {
    let cmd = &ctx.cmd;
    if cmd.len() < 2 {
        return resp_error("ERR wrong number of arguments for 'bitcount' command");
    }
    if !(cmd.len() == 2 || cmd.len() == 4 || cmd.len() == 5) {
        return resp_error("ERR syntax error");
    }

    let key: &[u8] = cmd[1];

    // Hot path: whole string — no range parsing
    if cmd.len() == 2 {
        if is_expired(ctx.store, *ctx.selected_db, key) {
            expire_on_read(ctx.store, *ctx.selected_db, key);
            return resp_integer(0);
        }
        let db = &ctx.store[*ctx.selected_db];
        let shard = store::get_shard(db, key);
        let map = read_lock!(shard);
        match map.get(key) {
            Some(ValueRef::String(b)) => resp_integer(popcount(&b) as i64),
            None => resp_integer(0),
            _ => resp_error("WRONGTYPE Operation against a key holding the wrong kind of value"),
        }
    } else {
        let start: i64 = match std::str::from_utf8(cmd[2])
            .ok()
            .and_then(|s| s.parse().ok())
        {
            Some(n) => n,
            None => return resp_error("ERR value is not an integer or out of range"),
        };
        let end: i64 = match std::str::from_utf8(cmd[3])
            .ok()
            .and_then(|s| s.parse().ok())
        {
            Some(n) => n,
            None => return resp_error("ERR value is not an integer or out of range"),
        };

        let bit_mode = if cmd.len() == 5 {
            match std::str::from_utf8(cmd[4])
                .unwrap_or("")
                .to_ascii_uppercase()
                .as_str()
            {
                "BIT" => true,
                "BYTE" => false,
                _ => return resp_error("ERR syntax error"),
            }
        } else {
            false
        };

        let gone = is_expired(ctx.store, *ctx.selected_db, key)
            && expire_on_read(ctx.store, *ctx.selected_db, key);

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

        let bytes: SharedBytes = match map.get(key).filter(|_| !gone) {
            Some(ValueRef::String(b)) => b.to_shared(),
            None => SharedBytes::from(&[][..]),
            _ => {
                return resp_error(
                    "WRONGTYPE Operation against a key holding the wrong kind of value",
                );
            }
        };
        drop(map);

        bitcount_range(&bytes, start, end, bit_mode)
    }
}

#[inline]
fn bitcount_range(bytes: &[u8], start: i64, end: i64, bit_mode: bool) -> Vec<u8> {
    // Redis: two negative bounds with start > end → empty range (before index normalization)
    if start < 0 && end < 0 && start > end {
        return resp_integer(0);
    }

    let strlen = bytes.len() as i64;
    let mut totlen = strlen;
    if bit_mode {
        totlen = strlen.saturating_mul(8);
    }

    let mut s = start;
    let mut e = end;
    if s < 0 {
        s += totlen;
    }
    if e < 0 {
        e += totlen;
    }
    if s < 0 {
        s = 0;
    }
    if e < 0 {
        e = 0;
    }
    if e >= totlen {
        e = totlen - 1;
    }

    // Missing key: Redis returns 0 after normalizing indexes (before start>end)
    if bytes.is_empty() {
        return resp_integer(0);
    }

    if s > e {
        return resp_integer(0);
    }

    if bit_mode {
        let mut count = 0u64;
        for bit in (s as usize)..=(e as usize) {
            let (bi, mask) = bit_byte_and_mask(bit as u64);
            if bi < bytes.len() && bytes[bi] & mask != 0 {
                count += 1;
            }
        }
        resp_integer(count as i64)
    } else {
        let si = s as usize;
        let ei = e as usize;
        resp_integer(popcount(&bytes[si..=ei]) as i64)
    }
}

// ── BITPOS ────────────────────────────────────────────────────────────────────
// Finds first set (1) or clear (0) bit.
fn cmd_bitpos(ctx: &mut Ctx) -> Vec<u8> {
    let cmd = &ctx.cmd;
    if cmd.len() < 3 {
        return resp_error("ERR wrong number of arguments for 'bitpos' command");
    }
    if !(cmd.len() == 3 || cmd.len() == 4 || cmd.len() == 5 || cmd.len() == 6) {
        return resp_error("ERR syntax error");
    }

    let key: &[u8] = cmd[1];

    let bit_ll: i64 = match parse_i64_arg(cmd[2]) {
        Ok(v) => v,
        Err(()) => return resp_error("ERR value is not an integer or out of range"),
    };
    if bit_ll != 0 && bit_ll != 1 {
        return resp_error("ERR The bit argument must be 1 or 0.");
    }
    let search_bit = bit_ll as u8;

    let (start_byte, end_byte, end_given, bit_mode) = match cmd.len() {
        3 => (0i64, -1i64, false, false),
        4 => {
            let s = match parse_i64_arg(cmd[3]) {
                Ok(v) => v,
                Err(()) => return resp_error("ERR value is not an integer or out of range"),
            };
            (s, -1, false, false)
        }
        5 => {
            let s = match parse_i64_arg(cmd[3]) {
                Ok(v) => v,
                Err(()) => return resp_error("ERR value is not an integer or out of range"),
            };
            let e = match parse_i64_arg(cmd[4]) {
                Ok(v) => v,
                Err(()) => return resp_error("ERR value is not an integer or out of range"),
            };
            (s, e, true, false)
        }
        6 => {
            let s = match parse_i64_arg(cmd[3]) {
                Ok(v) => v,
                Err(()) => return resp_error("ERR value is not an integer or out of range"),
            };
            let mode = std::str::from_utf8(cmd[5])
                .unwrap_or("")
                .to_ascii_uppercase();
            let bm = if mode == "BIT" {
                true
            } else if mode == "BYTE" {
                false
            } else {
                return resp_error("ERR syntax error");
            };
            let e = match parse_i64_arg(cmd[4]) {
                Ok(v) => v,
                Err(()) => return resp_error("ERR value is not an integer or out of range"),
            };
            (s, e, true, bm)
        }
        _ => unreachable!(),
    };

    if is_expired(ctx.store, *ctx.selected_db, key) {
        expire_on_read(ctx.store, *ctx.selected_db, key);
        return resp_integer(if search_bit == 0 { 0 } else { -1 });
    }

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

    let bytes = match map.get(key) {
        Some(ValueRef::String(b)) => b.to_shared(),
        None => {
            return resp_integer(if search_bit == 0 { 0 } else { -1 });
        }
        _ => {
            return resp_error("WRONGTYPE Operation against a key holding the wrong kind of value");
        }
    };
    drop(map);

    let len = bytes.len() as i64;

    if bit_mode {
        // BIT mode: indices are bit positions
        let total_bits = len * 8;
        let s = if start_byte < 0 {
            (total_bits + start_byte).max(0)
        } else {
            start_byte
        } as usize;
        let e = if end_byte < 0 {
            (total_bits + end_byte).max(0)
        } else {
            end_byte
        } as usize;
        for bit in s..=e.min((total_bits - 1).max(0) as usize) {
            let (bi, mask) = bit_byte_and_mask(bit as u64);
            if bi < bytes.len() {
                let found = (bytes[bi] & mask != 0) == (search_bit == 1);
                if found {
                    return resp_integer(bit as i64);
                }
            } else if search_bit == 0 {
                return resp_integer(bit as i64);
            }
        }
        return resp_integer(-1);
    }

    // BYTE mode
    let s = if start_byte < 0 {
        (len + start_byte).max(0)
    } else {
        start_byte.min(len)
    } as usize;
    let e = if end_byte < 0 {
        (len + end_byte).max(0)
    } else if end_given {
        end_byte.min(len - 1)
    } else {
        len - 1
    } as usize;

    for byte_i in s..=e {
        let b = if byte_i < bytes.len() {
            bytes[byte_i]
        } else {
            0u8
        };
        for bit_i in 0..8u64 {
            let mask = 1u8 << (7 - bit_i);
            let is_set = b & mask != 0;
            if is_set == (search_bit == 1) {
                return resp_integer((byte_i as i64) * 8 + bit_i as i64);
            }
        }
    }

    // Special case: searching for 0 with no explicit end — look past the string
    if search_bit == 0 && !end_given {
        return resp_integer(len * 8);
    }

    resp_integer(-1)
}

// ── BITOP ─────────────────────────────────────────────────────────────────────
// BITOP AND|OR|XOR|NOT|ONE|DIFF|DIFF1|ANDOR destkey key [key ...]
// Rust lesson: zip/fold patterns on byte slices
fn cmd_bitop(ctx: &mut Ctx) -> Vec<u8> {
    let cmd = &ctx.cmd;
    if cmd.len() < 4 {
        return resp_error("ERR wrong number of arguments for 'bitop' command");
    }
    let op = std::str::from_utf8(cmd[1])
        .unwrap_or("")
        .to_ascii_uppercase();
    let dest_key: Vec<u8> = cmd[2].to_vec();

    if op == "NOT" && cmd.len() != 4 {
        return resp_error("ERR BITOP NOT must be called with a single source key");
    }

    if matches!(op.as_str(), "DIFF" | "DIFF1" | "ANDOR") && cmd.len() < 5 {
        return resp_error(&format!(
            "ERR BITOP {} must be called with at least two source keys.",
            op
        ));
    }

    let db = &ctx.store[*ctx.selected_db];

    // Collect all source byte arrays (under their respective read locks)
    let mut sources: Vec<Vec<u8>> = Vec::new();
    for src_raw in &cmd[3..] {
        let key: &[u8] = src_raw;
        if is_expired(ctx.store, *ctx.selected_db, key) {
            delete_key_if_expired(ctx.store, *ctx.selected_db, key);
            sources.push(vec![]);
            continue;
        }
        let shard = store::get_shard(db, key);
        let map = read_lock!(shard);
        match map.get(key) {
            Some(ValueRef::String(b)) => sources.push(b.to_vec()),
            None => sources.push(vec![]),
            _ => {
                return resp_error(
                    "WRONGTYPE Operation against a key holding the wrong kind of value",
                );
            }
        }
    }

    // Find max length — output is padded to that length
    let max_len = sources.iter().map(|s| s.len()).max().unwrap_or(0);

    let result: Vec<u8> = match op.as_str() {
        "NOT" => {
            // NOT only works on single source
            sources[0].iter().map(|b| !b).collect()
        }
        "AND" => (0..max_len)
            .map(|i| {
                sources
                    .iter()
                    .map(|s| s.get(i).copied().unwrap_or(0))
                    .fold(0xFF, |acc, b| acc & b)
            })
            .collect(),
        "OR" => (0..max_len)
            .map(|i| {
                sources
                    .iter()
                    .map(|s| s.get(i).copied().unwrap_or(0))
                    .fold(0x00, |acc, b| acc | b)
            })
            .collect(),
        "XOR" => (0..max_len)
            .map(|i| {
                sources
                    .iter()
                    .map(|s| s.get(i).copied().unwrap_or(0))
                    .fold(0x00, |acc, b| acc ^ b)
            })
            .collect(),
        "ONE" => (0..max_len)
            .map(|i| {
                let mut output = sources.first().and_then(|s| s.get(i)).copied().unwrap_or(0);
                let mut common_bits = 0u8;
                for src in sources.iter().skip(1) {
                    let byte = src.get(i).copied().unwrap_or(0);
                    common_bits |= output & byte;
                    output ^= byte;
                    output &= !common_bits;
                }
                output
            })
            .collect(),
        "DIFF" | "DIFF1" | "ANDOR" => {
            let combine: fn(u8, u8) -> u8 = match op.as_str() {
                "DIFF" => |o, d| o & !d,
                "DIFF1" => |o, d| !o & d,
                "ANDOR" => |o, d| o & d,
                _ => unreachable!(),
            };
            (0..max_len)
                .map(|i| {
                    let o = sources.first().and_then(|s| s.get(i)).copied().unwrap_or(0);
                    let mut disj = 0u8;
                    for src in sources.iter().skip(1) {
                        disj |= src.get(i).copied().unwrap_or(0);
                    }
                    combine(o, disj)
                })
                .collect()
        }
        _ => {
            return resp_error(&format!(
                "ERR Don't know what to do for \"{}\"",
                op.to_lowercase()
            ));
        }
    };

    let result_len = result.len() as i64;
    // Store result at dest key
    let dest_shard = store::get_shard(db, &dest_key);
    let mut map = write_lock!(dest_shard);
    map.insert_str(dest_key, result.as_slice());
    drop(map);

    resp_integer(result_len)
}

// ── BITFIELD (basic subset) ───────────────────────────────────────────────────
// Full BITFIELD is complex. We implement GET, SET, INCRBY with u/i encodings.
// This teaches you: parsing subcommands in a loop, signed/unsigned bit fields.
fn cmd_bitfield(ctx: &mut Ctx, readonly: bool) -> Vec<u8> {
    // BITFIELD returns one result slot per subcommand; a failed GET or an
    // OVERFLOW FAIL yields a nil *inside* that array, which the dispatcher's
    // top-level null normalisation cannot reach.
    let resp = crate::commands::handler::effective_resp_for_reply(ctx);
    let cmd = &ctx.cmd;
    if cmd.len() < 2 {
        return resp_error("ERR wrong number of arguments for 'bitfield' command");
    }
    let key: Vec<u8> = cmd[1].to_vec();

    // We'll accumulate results for each subcommand
    let mut results: Vec<Vec<u8>> = Vec::new();
    let mut i = 2usize;

    // Read current bytes (we may need to mutate)
    let db = &ctx.store[*ctx.selected_db];
    let shard = store::get_shard(db, &key);

    let mut current_bytes: Vec<u8> = {
        let map = read_lock!(shard);
        match map.get(key.as_slice()) {
            Some(ValueRef::String(b)) => b.to_vec(),
            None => vec![],
            _ => {
                return resp_error(
                    "WRONGTYPE Operation against a key holding the wrong kind of value",
                );
            }
        }
    };
    let snapshot_before = current_bytes.clone();

    let mut overflow_mode = "WRAP"; // WRAP | SAT | FAIL
    let mut modified = false;

    while i < cmd.len() {
        let sub = std::str::from_utf8(cmd[i])
            .unwrap_or("")
            .to_ascii_uppercase();
        i += 1;

        match sub.as_str() {
            "OVERFLOW" => {
                if i >= cmd.len() {
                    return resp_error("ERR syntax error");
                }
                overflow_mode = match std::str::from_utf8(cmd[i])
                    .unwrap_or("")
                    .to_ascii_uppercase()
                    .as_str()
                {
                    "WRAP" => "WRAP",
                    "SAT" => "SAT",
                    "FAIL" => "FAIL",
                    _ => return resp_error("ERR Invalid OVERFLOW type specified"),
                };
                i += 1;
            }
            "GET" => {
                if i + 1 >= cmd.len() {
                    return resp_error("ERR syntax error");
                }
                let encoding = std::str::from_utf8(cmd[i]).unwrap_or("").to_string();
                i += 1;
                let (_, enc_bits) = match parse_encoding(&encoding) {
                    Some(x) => x,
                    None => {
                        return resp_error(
                            "ERR Invalid bitfield type. Use something like i16 u8. Note that u64 is not supported but i64 is.",
                        );
                    }
                };
                let offset = match parse_bitfield_offset(cmd[i], enc_bits) {
                    Ok(o) => o,
                    Err(()) => {
                        return resp_error("ERR bit offset is not an integer or out of range");
                    }
                };
                i += 1;
                let val = bitfield_get(&current_bytes, &encoding, offset);
                results.push(match val {
                    Some(v) => resp_integer(v),
                    None => resp_null_for(resp),
                });
            }
            op @ "SET" | op @ "INCRBY" => {
                if readonly {
                    return resp_error("ERR BITFIELD_RO only supports the GET subcommand");
                }
                if i + 2 >= cmd.len() {
                    return resp_error("ERR syntax error");
                }
                let encoding = std::str::from_utf8(cmd[i]).unwrap_or("").to_string();
                i += 1;
                let (_, enc_bits) = match parse_encoding(&encoding) {
                    Some(x) => x,
                    None => {
                        return resp_error(
                            "ERR Invalid bitfield type. Use something like i16 u8. Note that u64 is not supported but i64 is.",
                        );
                    }
                };
                let offset = match parse_bitfield_offset(cmd[i], enc_bits) {
                    Ok(o) => o,
                    Err(()) => {
                        return resp_error("ERR bit offset is not an integer or out of range");
                    }
                };
                i += 1;
                let value: i128 = std::str::from_utf8(cmd[i])
                    .ok()
                    .and_then(|s| s.parse().ok())
                    .unwrap_or(0);
                i += 1;

                let old_val = bitfield_get(&current_bytes, &encoding, offset);
                let new_val: i128 = if op == "SET" {
                    value
                } else {
                    old_val.unwrap_or(0) as i128 + value // no pre-wrap; clamp decides
                };
                let clamped = bitfield_clamp(&encoding, new_val, overflow_mode);
                match clamped {
                    Some(v) => {
                        bitfield_set(&mut current_bytes, &encoding, offset, v as i64);
                        modified = true;
                        if op == "SET" {
                            results.push(resp_integer(old_val.unwrap_or(0)));
                        } else {
                            results.push(resp_integer(v as i64));
                        }
                    }
                    None => {
                        // Overflow FAIL yields a nil in this slot of the
                        // results array — nested, so it needs the protocol's
                        // own null encoding.
                        results.push(resp_null_for(resp));
                    }
                }
            }
            _ => {
                return resp_error("ERR syntax error");
            }
        }
    }

    if modified && current_bytes == snapshot_before {
        modified = false;
    }
    if modified {
        let mut map = write_lock!(shard);
        map.insert_str(key, current_bytes.as_slice());
    } else {
        ctx.skip_write_side_effects = true;
    }

    resp_array(results)
}

// ── Bitfield encoding helpers ─────────────────────────────────────────────────
// encoding: "u8", "i16", "u32", etc. — u = unsigned, i = signed

/// `#N` means bit offset `N * encoding_bits` (Redis-compatible successive field layout).
/// A bit offset, as SETBIT's: below 2^32, so that a string is never made
/// bigger than 512 MB (Redis's `proto-max-bulk-len`).
fn parse_bitfield_offset(arg: &[u8], encoding_bits: u8) -> Result<usize, ()> {
    let s = std::str::from_utf8(arg).map_err(|_| ())?;
    let offset = if let Some(rest) = s.strip_prefix('#') {
        if rest.is_empty() {
            return Err(());
        }
        let idx: u64 = rest.parse().map_err(|_| ())?;
        (idx as u128).checked_mul(encoding_bits as u128).ok_or(())?
    } else {
        s.parse::<u64>().map_err(|_| ())? as u128
    };
    if offset >= 1 << 32 {
        return Err(());
    }
    usize::try_from(offset).map_err(|_| ())
}

fn parse_encoding(enc: &str) -> Option<(bool, u8)> {
    let (signed, rest) = if let Some(rest) = enc.strip_prefix('i') {
        (true, rest)
    } else {
        let rest = enc.strip_prefix('u')?;
        (false, rest)
    };
    let bits: u8 = rest.parse().ok()?;
    if bits == 0 || bits > 64 || !signed && bits > 63 {
        return None;
    }
    Some((signed, bits))
}

fn bitfield_get(bytes: &[u8], encoding: &str, bit_offset: usize) -> Option<i64> {
    let (signed, bits) = parse_encoding(encoding)?;
    let mut val: u64 = 0;
    for i in 0..bits as usize {
        let global_bit = bit_offset + i;
        let byte_i = global_bit / 8;
        let bit_i = 7 - (global_bit % 8);
        let b = bytes.get(byte_i).copied().unwrap_or(0);
        if b & (1 << bit_i) != 0 {
            val |= 1u64 << (bits as usize - 1 - i);
        }
    }
    if signed && bits < 64 && val & (1u64 << (bits - 1)) != 0 {
        // Sign extend
        let extended = val | !((1u64 << bits) - 1);
        Some(extended as i64)
    } else {
        Some(val as i64)
    }
}

fn bitfield_set(bytes: &mut Vec<u8>, encoding: &str, bit_offset: usize, value: i64) {
    let (_, bits) = match parse_encoding(encoding) {
        Some(e) => e,
        None => return,
    };
    let val = value as u64;
    let needed_bytes = (bit_offset + bits as usize).div_ceil(8);
    if bytes.len() < needed_bytes {
        bytes.resize(needed_bytes, 0);
    }
    for i in 0..bits as usize {
        let global_bit = bit_offset + i;
        let byte_i = global_bit / 8;
        let bit_i = 7 - (global_bit % 8);
        let src_bit = (bits as usize - 1 - i) as u64;
        if val & (1u64 << src_bit) != 0 {
            bytes[byte_i] |= 1 << bit_i;
        } else {
            bytes[byte_i] &= !(1 << bit_i);
        }
    }
}

fn bitfield_clamp(encoding: &str, value: i128, overflow: &str) -> Option<i128> {
    let (signed, bits) = parse_encoding(encoding)?;
    let (min, max): (i128, i128) = if signed {
        (-(1i128 << (bits - 1)), (1i128 << (bits - 1)) - 1)
    } else {
        (0, (1i128 << bits) - 1)
    };
    match overflow {
        "FAIL" => {
            if value < min || value > max {
                None
            } else {
                Some(value)
            }
        }
        "SAT" => Some(value.clamp(min, max)),
        _ => {
            // WRAP
            let range = max - min + 1;
            let mut v = value % range;
            if v < min {
                v += range;
            }
            Some(v)
        }
    }
}

// ── Public handle function ─────────────────────────────────────────────────────
pub fn handle(ctx: &mut Ctx, name: &str) -> Option<Vec<u8>> {
    match name {
        "GETBIT" => Some(cmd_getbit(ctx)),
        "SETBIT" => Some(cmd_setbit(ctx)),
        "BITCOUNT" => Some(cmd_bitcount(ctx)),
        "BITPOS" => Some(cmd_bitpos(ctx)),
        "BITOP" => Some(cmd_bitop(ctx)),
        "BITFIELD" => Some(cmd_bitfield(ctx, false)),
        "BITFIELD_RO" => Some(cmd_bitfield(ctx, true)),
        _ => None,
    }
}