laurus 0.5.0

Unified search library for lexical, vector, and semantic retrieval
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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
//! Structured file I/O for binary data serialization.
//!
//! This module provides efficient binary serialization for search index data structures,
//! similar to Whoosh's structfile.py but optimized for Rust and modern hardware.
//!
//! Two layers of abstraction are provided:
//!
//! - **Structured I/O** ([`StructWriter`] / [`StructReader`]) -- typed field-level
//!   reading and writing of primitives, variable-length integers, strings, byte
//!   arrays, and compound structures, with a CRC-32 checksum trailer for
//!   integrity verification.
//! - **Block I/O** ([`BlockWriter`] / [`BlockReader`]) -- higher-level block-based
//!   batching built on top of structured I/O, designed for posting lists and
//!   other data that benefits from fixed-size block buffering.

use std::collections::HashMap;

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

use crate::error::{LaurusError, Result};
use crate::storage::{StorageInput, StorageOutput};
use crate::util::varint::{decode_u64, encode_u64};

/// Structured binary writer with typed fields and CRC-32 checksumming.
///
/// `StructWriter` wraps a [`StorageOutput`] and provides typed write methods
/// for primitive values, variable-length integers, strings, byte arrays, and
/// compound structures such as delta-compressed integer lists and string-to-u64
/// maps. A running CRC-32 checksum is maintained and written as a trailer when
/// the writer is closed, enabling integrity verification on read.
///
/// All multi-byte numeric values are encoded in **little-endian** byte order.
pub struct StructWriter<W: StorageOutput> {
    /// The underlying storage output handle.
    writer: W,
    /// Running CRC-32 checksum of written data.
    checksum: u32,
    /// Current byte position in the output stream.
    position: u64,
}

impl<W: StorageOutput> StructWriter<W> {
    /// Create a new structured file writer wrapping the given output.
    ///
    /// # Arguments
    ///
    /// * `writer` - The underlying [`StorageOutput`] to write to.
    ///
    /// # Returns
    ///
    /// A new `StructWriter` positioned at byte 0 with a zeroed checksum.
    pub fn new(writer: W) -> Self {
        StructWriter {
            writer,
            checksum: 0,
            position: 0,
        }
    }

