zipora 3.1.5

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
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
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
//! Memory-mapped I/O implementations
//!
//! This module provides memory-mapped implementations of DataInput and DataOutput
//! traits for high-performance, zero-copy file operations.

use crate::error::{Result, ZiporaError};
#[cfg(feature = "mmap")]
use crate::io::{DataInput, DataOutput, VarInt};
#[cfg(feature = "mmap")]
use std::fs::{File, OpenOptions};
#[cfg(feature = "mmap")]
use std::io::{BufReader, Read as StdRead, Seek, SeekFrom};
#[cfg(feature = "mmap")]
use std::path::Path;

#[cfg(feature = "mmap")]
use memmap2::{Mmap, MmapMut, MmapOptions};

#[cfg(target_os = "linux")]
#[cfg(feature = "mmap")]
use crate::memory::hugepage::{HUGEPAGE_SIZE_1GB, HUGEPAGE_SIZE_2MB, HugePage};

/// Thresholds for adaptive memory mapping strategy
const SMALL_FILE_THRESHOLD: u64 = 4 * 1024; // 4KB - use buffered I/O to avoid mmap overhead
const HUGEPAGE_2MB_THRESHOLD: u64 = 1024 * 1024; // 1MB - use 2MB hugepages
const HUGEPAGE_1GB_THRESHOLD: u64 = 100 * 1024 * 1024; // 100MB - use 1GB hugepages

/// Access pattern hints for optimization
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AccessPattern {
    /// Sequential access pattern - optimizes for readahead
    Sequential,
    /// Random access pattern - disables readahead, optimizes for TLB efficiency  
    Random,
    /// Mixed access pattern - balanced optimization
    Mixed,
    /// Unknown pattern - uses conservative defaults
    Unknown,
}

/// Strategy used by MemoryMappedInput based on file size and system capabilities
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum InputStrategy {
    /// Use buffered I/O for small files to avoid mmap overhead
    BufferedIO,
    /// Standard memory mapping with 4KB pages
    StandardMmap,
    /// Memory mapping with hugepages (2MB or 1GB)
    /// Note: This currently incurs an upfront copy from the file into the hugepage.
    HugepageMmap,
}

/// Memory-mapped file input for fast reading operations
///
/// Provides efficient reading from memory-mapped files. The standard mmap
/// strategy avoids copying data, while the hugepage strategy incurs an upfront copy.
/// Ideal for large files and random access patterns.
///
/// # Examples
///
/// ```rust
/// use zipora::io::MemoryMappedInput;
/// use zipora::DataInput;
/// use std::fs::File;
/// use std::io::Write;
///
/// # use tempfile::NamedTempFile;
/// # let mut temp_file = NamedTempFile::new().unwrap();
/// # temp_file.write_all(&[0x01, 0x02, 0x03, 0x04]).unwrap();
/// # let temp_path = temp_file.path();
/// let file = File::open(temp_path).unwrap();
/// let mut input = MemoryMappedInput::new(file).unwrap();
///
/// let value = input.read_u32().unwrap();
/// assert_eq!(value, 0x04030201); // Little-endian
/// ```
#[cfg(feature = "mmap")]
pub struct MemoryMappedInput {
    strategy: InputStrategy,
    file_size: u64,
    position: usize,
    access_pattern: AccessPattern,

    // Strategy-specific storage
    mmap: Option<Mmap>,
    buffered_reader: Option<BufReader<File>>,
    #[cfg(target_os = "linux")]
    hugepage: Option<HugePage>,
}

#[cfg(feature = "mmap")]
impl MemoryMappedInput {
    /// Creates a new memory-mapped input from a file with adaptive strategy
    pub fn new(file: File) -> Result<Self> {
        Self::new_with_pattern(file, AccessPattern::Unknown)
    }

    /// Creates a new memory-mapped input with access pattern hint
    pub fn new_with_pattern(file: File, access_pattern: AccessPattern) -> Result<Self> {
        let file_size = file
            .metadata()
            .map_err(|e| ZiporaError::io_error(format!("Failed to get file metadata: {}", e)))?
            .len();

        let strategy = Self::select_strategy(file_size);

        let mut input = MemoryMappedInput {
            strategy,
            file_size,
            position: 0,
            access_pattern,
            mmap: None,
            buffered_reader: None,
            #[cfg(target_os = "linux")]
            hugepage: None,
        };

        input.initialize_strategy(file)?;
        Ok(input)
    }

    /// Selects the optimal strategy based on file size and system capabilities
    fn select_strategy(file_size: u64) -> InputStrategy {
        if file_size <= SMALL_FILE_THRESHOLD {
            // For small files, buffered I/O avoids mmap overhead
            InputStrategy::BufferedIO
        } else if file_size >= HUGEPAGE_2MB_THRESHOLD {
            // For large files, try hugepages for better TLB efficiency
            #[cfg(target_os = "linux")]
            {
                if crate::memory::hugepage::hugepages_available() {
                    InputStrategy::HugepageMmap
                } else {
                    InputStrategy::StandardMmap
                }
            }
            #[cfg(not(target_os = "linux"))]
            {
                InputStrategy::StandardMmap
            }
        } else {
            // Medium files use standard memory mapping
            InputStrategy::StandardMmap
        }
    }

