fast-cache 0.1.0

Embedded-first thread-per-core in-memory cache with optional Redis-compatible server
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
use std::borrow::Cow;
use std::ptr;

use crate::config::ReplicationCompression;
use bytes::Bytes as SharedBytes;

use crate::storage::{MutationOp, MutationRecord, StoredEntry, hash_key, hash_key_tag_from_hash};
use crate::{FastCacheError, Result};

pub const FCRP_MAGIC: &[u8; 4] = b"FCRP";
pub const FCRP_VERSION: u8 = 1;

const HEADER_LEN: usize = 16;
pub(crate) const FRAME_HEADER_LEN: usize = HEADER_LEN;
const FLAG_COMPRESSED: u8 = 0x01;
// u64::MAX is reserved as the wire sentinel for "no expiry". A legitimate
// `expire_at_ms` of u64::MAX would map to year ~584,942,417, so it is safe to
// reserve.
const EXPIRE_NONE: u64 = u64::MAX;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum FrameKind {
    Hello = 1,
    SnapshotBegin = 2,
    SnapshotChunk = 3,
    SnapshotEnd = 4,
    MutationBatch = 5,
    Ack = 6,
    Error = 7,
}

impl FrameKind {
    fn from_u8(value: u8) -> Result<Self> {
        match value {
            1 => Ok(Self::Hello),
            2 => Ok(Self::SnapshotBegin),
            3 => Ok(Self::SnapshotChunk),
            4 => Ok(Self::SnapshotEnd),
            5 => Ok(Self::MutationBatch),
            6 => Ok(Self::Ack),
            7 => Ok(Self::Error),
            other => Err(FastCacheError::Protocol(format!(
                "unsupported FCRP frame kind: {other}"
            ))),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReplicationCompressionMode {
    None,
    Zstd,
}

impl From<ReplicationCompression> for ReplicationCompressionMode {
    fn from(value: ReplicationCompression) -> Self {
        match value {
            ReplicationCompression::None => Self::None,
            ReplicationCompression::Zstd => Self::Zstd,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplicationFrame {
    pub kind: FrameKind,
    pub compressed: bool,
    pub payload: Vec<u8>,
}

#[derive(Debug)]
pub struct ReplicationFramePayload<'a> {
    pub kind: FrameKind,
    pub compressed: bool,
    pub payload: Cow<'a, [u8]>,
}

#[derive(Debug, Clone)]
pub struct ReplicationFrameBytesPayload {
    pub kind: FrameKind,
    pub compressed: bool,
    pub payload: SharedBytes,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShardWatermarks {
    values: Vec<u64>,
}

impl ShardWatermarks {
    pub fn new(shard_count: usize) -> Self {
        Self {
            values: vec![0; shard_count],
        }
    }

    pub fn from_vec(values: Vec<u64>) -> Self {
        Self { values }
    }

    pub fn as_slice(&self) -> &[u64] {
        &self.values
    }

    pub fn into_vec(self) -> Vec<u64> {
        self.values
    }

    pub fn get(&self, shard_id: usize) -> u64 {
        self.values.get(shard_id).copied().unwrap_or(0)
    }

    pub fn observe(&mut self, shard_id: usize, sequence: u64) {
        if shard_id >= self.values.len() {
            self.values.resize(shard_id + 1, 0);
        }
        self.values[shard_id] = self.values[shard_id].max(sequence);
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReplicationMutationOp {
    Set,
    Del,
    Expire,
}

impl From<&MutationOp> for ReplicationMutationOp {
    fn from(value: &MutationOp) -> Self {
        match value {
            MutationOp::Set => Self::Set,
            MutationOp::Del => Self::Del,
            MutationOp::Expire => Self::Expire,
        }
    }
}

impl ReplicationMutationOp {
    fn to_byte(self) -> u8 {
        match self {
            Self::Set => 1,
            Self::Del => 2,
            Self::Expire => 3,
        }
    }

    fn from_byte(value: u8) -> Result<Self> {
        match value {
            1 => Ok(Self::Set),
            2 => Ok(Self::Del),
            3 => Ok(Self::Expire),
            other => Err(FastCacheError::Protocol(format!(
                "unsupported FCRP mutation op: {other}"
            ))),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplicationMutation {
    pub shard_id: usize,
    pub sequence: u64,
    pub timestamp_ms: u64,
    pub op: ReplicationMutationOp,
    pub key_hash: u64,
    pub key_tag: u64,
    pub key: SharedBytes,
    pub value: SharedBytes,
    pub expire_at_ms: Option<u64>,
}

#[derive(Debug, Clone, Copy)]
pub struct BorrowedReplicationMutation<'a> {
    pub shard_id: usize,
    pub sequence: u64,
    pub timestamp_ms: u64,
    pub op: ReplicationMutationOp,
    pub key_hash: u64,
    pub key_tag: u64,
    pub key: &'a [u8],
    pub value: &'a [u8],
    pub expire_at_ms: Option<u64>,
}

#[derive(Debug, Clone)]
pub struct FrameBackedReplicationMutation<'a> {
    pub shard_id: usize,
    pub sequence: u64,
    pub op: ReplicationMutationOp,
    pub key_hash: u64,
    pub key: &'a [u8],
    pub value: SharedBytes,
    pub expire_at_ms: Option<u64>,
}

impl ReplicationMutation {
    pub fn from_record(record: &MutationRecord) -> Self {
        let key_hash = hash_key(record.key.as_ref());
        Self::from_record_with_key_hash(record, key_hash)
    }

    pub fn from_record_with_key_hash(record: &MutationRecord, key_hash: u64) -> Self {
        Self {
            shard_id: record.shard_id,
            sequence: record.sequence,
            timestamp_ms: record.timestamp_ms,
            op: ReplicationMutationOp::from(&record.op),
            key_hash,
            key_tag: hash_key_tag_from_hash(key_hash),
            key: record.key.clone(),
            value: record.value.clone(),
            expire_at_ms: record.expire_at_ms,
        }
    }

    pub fn estimated_uncompressed_len(&self) -> usize {
        mutation_record_payload_len(self.key.len(), self.value.len())
    }

    pub(crate) fn as_borrowed(&self) -> BorrowedReplicationMutation<'_> {
        BorrowedReplicationMutation {
            shard_id: self.shard_id,
            sequence: self.sequence,
            timestamp_ms: self.timestamp_ms,
            op: self.op,
            key_hash: self.key_hash,
            key_tag: self.key_tag,
            key: self.key.as_ref(),
            value: self.value.as_ref(),
            expire_at_ms: self.expire_at_ms,
        }
    }
}

#[derive(Debug, Clone)]
pub struct ReplicationSnapshot {
    pub entries: Vec<StoredEntry>,
    pub watermarks: ShardWatermarks,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplicationSnapshotChunk {
    pub watermarks: ShardWatermarks,
    pub chunk_index: u64,
    pub is_last: bool,
    pub entries: Vec<StoredEntry>,
}

pub fn encode_frame(
    kind: FrameKind,
    compression: ReplicationCompressionMode,
    zstd_level: i32,
    payload: &[u8],
) -> Result<Vec<u8>> {
    match compression {
        ReplicationCompressionMode::None => {
            let mut out = Vec::with_capacity(HEADER_LEN + payload.len());
            write_header(
                &mut out,
                kind,
                0,
                payload.len() as u32,
                payload.len() as u32,
            );
            out.extend_from_slice(payload);
            Ok(out)
        }
        ReplicationCompressionMode::Zstd => {
            let compressed = zstd::bulk::compress(payload, zstd_level).map_err(|error| {
                FastCacheError::Protocol(format!("FCRP zstd compression failed: {error}"))
            })?;
            let mut out = Vec::with_capacity(HEADER_LEN + compressed.len());
            write_header(
                &mut out,
                kind,
                FLAG_COMPRESSED,
                compressed.len() as u32,
                payload.len() as u32,
            );
            out.extend_from_slice(&compressed);
            Ok(out)
        }
    }
}

fn write_header(
    out: &mut Vec<u8>,
    kind: FrameKind,
    flags: u8,
    payload_len: u32,
    uncompressed_len: u32,
) {
    out.extend_from_slice(FCRP_MAGIC);
    out.push(FCRP_VERSION);
    out.push(kind as u8);
    out.push(flags);
    out.push(0);
    out.extend_from_slice(&payload_len.to_le_bytes());
    out.extend_from_slice(&uncompressed_len.to_le_bytes());
}

pub(crate) fn write_uncompressed_frame_header_at(
    frame: &mut [u8],
    kind: FrameKind,
    payload_len: usize,
) {
    debug_assert!(frame.len() >= HEADER_LEN);
    frame[..4].copy_from_slice(FCRP_MAGIC);
    frame[4] = FCRP_VERSION;
    frame[5] = kind as u8;
    frame[6] = 0;
    frame[7] = 0;
    frame[8..12].copy_from_slice(&(payload_len as u32).to_le_bytes());
    frame[12..16].copy_from_slice(&(payload_len as u32).to_le_bytes());
}

pub fn decode_frame(bytes: &[u8]) -> Result<ReplicationFrame> {
    let frame = decode_frame_payload(bytes)?;
    Ok(ReplicationFrame {
        kind: frame.kind,
        compressed: frame.compressed,
        payload: frame.payload.into_owned(),
    })
}

pub fn decode_frame_payload(bytes: &[u8]) -> Result<ReplicationFramePayload<'_>> {
    if bytes.len() < HEADER_LEN {
        return Err(FastCacheError::Protocol("FCRP frame is truncated".into()));
    }
    if &bytes[..4] != FCRP_MAGIC {
        return Err(FastCacheError::Protocol("FCRP magic mismatch".into()));
    }
    if bytes[4] != FCRP_VERSION {
        return Err(FastCacheError::Protocol(format!(
            "unsupported FCRP version: {}",
            bytes[4]
        )));
    }
    let kind = FrameKind::from_u8(bytes[5])?;
    let flags = bytes[6];
    let payload_len = u32::from_le_bytes(bytes[8..12].try_into().unwrap()) as usize;
    let uncompressed_len = u32::from_le_bytes(bytes[12..16].try_into().unwrap()) as usize;
    if HEADER_LEN + payload_len != bytes.len() {
        return Err(FastCacheError::Protocol(
            "FCRP frame length mismatch".into(),
        ));
    }
    let raw = &bytes[HEADER_LEN..];
    let compressed = flags & FLAG_COMPRESSED != 0;
    let payload = if compressed {
        Cow::Owned(
            zstd::bulk::decompress(raw, uncompressed_len).map_err(|error| {
                FastCacheError::Protocol(format!("FCRP zstd decompression failed: {error}"))
            })?,
        )
    } else {
        Cow::Borrowed(raw)
    };
    Ok(ReplicationFramePayload {
        kind,
        compressed,
        payload,
    })
}

pub fn decode_frame_payload_bytes(bytes: SharedBytes) -> Result<ReplicationFrameBytesPayload> {
    if bytes.len() < HEADER_LEN {
        return Err(FastCacheError::Protocol("FCRP frame is truncated".into()));
    }
    if &bytes[..4] != FCRP_MAGIC {
        return Err(FastCacheError::Protocol("FCRP magic mismatch".into()));
    }
    if bytes[4] != FCRP_VERSION {
        return Err(FastCacheError::Protocol(format!(
            "unsupported FCRP version: {}",
            bytes[4]
        )));
    }

    let kind = FrameKind::from_u8(bytes[5])?;
    let flags = bytes[6];
    let payload_len = u32::from_le_bytes(bytes[8..12].try_into().unwrap()) as usize;
    let uncompressed_len = u32::from_le_bytes(bytes[12..16].try_into().unwrap()) as usize;
    if HEADER_LEN + payload_len != bytes.len() {
        return Err(FastCacheError::Protocol(
            "FCRP frame length mismatch".into(),
        ));
    }

    let compressed = flags & FLAG_COMPRESSED != 0;
    let payload = match compressed {
        true => {
            let raw = &bytes[HEADER_LEN..];
            SharedBytes::from(
                zstd::bulk::decompress(raw, uncompressed_len).map_err(|error| {
                    FastCacheError::Protocol(format!("FCRP zstd decompression failed: {error}"))
                })?,
            )
        }
        false => bytes.slice(HEADER_LEN..),
    };
    Ok(ReplicationFrameBytesPayload {
        kind,
        compressed,
        payload,
    })
}

pub fn encode_mutation_batch(mutations: &[ReplicationMutation]) -> Vec<u8> {
    let payload_len = mutation_batch_payload_len(mutations);
    let mut out = Vec::with_capacity(payload_len);
    write_mutation_batch_payload(&mut out, mutations);
    out
}

pub(crate) fn encode_mutation_batch_frame_with_payload_len(
    mutations: &[ReplicationMutation],
    payload_len: usize,
    compression: ReplicationCompressionMode,
    zstd_level: i32,
) -> Result<(Vec<u8>, usize)> {
    match compression {
        ReplicationCompressionMode::None => {
            let mut out = Vec::with_capacity(HEADER_LEN + payload_len);
            write_header(
                &mut out,
                FrameKind::MutationBatch,
                0,
                payload_len as u32,
                payload_len as u32,
            );
            write_mutation_batch_payload(&mut out, mutations);
            Ok((out, payload_len))
        }
        ReplicationCompressionMode::Zstd => {
            let mut payload = Vec::with_capacity(payload_len);
            write_mutation_batch_payload(&mut payload, mutations);
            encode_frame(FrameKind::MutationBatch, compression, zstd_level, &payload)
                .map(|frame| (frame, payload_len))
        }
    }
}

fn mutation_batch_payload_len(mutations: &[ReplicationMutation]) -> usize {
    4 + mutations
        .iter()
        .map(ReplicationMutation::estimated_uncompressed_len)
        .sum::<usize>()
}

pub(crate) fn mutation_record_payload_len(key_len: usize, value_len: usize) -> usize {
    4 + 8 + 8 + 1 + 8 + 8 + 8 + 4 + 4 + key_len + value_len
}

pub(crate) fn borrowed_mutation_record_payload_len(
    mutation: BorrowedReplicationMutation<'_>,
) -> usize {
    mutation_record_payload_len(mutation.key.len(), mutation.value.len())
}

pub(crate) fn mutation_batch_record_count(bytes: &[u8]) -> Result<usize> {
    let Some(count) = bytes.get(..4) else {
        return Err(FastCacheError::Protocol("FCRP payload is truncated".into()));
    };
    Ok(u32::from_le_bytes(count.try_into().unwrap()) as usize)
}

pub(crate) fn write_borrowed_mutation_payload_record(
    out: &mut Vec<u8>,
    mutation: BorrowedReplicationMutation<'_>,
) {
    let start = out.len();
    let record_len = borrowed_mutation_record_payload_len(mutation);
    out.reserve(record_len);
    // The frame builder just reserved `record_len`, and every write below
    // advances by exactly the encoded field width. This avoids one bounds and
    // capacity check per scalar field on the replication hot path.
    unsafe {
        let mut cursor = out.as_mut_ptr().add(start);
        write_u32_le(&mut cursor, mutation.shard_id as u32);
        write_u64_le(&mut cursor, mutation.sequence);
        write_u64_le(&mut cursor, mutation.timestamp_ms);
        write_u8(&mut cursor, mutation.op.to_byte());
        write_u64_le(&mut cursor, mutation.key_hash);
        write_u64_le(&mut cursor, mutation.key_tag);
        write_u64_le(&mut cursor, mutation.expire_at_ms.unwrap_or(EXPIRE_NONE));
        write_u32_le(&mut cursor, mutation.key.len() as u32);
        write_u32_le(&mut cursor, mutation.value.len() as u32);
        write_bytes(&mut cursor, mutation.key);
        write_bytes(&mut cursor, mutation.value);
        debug_assert_eq!(
            cursor.offset_from(out.as_ptr().add(start)),
            record_len as isize
        );
        out.set_len(start + record_len);
    }
}

#[inline(always)]
unsafe fn write_u8(cursor: &mut *mut u8, value: u8) {
    // SAFETY: the caller reserved enough capacity for the whole encoded record.
    unsafe {
        ptr::write(*cursor, value);
        *cursor = (*cursor).add(1);
    }
}

#[inline(always)]
unsafe fn write_u32_le(cursor: &mut *mut u8, value: u32) {
    // SAFETY: the caller reserved enough capacity for the whole encoded record;
    // unaligned writes are intentional because the frame is byte packed.
    unsafe {
        ptr::write_unaligned((*cursor).cast::<u32>(), value.to_le());
        *cursor = (*cursor).add(4);
    }
}

#[inline(always)]
unsafe fn write_u64_le(cursor: &mut *mut u8, value: u64) {
    // SAFETY: the caller reserved enough capacity for the whole encoded record;
    // unaligned writes are intentional because the frame is byte packed.
    unsafe {
        ptr::write_unaligned((*cursor).cast::<u64>(), value.to_le());
        *cursor = (*cursor).add(8);
    }
}

#[inline(always)]
unsafe fn write_bytes(cursor: &mut *mut u8, bytes: &[u8]) {
    // SAFETY: the caller reserved enough capacity for the whole encoded record.
    unsafe {
        ptr::copy_nonoverlapping(bytes.as_ptr(), *cursor, bytes.len());
        *cursor = (*cursor).add(bytes.len());
    }
}

fn write_mutation_batch_payload(out: &mut Vec<u8>, mutations: &[ReplicationMutation]) {
    out.extend_from_slice(&(mutations.len() as u32).to_le_bytes());
    for mutation in mutations {
        write_borrowed_mutation_payload_record(out, mutation.as_borrowed());
    }
}

pub fn decode_mutation_batch(bytes: &[u8]) -> Result<Vec<ReplicationMutation>> {
    let mut cursor = Cursor::new(bytes);
    let count = cursor.u32()? as usize;
    let mut mutations = Vec::with_capacity(count);
    for _ in 0..count {
        let shard_id = cursor.u32()? as usize;
        let sequence = cursor.u64()?;
        let timestamp_ms = cursor.u64()?;
        let op = ReplicationMutationOp::from_byte(cursor.u8()?)?;
        let key_hash = cursor.u64()?;
        let key_tag = cursor.u64()?;
        let expire_raw = cursor.u64()?;
        let key_len = cursor.u32()? as usize;
        let value_len = cursor.u32()? as usize;
        let key = SharedBytes::from(cursor.bytes(key_len)?.to_vec());
        let value = SharedBytes::from(cursor.bytes(value_len)?.to_vec());
        mutations.push(ReplicationMutation {
            shard_id,
            sequence,
            timestamp_ms,
            op,
            key_hash,
            key_tag,
            key,
            value,
            expire_at_ms: (expire_raw != EXPIRE_NONE).then_some(expire_raw),
        });
    }
    cursor.finish()?;
    Ok(mutations)
}

pub fn visit_mutation_batch_payload<F>(bytes: &[u8], mut visit: F) -> Result<()>
where
    F: FnMut(BorrowedReplicationMutation<'_>) -> Result<()>,
{
    let mut cursor = Cursor::new(bytes);
    let count = cursor.u32()? as usize;
    for _ in 0..count {
        let shard_id = cursor.u32()? as usize;
        let sequence = cursor.u64()?;
        let timestamp_ms = cursor.u64()?;
        let op = ReplicationMutationOp::from_byte(cursor.u8()?)?;
        let key_hash = cursor.u64()?;
        let key_tag = cursor.u64()?;
        let expire_raw = cursor.u64()?;
        let key_len = cursor.u32()? as usize;
        let value_len = cursor.u32()? as usize;
        let key = cursor.bytes(key_len)?;
        let value = cursor.bytes(value_len)?;
        visit(BorrowedReplicationMutation {
            shard_id,
            sequence,
            timestamp_ms,
            op,
            key_hash,
            key_tag,
            key,
            value,
            expire_at_ms: (expire_raw != EXPIRE_NONE).then_some(expire_raw),
        })?;
    }
    cursor.finish()
}

pub fn visit_mutation_batch_payload_bytes<F>(bytes: SharedBytes, mut visit: F) -> Result<()>
where
    F: FnMut(FrameBackedReplicationMutation<'_>) -> Result<()>,
{
    let mut cursor = Cursor::new(bytes.as_ref());
    let count = cursor.u32()? as usize;
    for _ in 0..count {
        let shard_id = cursor.u32()? as usize;
        let sequence = cursor.u64()?;
        let _timestamp_ms = cursor.u64()?;
        let op = ReplicationMutationOp::from_byte(cursor.u8()?)?;
        let key_hash = cursor.u64()?;
        let _key_tag = cursor.u64()?;
        let expire_raw = cursor.u64()?;
        let key_len = cursor.u32()? as usize;
        let value_len = cursor.u32()? as usize;
        let key = cursor.bytes(key_len)?;
        let value_start = cursor.pos;
        let _ = cursor.bytes(value_len)?;
        let value = bytes.slice(value_start..value_start + value_len);
        visit(FrameBackedReplicationMutation {
            shard_id,
            sequence,
            op,
            key_hash,
            key,
            value,
            expire_at_ms: (expire_raw != EXPIRE_NONE).then_some(expire_raw),
        })?;
    }
    cursor.finish()
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HelloRole {
    Replica = 1,
    ServiceSubscriber = 2,
}

impl HelloRole {
    fn to_byte(self) -> u8 {
        self as u8
    }

    fn from_byte(value: u8) -> Result<Self> {
        match value {
            1 => Ok(Self::Replica),
            2 => Ok(Self::ServiceSubscriber),
            other => Err(FastCacheError::Protocol(format!(
                "unsupported FCRP hello role: {other}"
            ))),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplicationHello {
    pub version: u8,
    pub role: HelloRole,
    pub auth_token: Option<String>,
    pub since: Option<ShardWatermarks>,
}

pub fn encode_hello(hello: &ReplicationHello) -> Vec<u8> {
    let mut out = Vec::new();
    out.push(hello.version);
    out.push(hello.role.to_byte());
    let token = hello.auth_token.as_deref().unwrap_or("");
    out.extend_from_slice(&(token.len() as u32).to_le_bytes());
    out.extend_from_slice(token.as_bytes());
    match &hello.since {
        Some(watermarks) => {
            out.push(1);
            out.extend_from_slice(&(watermarks.as_slice().len() as u32).to_le_bytes());
            for value in watermarks.as_slice() {
                out.extend_from_slice(&value.to_le_bytes());
            }
        }
        None => out.push(0),
    }
    out
}

pub fn decode_hello(bytes: &[u8]) -> Result<ReplicationHello> {
    let mut cursor = Cursor::new(bytes);
    let version = cursor.u8()?;
    let role = HelloRole::from_byte(cursor.u8()?)?;
    let token_len = cursor.u32()? as usize;
    let token_bytes = cursor.bytes(token_len)?;
    let auth_token = if token_len == 0 {
        None
    } else {
        Some(
            std::str::from_utf8(token_bytes)
                .map_err(|error| {
                    FastCacheError::Protocol(format!("FCRP hello auth token is not UTF-8: {error}"))
                })?
                .to_string(),
        )
    };
    let has_since = cursor.u8()? != 0;
    let since = if has_since {
        let count = cursor.u32()? as usize;
        let mut values = Vec::with_capacity(count);
        for _ in 0..count {
            values.push(cursor.u64()?);
        }
        Some(ShardWatermarks::from_vec(values))
    } else {
        None
    };
    cursor.finish()?;
    Ok(ReplicationHello {
        version,
        role,
        auth_token,
        since,
    })
}

pub fn encode_error(message: &str) -> Vec<u8> {
    let mut out = Vec::with_capacity(4 + message.len());
    out.extend_from_slice(&(message.len() as u32).to_le_bytes());
    out.extend_from_slice(message.as_bytes());
    out
}

pub fn decode_error(bytes: &[u8]) -> Result<String> {
    let mut cursor = Cursor::new(bytes);
    let len = cursor.u32()? as usize;
    let body = cursor.bytes(len)?;
    cursor.finish()?;
    std::str::from_utf8(body)
        .map(|s| s.to_string())
        .map_err(|error| FastCacheError::Protocol(format!("FCRP error payload not UTF-8: {error}")))
}

pub fn encode_ack(watermarks: &ShardWatermarks) -> Vec<u8> {
    let mut out = Vec::with_capacity(4 + watermarks.as_slice().len() * 8);
    out.extend_from_slice(&(watermarks.as_slice().len() as u32).to_le_bytes());
    for value in watermarks.as_slice() {
        out.extend_from_slice(&value.to_le_bytes());
    }
    out
}

pub fn decode_ack(bytes: &[u8]) -> Result<ShardWatermarks> {
    let mut cursor = Cursor::new(bytes);
    let count = cursor.u32()? as usize;
    let mut values = Vec::with_capacity(count);
    for _ in 0..count {
        values.push(cursor.u64()?);
    }
    cursor.finish()?;
    Ok(ShardWatermarks::from_vec(values))
}

pub fn encode_snapshot_chunk(chunk: &ReplicationSnapshotChunk) -> Vec<u8> {
    let mut out = Vec::new();
    out.extend_from_slice(&chunk.chunk_index.to_le_bytes());
    out.push(u8::from(chunk.is_last));
    out.extend_from_slice(&(chunk.watermarks.as_slice().len() as u32).to_le_bytes());
    for watermark in chunk.watermarks.as_slice() {
        out.extend_from_slice(&watermark.to_le_bytes());
    }
    out.extend_from_slice(&(chunk.entries.len() as u32).to_le_bytes());
    for entry in &chunk.entries {
        out.extend_from_slice(&(entry.key.len() as u32).to_le_bytes());
        out.extend_from_slice(&(entry.value.len() as u32).to_le_bytes());
        out.extend_from_slice(&entry.expire_at_ms.unwrap_or(EXPIRE_NONE).to_le_bytes());
        out.extend_from_slice(entry.key.as_ref());
        out.extend_from_slice(entry.value.as_ref());
    }
    out
}

pub fn decode_snapshot_chunk(bytes: &[u8]) -> Result<ReplicationSnapshotChunk> {
    let mut cursor = Cursor::new(bytes);
    let chunk_index = cursor.u64()?;
    let is_last = cursor.u8()? != 0;
    let watermark_count = cursor.u32()? as usize;
    let mut watermarks = Vec::with_capacity(watermark_count);
    for _ in 0..watermark_count {
        watermarks.push(cursor.u64()?);
    }
    let entry_count = cursor.u32()? as usize;
    let mut entries = Vec::with_capacity(entry_count);
    for _ in 0..entry_count {
        let key_len = cursor.u32()? as usize;
        let value_len = cursor.u32()? as usize;
        let expire_raw = cursor.u64()?;
        let key = cursor.bytes(key_len)?.to_vec();
        let value = cursor.bytes(value_len)?.to_vec();
        entries.push(StoredEntry {
            key,
            value,
            expire_at_ms: (expire_raw != EXPIRE_NONE).then_some(expire_raw),
        });
    }
    cursor.finish()?;
    Ok(ReplicationSnapshotChunk {
        watermarks: ShardWatermarks::from_vec(watermarks),
        chunk_index,
        is_last,
        entries,
    })
}

struct Cursor<'a> {
    bytes: &'a [u8],
    pos: usize,
}

impl<'a> Cursor<'a> {
    fn new(bytes: &'a [u8]) -> Self {
        Self { bytes, pos: 0 }
    }

    fn u8(&mut self) -> Result<u8> {
        let Some(value) = self.bytes.get(self.pos).copied() else {
            return Err(FastCacheError::Protocol("FCRP payload is truncated".into()));
        };
        self.pos += 1;
        Ok(value)
    }

    fn u32(&mut self) -> Result<u32> {
        let bytes = self.bytes(4)?;
        Ok(u32::from_le_bytes(bytes.try_into().unwrap()))
    }

    fn u64(&mut self) -> Result<u64> {
        let bytes = self.bytes(8)?;
        Ok(u64::from_le_bytes(bytes.try_into().unwrap()))
    }

    fn bytes(&mut self, len: usize) -> Result<&'a [u8]> {
        if self.pos + len > self.bytes.len() {
            return Err(FastCacheError::Protocol("FCRP payload is truncated".into()));
        }
        let bytes = &self.bytes[self.pos..self.pos + len];
        self.pos += len;
        Ok(bytes)
    }

    fn finish(&self) -> Result<()> {
        if self.pos != self.bytes.len() {
            return Err(FastCacheError::Protocol(
                "FCRP payload contains trailing bytes".into(),
            ));
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::storage::hash_key_tag;

    use super::*;

    fn sample_mutation(sequence: u64) -> ReplicationMutation {
        let key = b"alpha".to_vec();
        ReplicationMutation {
            shard_id: 2,
            sequence,
            timestamp_ms: 42,
            op: ReplicationMutationOp::Set,
            key_hash: hash_key(&key),
            key_tag: hash_key_tag(&key),
            key: SharedBytes::from(key),
            value: SharedBytes::from_static(b"value"),
            expire_at_ms: Some(99),
        }
    }

    #[test]
    fn mutation_batch_round_trips() {
        let mutations = vec![sample_mutation(1), sample_mutation(2)];
        let encoded = encode_mutation_batch(&mutations);
        let decoded = decode_mutation_batch(&encoded).expect("decode");
        assert_eq!(decoded, mutations);
    }

    #[test]
    fn mutation_batch_frame_round_trips_without_payload_copy() {
        let mutations = vec![sample_mutation(1), sample_mutation(2)];
        let payload = encode_mutation_batch(&mutations);
        let (encoded, uncompressed_len) = encode_mutation_batch_frame_with_payload_len(
            &mutations,
            payload.len(),
            ReplicationCompressionMode::None,
            0,
        )
        .expect("encode");
        let decoded = decode_frame(&encoded).expect("decode frame");
        assert_eq!(decoded.kind, FrameKind::MutationBatch);
        assert!(!decoded.compressed);
        assert_eq!(uncompressed_len, payload.len());
        assert_eq!(decoded.payload, payload);
        assert_eq!(
            decode_mutation_batch(&decoded.payload).expect("decode batch"),
            mutations
        );
    }

    #[test]
    fn mutation_batch_payload_bytes_visits_frame_backed_values() {
        let mutations = vec![sample_mutation(1), sample_mutation(2)];
        let payload = encode_mutation_batch(&mutations);
        let (encoded, _) = encode_mutation_batch_frame_with_payload_len(
            &mutations,
            payload.len(),
            ReplicationCompressionMode::None,
            0,
        )
        .expect("encode");

        let decoded = decode_frame_payload_bytes(SharedBytes::from(encoded)).expect("decode");
        let payload_start = decoded.payload.as_ptr() as usize;
        let payload_end = payload_start + decoded.payload.len();
        let mut visited = Vec::new();
        visit_mutation_batch_payload_bytes(decoded.payload, |mutation| {
            let value_start = mutation.value.as_ptr() as usize;
            let value_end = value_start + mutation.value.len();
            assert!(value_start >= payload_start);
            assert!(value_end <= payload_end);
            visited.push(mutation.value);
            Ok(())
        })
        .expect("visit");

        assert_eq!(
            visited,
            vec![mutations[0].value.clone(), mutations[1].value.clone()]
        );
    }

    #[test]
    fn zstd_frame_round_trips() {
        let payload = encode_mutation_batch(&[sample_mutation(1)]);
        let encoded = encode_frame(
            FrameKind::MutationBatch,
            ReplicationCompressionMode::Zstd,
            3,
            &payload,
        )
        .expect("encode");
        let decoded = decode_frame(&encoded).expect("decode");
        assert_eq!(decoded.kind, FrameKind::MutationBatch);
        assert!(decoded.compressed);
        assert_eq!(decoded.payload, payload);
    }

    #[test]
    fn snapshot_chunk_round_trips() {
        let chunk = ReplicationSnapshotChunk {
            watermarks: ShardWatermarks::from_vec(vec![1, 2]),
            chunk_index: 3,
            is_last: true,
            entries: vec![StoredEntry {
                key: b"k".to_vec(),
                value: b"v".to_vec(),
                expire_at_ms: None,
            }],
        };
        let encoded = encode_snapshot_chunk(&chunk);
        let decoded = decode_snapshot_chunk(&encoded).expect("decode");
        assert_eq!(decoded, chunk);
    }
}