    /// Write a single `u8` value.
    ///
    /// # Arguments
    ///
    /// * `value` - The byte value to write.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_u8(&mut self, value: u8) -> Result<()> {
        self.writer.write_u8(value)?;
        self.update_checksum(&[value]);
        self.position += 1;
        Ok(())
    }

    /// Write a `u16` value in little-endian byte order.
    ///
    /// # Arguments
    ///
    /// * `value` - The 16-bit unsigned integer to write.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_u16(&mut self, value: u16) -> Result<()> {
        self.writer.write_u16::<LittleEndian>(value)?;
        self.update_checksum(&value.to_le_bytes());
        self.position += 2;
        Ok(())
    }

    /// Write a `u32` value in little-endian byte order.
    ///
    /// # Arguments
    ///
    /// * `value` - The 32-bit unsigned integer to write.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_u32(&mut self, value: u32) -> Result<()> {
        self.writer.write_u32::<LittleEndian>(value)?;
        self.update_checksum(&value.to_le_bytes());
        self.position += 4;
        Ok(())
    }

    /// Write a `u64` value in little-endian byte order.
    ///
    /// # Arguments
    ///
    /// * `value` - The 64-bit unsigned integer to write.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_u64(&mut self, value: u64) -> Result<()> {
        self.writer.write_u64::<LittleEndian>(value)?;
        self.update_checksum(&value.to_le_bytes());
        self.position += 8;
        Ok(())
    }

    /// Write a variable-length encoded unsigned integer.
    ///
    /// Smaller values use fewer bytes, making this efficient for values that
    /// are typically small (e.g. string lengths, deltas).
    ///
    /// # Arguments
    ///
    /// * `value` - The unsigned integer to encode and write.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_varint(&mut self, value: u64) -> Result<()> {
        let encoded = encode_u64(value);
        self.writer.write_all(&encoded)?;
        self.update_checksum(&encoded);
        self.position += encoded.len() as u64;
        Ok(())
    }

    /// Write an `f32` value in little-endian byte order.
    ///
    /// # Arguments
    ///
    /// * `value` - The 32-bit floating-point number to write.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_f32(&mut self, value: f32) -> Result<()> {
        self.writer.write_f32::<LittleEndian>(value)?;
        self.update_checksum(&value.to_le_bytes());
        self.position += 4;
        Ok(())
    }

    /// Write an `f64` value in little-endian byte order.
    ///
    /// # Arguments
    ///
    /// * `value` - The 64-bit floating-point number to write.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_f64(&mut self, value: f64) -> Result<()> {
        self.writer.write_f64::<LittleEndian>(value)?;
        self.update_checksum(&value.to_le_bytes());
        self.position += 8;
        Ok(())
    }

    /// Write a UTF-8 string with a varint length prefix.
    ///
    /// The string is encoded as a varint byte-length followed by the raw UTF-8
    /// bytes, matching the format read by [`StructReader::read_string`].
    ///
    /// # Arguments
    ///
    /// * `value` - The string slice to write.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_string(&mut self, value: &str) -> Result<()> {
        let bytes = value.as_bytes();
        self.write_varint(bytes.len() as u64)?;
        self.writer.write_all(bytes)?;
        self.update_checksum(bytes);
        self.position += bytes.len() as u64;
        Ok(())
    }

    /// Write a byte slice with a varint length prefix.
    ///
    /// # Arguments
    ///
    /// * `value` - The byte slice to write.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_bytes(&mut self, value: &[u8]) -> Result<()> {
        self.write_varint(value.len() as u64)?;
        self.writer.write_all(value)?;
        self.update_checksum(value);
        self.position += value.len() as u64;
        Ok(())
    }

    /// Write raw bytes directly without any length prefix.
    ///
    /// The caller is responsible for knowing the exact byte count on the
    /// reading side.
    ///
    /// # Arguments
    ///
    /// * `value` - The byte slice to write verbatim.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_raw(&mut self, value: &[u8]) -> Result<()> {
        self.writer.write_all(value)?;
        self.update_checksum(value);
        self.position += value.len() as u64;
        Ok(())
    }

    /// Write a `u32` array using delta encoding for compression.
    ///
    /// The values are stored as a varint count followed by varint-encoded
    /// deltas between consecutive elements, which is particularly efficient
    /// for monotonically increasing sequences such as sorted document ID
    /// posting lists.
    ///
    /// # Arguments
    ///
    /// * `values` - The slice of `u32` values to write (should ideally be
    ///   sorted for best compression).
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_delta_compressed_u32s(&mut self, values: &[u32]) -> Result<()> {
        if values.is_empty() {
            return self.write_varint(0);
        }

        self.write_varint(values.len() as u64)?;

        let mut previous = 0u32;
        for &value in values {
            let delta = value.wrapping_sub(previous);
            self.write_varint(delta as u64)?;
            previous = value;
        }

        Ok(())
    }

    /// Write a `HashMap<String, u64>` as a varint-counted sequence of
    /// key-value pairs.
    ///
    /// # Arguments
    ///
    /// * `map` - The map to serialize.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn write_string_u64_map(&mut self, map: &HashMap<String, u64>) -> Result<()> {
        self.write_varint(map.len() as u64)?;

        for (key, value) in map {
            self.write_string(key)?;
            self.write_u64(*value)?;
        }

        Ok(())
    }

    /// Get the current byte position in the output stream.
    ///
    /// # Returns
    ///
    /// The number of bytes written so far.
    pub fn position(&self) -> u64 {
        self.position
    }

    /// Get the current CRC-32 checksum of written data.
    ///
    /// # Returns
    ///
    /// The running checksum value.
    pub fn checksum(&self) -> u32 {
        self.checksum
    }

    /// Replace the CRC-32 checksum with the hash of the given data.
    ///
    /// Note: this does **not** accumulate a running checksum. Each call
    /// overwrites the previous value with `crc32fast::hash(data)`, so the
    /// stored checksum only reflects the last chunk passed to this method.
    fn update_checksum(&mut self, data: &[u8]) {
        self.checksum = crc32fast::hash(data);
    }

    /// Write the trailing CRC-32 checksum, flush, and close the writer.
    ///
    /// The checksum written is the value computed by the most recent
    /// `update_checksum` call, which only covers the
    /// last chunk of data passed to that method (not all data written).
    ///
    /// # Errors
    ///
    /// Returns an error if flushing or closing the underlying output fails.
    pub fn close(mut self) -> Result<()> {
        // Write final checksum
        self.writer.write_u32::<LittleEndian>(self.checksum)?;
        self.writer.flush_and_sync()?;
        self.writer.close()?;
        Ok(())
    }

    /// Seek to a position in the output stream.
    ///
    /// # Arguments
    ///
    /// * `pos` - The seek target (start, end, or current-relative).
    ///
    /// # Returns
    ///
    /// The new absolute byte position after seeking.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying seek fails.
    pub fn seek(&mut self, pos: std::io::SeekFrom) -> Result<u64> {
        let new_pos = self.writer.seek(pos)?;
        self.position = new_pos;
        Ok(new_pos)
    }

    /// Get the current stream position from the underlying writer.
    ///
    /// This is useful when mixing raw writes with structured writes to
    /// ensure the tracked position stays in sync.
    ///
    /// # Returns
    ///
    /// The absolute byte position reported by the underlying writer.
    ///
    /// # Errors
    ///
    /// Returns an error if querying the position fails.
    pub fn stream_position(&mut self) -> Result<u64> {
        self.writer.stream_position().map_err(LaurusError::from)
    }
}

