wedb_embed 0.1.8

Embedded database engine providing Redis-like APIs, built on fjall / 嵌入式数据库引擎,提供类似 Redis 的接口,底层基于 fjall 开发
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
pub use super::r#const::*;
use crate::{
  bitmap::opt::{
    BitOp, BitfieldEncoding, BitfieldOpType, BitfieldOperation, BitfieldOverflow, BitfieldValue,
  },
  error::{Error, Result},
};

/// Computes segment index for the given bit offset (aligned with Kvrocks SegmentSubKeyIndexForBit).
/// 计算指定位偏移所属的分段索引(对标 Kvrocks SegmentSubKeyIndexForBit / kBitmapSegmentBits)
#[inline]
pub const fn segment_index_for_bit(bit_offset: u64) -> u32 {
  (bit_offset / (BITMAP_SEGMENT_BITS as u64)) as u32
}

/// Computes starting byte offset of segment for the given bit offset.
/// 计算指定位偏移所属分段的字节起点偏移
#[inline]
pub const fn segment_byte_offset_for_bit(bit_offset: u64) -> u32 {
  segment_index_for_bit(bit_offset) * (BITMAP_SEGMENT_BYTES as u32)
}

/// Expands bitmap segment capacity up to min_bytes (aligned with Kvrocks ExpandBitmapSegment).
/// 扩展分段字节容量至 min_bytes(按需倍增至 1024 字节,对标 Kvrocks ExpandBitmapSegment)
#[inline]
pub fn expand_bitmap_segment(segment: &mut Vec<u8>, min_bytes: usize) {
  debug_assert!(min_bytes <= BITMAP_SEGMENT_BYTES);
  let old_size = segment.len();
  if min_bytes > old_size {
    let new_size = (old_size * 2).clamp(min_bytes, BITMAP_SEGMENT_BYTES);
    segment.resize(new_size, 0);
  }
}

/// Gets bit value in segment using LSB order (aligned with Kvrocks util::lsb::GetBit).
/// 获取分段中指定位的值(LSB 顺序,对标 Apache Kvrocks util::lsb::GetBit)
#[inline]
pub fn get_bit_lsb(segment: &[u8], bit_offset_in_segment: usize) -> u8 {
  let byte_idx = bit_offset_in_segment >> 3;
  if byte_idx < segment.len() {
    (segment[byte_idx] >> (bit_offset_in_segment & 7)) & 1
  } else {
    0
  }
}

/// Sets bit value in segment using LSB order, returning old bit (aligned with Kvrocks SetBitTo).
/// 设置分段中指定位的值(LSB 顺序,返回原位值,对标 Apache Kvrocks util::lsb::SetBitTo)
#[inline]
pub fn set_bit_lsb(segment: &mut [u8], bit_offset_in_segment: usize, bit: u8) -> u8 {
  let byte_idx = bit_offset_in_segment >> 3;
  let shift = bit_offset_in_segment & 7;
  let old = (segment[byte_idx] >> shift) & 1;
  if bit != 0 {
    segment[byte_idx] |= 1 << shift;
  } else {
    segment[byte_idx] &= !(1 << shift);
  }
  old
}

/// Gets bit value in byte slice using MSB order.
/// 获取连续字节中指定位的值(MSB 顺序,用于标准 Redis 兼容格式)
#[inline]
pub fn get_bit_from_bytes(bytes: &[u8], bit_offset: usize) -> u8 {
  let byte_idx = bit_offset >> 3;
  if byte_idx < bytes.len() {
    (bytes[byte_idx] >> (7 - (bit_offset & 7))) & 1
  } else {
    0
  }
}

