reddb-io-file 1.23.1

RedDB file artifact layer: single-file .rdb layout, WAL, snapshots, checkpoints, locks, and recovery.
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
//! Main WAL record byte contract.
//!
//! Runtime code owns the semantic meaning of each record. This module owns the
//! persisted record tags, body framing, compression tag, term envelope, and
//! record checksum.
//!
//! Main WAL files are a sequence of frames after the file header. Every frame
//! starts with a stable record type tag, carries a versioned body, and ends with
//! a CRC32 checksum over the persisted bytes that precede the checksum.

use crate::{WAL_FILE_VERSION, WAL_FILE_VERSION_V2};
use std::io::{self, Read};

pub const MAIN_WAL_DEFAULT_COMPRESS_THRESHOLD: usize = 256;
const MAX_MAIN_WAL_PAYLOAD: usize = 256 * 1024 * 1024;
const MAX_MAIN_WAL_ITEMS: usize = 1_000_000;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum MainWalRecordType {
    Begin = 1,
    Commit = 2,
    Rollback = 3,
    PageWrite = 4,
    Checkpoint = 5,
    PageWriteCompressed = 6,
    TxCommitBatch = 7,
    FullPageImage = 8,
    VectorInsert = 9,
    ProbabilisticDelta = 10,
}

impl MainWalRecordType {
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            1 => Some(Self::Begin),
            2 => Some(Self::Commit),
            3 => Some(Self::Rollback),
            4 => Some(Self::PageWrite),
            5 => Some(Self::Checkpoint),
            6 => Some(Self::PageWriteCompressed),
            7 => Some(Self::TxCommitBatch),
            8 => Some(Self::FullPageImage),
            9 => Some(Self::VectorInsert),
            10 => Some(Self::ProbabilisticDelta),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum MainWalCompression {
    None = 0,
    Zstd = 1,
}

impl MainWalCompression {
    fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(Self::None),
            1 => Some(Self::Zstd),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum MainWalRecordFrame {
    Begin {
        tx_id: u64,
    },
    Commit {
        tx_id: u64,
    },
    Rollback {
        tx_id: u64,
    },
    PageWrite {
        tx_id: u64,
        page_id: u32,
        data: Vec<u8>,
    },
    TxCommitBatch {
        tx_id: u64,
        actions: Vec<Vec<u8>>,
    },
    FullPageImage {
        tx_id: u64,
        page_id: u32,
        ckpt_epoch: u64,
        data: Vec<u8>,
    },
    VectorInsert {
        collection: String,
        entity_id: u64,
        vector: Vec<f32>,
    },
    ProbabilisticDelta {
        kind: u8,
        operation: u8,
        name: String,
        operands: Vec<Vec<u8>>,
    },
    Checkpoint {
        lsn: u64,
    },
}

/// Borrowed view of a [`MainWalRecordFrame`].
///
/// The append path encodes straight out of the runtime record, so the payload
/// of a `PageWrite`, `FullPageImage` or `TxCommitBatch` never has to be cloned
/// into an owned frame first. The encoder works exclusively on this view — the
/// owned frame encodes by borrowing itself — so both paths emit the same bytes
/// by construction, not by convention.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MainWalRecordFrameRef<'a> {
    Begin {
        tx_id: u64,
    },
    Commit {
        tx_id: u64,
    },
    Rollback {
        tx_id: u64,
    },
    PageWrite {
        tx_id: u64,
        page_id: u32,
        data: &'a [u8],
    },
    TxCommitBatch {
        tx_id: u64,
        actions: &'a [Vec<u8>],
    },
    FullPageImage {
        tx_id: u64,
        page_id: u32,
        ckpt_epoch: u64,
        data: &'a [u8],
    },
    VectorInsert {
        collection: &'a str,
        entity_id: u64,
        vector: &'a [f32],
    },
    ProbabilisticDelta {
        kind: u8,
        operation: u8,
        name: &'a str,
        operands: &'a [Vec<u8>],
    },
    Checkpoint {
        lsn: u64,
    },
}

impl<'a> From<&'a MainWalRecordFrame> for MainWalRecordFrameRef<'a> {
    fn from(frame: &'a MainWalRecordFrame) -> Self {
        match frame {
            MainWalRecordFrame::Begin { tx_id } => MainWalRecordFrameRef::Begin { tx_id: *tx_id },
            MainWalRecordFrame::Commit { tx_id } => MainWalRecordFrameRef::Commit { tx_id: *tx_id },
            MainWalRecordFrame::Rollback { tx_id } => {
                MainWalRecordFrameRef::Rollback { tx_id: *tx_id }
            }
            MainWalRecordFrame::PageWrite {
                tx_id,
                page_id,
                data,
            } => MainWalRecordFrameRef::PageWrite {
                tx_id: *tx_id,
                page_id: *page_id,
                data,
            },
            MainWalRecordFrame::TxCommitBatch { tx_id, actions } => {
                MainWalRecordFrameRef::TxCommitBatch {
                    tx_id: *tx_id,
                    actions,
                }
            }
            MainWalRecordFrame::FullPageImage {
                tx_id,
                page_id,
                ckpt_epoch,
                data,
            } => MainWalRecordFrameRef::FullPageImage {
                tx_id: *tx_id,
                page_id: *page_id,
                ckpt_epoch: *ckpt_epoch,
                data,
            },
            MainWalRecordFrame::VectorInsert {
                collection,
                entity_id,
                vector,
            } => MainWalRecordFrameRef::VectorInsert {
                collection,
                entity_id: *entity_id,
                vector,
            },
            MainWalRecordFrame::ProbabilisticDelta {
                kind,
                operation,
                name,
                operands,
            } => MainWalRecordFrameRef::ProbabilisticDelta {
                kind: *kind,
                operation: *operation,
                name,
                operands,
            },
            MainWalRecordFrame::Checkpoint { lsn } => {
                MainWalRecordFrameRef::Checkpoint { lsn: *lsn }
            }
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MainWalRecordAuthority {
    pub term: u64,
    pub ownership_epoch: Option<u64>,
}

pub fn encode_main_wal_record_frame(frame: &MainWalRecordFrame, term: u64) -> io::Result<Vec<u8>> {
    encode_main_wal_record_frame_with_authority(
        frame,
        MainWalRecordAuthority {
            term,
            ownership_epoch: None,
        },
    )
}

pub fn encode_main_wal_record_frame_with_authority(
    frame: &MainWalRecordFrame,
    authority: MainWalRecordAuthority,
) -> io::Result<Vec<u8>> {
    let mut out = Vec::new();
    encode_main_wal_record_frame_with_authority_into(frame, authority, &mut out)?;
    Ok(out)
}

/// Encode a frame into `out`. Accepts an owned `&MainWalRecordFrame` or a
/// borrowed [`MainWalRecordFrameRef`] — the append path passes the latter, so
/// the payload is written straight from the caller's buffers with no copy.
pub fn encode_main_wal_record_frame_into<'a>(
    frame: impl Into<MainWalRecordFrameRef<'a>>,
    term: u64,
    out: &mut Vec<u8>,
) -> io::Result<()> {
    encode_main_wal_record_frame_with_authority_into(
        frame,
        MainWalRecordAuthority {
            term,
            ownership_epoch: None,
        },
        out,
    )
}

/// The single encoder. Every other encode entry point funnels here, so the
/// owned and borrowed paths cannot drift in the bytes they persist.
pub fn encode_main_wal_record_frame_with_authority_into<'a>(
    frame: impl Into<MainWalRecordFrameRef<'a>>,
    authority: MainWalRecordAuthority,
    out: &mut Vec<u8>,
) -> io::Result<()> {
    let start = out.len();
    match frame.into() {
        MainWalRecordFrameRef::Begin { tx_id } => {
            write_type_and_authority(out, MainWalRecordType::Begin, authority);
            out.extend_from_slice(&tx_id.to_le_bytes());
        }
        MainWalRecordFrameRef::Commit { tx_id } => {
            write_type_and_authority(out, MainWalRecordType::Commit, authority);
            out.extend_from_slice(&tx_id.to_le_bytes());
        }
        MainWalRecordFrameRef::Rollback { tx_id } => {
            write_type_and_authority(out, MainWalRecordType::Rollback, authority);
            out.extend_from_slice(&tx_id.to_le_bytes());
        }
        MainWalRecordFrameRef::PageWrite {
            tx_id,
            page_id,
            data,
        } => {
            if data.len() >= MAIN_WAL_DEFAULT_COMPRESS_THRESHOLD {
                if let Ok(compressed) = zstd::bulk::compress(data, 3) {
                    if compressed.len() < data.len() {
                        write_type_and_authority(
                            out,
                            MainWalRecordType::PageWriteCompressed,
                            authority,
                        );
                        out.extend_from_slice(&tx_id.to_le_bytes());
                        out.extend_from_slice(&page_id.to_le_bytes());
                        out.push(MainWalCompression::Zstd as u8);
                        write_u32_len(out, data.len(), "main wal original page length")?;
                        write_u32_len(out, compressed.len(), "main wal compressed page length")?;
                        out.extend_from_slice(&compressed);
                        append_crc(out, start);
                        return Ok(());
                    }
                }
            }

            write_type_and_authority(out, MainWalRecordType::PageWrite, authority);
            out.extend_from_slice(&tx_id.to_le_bytes());
            out.extend_from_slice(&page_id.to_le_bytes());
            write_u32_len(out, data.len(), "main wal page length")?;
            out.extend_from_slice(data);
        }
        MainWalRecordFrameRef::TxCommitBatch { tx_id, actions } => {
            write_type_and_authority(out, MainWalRecordType::TxCommitBatch, authority);
            out.extend_from_slice(&tx_id.to_le_bytes());
            write_u32_len(out, actions.len(), "main wal action count")?;
            for action in actions {
                write_u32_len(out, action.len(), "main wal action length")?;
                out.extend_from_slice(action);
            }
        }
        MainWalRecordFrameRef::FullPageImage {
            tx_id,
            page_id,
            ckpt_epoch,
            data,
        } => {
            write_type_and_authority(out, MainWalRecordType::FullPageImage, authority);
            out.extend_from_slice(&tx_id.to_le_bytes());
            out.extend_from_slice(&page_id.to_le_bytes());
            out.extend_from_slice(&ckpt_epoch.to_le_bytes());
            write_u32_len(out, data.len(), "main wal full-page image length")?;
            out.extend_from_slice(data);
        }
        MainWalRecordFrameRef::VectorInsert {
            collection,
            entity_id,
            vector,
        } => {
            write_type_and_authority(out, MainWalRecordType::VectorInsert, authority);
            write_u32_len(out, collection.len(), "main wal collection name length")?;
            out.extend_from_slice(collection.as_bytes());
            out.extend_from_slice(&entity_id.to_le_bytes());
            write_u32_len(out, vector.len(), "main wal vector length")?;
            for value in vector {
                out.extend_from_slice(&value.to_le_bytes());
            }
        }
        MainWalRecordFrameRef::ProbabilisticDelta {
            kind,
            operation,
            name,
            operands,
        } => {
            write_type_and_authority(out, MainWalRecordType::ProbabilisticDelta, authority);
            out.push(kind);
            out.push(operation);
            write_u32_len(out, name.len(), "main wal probabilistic name length")?;
            out.extend_from_slice(name.as_bytes());
            write_u32_len(out, operands.len(), "main wal probabilistic operand count")?;
            for operand in operands {
                write_u32_len(out, operand.len(), "main wal probabilistic operand length")?;
                out.extend_from_slice(operand);
            }
        }
        MainWalRecordFrameRef::Checkpoint { lsn } => {
            write_type_and_authority(out, MainWalRecordType::Checkpoint, authority);
            out.extend_from_slice(&lsn.to_le_bytes());
        }
    }

    append_crc(out, start);
    Ok(())
}

pub fn decode_main_wal_record_frame<R: Read>(
    reader: &mut R,
    format_version: u8,
    default_term: u64,
) -> io::Result<Option<(u64, MainWalRecordFrame)>> {
    Ok(decode_main_wal_record_frame_with_authority(
        reader,
        format_version,
        MainWalRecordAuthority {
            term: default_term,
            ownership_epoch: None,
        },
    )?
    .map(|(authority, frame)| (authority.term, frame)))
}

pub fn decode_main_wal_record_frame_with_authority<R: Read>(
    reader: &mut R,
    format_version: u8,
    default_authority: MainWalRecordAuthority,
) -> io::Result<Option<(MainWalRecordAuthority, MainWalRecordFrame)>> {
    let mut checksum_bytes = Vec::new();
    let mut type_buf = [0u8; 1];
    match reader.read_exact(&mut type_buf) {
        Ok(()) => checksum_bytes.extend_from_slice(&type_buf),
        Err(err) if err.kind() == io::ErrorKind::UnexpectedEof => return Ok(None),
        Err(err) => return Err(err),
    }

    let record_type = MainWalRecordType::from_u8(type_buf[0])
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Invalid record type"))?;

    let authority = match format_version {
        WAL_FILE_VERSION => {
            let term = read_u64_tracked(reader, &mut checksum_bytes)?;
            let ownership_epoch = read_u64_tracked(reader, &mut checksum_bytes)?;
            MainWalRecordAuthority {
                term,
                ownership_epoch: if ownership_epoch == 0 {
                    None
                } else {
                    Some(ownership_epoch)
                },
            }
        }
        WAL_FILE_VERSION_V2 => default_authority,
        _ => {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Unsupported WAL version: {format_version}"),
            ));
        }
    };

    let frame = match record_type {
        MainWalRecordType::Begin => MainWalRecordFrame::Begin {
            tx_id: read_u64_tracked(reader, &mut checksum_bytes)?,
        },
        MainWalRecordType::Commit => MainWalRecordFrame::Commit {
            tx_id: read_u64_tracked(reader, &mut checksum_bytes)?,
        },
        MainWalRecordType::Rollback => MainWalRecordFrame::Rollback {
            tx_id: read_u64_tracked(reader, &mut checksum_bytes)?,
        },
        MainWalRecordType::PageWrite => {
            let tx_id = read_u64_tracked(reader, &mut checksum_bytes)?;
            let page_id = read_u32_tracked(reader, &mut checksum_bytes)?;
            let data = read_bytes_tracked(reader, &mut checksum_bytes)?;
            MainWalRecordFrame::PageWrite {
                tx_id,
                page_id,
                data,
            }
        }
        MainWalRecordType::PageWriteCompressed => {
            let tx_id = read_u64_tracked(reader, &mut checksum_bytes)?;
            let page_id = read_u32_tracked(reader, &mut checksum_bytes)?;
            let compression = read_compression_tracked(reader, &mut checksum_bytes)?;
            let original_len = read_u32_tracked(reader, &mut checksum_bytes)? as usize;
            validate_len(original_len, "main WAL original page length")?;
            let compressed = read_bytes_tracked(reader, &mut checksum_bytes)?;
            let data = match compression {
                MainWalCompression::Zstd => {
                    let mut out = vec![0u8; original_len];
                    zstd::bulk::decompress_to_buffer(&compressed, &mut out).map_err(|err| {
                        io::Error::new(
                            io::ErrorKind::InvalidData,
                            format!("WAL zstd decompress failed: {err}"),
                        )
                    })?;
                    out
                }
                MainWalCompression::None => compressed,
            };
            MainWalRecordFrame::PageWrite {
                tx_id,
                page_id,
                data,
            }
        }
        MainWalRecordType::TxCommitBatch => {
            let tx_id = read_u64_tracked(reader, &mut checksum_bytes)?;
            let count = read_u32_tracked(reader, &mut checksum_bytes)? as usize;
            validate_count(count, "main WAL action count")?;
            let mut actions = Vec::with_capacity(count);
            for _ in 0..count {
                actions.push(read_bytes_tracked(reader, &mut checksum_bytes)?);
            }
            MainWalRecordFrame::TxCommitBatch { tx_id, actions }
        }
        MainWalRecordType::FullPageImage => {
            let tx_id = read_u64_tracked(reader, &mut checksum_bytes)?;
            let page_id = read_u32_tracked(reader, &mut checksum_bytes)?;
            let ckpt_epoch = read_u64_tracked(reader, &mut checksum_bytes)?;
            let data = read_bytes_tracked(reader, &mut checksum_bytes)?;
            MainWalRecordFrame::FullPageImage {
                tx_id,
                page_id,
                ckpt_epoch,
                data,
            }
        }
        MainWalRecordType::VectorInsert => {
            let collection = String::from_utf8(read_bytes_tracked(reader, &mut checksum_bytes)?)
                .map_err(|err| {
                    io::Error::new(
                        io::ErrorKind::InvalidData,
                        format!("invalid collection utf8: {err}"),
                    )
                })?;
            let entity_id = read_u64_tracked(reader, &mut checksum_bytes)?;
            let count = read_u32_tracked(reader, &mut checksum_bytes)? as usize;
            validate_count(count, "main WAL vector length")?;
            let mut vector = Vec::with_capacity(count);
            for _ in 0..count {
                vector.push(f32::from_le_bytes(read_array_tracked(
                    reader,
                    &mut checksum_bytes,
                )?));
            }
            MainWalRecordFrame::VectorInsert {
                collection,
                entity_id,
                vector,
            }
        }
        MainWalRecordType::ProbabilisticDelta => {
            let kind = read_u8_tracked(reader, &mut checksum_bytes)?;
            let operation = read_u8_tracked(reader, &mut checksum_bytes)?;
            let name = String::from_utf8(read_bytes_tracked(reader, &mut checksum_bytes)?)
                .map_err(|err| {
                    io::Error::new(
                        io::ErrorKind::InvalidData,
                        format!("invalid probabilistic name utf8: {err}"),
                    )
                })?;
            let count = read_u32_tracked(reader, &mut checksum_bytes)? as usize;
            validate_count(count, "main WAL probabilistic operand count")?;
            let mut operands = Vec::with_capacity(count);
            for _ in 0..count {
                operands.push(read_bytes_tracked(reader, &mut checksum_bytes)?);
            }
            MainWalRecordFrame::ProbabilisticDelta {
                kind,
                operation,
                name,
                operands,
            }
        }
        MainWalRecordType::Checkpoint => MainWalRecordFrame::Checkpoint {
            lsn: read_u64_tracked(reader, &mut checksum_bytes)?,
        },
    };

    let stored_crc = read_u32_untracked(reader)?;
    if crc32(&checksum_bytes) != stored_crc {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "WAL record checksum mismatch",
        ));
    }

    Ok(Some((authority, frame)))
}