    /// Initializes the chosen strategy
    fn initialize_strategy(&mut self, mut file: File) -> Result<()> {
        match self.strategy {
            InputStrategy::BufferedIO => {
                // For small files, use buffered I/O to avoid mmap overhead
                let buf_size = std::cmp::min(self.file_size as usize, 8192); // Max 8KB buffer
                file.seek(SeekFrom::Start(0))
                    .map_err(|e| ZiporaError::io_error(format!("Failed to seek file: {}", e)))?;
                self.buffered_reader = Some(BufReader::with_capacity(buf_size, file));
            }

            InputStrategy::StandardMmap => {
                // SAFETY: MmapOptions::map creates valid memory mapping from file descriptor
                let mmap = unsafe {
                    MmapOptions::new().map(&file).map_err(|e| {
                        ZiporaError::io_error(format!("Failed to memory-map file: {}", e))
                    })?
                };

                // Apply advanced madvise hints
                self.apply_madvise_hints(&mmap)?;
                self.mmap = Some(mmap);
            }

            #[cfg(target_os = "linux")]
            InputStrategy::HugepageMmap => {
                // Try to use hugepages for large files
                let hugepage_size = if self.file_size >= HUGEPAGE_1GB_THRESHOLD {
                    HUGEPAGE_SIZE_1GB
                } else {
                    HUGEPAGE_SIZE_2MB
                };

                match HugePage::new(self.file_size as usize, hugepage_size) {
                    Ok(mut hugepage) => {
                        // Note: "HugepageMmap" is not true zero-copy. It allocates a hugepage and copies the file into it.
                        self.copy_file_to_hugepage(&mut file, &mut hugepage)?;
                        self.hugepage = Some(hugepage);
                    }
                    Err(_) => {
                        // Fallback to standard mmap if hugepage allocation fails
                        self.strategy = InputStrategy::StandardMmap;
                        self.initialize_strategy(file)?;
                    }
                }
            }

            #[cfg(not(target_os = "linux"))]
            InputStrategy::HugepageMmap => {
                // Fallback to standard mmap on non-Linux systems
                self.strategy = InputStrategy::StandardMmap;
                self.initialize_strategy(file)?;
            }
        }

        Ok(())
    }

    #[cfg(target_os = "linux")]
    fn copy_file_to_hugepage(&mut self, file: &mut File, hugepage: &mut HugePage) -> Result<()> {
        file.seek(SeekFrom::Start(0))
            .map_err(|e| ZiporaError::io_error(format!("Failed to seek file: {}", e)))?;

        let mut buffer = vec![0u8; 64 * 1024]; // 64KB read buffer
        let mut total_read = 0;
        let hugepage_slice = hugepage.as_mut_slice();

        while total_read < self.file_size as usize {
            let bytes_read = file
                .read(&mut buffer)
                .map_err(|e| ZiporaError::io_error(format!("Failed to read file: {}", e)))?;

            if bytes_read == 0 {
                break;
            }

            hugepage_slice[total_read..total_read + bytes_read]
                .copy_from_slice(&buffer[..bytes_read]);
            total_read += bytes_read;
        }

        Ok(())
    }

    /// Apply advanced madvise hints for optimal performance
    fn apply_madvise_hints(&self, mmap: &Mmap) -> Result<()> {
        #[cfg(target_os = "linux")]
        {
            let ptr = mmap.as_ptr() as *mut libc::c_void;
            let len = mmap.len();

            // SAFETY: ptr is valid from mmap, len is the mapped size, madvise hints are safe
            unsafe {
                // Set access pattern hint
                match self.access_pattern {
                    AccessPattern::Sequential => {
                        // Optimize for sequential access with aggressive readahead
                        libc::madvise(ptr, len, libc::MADV_SEQUENTIAL);
                        libc::madvise(ptr, len, libc::MADV_WILLNEED);

                        // For large sequential files, prefetch the entire file
                        if len > 64 * 1024 {
                            self.prefetch_sequential(ptr, len)?;
                        }
                    }
                    AccessPattern::Random => {
                        // Disable readahead for random access
                        libc::madvise(ptr, len, libc::MADV_RANDOM);

                        // Enable transparent hugepages for better TLB efficiency
                        if len > 2 * 1024 * 1024 {
                            libc::madvise(ptr, len, libc::MADV_HUGEPAGE);
                        }
                    }
                    AccessPattern::Mixed => {
                        // Use normal access pattern with some readahead
                        libc::madvise(ptr, len, libc::MADV_NORMAL);

                        // Conservative prefetch for mixed workloads
                        if len > 2 * 1024 * 1024 {
                            libc::madvise(ptr, len, libc::MADV_HUGEPAGE);
                        }
                    }
                    AccessPattern::Unknown => {
                        // Conservative defaults
                        libc::madvise(ptr, len, libc::MADV_NORMAL);
                    }
                }

                // Enable memory locking for small, frequently accessed files
                if len <= 16 * 1024 * 1024 && self.access_pattern == AccessPattern::Sequential {
                    // Try to lock pages in memory (ignore failures)
                    let _ = libc::mlock(ptr, len);
                }
            }
        }

        Ok(())
    }

    #[cfg(target_os = "linux")]
    fn prefetch_sequential(&self, ptr: *mut libc::c_void, len: usize) -> Result<()> {
        // Implement intelligent prefetching for sequential access
        const PREFETCH_WINDOW: usize = 2 * 1024 * 1024; // 2MB prefetch window
        const CACHE_LINE_SIZE: usize = 64;

        // SAFETY: ptr is valid mmap pointer, offset calculations keep us within bounds
        unsafe {
            let mut offset = 0;
            while offset < len {
                let prefetch_size = std::cmp::min(PREFETCH_WINDOW, len - offset);
                let prefetch_ptr = (ptr as *const u8).add(offset) as *const libc::c_void;

                // Use POSIX_FADV_WILLNEED for async prefetch
                // This is more efficient than synchronous readahead
                libc::madvise(
                    prefetch_ptr as *mut libc::c_void,
                    prefetch_size,
                    libc::MADV_WILLNEED,
                );

                // Hardware prefetch hints for L1/L2 cache
                for i in (0..prefetch_size).step_by(CACHE_LINE_SIZE) {
                    let cache_line = (prefetch_ptr as *const u8).add(i);
                    #[cfg(target_arch = "x86_64")]
                    {
                        // Use PREFETCHNTA for streaming data
                        std::arch::x86_64::_mm_prefetch(
                            cache_line as *const i8,
                            std::arch::x86_64::_MM_HINT_NTA,
                        );
                    }
                }

                offset += prefetch_size;
            }
        }

        Ok(())
    }