/// Sets specific bit in contiguous bytes using MSB ordering for standard Redis compatibility.
/// 设置连续字节中指定位的值(MSB 顺序,用于标准 Redis 兼容格式)
#[inline]
pub fn set_bit_in_bytes(bytes: &mut Vec<u8>, bit_offset: usize, bit: u8) -> u8 {
  let byte_idx = bit_offset >> 3;
  if byte_idx >= bytes.len() {
    bytes.resize(byte_idx + 1, 0);
  }
  let shift = 7 - (bit_offset & 7);
  let old = (bytes[byte_idx] >> shift) & 1;
  if bit != 0 {
    bytes[byte_idx] |= 1 << shift;
  } else {
    bytes[byte_idx] &= !(1 << shift);
  }
  old
}

/// High-performance MSB bit position search aligned with Apache Kvrocks util::msb::RawBitpos.
/// 高性能大端序 MSB 位检索(对标 Apache Kvrocks util::msb::RawBitpos)
#[inline]
pub fn raw_bitpos(bytes: &[u8], bit: u8) -> Option<usize> {
  let mut offset = 0usize;
  let (chunks, remainder) = bytes.as_chunks::<8>();
  for chunk in chunks {
    let word = u64::from_be_bytes(*chunk);
    if bit == 1 {
      if word != 0 {
        return Some(offset + word.leading_zeros() as usize);
      }
    } else if word != u64::MAX {
      return Some(offset + (!word).leading_zeros() as usize);
    }
    offset += 64;
  }
  for &b in remainder {
    if bit == 1 {
      if b != 0 {
        return Some(offset + b.leading_zeros() as usize);
      }
    } else if b != 0xFF {
      return Some(offset + (!b).leading_zeros() as usize);
    }
    offset += 8;
  }
  None
}

/// High-performance LSB bit position search for bitmap segment storage acceleration aligned with util::lsb.
/// 高性能小端序 LSB 位检索(用于 Kvrocks Bitmap 分段原生存储加速,对标 util::lsb)
#[inline]
pub fn raw_bitpos_lsb(bytes: &[u8], bit: u8) -> Option<usize> {
  let mut offset = 0usize;
  let (chunks, remainder) = bytes.as_chunks::<8>();
  for chunk in chunks {
    let word = u64::from_le_bytes(*chunk);
    if bit == 1 {
      if word != 0 {
        return Some(offset + word.trailing_zeros() as usize);
      }
    } else if word != u64::MAX {
      return Some(offset + (!word).trailing_zeros() as usize);
    }
    offset += 64;
  }
  for &b in remainder {
    if bit == 1 {
      if b != 0 {
        return Some(offset + b.trailing_zeros() as usize);
      }
    } else if b != 0xFF {
      return Some(offset + (!b).trailing_zeros() as usize);
    }
    offset += 8;
  }
  None
}

/// Finds first target bit within [start_bit, stop_bit] in a single byte using LSB order with O(1) branchless bitwise operations.
/// 在单字节中按 LSB 顺序查找指定区间 [start_bit, stop_bit] 内首个目标位(O(1) 零分支快速位运算)
#[inline]
pub const fn find_bit_in_byte_lsb(
  b: u8,
  bit: u8,
  start_bit: usize,
  stop_bit: usize,
) -> Option<usize> {
  debug_assert!(start_bit <= stop_bit && stop_bit < 8);
  let mask = (((1u16 << (stop_bit - start_bit + 1)) - 1) as u8) << start_bit;
  let target = if bit == 1 { b } else { !b };
  let masked = target & mask;
  if masked != 0 {
    Some(masked.trailing_zeros() as usize)
  } else {
    None
  }
}

/// Finds first target bit within [start_bit, stop_bit] in a single byte using MSB order with O(1) branchless bitwise operations.
/// 在单字节中按 MSB 顺序查找指定区间 [start_bit, stop_bit] 内首个目标位(O(1) 零分支快速位运算)
#[inline]
pub const fn find_bit_in_byte_msb(
  b: u8,
  bit: u8,
  start_bit: usize,
  stop_bit: usize,
) -> Option<usize> {
  debug_assert!(start_bit <= stop_bit && stop_bit < 8);
  let mask = (((1u16 << (stop_bit - start_bit + 1)) - 1) as u8) << (7 - stop_bit);
  let target = if bit == 1 { b } else { !b };
  let masked = target & mask;
  if masked != 0 {
    Some(masked.leading_zeros() as usize)
  } else {
    None
  }
}