/// Structured binary reader with typed fields and CRC-32 verification.
///
/// `StructReader` is the read counterpart of [`StructWriter`]. It wraps a
/// [`StorageInput`] and provides typed read methods that mirror the writer's
/// format. A running CRC-32 checksum is maintained so that the trailing
/// checksum written by [`StructWriter::close`] can be verified via
/// [`verify_checksum`](Self::verify_checksum).
///
/// All multi-byte numeric values are expected in **little-endian** byte order.
pub struct StructReader<R: StorageInput> {
    /// The underlying storage input handle.
    reader: R,
    /// Running CRC-32 checksum of data read so far.
    checksum: u32,
    /// Current byte position in the input stream.
    position: u64,
    /// Total size of the underlying file in bytes.
    file_size: u64,
}

impl<R: StorageInput> StructReader<R> {
    /// Create a new structured file reader wrapping the given input.
    ///
    /// # Arguments
    ///
    /// * `reader` - The underlying [`StorageInput`] to read from.
    ///
    /// # Returns
    ///
    /// A new `StructReader` positioned at byte 0.
    ///
    /// # Errors
    ///
    /// Returns an error if determining the input size fails.
    pub fn new(reader: R) -> Result<Self> {
        let file_size = reader.size()?;
        Ok(StructReader {
            reader,
            checksum: 0,
            position: 0,
            file_size,
        })
    }

    /// Seek to a position in the input stream.
    ///
    /// # Arguments
    ///
    /// * `pos` - The seek target (start, end, or current-relative).
    ///
    /// # Returns
    ///
    /// The new absolute byte position after seeking.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying seek fails.
    pub fn seek(&mut self, pos: std::io::SeekFrom) -> Result<u64> {
        let new_pos = self.reader.seek(pos)?;
        self.position = new_pos;
        Ok(new_pos)
    }