fn write_type_and_authority(
    out: &mut Vec<u8>,
    record_type: MainWalRecordType,
    authority: MainWalRecordAuthority,
) {
    out.push(record_type as u8);
    out.extend_from_slice(&authority.term.to_le_bytes());
    out.extend_from_slice(&authority.ownership_epoch.unwrap_or(0).to_le_bytes());
}

fn write_u32_len(out: &mut Vec<u8>, len: usize, label: &'static str) -> io::Result<()> {
    let len = u32::try_from(len).map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, label))?;
    out.extend_from_slice(&len.to_le_bytes());
    Ok(())
}

fn append_crc(out: &mut Vec<u8>, start: usize) {
    let checksum = crc32(&out[start..]);
    out.extend_from_slice(&checksum.to_le_bytes());
}

fn crc32(bytes: &[u8]) -> u32 {
    let mut hasher = crc32fast::Hasher::new();
    hasher.update(bytes);
    hasher.finalize()
}

fn read_compression_tracked<R: Read>(
    reader: &mut R,
    checksum_bytes: &mut Vec<u8>,
) -> io::Result<MainWalCompression> {
    let value = read_array_tracked::<_, 1>(reader, checksum_bytes)?[0];
    MainWalCompression::from_u8(value).ok_or_else(|| {
        io::Error::new(
            io::ErrorKind::InvalidData,
            format!("Unknown WAL compression algorithm: {value}"),
        )
    })
}