/// High-performance 64-bit native CPU popcount bit counting aligned with Apache Kvrocks util::RawPopcount.
/// 高性能 64 位原生 CPU POPCNT 位统计(对标 Apache Kvrocks util::RawPopcount)
#[inline]
pub fn raw_popcount(bytes: &[u8]) -> u64 {
  let mut count = 0u64;
  let (chunks, remainder) = bytes.as_chunks::<8>();
  for chunk in chunks {
    let word = u64::from_ne_bytes(*chunk);
    count += word.count_ones() as u64;
  }
  for &b in remainder {
    count += b.count_ones() as u64;
  }
  count
}

pub use crate::meta::normalize_bitmap_range as normalize_range;

/// Normalizes bit index into byte range and padding masks aligned with Kvrocks NormalizeToByteRangeWithPaddingMask.
/// 位图位索引标准化为字节范围与位掩码(对标 Kvrocks NormalizeToByteRangeWithPaddingMask)
#[inline]
pub const fn normalize_bit_range_to_byte_mask(
  start_bit: i64,
  end_bit: i64,
) -> (usize, usize, u8, u8) {
  debug_assert!(start_bit <= end_bit);
  let first_byte_neg_mask = (!((1u16 << (8 - (start_bit & 7))) - 1)) as u8;
  let last_byte_neg_mask = ((1u16 << (7 - (end_bit & 7))) - 1) as u8;
  let start_byte = (start_bit >> 3) as usize;
  let end_byte = (end_bit >> 3) as usize;
  (
    start_byte,
    end_byte,
    first_byte_neg_mask,
    last_byte_neg_mask,
  )
}

/// Range and mask normalization supporting byte and bit indices aligned with Kvrocks.
/// 支持字节与位索引的范围与掩码归一化(对标 Kvrocks)
#[inline]
pub const fn normalize_to_byte_range_with_padding_mask(
  is_bit_index: bool,
  start: i64,
  end: i64,
) -> (usize, usize, u8, u8) {
  if is_bit_index {
    normalize_bit_range_to_byte_mask(start, end)
  } else {
    (start as usize, end as usize, 0, 0)
  }
}

/// Small 9-byte local buffer for cross-segment high-precision bitfield operations aligned with Kvrocks ArrayBitfieldBitmap.
/// 9 字节局部小缓冲结构,用于跨分段高精度读取和写入 Bitfield(对标 Kvrocks ArrayBitfieldBitmap)
#[derive(Debug, Clone)]
pub struct ArrayBitfieldBitmap {
  pub buf: [u8; 9],
  pub byte_offset: u32,
}

impl Default for ArrayBitfieldBitmap {
  fn default() -> Self {
    Self::new(0)
  }
}

impl ArrayBitfieldBitmap {
  pub const SIZE: usize = 9;

  #[inline]
  pub const fn new(byte_offset: u32) -> Self {
    Self {
      buf: [0u8; Self::SIZE],
      byte_offset,
    }
  }

  #[inline]
  pub fn set_byte_offset(&mut self, byte_offset: u32) {
    self.byte_offset = byte_offset;
  }

  #[inline]
  pub fn reset(&mut self) {
    self.buf.fill(0);
  }

  #[inline]
  pub fn set(&mut self, byte_offset: u32, src: &[u8]) -> Result<()> {
    let bytes = src.len();
    if byte_offset < self.byte_offset
      || (byte_offset + bytes as u32) > (self.byte_offset + Self::SIZE as u32)
    {
      return Err(Error::invalid_data(
        "The range [offset, offset + bytes) is out of bitfield buffer",
      ));
    }
    let rel_offset = (byte_offset - self.byte_offset) as usize;
    self.buf[rel_offset..rel_offset + bytes].copy_from_slice(src);
    Ok(())
  }