    /// Creates a new memory-mapped input from a file path
    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self> {
        let file = File::open(path)
            .map_err(|e| ZiporaError::io_error(format!("Failed to open file: {}", e)))?;
        Self::new(file)
    }

    /// Creates a new memory-mapped input from a file path with access pattern hint
    pub fn from_path_with_pattern<P: AsRef<Path>>(
        path: P,
        access_pattern: AccessPattern,
    ) -> Result<Self> {
        let file = File::open(path)
            .map_err(|e| ZiporaError::io_error(format!("Failed to open file: {}", e)))?;
        Self::new_with_pattern(file, access_pattern)
    }

    /// Returns the total size of the file
    #[inline]
    pub fn len(&self) -> usize {
        self.file_size as usize
    }

    /// Returns true if the file is empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.file_size == 0
    }

    /// Returns the current position
    pub fn position(&self) -> usize {
        self.position
    }

    /// Returns the strategy being used
    pub fn strategy(&self) -> InputStrategy {
        self.strategy
    }

    /// Seeks to a specific position
    pub fn seek(&mut self, pos: usize) -> Result<()> {
        if pos > self.file_size as usize {
            return Err(ZiporaError::out_of_bounds(pos, self.file_size as usize));
        }

        match self.strategy {
            InputStrategy::BufferedIO => {
                if let Some(ref mut reader) = self.buffered_reader {
                    reader
                        .seek(SeekFrom::Start(pos as u64))
                        .map_err(|e| ZiporaError::io_error(format!("Failed to seek: {}", e)))?;
                }
            }
            _ => {
                // For memory-mapped strategies, just update position
            }
        }

        self.position = pos;
        Ok(())
    }

    /// Returns the remaining bytes available for reading
    pub fn remaining(&self) -> usize {
        (self.file_size as usize).saturating_sub(self.position)
    }

    /// Reads a slice of bytes (zero-copy when possible)
    pub fn read_slice(&mut self, len: usize) -> Result<Vec<u8>> {
        let end_pos = self.position + len;
        if end_pos > self.file_size as usize {
            return Err(ZiporaError::out_of_bounds(end_pos, self.file_size as usize));
        }

        let data = match self.strategy {
            InputStrategy::BufferedIO => {
                if let Some(ref mut reader) = self.buffered_reader {
                    let mut buffer = vec![0u8; len];
                    reader
                        .read_exact(&mut buffer)
                        .map_err(|e| ZiporaError::io_error(format!("Failed to read: {}", e)))?;
                    buffer
                } else {
                    return Err(ZiporaError::invalid_data("Buffered reader not initialized"));
                }
            }

            InputStrategy::StandardMmap => {
                if let Some(ref mmap) = self.mmap {
                    mmap[self.position..end_pos].to_vec()
                } else {
                    return Err(ZiporaError::invalid_data("Memory map not initialized"));
                }
            }

            #[cfg(target_os = "linux")]
            InputStrategy::HugepageMmap => {
                if let Some(ref hugepage) = self.hugepage {
                    hugepage.as_slice()[self.position..end_pos].to_vec()
                } else {
                    return Err(ZiporaError::invalid_data("Hugepage not initialized"));
                }
            }

            #[cfg(not(target_os = "linux"))]
            InputStrategy::HugepageMmap => {
                return Err(ZiporaError::not_supported(
                    "Hugepages not available on this platform",
                ));
            }
        };

        self.position = end_pos;
        Ok(data)
    }

    /// Reads a slice of bytes without copying (zero-copy, memory-mapped only)
    pub fn read_slice_zero_copy(&mut self, len: usize) -> Result<&[u8]> {
        let end_pos = self.position + len;
        if end_pos > self.file_size as usize {
            return Err(ZiporaError::out_of_bounds(end_pos, self.file_size as usize));
        }

        let slice = match self.strategy {
            InputStrategy::BufferedIO => {
                return Err(ZiporaError::not_supported(
                    "Zero-copy not available for buffered I/O strategy",
                ));
            }

            InputStrategy::StandardMmap => {
                if let Some(ref mmap) = self.mmap {
                    &mmap[self.position..end_pos]
                } else {
                    return Err(ZiporaError::invalid_data("Memory map not initialized"));
                }
            }

            #[cfg(target_os = "linux")]
            InputStrategy::HugepageMmap => {
                if let Some(ref hugepage) = self.hugepage {
                    &hugepage.as_slice()[self.position..end_pos]
                } else {
                    return Err(ZiporaError::invalid_data("Hugepage not initialized"));
                }
            }

            #[cfg(not(target_os = "linux"))]
            InputStrategy::HugepageMmap => {
                return Err(ZiporaError::not_supported(
                    "Hugepages not available on this platform",
                ));
            }
        };

        self.position = end_pos;
        Ok(slice)
    }

    /// Peeks at bytes without advancing the position (zero-copy when possible)
    pub fn peek_slice(&self, len: usize) -> Result<Vec<u8>> {
        let end_pos = self.position + len;
        if end_pos > self.file_size as usize {
            return Err(ZiporaError::out_of_bounds(end_pos, self.file_size as usize));
        }

        match self.strategy {
            InputStrategy::BufferedIO => {
                // For buffered I/O, peek is not efficiently supported
                Err(ZiporaError::not_supported(
                    "Peek not efficiently supported for buffered I/O strategy",
                ))
            }

            InputStrategy::StandardMmap => {
                if let Some(ref mmap) = self.mmap {
                    Ok(mmap[self.position..end_pos].to_vec())
                } else {
                    Err(ZiporaError::invalid_data("Memory map not initialized"))
                }
            }

            #[cfg(target_os = "linux")]
            InputStrategy::HugepageMmap => {
                if let Some(ref hugepage) = self.hugepage {
                    Ok(hugepage.as_slice()[self.position..end_pos].to_vec())
                } else {
                    Err(ZiporaError::invalid_data("Hugepage not initialized"))
                }
            }

            #[cfg(not(target_os = "linux"))]
            InputStrategy::HugepageMmap => Err(ZiporaError::not_supported(
                "Hugepages not available on this platform",
            )),
        }
    }

    /// Peeks at bytes without advancing the position (zero-copy, memory-mapped only)
    pub fn peek_slice_zero_copy(&self, len: usize) -> Result<&[u8]> {
        let end_pos = self.position + len;
        if end_pos > self.file_size as usize {
            return Err(ZiporaError::out_of_bounds(end_pos, self.file_size as usize));
        }

        match self.strategy {
            InputStrategy::BufferedIO => Err(ZiporaError::not_supported(
                "Zero-copy peek not available for buffered I/O strategy",
            )),

            InputStrategy::StandardMmap => {
                if let Some(ref mmap) = self.mmap {
                    Ok(&mmap[self.position..end_pos])
                } else {
                    Err(ZiporaError::invalid_data("Memory map not initialized"))
                }
            }

            #[cfg(target_os = "linux")]
            InputStrategy::HugepageMmap => {
                if let Some(ref hugepage) = self.hugepage {
                    Ok(&hugepage.as_slice()[self.position..end_pos])
                } else {
                    Err(ZiporaError::invalid_data("Hugepage not initialized"))
                }
            }

            #[cfg(not(target_os = "linux"))]
            InputStrategy::HugepageMmap => Err(ZiporaError::not_supported(
                "Hugepages not available on this platform",
            )),
        }
    }
}