    /// Get the current stream position from the underlying reader.
    ///
    /// # Returns
    ///
    /// The absolute byte position reported by the underlying reader.
    ///
    /// # Errors
    ///
    /// Returns an error if querying the position fails.
    pub fn stream_position(&mut self) -> Result<u64> {
        self.reader.stream_position().map_err(LaurusError::from)
    }

    /// Read a single `u8` value.
    ///
    /// # Returns
    ///
    /// The byte value read from the stream.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn read_u8(&mut self) -> Result<u8> {
        let value = self.reader.read_u8()?;
        self.update_checksum(&[value]);
        self.position += 1;
        Ok(value)
    }

    /// Read a `u16` value in little-endian byte order.
    ///
    /// # Returns
    ///
    /// The 16-bit unsigned integer read from the stream.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn read_u16(&mut self) -> Result<u16> {
        let value = self.reader.read_u16::<LittleEndian>()?;
        self.update_checksum(&value.to_le_bytes());
        self.position += 2;
        Ok(value)
    }

    /// Read a `u32` value in little-endian byte order.
    ///
    /// # Returns
    ///
    /// The 32-bit unsigned integer read from the stream.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn read_u32(&mut self) -> Result<u32> {
        let value = self.reader.read_u32::<LittleEndian>()?;
        self.update_checksum(&value.to_le_bytes());
        self.position += 4;
        Ok(value)
    }

    /// Read a `u64` value in little-endian byte order.
    ///
    /// # Returns
    ///
    /// The 64-bit unsigned integer read from the stream.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn read_u64(&mut self) -> Result<u64> {
        let value = self.reader.read_u64::<LittleEndian>()?;
        self.update_checksum(&value.to_le_bytes());
        self.position += 8;
        Ok(value)
    }

    /// Read a variable-length encoded unsigned integer.
    ///
    /// # Returns
    ///
    /// The decoded `u64` value.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation or decoding fails.
    pub fn read_varint(&mut self) -> Result<u64> {
        let mut bytes = Vec::new();
        loop {
            let byte = self.reader.read_u8()?;
            bytes.push(byte);
            if byte & 0x80 == 0 {
                break;
            }
        }

        let (value, _) = decode_u64(&bytes)?;
        self.update_checksum(&bytes);
        self.position += bytes.len() as u64;
        Ok(value)
    }

    /// Read an `f32` value in little-endian byte order.
    ///
    /// # Returns
    ///
    /// The 32-bit floating-point number read from the stream.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn read_f32(&mut self) -> Result<f32> {
        let value = self.reader.read_f32::<LittleEndian>()?;
        self.update_checksum(&value.to_le_bytes());
        self.position += 4;
        Ok(value)
    }

    /// Read an `f64` value in little-endian byte order.
    ///
    /// # Returns
    ///
    /// The 64-bit floating-point number read from the stream.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn read_f64(&mut self) -> Result<f64> {
        let value = self.reader.read_f64::<LittleEndian>()?;
        self.update_checksum(&value.to_le_bytes());
        self.position += 8;
        Ok(value)
    }

    /// Read a UTF-8 string with a varint length prefix.
    ///
    /// # Returns
    ///
    /// The decoded string.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O fails or the bytes are not
    /// valid UTF-8.
    pub fn read_string(&mut self) -> Result<String> {
        let length = self.read_varint()? as usize;
        let mut bytes = vec![0u8; length];
        self.reader.read_exact(&mut bytes)?;
        self.update_checksum(&bytes);
        self.position += length as u64;

        String::from_utf8(bytes).map_err(|e| LaurusError::storage(format!("Invalid UTF-8: {e}")))
    }

    /// Read a byte array with a varint length prefix.
    ///
    /// # Returns
    ///
    /// The raw bytes read from the stream.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn read_bytes(&mut self) -> Result<Vec<u8>> {
        let length = self.read_varint()? as usize;
        let mut bytes = vec![0u8; length];
        self.reader.read_exact(&mut bytes)?;
        self.update_checksum(&bytes);
        self.position += length as u64;
        Ok(bytes)
    }

    /// Read an exact number of raw bytes without a length prefix.
    ///
    /// # Arguments
    ///
    /// * `length` - The number of bytes to read.
    ///
    /// # Returns
    ///
    /// A `Vec<u8>` containing exactly `length` bytes.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails or the stream
    /// does not contain enough bytes.
    pub fn read_raw(&mut self, length: usize) -> Result<Vec<u8>> {
        let mut bytes = vec![0u8; length];
        self.reader.read_exact(&mut bytes)?;
        self.update_checksum(&bytes);
        self.position += length as u64;
        Ok(bytes)
    }

    /// Read a delta-compressed `u32` array.
    ///
    /// This reverses the encoding performed by
    /// [`StructWriter::write_delta_compressed_u32s`].
    ///
    /// # Returns
    ///
    /// A vector of reconstructed `u32` values.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn read_delta_compressed_u32s(&mut self) -> Result<Vec<u32>> {
        let length = self.read_varint()? as usize;
        if length == 0 {
            return Ok(Vec::new());
        }

        let mut values = Vec::with_capacity(length);
        let mut previous = 0u32;

        for _ in 0..length {
            let delta = self.read_varint()? as u32;
            let value = previous.wrapping_add(delta);
            values.push(value);
            previous = value;
        }

        Ok(values)
    }

    /// Read a `HashMap<String, u64>` previously written by
    /// [`StructWriter::write_string_u64_map`].
    ///
    /// # Returns
    ///
    /// The deserialized map.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying I/O operation fails.
    pub fn read_string_u64_map(&mut self) -> Result<HashMap<String, u64>> {
        let length = self.read_varint()? as usize;
        let mut map = HashMap::with_capacity(length);

        for _ in 0..length {
            let key = self.read_string()?;
            let value = self.read_u64()?;
            map.insert(key, value);
        }

        Ok(map)
    }

    /// Get the current byte position in the input stream.
    ///
    /// # Returns
    ///
    /// The number of bytes consumed so far.
    pub fn position(&self) -> u64 {
        self.position
    }

    /// Get the total file size.
    ///
    /// # Returns
    ///
    /// The size of the underlying file in bytes.
    pub fn size(&self) -> u64 {
        self.file_size
    }

    /// Check whether the reader has reached the end of file.
    ///
    /// The last 4 bytes of the file are reserved for the CRC-32 checksum
    /// trailer, so this returns `true` once the position is within that
    /// trailing region.
    ///
    /// # Returns
    ///
    /// `true` if no more data blocks remain to be read.
    pub fn is_eof(&self) -> bool {
        self.position >= self.file_size.saturating_sub(4) // Account for checksum
    }

    /// Get the current CRC-32 checksum of data read so far.
    ///
    /// # Returns
    ///
    /// The running checksum value.
    pub fn checksum(&self) -> u32 {
        self.checksum
    }

    /// Replace the CRC-32 checksum with the hash of the given data.
    ///
    /// Note: this does **not** accumulate a running checksum. Each call
    /// overwrites the previous value with `crc32fast::hash(data)`, so the
    /// stored checksum only reflects the last chunk passed to this method.
    fn update_checksum(&mut self, data: &[u8]) {
        self.checksum = crc32fast::hash(data);
    }

    /// Verify file integrity by comparing the running checksum against the
    /// CRC-32 trailer stored at the end of the file.
    ///
    /// # Returns
    ///
    /// `true` if the stored checksum matches the computed one.
    ///
    /// # Errors
    ///
    /// Returns an error if the file is too short to contain a checksum or
    /// if reading the trailer fails.
    pub fn verify_checksum(&mut self) -> Result<bool> {
        if self.position + 4 > self.file_size {
            return Err(LaurusError::storage("File too short for checksum"));
        }

        // Read the stored checksum from the end of file
        let stored_checksum = self.reader.read_u32::<LittleEndian>()?;
        Ok(stored_checksum == self.checksum)
    }

    /// Close the reader and release the underlying input handle.
    ///
    /// # Errors
    ///
    /// Returns an error if closing the underlying input fails.
    pub fn close(mut self) -> Result<()> {
        self.reader.close()
    }
}