  #[inline]
  pub fn get(&self, byte_offset: u32, dst: &mut [u8]) -> Result<()> {
    let bytes = dst.len();
    if byte_offset < self.byte_offset
      || (byte_offset + bytes as u32) > (self.byte_offset + Self::SIZE as u32)
    {
      return Err(Error::invalid_data(
        "The range [offset, offset + bytes) is out of bitfield buffer",
      ));
    }
    let rel_offset = (byte_offset - self.byte_offset) as usize;
    dst.copy_from_slice(&self.buf[rel_offset..rel_offset + bytes]);
    Ok(())
  }

  #[inline]
  pub fn get_unsigned_bitfield(&self, bit_offset: u64, bits: u8) -> Result<u64> {
    if bits == 0 || bits > 63 {
      return Err(Error::invalid_data("Invalid unsigned bits (1..=63)"));
    }
    self.read_raw_bitfield(bit_offset, bits)
  }

  #[inline]
  pub fn get_signed_bitfield(&self, bit_offset: u64, bits: u8) -> Result<i64> {
    if bits == 0 || bits > 64 {
      return Err(Error::invalid_data("Invalid signed bits (1..=64)"));
    }
    let raw = self.read_raw_bitfield(bit_offset, bits)?;
    let mut val = raw as i64;
    let msb = 1u64 << (bits - 1);
    if (raw & msb) != 0 && bits < 64 {
      let mask = u64::MAX << bits;
      val |= mask as i64;
    }
    Ok(val)
  }

  #[inline]
  fn read_raw_bitfield(&self, bit_offset: u64, bits: u8) -> Result<u64> {
    let first_byte = (bit_offset / 8) as u32;
    let last_byte = ((bit_offset + bits as u64 - 1) / 8 + 1) as u32;
    let bytes = (last_byte - first_byte) as usize;

    if first_byte < self.byte_offset
      || (first_byte + bytes as u32) > (self.byte_offset + Self::SIZE as u32)
    {
      return Err(Error::invalid_data("Bitfield range out of buffer"));
    }

    let rel_bit_offset = (bit_offset - (self.byte_offset as u64 * 8)) as usize;
    let mut word_bytes = [0u8; 16];
    word_bytes[7..16].copy_from_slice(&self.buf);
    let word = u128::from_be_bytes(word_bytes);
    let shift = 72 - rel_bit_offset - (bits as usize);
    let mask = if bits == 64 {
      u64::MAX
    } else {
      (1u64 << bits) - 1
    };
    Ok(((word >> shift) as u64) & mask)
  }

  #[inline]
  pub fn set_bitfield(&mut self, bit_offset: u64, bits: u8, value: u64) -> Result<()> {
    let first_byte = (bit_offset / 8) as u32;
    let last_byte = ((bit_offset + bits as u64 - 1) / 8 + 1) as u32;
    let bytes = (last_byte - first_byte) as usize;

    if first_byte < self.byte_offset
      || (first_byte + bytes as u32) > (self.byte_offset + Self::SIZE as u32)
    {
      return Err(Error::invalid_data("Bitfield range out of buffer"));
    }

    let rel_bit_offset = (bit_offset - (self.byte_offset as u64 * 8)) as usize;
    let mut word_bytes = [0u8; 16];
    word_bytes[7..16].copy_from_slice(&self.buf);
    let mut word = u128::from_be_bytes(word_bytes);
    let shift = 72 - rel_bit_offset - (bits as usize);
    let bit_mask = if bits == 64 {
      u64::MAX as u128
    } else {
      (1u128 << bits) - 1
    };
    let mask = bit_mask << shift;
    let val = ((value as u128) & bit_mask) << shift;
    word = (word & !mask) | val;
    let updated_bytes = word.to_be_bytes();
    self.buf.copy_from_slice(&updated_bytes[7..16]);
    Ok(())
  }
}