#[cfg(feature = "mmap")]
impl DataInput for MemoryMappedInput {
    fn read_u8(&mut self) -> Result<u8> {
        let data = self.read_slice(1)?;
        Ok(data[0])
    }

    fn read_u16(&mut self) -> Result<u16> {
        let data = self.read_slice(2)?;
        Ok(u16::from_le_bytes([data[0], data[1]]))
    }

    fn read_u32(&mut self) -> Result<u32> {
        let data = self.read_slice(4)?;
        Ok(u32::from_le_bytes([data[0], data[1], data[2], data[3]]))
    }

    fn read_u64(&mut self) -> Result<u64> {
        let data = self.read_slice(8)?;
        Ok(u64::from_le_bytes([
            data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
        ]))
    }

    fn read_var_int(&mut self) -> Result<u64> {
        let mut result = 0u64;
        let mut shift = 0;

        for _ in 0..VarInt::MAX_ENCODED_LEN {
            let byte = self.read_u8()?;
            result |= ((byte & 0x7F) as u64) << shift;

            if byte & 0x80 == 0 {
                return Ok(result);
            }

            shift += 7;
            if shift >= 64 {
                return Err(ZiporaError::invalid_data("Variable integer overflow"));
            }
        }

        Err(ZiporaError::invalid_data("Variable integer too long"))
    }

    fn read_bytes(&mut self, buf: &mut [u8]) -> Result<()> {
        let data = self.read_slice(buf.len())?;
        buf.copy_from_slice(&data);
        Ok(())
    }

    fn read_length_prefixed_string(&mut self) -> Result<String> {
        let len = self.read_var_int()? as usize;
        let data = self.read_slice(len)?;
        String::from_utf8(data)
            .map_err(|e| ZiporaError::invalid_data(format!("Invalid UTF-8 string: {}", e)))
    }

    fn skip(&mut self, n: usize) -> Result<()> {
        let new_pos = self.position + n;
        self.seek(new_pos)
    }
}

/// Memory-mapped file output for efficient writing operations
///
/// Provides efficient writing to memory-mapped files with automatic growth.
/// Useful for sequential writing patterns and large file generation.
///
/// # Examples
///
/// ```rust
/// use zipora::io::MemoryMappedOutput;
/// use zipora::DataOutput;
/// use tempfile::NamedTempFile;
///
/// let temp_file = NamedTempFile::new().unwrap();
/// let mut output = MemoryMappedOutput::create(temp_file.path(), 1024).unwrap();
///
/// output.write_u32(0x12345678).unwrap();
/// output.write_length_prefixed_string("hello").unwrap();
/// output.flush().unwrap();
/// ```
#[cfg(feature = "mmap")]
pub struct MemoryMappedOutput {
    file: File,
    mmap: MmapMut,
    position: usize,
    capacity: usize,
}

#[cfg(feature = "mmap")]
impl MemoryMappedOutput {
    /// Creates a new memory-mapped output file with specified initial size
    pub fn create<P: AsRef<Path>>(path: P, initial_size: usize) -> Result<Self> {
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)
            .map_err(|e| ZiporaError::io_error(format!("Failed to create file: {}", e)))?;

        // Set the file size
        file.set_len(initial_size as u64)
            .map_err(|e| ZiporaError::io_error(format!("Failed to set file size: {}", e)))?;

        // SAFETY: MmapOptions::map_mut creates valid mutable memory mapping from file
        let mmap = unsafe {
            MmapOptions::new()
                .map_mut(&file)
                .map_err(|e| ZiporaError::io_error(format!("Failed to memory-map file: {}", e)))?
        };