fn read_bytes_tracked<R: Read>(
    reader: &mut R,
    checksum_bytes: &mut Vec<u8>,
) -> io::Result<Vec<u8>> {
    let len = read_u32_tracked(reader, checksum_bytes)? as usize;
    validate_len(len, "main WAL payload length")?;
    let mut bytes = vec![0u8; len];
    reader.read_exact(&mut bytes)?;
    checksum_bytes.extend_from_slice(&bytes);
    Ok(bytes)
}

fn validate_len(len: usize, label: &'static str) -> io::Result<()> {
    if len > MAX_MAIN_WAL_PAYLOAD {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("implausible {label}: {len}"),
        ));
    }
    Ok(())
}

fn validate_count(count: usize, label: &'static str) -> io::Result<()> {
    if count > MAX_MAIN_WAL_ITEMS {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("implausible {label}: {count}"),
        ));
    }
    Ok(())
}

fn read_u64_tracked<R: Read>(reader: &mut R, checksum_bytes: &mut Vec<u8>) -> io::Result<u64> {
    Ok(u64::from_le_bytes(read_array_tracked(
        reader,
        checksum_bytes,
    )?))
}

fn read_u32_tracked<R: Read>(reader: &mut R, checksum_bytes: &mut Vec<u8>) -> io::Result<u32> {
    Ok(u32::from_le_bytes(read_array_tracked(
        reader,
        checksum_bytes,
    )?))
}