/// Signed bitfield addition with overflow handling aligned with Kvrocks detail::SignedBitfieldPlus.
/// 有符号 BITFIELD 溢出加法运算(对标 Kvrocks detail::SignedBitfieldPlus)
#[inline]
pub fn signed_bitfield_plus(
  value: u64,
  incr: i64,
  bits: u8,
  overflow: BitfieldOverflow,
) -> (u64, bool) {
  let max = if bits == 64 {
    i64::MAX
  } else {
    (1i64 << (bits - 1)) - 1
  };
  let min = -max - 1;

  let signed_val = value as i64;
  let max_incr = (max as u64).wrapping_sub(value) as i64;
  let min_incr = min.wrapping_sub(signed_val);

  if signed_val > max
    || (bits != 64 && incr > max_incr)
    || (signed_val >= 0 && incr >= 0 && incr > max_incr)
  {
    match overflow {
      BitfieldOverflow::Wrap => (wrapped_signed_bitfield_plus(value, incr, bits), true),
      BitfieldOverflow::Sat => (max as u64, true),
      BitfieldOverflow::Fail => (0, true),
    }
  } else if signed_val < min
    || (bits != 64 && incr < min_incr)
    || (signed_val < 0 && incr < 0 && incr < min_incr)
  {
    match overflow {
      BitfieldOverflow::Wrap => (wrapped_signed_bitfield_plus(value, incr, bits), true),
      BitfieldOverflow::Sat => (min as u64, true),
      BitfieldOverflow::Fail => (0, true),
    }
  } else {
    (signed_val.wrapping_add(incr) as u64, false)
  }
}

#[inline]
const fn wrapped_signed_bitfield_plus(value: u64, incr: i64, bits: u8) -> u64 {
  let res = value.wrapping_add(incr as u64);
  if bits < 64 {
    let mask = u64::MAX << bits;
    if (res & (1u64 << (bits - 1))) != 0 {
      res | mask
    } else {
      res & !mask
    }
  } else {
    res
  }
}

/// Unsigned bitfield addition with overflow handling aligned with Kvrocks detail::UnsignedBitfieldPlus.
/// 无符号 BITFIELD 溢出加法运算(对标 Kvrocks detail::UnsignedBitfieldPlus)
#[inline]
pub fn unsigned_bitfield_plus(
  value: u64,
  incr: i64,
  bits: u8,
  overflow: BitfieldOverflow,
) -> (u64, bool) {
  let max = if bits == 64 {
    u64::MAX
  } else {
    (1u64 << bits) - 1
  };
  let max_incr = max.wrapping_sub(value) as i64;
  let min_incr = (!value).wrapping_add(1) as i64;

  if value > max || (incr > 0 && incr > max_incr) {
    match overflow {
      BitfieldOverflow::Wrap => (wrapped_unsigned_bitfield_plus(value, incr, bits), true),
      BitfieldOverflow::Sat => (max, true),
      BitfieldOverflow::Fail => (0, true),
    }
  } else if incr < 0 && incr < min_incr {
    match overflow {
      BitfieldOverflow::Wrap => (wrapped_unsigned_bitfield_plus(value, incr, bits), true),
      BitfieldOverflow::Sat => (0, true),
      BitfieldOverflow::Fail => (0, true),
    }
  } else {
    (value.wrapping_add(incr as u64), false)
  }
}

#[inline]
const fn wrapped_unsigned_bitfield_plus(value: u64, incr: i64, bits: u8) -> u64 {
  let mask = if bits == 64 { 0 } else { u64::MAX << bits };
  let res = value.wrapping_add(incr as u64);
  res & !mask
}