        Ok(MemoryMappedOutput {
            file,
            mmap,
            position: 0,
            capacity: initial_size,
        })
    }

    /// Opens an existing file for memory-mapped writing
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .open(path)
            .map_err(|e| ZiporaError::io_error(format!("Failed to open file: {}", e)))?;

        let file_size = file
            .metadata()
            .map_err(|e| ZiporaError::io_error(format!("Failed to get file metadata: {}", e)))?
            .len() as usize;

        // SAFETY: MmapOptions::map_mut creates valid mutable memory mapping from existing file
        let mmap = unsafe {
            MmapOptions::new()
                .map_mut(&file)
                .map_err(|e| ZiporaError::io_error(format!("Failed to memory-map file: {}", e)))?
        };

        Ok(MemoryMappedOutput {
            file,
            mmap,
            position: 0,
            capacity: file_size,
        })
    }

    /// Returns the current position in the mapped region
    pub fn position(&self) -> usize {
        self.position
    }

    /// Returns the total capacity of the mapped region
    #[inline]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Returns the remaining space available for writing
    pub fn remaining(&self) -> usize {
        self.capacity.saturating_sub(self.position)
    }

    /// Seeks to a specific position in the mapped region
    pub fn seek(&mut self, pos: usize) -> Result<()> {
        if pos > self.capacity {
            return Err(ZiporaError::out_of_bounds(pos, self.capacity));
        }
        self.position = pos;
        Ok(())
    }

    /// Ensures the mapped region has at least the specified capacity
    fn ensure_capacity(&mut self, required: usize) -> Result<()> {
        if required <= self.capacity {
            return Ok(());
        }

        // Grow by 50% or to required size, whichever is larger
        let new_size = std::cmp::max(required, self.capacity + (self.capacity / 2));

        // Flush current mmap and grow the file
        drop(std::mem::replace(
            &mut self.mmap,
            // SAFETY: MmapMut::map_anon(0) cannot fail because:
            // 1. Zero-byte anonymous memory mappings don't require actual system resources
            // 2. They're trivial operations that always succeed on all platforms (Linux/macOS/Windows)
            // 3. This is used as a placeholder during mem::replace() to allow dropping the old mmap
            // 4. The memmap2 library guarantees zero-byte anonymous mappings succeed
            MmapMut::map_anon(0).expect("zero-length anonymous mmap"),
        ));

        self.file
            .set_len(new_size as u64)
            .map_err(|e| ZiporaError::io_error(format!("Failed to grow file: {}", e)))?;

        // SAFETY: File size set above, remapping with valid file descriptor
        self.mmap = unsafe {
            MmapOptions::new()
                .map_mut(&self.file)
                .map_err(|e| ZiporaError::io_error(format!("Failed to remap file: {}", e)))?
        };

        self.capacity = new_size;
        Ok(())
    }

    /// Writes a slice of bytes to the mapped region
    pub fn write_slice(&mut self, data: &[u8]) -> Result<()> {
        let required_capacity = self.position + data.len();
        self.ensure_capacity(required_capacity)?;

        self.mmap[self.position..self.position + data.len()].copy_from_slice(data);
        self.position += data.len();
        Ok(())
    }

    /// Truncates the file to the current position
    pub fn truncate(&mut self) -> Result<()> {
        // Flush to ensure all data is written
        self.mmap
            .flush()
            .map_err(|e| ZiporaError::io_error(format!("Failed to flush mmap: {}", e)))?;

        // Unmap before truncating
        drop(std::mem::replace(
            &mut self.mmap,
            // SAFETY: MmapMut::map_anon(0) cannot fail because:
            // 1. Zero-byte anonymous memory mappings don't require actual system resources
            // 2. They're trivial operations that always succeed on all platforms (Linux/macOS/Windows)
            // 3. This is used as a placeholder during mem::replace() to allow dropping the old mmap
            // 4. The memmap2 library guarantees zero-byte anonymous mappings succeed
            MmapMut::map_anon(0).expect("zero-length anonymous mmap"),
        ));

        // Truncate the file
        self.file
            .set_len(self.position as u64)
            .map_err(|e| ZiporaError::io_error(format!("Failed to truncate file: {}", e)))?;

        // Remap with new size
        // SAFETY: File truncated to position above, remapping with valid file descriptor
        self.mmap = unsafe {
            MmapOptions::new().map_mut(&self.file).map_err(|e| {
                ZiporaError::io_error(format!("Failed to remap truncated file: {}", e))
            })?
        };

        self.capacity = self.position;
        Ok(())
    }
}

#[cfg(feature = "mmap")]
impl DataOutput for MemoryMappedOutput {
    fn write_u8(&mut self, value: u8) -> Result<()> {
        self.write_slice(&[value])
    }

    fn write_u16(&mut self, value: u16) -> Result<()> {
        self.write_slice(&value.to_le_bytes())
    }

    fn write_u32(&mut self, value: u32) -> Result<()> {
        self.write_slice(&value.to_le_bytes())
    }

    fn write_u64(&mut self, value: u64) -> Result<()> {
        self.write_slice(&value.to_le_bytes())
    }

    fn write_var_int(&mut self, value: u64) -> Result<()> {
        let encoded = VarInt::encode(value);
        self.write_slice(&encoded)
    }

    fn write_bytes(&mut self, data: &[u8]) -> Result<()> {
        self.write_slice(data)
    }

    fn write_length_prefixed_string(&mut self, s: &str) -> Result<()> {
        self.write_var_int(s.len() as u64)?;
        self.write_slice(s.as_bytes())
    }

    fn flush(&mut self) -> Result<()> {
        self.mmap.flush().map_err(|e| {
            ZiporaError::io_error(format!("Failed to flush memory-mapped file: {}", e))
        })
    }
}

// Provide stub implementations when mmap feature is disabled
#[cfg(not(feature = "mmap"))]
pub struct MemoryMappedInput;

#[cfg(not(feature = "mmap"))]
impl MemoryMappedInput {
    pub fn new(_file: std::fs::File) -> Result<Self> {
        Err(ZiporaError::invalid_data(
            "Memory mapping is not available. Enable the 'mmap' feature to use MemoryMappedInput.",
        ))
    }