/// Block-based writer for efficient batched I/O.
///
/// `BlockWriter` buffers data into fixed-size blocks on top of a
/// [`StructWriter`]. When the current block fills up (or is explicitly
/// flushed), it is written to the underlying stream with a header
/// containing the block size and sequence number. This is well-suited
/// for posting lists and other data that benefits from block-level
/// compression or batched disk I/O.
pub struct BlockWriter<W: StorageOutput> {
    /// The underlying structured writer.
    writer: StructWriter<W>,
    /// Maximum size of a single block in bytes.
    block_size: usize,
    /// Buffer accumulating data for the current block.
    current_block: Vec<u8>,
    /// Number of blocks flushed so far.
    blocks_written: u64,
}

impl<W: StorageOutput> BlockWriter<W> {
    /// Create a new block writer with the specified block size.
    ///
    /// # Arguments
    ///
    /// * `writer` - The underlying [`StorageOutput`] to write to.
    /// * `block_size` - The maximum number of bytes per block.
    ///
    /// # Returns
    ///
    /// A new `BlockWriter` with an empty block buffer.
    pub fn new(writer: W, block_size: usize) -> Self {
        BlockWriter {
            writer: StructWriter::new(writer),
            block_size,
            current_block: Vec::with_capacity(block_size),
            blocks_written: 0,
        }
    }