/// Executes a single bitfield logical operation aligned with Kvrocks BitfieldOp.
/// 执行单步 BITFIELD 逻辑运算(对标 Kvrocks BitfieldOp)
#[inline]
pub fn bitfield_op_calc(
  op: &BitfieldOperation,
  old_value: u64,
) -> (Option<BitfieldValue>, u64, bool) {
  if op.op_type == BitfieldOpType::Get {
    let val = if op.encoding.is_signed() {
      BitfieldValue::Signed(old_value as i64)
    } else {
      BitfieldValue::Unsigned(old_value)
    };
    return (Some(val), old_value, false);
  }

  let (new_value, is_overflow) = match op.encoding {
    BitfieldEncoding::Signed(bits) => {
      let input_val = if op.op_type == BitfieldOpType::Set {
        op.value as u64
      } else {
        old_value
      };
      let incr = if op.op_type == BitfieldOpType::Set {
        0
      } else {
        op.value
      };
      signed_bitfield_plus(input_val, incr, bits, op.overflow)
    }
    BitfieldEncoding::Unsigned(bits) => {
      let input_val = if op.op_type == BitfieldOpType::Set {
        op.value as u64
      } else {
        old_value
      };
      let incr = if op.op_type == BitfieldOpType::Set {
        0
      } else {
        op.value
      };
      unsigned_bitfield_plus(input_val, incr, bits, op.overflow)
    }
  };

  if op.overflow == BitfieldOverflow::Fail && is_overflow {
    return (None, old_value, true);
  }

  let returned_val = if op.op_type == BitfieldOpType::Set {
    if op.encoding.is_signed() {
      BitfieldValue::Signed(old_value as i64)
    } else {
      BitfieldValue::Unsigned(old_value)
    }
  } else if op.encoding.is_signed() {
    BitfieldValue::Signed(new_value as i64)
  } else {
    BitfieldValue::Unsigned(new_value)
  };

  (Some(returned_val), new_value, false)
}

/// 64-bit word vectorized bitwise AND operation.
/// 64 位原生字向量化位与操作
#[inline]
pub fn bitwise_and(dst: &mut [u8], src: &[u8]) {
  let common_len = dst.len().min(src.len());
  let (dst_chunks, dst_rem) = dst[..common_len].as_chunks_mut::<8>();
  let (src_chunks, src_rem) = src[..common_len].as_chunks::<8>();

  for (d, s) in dst_chunks.iter_mut().zip(src_chunks.iter()) {
    let dw = u64::from_ne_bytes(*d);
    let sw = u64::from_ne_bytes(*s);
    *d = (dw & sw).to_ne_bytes();
  }
  for (d, s) in dst_rem.iter_mut().zip(src_rem.iter()) {
    *d &= *s;
  }
  dst[common_len..].fill(0);
}

/// 64-bit word vectorized bitwise OR operation.
/// 64 位原生字向量化位或操作
#[inline]
pub fn bitwise_or(dst: &mut [u8], src: &[u8]) {
  let len = dst.len().min(src.len());
  let (dst_chunks, dst_rem) = dst[..len].as_chunks_mut::<8>();
  let (src_chunks, src_rem) = src[..len].as_chunks::<8>();

  for (d, s) in dst_chunks.iter_mut().zip(src_chunks.iter()) {
    let dw = u64::from_ne_bytes(*d);
    let sw = u64::from_ne_bytes(*s);
    *d = (dw | sw).to_ne_bytes();
  }
  for (d, s) in dst_rem.iter_mut().zip(src_rem.iter()) {
    *d |= *s;
  }
}

/// 64-bit word vectorized bitwise XOR operation.
/// 64 位原生字向量化位异或操作
#[inline]
pub fn bitwise_xor(dst: &mut [u8], src: &[u8]) {
  let len = dst.len().min(src.len());
  let (dst_chunks, dst_rem) = dst[..len].as_chunks_mut::<8>();
  let (src_chunks, src_rem) = src[..len].as_chunks::<8>();

  for (d, s) in dst_chunks.iter_mut().zip(src_chunks.iter()) {
    let dw = u64::from_ne_bytes(*d);
    let sw = u64::from_ne_bytes(*s);
    *d = (dw ^ sw).to_ne_bytes();
  }
  for (d, s) in dst_rem.iter_mut().zip(src_rem.iter()) {
    *d ^= *s;
  }
}