fn read_u8_tracked<R: Read>(reader: &mut R, checksum_bytes: &mut Vec<u8>) -> io::Result<u8> {
    Ok(read_array_tracked::<_, 1>(reader, checksum_bytes)?[0])
}

fn read_array_tracked<R: Read, const N: usize>(
    reader: &mut R,
    checksum_bytes: &mut Vec<u8>,
) -> io::Result<[u8; N]> {
    let mut bytes = [0u8; N];
    reader.read_exact(&mut bytes)?;
    checksum_bytes.extend_from_slice(&bytes);
    Ok(bytes)
}

fn read_u32_untracked<R: Read>(reader: &mut R) -> io::Result<u32> {
    let mut bytes = [0u8; 4];
    reader.read_exact(&mut bytes)?;
    Ok(u32::from_le_bytes(bytes))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;

    #[test]
    fn main_wal_record_types_are_stable() {
        assert_eq!(
            MainWalRecordType::from_u8(1),
            Some(MainWalRecordType::Begin)
        );
        assert_eq!(
            MainWalRecordType::from_u8(9),
            Some(MainWalRecordType::VectorInsert)
        );
        assert_eq!(
            MainWalRecordType::from_u8(10),
            Some(MainWalRecordType::ProbabilisticDelta)
        );
        assert_eq!(MainWalRecordType::from_u8(11), None);
    }

    #[test]
    fn main_wal_records_round_trip_current_format() {
        let frames = vec![
            MainWalRecordFrame::Begin { tx_id: 1 },
            MainWalRecordFrame::Commit { tx_id: 2 },
            MainWalRecordFrame::Rollback { tx_id: 3 },
            MainWalRecordFrame::Checkpoint { lsn: 4 },
            MainWalRecordFrame::PageWrite {
                tx_id: 5,
                page_id: 6,
                data: vec![1, 2, 3],
            },
            MainWalRecordFrame::TxCommitBatch {
                tx_id: 7,
                actions: vec![b"insert".to_vec(), b"update".to_vec()],
            },
            MainWalRecordFrame::FullPageImage {
                tx_id: 8,
                page_id: 9,
                ckpt_epoch: 10,
                data: vec![0xAA; 128],
            },
            MainWalRecordFrame::VectorInsert {
                collection: "vectors".into(),
                entity_id: 11,
                vector: vec![1.0, -0.5, 0.25],
            },
            MainWalRecordFrame::ProbabilisticDelta {
                kind: 1,
                operation: 1,
                name: "visitors".into(),
                operands: vec![b"alice".to_vec()],
            },
        ];

        for frame in frames {
            let encoded = encode_main_wal_record_frame(&frame, 42).unwrap();
            let mut cursor = Cursor::new(encoded);
            let (term, decoded) = decode_main_wal_record_frame(&mut cursor, WAL_FILE_VERSION, 0)
                .unwrap()
                .unwrap();
            assert_eq!(term, 42);
            assert_eq!(decoded, frame);
        }
    }

    #[test]
    fn main_wal_record_round_trip_current_format_authority_epoch() {
        let frame = MainWalRecordFrame::Begin { tx_id: 42 };
        let authority = MainWalRecordAuthority {
            term: 7,
            ownership_epoch: Some(11),
        };
        let encoded = encode_main_wal_record_frame_with_authority(&frame, authority).unwrap();

        let mut cursor = Cursor::new(encoded);
        let (decoded_authority, decoded) = decode_main_wal_record_frame_with_authority(
            &mut cursor,
            WAL_FILE_VERSION,
            MainWalRecordAuthority {
                term: 1,
                ownership_epoch: None,
            },
        )
        .unwrap()
        .unwrap();

        assert_eq!(decoded_authority, authority);
        assert_eq!(decoded, frame);
    }

    #[test]
    fn main_wal_record_accepts_legacy_v2_without_term() {
        let mut encoded = Vec::new();
        encoded.push(MainWalRecordType::Begin as u8);
        encoded.extend_from_slice(&42u64.to_le_bytes());
        let checksum = crc32(&encoded);
        encoded.extend_from_slice(&checksum.to_le_bytes());

        let mut cursor = Cursor::new(encoded);
        let (term, frame) = decode_main_wal_record_frame(&mut cursor, WAL_FILE_VERSION_V2, 99)
            .unwrap()
            .unwrap();
        assert_eq!(term, 99);
        assert_eq!(frame, MainWalRecordFrame::Begin { tx_id: 42 });
    }

    #[test]
    fn main_wal_record_detects_checksum_mismatch() {
        let frame = MainWalRecordFrame::Begin { tx_id: 42 };
        let mut encoded = encode_main_wal_record_frame(&frame, 1).unwrap();
        let last = encoded.len() - 1;
        encoded[last] ^= 0xFF;

        let mut cursor = Cursor::new(encoded);
        assert_eq!(
            decode_main_wal_record_frame(&mut cursor, WAL_FILE_VERSION, 0)
                .unwrap_err()
                .to_string(),
            "WAL record checksum mismatch"
        );
    }

    #[test]
    fn main_wal_record_rejects_implausible_payload_before_allocating() {
        let mut encoded = Vec::new();
        write_type_and_authority(
            &mut encoded,
            MainWalRecordType::PageWrite,
            MainWalRecordAuthority {
                term: 1,
                ownership_epoch: None,
            },
        );
        encoded.extend_from_slice(&7u64.to_le_bytes());
        encoded.extend_from_slice(&3u32.to_le_bytes());
        encoded.extend_from_slice(&u32::MAX.to_le_bytes());

        let mut cursor = Cursor::new(encoded);
        assert_eq!(
            decode_main_wal_record_frame(&mut cursor, WAL_FILE_VERSION, 1)
                .unwrap_err()
                .to_string(),
            "implausible main WAL payload length: 4294967295"
        );
    }

    #[test]
    fn main_wal_record_rejects_implausible_count_before_allocating() {
        let mut encoded = Vec::new();
        write_type_and_authority(
            &mut encoded,
            MainWalRecordType::TxCommitBatch,
            MainWalRecordAuthority {
                term: 1,
                ownership_epoch: None,
            },
        );
        encoded.extend_from_slice(&7u64.to_le_bytes());
        encoded.extend_from_slice(&u32::MAX.to_le_bytes());

        let mut cursor = Cursor::new(encoded);
        assert_eq!(
            decode_main_wal_record_frame(&mut cursor, WAL_FILE_VERSION, 1)
                .unwrap_err()
                .to_string(),
            "implausible main WAL action count: 4294967295"
        );
    }

    #[test]
    fn main_wal_record_compresses_and_decompresses_page_writes() {
        let frame = MainWalRecordFrame::PageWrite {
            tx_id: 7,
            page_id: 3,
            data: vec![0xAB; 1024],
        };
        let encoded = encode_main_wal_record_frame(&frame, 1).unwrap();
        assert_eq!(encoded[0], MainWalRecordType::PageWriteCompressed as u8);

        let mut cursor = Cursor::new(encoded);
        let (_, decoded) = decode_main_wal_record_frame(&mut cursor, WAL_FILE_VERSION, 0)
            .unwrap()
            .unwrap();
        assert_eq!(decoded, frame);
    }
}