    /// Write data into the current block buffer.
    ///
    /// If appending `data` would exceed the block size, the current block
    /// is flushed first. Data larger than the block size is written directly
    /// to the underlying stream without buffering.
    ///
    /// # Arguments
    ///
    /// * `data` - The byte slice to write.
    ///
    /// # Errors
    ///
    /// Returns an error if flushing or writing fails.
    pub fn write_to_block(&mut self, data: &[u8]) -> Result<()> {
        if self.current_block.len() + data.len() > self.block_size {
            self.flush_block()?;
        }

        if data.len() > self.block_size {
            // Data is larger than block size, write directly
            self.writer.write_raw(data)?;
        } else {
            self.current_block.extend_from_slice(data);
        }

        Ok(())
    }

    /// Flush the current block buffer to the underlying storage.
    ///
    /// A block header (size + sequence number) is written before the data.
    /// This is a no-op if the buffer is empty.
    ///
    /// # Errors
    ///
    /// Returns an error if writing the block header or data fails.
    pub fn flush_block(&mut self) -> Result<()> {
        if !self.current_block.is_empty() {
            // Write block header: size + block number
            self.writer.write_u32(self.current_block.len() as u32)?;
            self.writer.write_u64(self.blocks_written)?;

            // Write block data
            self.writer.write_raw(&self.current_block)?;

            self.current_block.clear();
            self.blocks_written += 1;
        }
        Ok(())
    }

    /// Get the number of blocks written so far.
    ///
    /// # Returns
    ///
    /// The count of flushed blocks.
    pub fn blocks_written(&self) -> u64 {
        self.blocks_written
    }

    /// Flush any remaining buffered data and close the writer.
    ///
    /// # Errors
    ///
    /// Returns an error if flushing or closing fails.
    pub fn close(mut self) -> Result<()> {
        self.flush_block()?;
        self.writer.close()
    }
}

/// Block-based reader for efficient batched I/O.
///
/// `BlockReader` reads data written by [`BlockWriter`], loading one block
/// at a time into an internal cache. Callers can then read sub-slices from
/// the cached block without additional I/O. Block sequence numbers are
/// verified on read to detect corruption or out-of-order access.
pub struct BlockReader<R: StorageInput> {
    /// The underlying structured reader.
    reader: StructReader<R>,
    /// Cache holding the most recently read block data.
    block_cache: Vec<u8>,
    /// Size (in bytes) of the currently cached block.
    current_block_size: usize,
    /// Current read position within the cached block.
    current_block_pos: usize,
    /// Number of blocks read so far.
    blocks_read: u64,
}