/// 64-bit word vectorized bitwise NOT operation.
/// 64 位原生字向量化位非操作
#[inline]
pub fn bitwise_not(dst: &mut [u8], src: &[u8]) {
  let len = dst.len().min(src.len());
  let (dst_chunks, dst_rem) = dst[..len].as_chunks_mut::<8>();
  let (src_chunks, src_rem) = src[..len].as_chunks::<8>();

  for (d, s) in dst_chunks.iter_mut().zip(src_chunks.iter()) {
    let sw = u64::from_ne_bytes(*s);
    *d = (!sw).to_ne_bytes();
  }
  for (d, s) in dst_rem.iter_mut().zip(src_rem.iter()) {
    *d = !*s;
  }
}

/// High-performance 64-bit word slice bitmap operation writing into buffer aligned with Kvrocks Bitmap::BitOp.
/// 高性能 64 位字切片位图操作(零堆分配写入给定缓冲,对标 Kvrocks Bitmap::BitOp)
#[inline]
pub fn bit_op_exec_into(op: BitOp, src_slices: &[&[u8]], out: &mut [u8]) -> Result<usize> {
  let out_len = out.len();
  match op {
    BitOp::And => {
      if let Some((first, rest)) = src_slices.split_first() {
        let copy_len = first.len().min(out_len);
        out[..copy_len].copy_from_slice(&first[..copy_len]);
        out[copy_len..out_len].fill(0);
        for &src in rest {
          bitwise_and(out, src);
        }
      } else {
        out.fill(0);
      }
    }
    BitOp::Or => {
      if let Some((first, rest)) = src_slices.split_first() {
        let copy_len = first.len().min(out_len);
        out[..copy_len].copy_from_slice(&first[..copy_len]);
        out[copy_len..out_len].fill(0);
        for &src in rest {
          bitwise_or(out, src);
        }
      } else {
        out.fill(0);
      }
    }
    BitOp::Xor => {
      if let Some((first, rest)) = src_slices.split_first() {
        let copy_len = first.len().min(out_len);
        out[..copy_len].copy_from_slice(&first[..copy_len]);
        out[copy_len..out_len].fill(0);
        for &src in rest {
          bitwise_xor(out, src);
        }
      } else {
        out.fill(0);
      }
    }
    BitOp::Not => {
      if src_slices.len() != 1 {
        return Err(Error::invalid_data(
          "ERR BITOP NOT takes exactly one source key",
        ));
      }
      let src = src_slices[0];
      let src_len = src.len().min(out_len);
      bitwise_not(&mut out[..src_len], &src[..src_len]);
      if out_len > src_len {
        out[src_len..out_len].fill(0xFF);
      }
    }
  }

  Ok(out_len)
}

/// High-performance 64-bit word slice bitmap operation for AND / OR / XOR / NOT.
/// 高性能 64 位字切片位图操作(AND / OR / XOR / NOT)
pub fn bit_op_exec(op: &str, src_slices: &[&[u8]]) -> Result<Vec<u8>> {
  let bit_op = op.parse::<BitOp>()?;
  let max_len = src_slices.iter().map(|s| s.len()).max().unwrap_or(0);
  let mut out = vec![0u8; max_len];
  let written = bit_op_exec_into(bit_op, src_slices, &mut out)?;
  out.truncate(written);
  Ok(out)
}