    pub fn from_path<P: AsRef<std::path::Path>>(_path: P) -> Result<Self> {
        Err(ZiporaError::invalid_data(
            "Memory mapping is not available. Enable the 'mmap' feature to use MemoryMappedInput.",
        ))
    }
}

#[cfg(not(feature = "mmap"))]
pub struct MemoryMappedOutput;

#[cfg(not(feature = "mmap"))]
impl MemoryMappedOutput {
    pub fn create<P: AsRef<std::path::Path>>(_path: P, _initial_size: usize) -> Result<Self> {
        Err(ZiporaError::invalid_data(
            "Memory mapping is not available. Enable the 'mmap' feature to use MemoryMappedOutput.",
        ))
    }

    pub fn open<P: AsRef<std::path::Path>>(_path: P) -> Result<Self> {
        Err(ZiporaError::invalid_data(
            "Memory mapping is not available. Enable the 'mmap' feature to use MemoryMappedOutput.",
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::{NamedTempFile, TempDir};

    #[cfg(feature = "mmap")]
    #[test]
    fn test_memory_mapped_input_basic() {
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(&[0x01, 0x02, 0x03, 0x04]).unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();

        assert_eq!(input.len(), 4);
        assert!(!input.is_empty());
        assert_eq!(input.position(), 0);
        assert_eq!(input.remaining(), 4);

        assert_eq!(input.read_u8().unwrap(), 0x01);
        assert_eq!(input.position(), 1);
        assert_eq!(input.remaining(), 3);

        assert_eq!(input.read_u8().unwrap(), 0x02);
        assert_eq!(input.read_u16().unwrap(), 0x0403); // Little-endian
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_memory_mapped_input_from_path() {
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(b"Hello, World!").unwrap();
        temp_file.flush().unwrap();

        let mut input = MemoryMappedInput::from_path(temp_file.path()).unwrap();
        assert_eq!(input.len(), 13);

        let slice = input.read_slice(5).unwrap();
        assert_eq!(slice, b"Hello");

        input.seek(7).unwrap();
        let slice = input.read_slice(6).unwrap();
        assert_eq!(slice, b"World!");
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_memory_mapped_input_operations() {
        let mut temp_file = NamedTempFile::new().unwrap();

        // Write test data
        temp_file.write_all(&0x12345678u32.to_le_bytes()).unwrap();
        temp_file
            .write_all(&0x9ABCDEF012345678u64.to_le_bytes())
            .unwrap();

        // Write variable integer
        let var_bytes = VarInt::encode(300);
        temp_file.write_all(&var_bytes).unwrap();

        // Write length-prefixed string
        let test_str = "Hello, Memory Mapping!";
        let str_len_bytes = VarInt::encode(test_str.len() as u64);
        temp_file.write_all(&str_len_bytes).unwrap();
        temp_file.write_all(test_str.as_bytes()).unwrap();

        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();

        // Test reading
        assert_eq!(input.read_u32().unwrap(), 0x12345678);
        assert_eq!(input.read_u64().unwrap(), 0x9ABCDEF012345678);
        assert_eq!(input.read_var_int().unwrap(), 300);
        assert_eq!(input.read_length_prefixed_string().unwrap(), test_str);
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_memory_mapped_input_peek_and_skip() {
        let mut temp_file = NamedTempFile::new().unwrap();
        // Create a larger file to force standard mmap strategy
        let mut test_data = b"0123456789".to_vec();
        test_data.resize(8192, b'X'); // 8KB file - above 4KB threshold
        temp_file.write_all(&test_data).unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();

        // Should use standard mmap for larger file
        assert_eq!(input.strategy(), InputStrategy::StandardMmap);

        // Test peek
        let peeked = input.peek_slice(3).unwrap();
        assert_eq!(peeked, b"012");
        assert_eq!(input.position(), 0); // Position shouldn't change

        // Test skip
        input.skip(2).unwrap();
        assert_eq!(input.position(), 2);

        let slice = input.read_slice(3).unwrap();
        assert_eq!(slice, b"234");
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_memory_mapped_output_basic() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test_output.dat");

        let mut output = MemoryMappedOutput::create(&file_path, 1024).unwrap();
        assert_eq!(output.capacity(), 1024);
        assert_eq!(output.position(), 0);
        assert_eq!(output.remaining(), 1024);

        output.write_u32(0x12345678).unwrap();
        assert_eq!(output.position(), 4);

        output.write_slice(b"Hello").unwrap();
        assert_eq!(output.position(), 9);

        output.flush().unwrap();

        // Verify by reading back
        let file = File::open(&file_path).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();
        assert_eq!(input.read_u32().unwrap(), 0x12345678);

        let slice = input.read_slice(5).unwrap();
        assert_eq!(slice, b"Hello");
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_memory_mapped_output_growth() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test_growth.dat");

        let mut output = MemoryMappedOutput::create(&file_path, 10).unwrap();
        assert_eq!(output.capacity(), 10);

        // Write more data than initial capacity
        let large_data = vec![0xAB; 20];
        output.write_slice(&large_data).unwrap();

        // Capacity should have grown
        assert!(output.capacity() >= 20);
        assert_eq!(output.position(), 20);

        output.flush().unwrap();

        // Verify data
        let file = File::open(&file_path).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();
        let read_data = input.read_slice(20).unwrap();
        assert_eq!(read_data, large_data);
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_memory_mapped_output_operations() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test_ops.dat");

        let mut output = MemoryMappedOutput::create(&file_path, 1024).unwrap();

        // Test various write operations
        output.write_u8(0xFF).unwrap();
        output.write_u16(0x1234).unwrap();
        output.write_u32(0x56789ABC).unwrap();
        output.write_u64(0xDEF0123456789ABC).unwrap();
        output.write_var_int(12345).unwrap();
        output.write_length_prefixed_string("Test String").unwrap();

        output.flush().unwrap();

        // Verify by reading back
        let file = File::open(&file_path).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();

        assert_eq!(input.read_u8().unwrap(), 0xFF);
        assert_eq!(input.read_u16().unwrap(), 0x1234);
        assert_eq!(input.read_u32().unwrap(), 0x56789ABC);
        assert_eq!(input.read_u64().unwrap(), 0xDEF0123456789ABC);
        assert_eq!(input.read_var_int().unwrap(), 12345);
        assert_eq!(input.read_length_prefixed_string().unwrap(), "Test String");
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_memory_mapped_output_truncate() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test_truncate.dat");

        let mut output = MemoryMappedOutput::create(&file_path, 1024).unwrap();
        output.write_slice(b"Hello, World!").unwrap();

        let written_len = output.position();
        output.truncate().unwrap();

        assert_eq!(output.capacity(), written_len);

        // Verify file size
        let metadata = std::fs::metadata(&file_path).unwrap();
        assert_eq!(metadata.len() as usize, written_len);
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_memory_mapped_input_bounds_checking() {
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(&[1, 2, 3]).unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();

        // Reading beyond bounds should fail
        assert!(input.read_slice(5).is_err());
        assert!(input.peek_slice(5).is_err());

        input.seek(2).unwrap();
        assert!(input.read_u16().is_err()); // Would read beyond end

        // Seeking beyond bounds should fail
        assert!(input.seek(10).is_err());
    }

    #[cfg(not(feature = "mmap"))]
    #[test]
    fn test_mmap_disabled_error() {
        use std::fs::File;

        // When mmap feature is disabled, should return appropriate errors
        let temp_file = NamedTempFile::new().unwrap();
        let file = File::open(temp_file.path()).unwrap();

        let result = MemoryMappedInput::new(file);
        assert!(result.is_err());

        let result = MemoryMappedOutput::create(temp_file.path(), 1024);
        assert!(result.is_err());
    }

    // Tests for adaptive memory mapping behavior
    #[cfg(feature = "mmap")]
    #[test]
    fn test_small_file_uses_buffered_io() {
        let mut temp_file = NamedTempFile::new().unwrap();
        let small_data = vec![0x42u8; 2048]; // 2KB file - below 4KB threshold
        temp_file.write_all(&small_data).unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let input = MemoryMappedInput::new(file).unwrap();

        // Should use buffered I/O for small files
        assert_eq!(input.strategy(), InputStrategy::BufferedIO);
        assert_eq!(input.len(), 2048);
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_medium_file_uses_standard_mmap() {
        let mut temp_file = NamedTempFile::new().unwrap();
        let medium_data = vec![0x55u8; 64 * 1024]; // 64KB file - between 4KB and 1MB
        temp_file.write_all(&medium_data).unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let input = MemoryMappedInput::new(file).unwrap();

        // Should use standard memory mapping for medium files
        assert_eq!(input.strategy(), InputStrategy::StandardMmap);
        assert_eq!(input.len(), 64 * 1024);
    }

    #[cfg(all(feature = "mmap", target_os = "linux"))]
    #[test]
    fn test_large_file_attempts_hugepages() {
        let mut temp_file = NamedTempFile::new().unwrap();
        let large_data = vec![0x77u8; 2 * 1024 * 1024]; // 2MB file - above hugepage threshold
        temp_file.write_all(&large_data).unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let input = MemoryMappedInput::new(file).unwrap();

        // Should attempt hugepages (may fallback to standard mmap if hugepages unavailable)
        let strategy = input.strategy();
        assert!(
            strategy == InputStrategy::HugepageMmap || strategy == InputStrategy::StandardMmap,
            "Large file should use hugepages or fallback to standard mmap"
        );
        assert_eq!(input.len(), 2 * 1024 * 1024);
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_access_pattern_hints() {
        let mut temp_file = NamedTempFile::new().unwrap();
        let test_data = vec![0x88u8; 16 * 1024]; // 16KB file
        temp_file.write_all(&test_data).unwrap();
        temp_file.flush().unwrap();

        // Test sequential access pattern
        let file = File::open(temp_file.path()).unwrap();
        let input = MemoryMappedInput::new_with_pattern(file, AccessPattern::Sequential).unwrap();
        assert_eq!(input.strategy(), InputStrategy::StandardMmap);

        // Test random access pattern
        let file = File::open(temp_file.path()).unwrap();
        let input = MemoryMappedInput::new_with_pattern(file, AccessPattern::Random).unwrap();
        assert_eq!(input.strategy(), InputStrategy::StandardMmap);

        // Test mixed access pattern
        let file = File::open(temp_file.path()).unwrap();
        let input = MemoryMappedInput::new_with_pattern(file, AccessPattern::Mixed).unwrap();
        assert_eq!(input.strategy(), InputStrategy::StandardMmap);
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_adaptive_read_operations() {
        // Test small file with buffered I/O
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(&0x12345678u32.to_le_bytes()).unwrap();
        temp_file
            .write_all(&0x9ABCDEF012345678u64.to_le_bytes())
            .unwrap();
        let var_bytes = VarInt::encode(42);
        temp_file.write_all(&var_bytes).unwrap();
        let test_str = "Adaptive test";
        let str_len_bytes = VarInt::encode(test_str.len() as u64);
        temp_file.write_all(&str_len_bytes).unwrap();
        temp_file.write_all(test_str.as_bytes()).unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();

        // Should use buffered I/O for small file
        assert_eq!(input.strategy(), InputStrategy::BufferedIO);

        // Test that all read operations work correctly
        assert_eq!(input.read_u32().unwrap(), 0x12345678);
        assert_eq!(input.read_u64().unwrap(), 0x9ABCDEF012345678);
        assert_eq!(input.read_var_int().unwrap(), 42);
        assert_eq!(input.read_length_prefixed_string().unwrap(), test_str);
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_zero_copy_operations() {
        let mut temp_file = NamedTempFile::new().unwrap();
        let test_data = vec![0x99u8; 32 * 1024]; // 32KB file - uses standard mmap
        temp_file.write_all(&test_data).unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();

        assert_eq!(input.strategy(), InputStrategy::StandardMmap);

        // Test zero-copy read
        let slice = input.read_slice_zero_copy(1024).unwrap();
        assert_eq!(slice.len(), 1024);
        assert!(slice.iter().all(|&b| b == 0x99));

        // Test zero-copy peek
        let peeked = input.peek_slice_zero_copy(512).unwrap();
        assert_eq!(peeked.len(), 512);
        assert!(peeked.iter().all(|&b| b == 0x99));

        // Position should not have changed after peek
        assert_eq!(input.position(), 1024);
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_buffered_io_limitations() {
        let mut temp_file = NamedTempFile::new().unwrap();
        let small_data = vec![0xAAu8; 1024]; // Small file - uses buffered I/O
        temp_file.write_all(&small_data).unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();

        assert_eq!(input.strategy(), InputStrategy::BufferedIO);

        // Zero-copy operations should not be available for buffered I/O
        assert!(input.read_slice_zero_copy(100).is_err());
        assert!(input.peek_slice_zero_copy(100).is_err());

        // Regular peek should also not be efficiently supported
        assert!(input.peek_slice(100).is_err());

        // But regular read operations should work
        let data = input.read_slice(100).unwrap();
        assert_eq!(data.len(), 100);
        assert!(data.iter().all(|&b| b == 0xAA));
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_seek_and_position_adaptive() {
        let mut temp_file = NamedTempFile::new().unwrap();
        // Create a larger file to force standard mmap strategy for consistent behavior
        let original_data = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        temp_file.write_all(original_data).unwrap();
        // Pad with zeros to make it larger than 4KB threshold
        let padding = vec![0u8; 8192 - original_data.len()];
        temp_file.write_all(&padding).unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();

        // Should use standard mmap for larger file
        assert_eq!(input.strategy(), InputStrategy::StandardMmap);

        assert_eq!(input.position(), 0);
        assert_eq!(input.remaining(), 8192);

        // Test seeking
        input.seek(10).unwrap();
        assert_eq!(input.position(), 10);
        assert_eq!(input.remaining(), 8192 - 10);

        // Read and verify position updates
        let data = input.read_slice(5).unwrap();
        assert_eq!(data, b"ABCDE");
        assert_eq!(input.position(), 15);

        // Test skip
        input.skip(5).unwrap();
        assert_eq!(input.position(), 20);

        let data = input.read_slice(3).unwrap();
        // Position 20-22 in "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" is "KLM"
        assert_eq!(data, b"KLM");
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_bounds_checking_adaptive() {
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(b"Small").unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let mut input = MemoryMappedInput::new(file).unwrap();

        // Should use buffered I/O
        assert_eq!(input.strategy(), InputStrategy::BufferedIO);

        // Reading beyond bounds should fail
        assert!(input.read_slice(10).is_err());

        // Seeking beyond bounds should fail
        assert!(input.seek(20).is_err());

        // Reading exactly at bounds should work
        let data = input.read_slice(5).unwrap();
        assert_eq!(data, b"Small");

        // Reading after consuming all data should fail
        assert!(input.read_slice(1).is_err());
    }

    #[cfg(feature = "mmap")]
    #[test]
    fn test_file_size_thresholds() {
        // Test exactly at threshold boundaries
        let threshold_tests = vec![
            (SMALL_FILE_THRESHOLD as usize - 1, InputStrategy::BufferedIO), // Just below 4KB
            (SMALL_FILE_THRESHOLD as usize, InputStrategy::BufferedIO),     // Exactly 4KB
            (
                SMALL_FILE_THRESHOLD as usize + 1,
                InputStrategy::StandardMmap,
            ), // Just above 4KB
            (
                HUGEPAGE_2MB_THRESHOLD as usize - 1,
                InputStrategy::StandardMmap,
            ), // Just below 1MB
        ];

        for (size, expected_strategy) in threshold_tests {
            let mut temp_file = NamedTempFile::new().unwrap();
            let test_data = vec![0xBBu8; size];
            temp_file.write_all(&test_data).unwrap();
            temp_file.flush().unwrap();

            let file = File::open(temp_file.path()).unwrap();
            let input = MemoryMappedInput::new(file).unwrap();

            assert_eq!(
                input.strategy(),
                expected_strategy,
                "File size {} should use strategy {:?}, got {:?}",
                size,
                expected_strategy,
                input.strategy()
            );
        }
    }

    #[cfg(all(feature = "mmap", target_os = "linux"))]
    #[test]
    fn test_hugepage_fallback() {
        // Create a large file that would normally use hugepages
        let mut temp_file = NamedTempFile::new().unwrap();
        let large_data = vec![0xCCu8; 5 * 1024 * 1024]; // 5MB file
        temp_file.write_all(&large_data).unwrap();
        temp_file.flush().unwrap();

        let file = File::open(temp_file.path()).unwrap();
        let input = MemoryMappedInput::new(file).unwrap();

        // Should attempt hugepages but may fallback to standard mmap
        let strategy = input.strategy();
        assert!(
            strategy == InputStrategy::HugepageMmap || strategy == InputStrategy::StandardMmap,
            "Large file should attempt hugepages with graceful fallback"
        );

        // File should still be readable regardless of strategy
        assert_eq!(input.len(), 5 * 1024 * 1024);
    }
}