impl<R: StorageInput> BlockReader<R> {
    /// Create a new block reader wrapping the given input.
    ///
    /// # Arguments
    ///
    /// * `reader` - The underlying [`StorageInput`] to read from.
    ///
    /// # Returns
    ///
    /// A new `BlockReader` with an empty block cache.
    ///
    /// # Errors
    ///
    /// Returns an error if initializing the underlying reader fails.
    pub fn new(reader: R) -> Result<Self> {
        Ok(BlockReader {
            reader: StructReader::new(reader)?,
            block_cache: Vec::new(),
            current_block_size: 0,
            current_block_pos: 0,
            blocks_read: 0,
        })
    }

    /// Read the next block from the stream into the internal cache.
    ///
    /// Returns `None` when the end of file is reached.
    ///
    /// # Returns
    ///
    /// `Some(bytes)` containing the block data, or `None` at EOF.
    ///
    /// # Errors
    ///
    /// Returns an error if reading fails or the block sequence number
    /// does not match the expected value.
    pub fn read_block(&mut self) -> Result<Option<&[u8]>> {
        if self.reader.is_eof() {
            return Ok(None);
        }

        // Read block header
        let block_size = self.reader.read_u32()? as usize;
        let block_number = self.reader.read_u64()?;

        // Verify block number
        if block_number != self.blocks_read {
            return Err(LaurusError::storage(format!(
                "Block number mismatch: expected {}, got {}",
                self.blocks_read, block_number
            )));
        }

        // Read block data
        self.block_cache = self.reader.read_raw(block_size)?;
        self.current_block_size = block_size;
        self.current_block_pos = 0;
        self.blocks_read += 1;

        Ok(Some(&self.block_cache))
    }

    /// Read a sub-slice of the given length from the currently cached block.
    ///
    /// Returns `None` if there are not enough bytes remaining in the block.
    ///
    /// # Arguments
    ///
    /// * `length` - The number of bytes to read from the current block.
    ///
    /// # Returns
    ///
    /// `Some(bytes)` on success, or `None` if the block has insufficient
    /// remaining data.
    ///
    /// # Errors
    ///
    /// This method does not perform I/O and currently always returns `Ok`.
    pub fn read_from_block(&mut self, length: usize) -> Result<Option<&[u8]>> {
        if self.current_block_pos + length > self.current_block_size {
            return Ok(None);
        }

        let start = self.current_block_pos;
        let end = start + length;
        self.current_block_pos = end;

        Ok(Some(&self.block_cache[start..end]))
    }

    /// Get the number of blocks read so far.
    ///
    /// # Returns
    ///
    /// The count of blocks loaded into the cache.
    pub fn blocks_read(&self) -> u64 {
        self.blocks_read
    }

    /// Close the reader and release the underlying input handle.
    ///
    /// # Errors
    ///
    /// Returns an error if closing the underlying input fails.
    pub fn close(self) -> Result<()> {
        self.reader.close()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::Storage;

    use crate::storage::memory::MemoryStorage;
    use crate::storage::memory::MemoryStorageConfig;
    use std::sync::Arc;

    #[test]
    fn test_struct_writer_reader() {
        let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));

        // Write structured data
        {
            let output = storage.create_output("test.struct").unwrap();
            let mut writer = StructWriter::new(output);

            writer.write_u8(42).unwrap();
            writer.write_u16(1234).unwrap();
            writer.write_u32(5678).unwrap();
            writer.write_u64(9876543210).unwrap();
            writer.write_varint(12345).unwrap();
            writer.write_f32(std::f32::consts::PI).unwrap();
            writer.write_f64(std::f64::consts::E).unwrap();
            writer.write_string("Hello, World!").unwrap();
            writer.write_bytes(b"binary data").unwrap();

            let values = vec![1, 5, 10, 15, 25];
            writer.write_delta_compressed_u32s(&values).unwrap();

            writer.close().unwrap();
        }