/// String-mode BITCOUNT in MSB order aligned with Apache Kvrocks BitmapString::BitCount.
/// 字符串模式 BITCOUNT(MSB 顺序,对标 Apache Kvrocks BitmapString::BitCount)
pub fn string_bitcount(
  val: &[u8],
  start: Option<i64>,
  end: Option<i64>,
  is_bit_index: bool,
) -> u64 {
  let strlen = val.len() as i64;
  let totlen = if is_bit_index { strlen << 3 } else { strlen };
  let s = start.unwrap_or(0);
  let e = end.unwrap_or(-1);
  if s < 0 && e < 0 && s > e {
    return 0;
  }
  let (norm_s, norm_e) = normalize_range(s, e, totlen);
  if norm_s > norm_e {
    return 0;
  }

  let (start_byte, stop_byte, first_mask, last_mask) =
    normalize_to_byte_range_with_padding_mask(is_bit_index, norm_s, norm_e);

  if start_byte >= val.len() {
    return 0;
  }
  let actual_stop = stop_byte.min(val.len().saturating_sub(1));
  if start_byte > actual_stop {
    return 0;
  }

  let bytes = &val[start_byte..=actual_stop];
  let cnt = raw_popcount(bytes);

  let mut mask_cnt = 0u64;
  if first_mask != 0 && start_byte < val.len() {
    mask_cnt += (val[start_byte] & first_mask).count_ones() as u64;
  }
  if last_mask != 0 && actual_stop == stop_byte && actual_stop < val.len() {
    mask_cnt += (val[actual_stop] & last_mask).count_ones() as u64;
  }
  cnt.saturating_sub(mask_cnt)
}

/// String-mode BITPOS in MSB order aligned with Apache Kvrocks BitmapString::BitPos.
/// 字符串模式 BITPOS(MSB 顺序,对标 Apache Kvrocks BitmapString::BitPos)
pub fn string_bitpos(
  val: &[u8],
  bit: u8,
  start: Option<i64>,
  end: Option<i64>,
  stop_given: bool,
  is_bit_index: bool,
) -> i64 {
  let strlen = val.len() as i64;
  let length = if is_bit_index { strlen * 8 } else { strlen };
  let s = start.unwrap_or(0);
  let e = end.unwrap_or(-1);
  let (norm_s, norm_e) = normalize_range(s, e, length);
  if norm_s > norm_e {
    return -1;
  }

  let mut byte_start = (if is_bit_index { norm_s / 8 } else { norm_s }) as usize;
  let byte_stop = (if is_bit_index { norm_e / 8 } else { norm_e }) as usize;
  let bit_in_start_byte = if is_bit_index {
    (norm_s % 8) as usize
  } else {
    0
  };
  let bit_in_stop_byte = if is_bit_index {
    (norm_e % 8) as usize
  } else {
    7
  };

  if is_bit_index && byte_start == byte_stop {
    if byte_start < val.len()
      && let Some(bit_idx) =
        find_bit_in_byte_msb(val[byte_start], bit, bit_in_start_byte, bit_in_stop_byte)
    {
      return (byte_start * 8 + bit_idx) as i64;
    }
    return -1;
  }

  if is_bit_index && bit_in_start_byte != 0 {
    if byte_start < val.len()
      && let Some(bit_idx) = find_bit_in_byte_msb(val[byte_start], bit, bit_in_start_byte, 7)
    {
      return (byte_start * 8 + bit_idx) as i64;
    }
    byte_start += 1;
  }

  if byte_start > byte_stop || byte_start >= val.len() {
    return if stop_given && bit == 0 {
      -1
    } else if bit == 0 {
      strlen * 8
    } else {
      -1
    };
  }

  let actual_stop = byte_stop.min(val.len() - 1);
  let bytes_cnt = actual_stop - byte_start + 1;
  let pos_opt = raw_bitpos(&val[byte_start..byte_start + bytes_cnt], bit);

  match pos_opt {
    Some(pos) => {
      let abs_pos = (pos + byte_start * 8) as i64;
      if is_bit_index && abs_pos > norm_e {
        return -1;
      }
      abs_pos
    }
    None => {
      if stop_given && bit == 0 {
        -1
      } else if bit == 0 {
        strlen * 8
      } else {
        -1
      }
    }
  }
}