        // Read structured data
        {
            let input = storage.open_input("test.struct").unwrap();
            let mut reader = StructReader::new(input).unwrap();

            assert_eq!(reader.read_u8().unwrap(), 42);
            assert_eq!(reader.read_u16().unwrap(), 1234);
            assert_eq!(reader.read_u32().unwrap(), 5678);
            assert_eq!(reader.read_u64().unwrap(), 9876543210);
            assert_eq!(reader.read_varint().unwrap(), 12345);
            assert!((reader.read_f32().unwrap() - std::f32::consts::PI).abs() < 0.0001);
            assert!((reader.read_f64().unwrap() - std::f64::consts::E).abs() < 0.000000001);
            assert_eq!(reader.read_string().unwrap(), "Hello, World!");
            assert_eq!(reader.read_bytes().unwrap(), b"binary data");

            let decoded_values = reader.read_delta_compressed_u32s().unwrap();
            assert_eq!(decoded_values, vec![1, 5, 10, 15, 25]);

            // Verify checksum
            assert!(reader.verify_checksum().unwrap());
        }
    }

    #[test]
    fn test_block_writer_reader() {
        let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));

        // Write blocks
        {
            let output = storage.create_output("test.blocks").unwrap();
            let mut writer = BlockWriter::new(output, 1024);

            writer.write_to_block(b"First block data").unwrap();
            writer.write_to_block(b"More data in first block").unwrap();
            writer.flush_block().unwrap();

            writer.write_to_block(b"Second block data").unwrap();
            writer.close().unwrap();
        }

        // Read blocks
        {
            let input = storage.open_input("test.blocks").unwrap();
            let mut reader = BlockReader::new(input).unwrap();

            // Read first block
            let block1 = reader.read_block().unwrap().unwrap();
            assert!(block1.starts_with(b"First block data"));

            // Read second block
            let block2 = reader.read_block().unwrap().unwrap();
            assert!(block2.starts_with(b"Second block data"));

            // No more blocks
            assert!(reader.read_block().unwrap().is_none());

            reader.close().unwrap();
        }
    }

    #[test]
    fn test_string_u64_map() {
        let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));

        let mut original_map = HashMap::new();
        original_map.insert("term1".to_string(), 100);
        original_map.insert("term2".to_string(), 200);
        original_map.insert("term3".to_string(), 300);

        // Write map
        {
            let output = storage.create_output("test.map").unwrap();
            let mut writer = StructWriter::new(output);
            writer.write_string_u64_map(&original_map).unwrap();
            writer.close().unwrap();
        }

        // Read map
        {
            let input = storage.open_input("test.map").unwrap();
            let mut reader = StructReader::new(input).unwrap();
            let read_map = reader.read_string_u64_map().unwrap();

            assert_eq!(read_map.len(), original_map.len());
            for (key, value) in &original_map {
                assert_eq!(read_map.get(key), Some(value));
            }

            reader.close().unwrap();
        }
    }

    #[test]
    fn test_delta_compression() {
        let values = vec![1000, 1005, 1010, 1020, 1050, 1100];
        let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));

        // Write compressed values
        {
            let output = storage.create_output("test.delta").unwrap();
            let mut writer = StructWriter::new(output);
            writer.write_delta_compressed_u32s(&values).unwrap();
            writer.close().unwrap();
        }

        // Read and verify
        {
            let input = storage.open_input("test.delta").unwrap();
            let mut reader = StructReader::new(input).unwrap();
            let decoded = reader.read_delta_compressed_u32s().unwrap();
            assert_eq!(decoded, values);
            reader.close().unwrap();
        }
    }
}