winx-code-agent 0.2.312

High-performance Rust implementation of WCGW for LLM code agents
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
use memmap2::{Mmap, MmapOptions};
use rayon::prelude::*;
use std::cmp::min;
use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tracing::{debug, info, trace, warn};

use crate::errors::{Result, WinxError};

/// Maximum file size for direct reading (10MB)
pub const DIRECT_READ_THRESHOLD: u64 = 10_000_000;

/// Maximum file size for single memory mapping (1GB)
pub const MAX_MMAP_SIZE: u64 = 1_000_000_000;

/// Maximum file size for segmented memory mapping (4GB)
pub const MAX_SEGMENTED_MMAP_SIZE: u64 = 4_000_000_000;

/// Segment size for large file memory mapping (256MB)
pub const SEGMENT_SIZE: u64 = 256_000_000;

const DIRECT_READ_CHUNK_SIZE: usize = 1_048_576;
const MMAP_PARALLEL_CHUNK_SIZE: usize = 1_048_576;
const STREAMING_CHUNK_SIZE: usize = 4_194_304;

/// Read file contents optimally based on file size
///
/// This function chooses the optimal reading strategy based on file size:
/// - Small files: Direct read with standard File I/O
/// - Medium files: Memory-mapped reading for performance
/// - Large files: Segmented memory-mapped reading
/// - Extreme files: Windowed access with streaming
///
/// # Arguments
///
/// * `path` - Path to the file to read
/// * `max_file_size` - Maximum allowed file size
///
/// # Returns
///
/// A vector containing the file contents
///
/// # Errors
///
/// Returns an error if the file cannot be read or exceeds the size limit
pub fn read_file_optimized(path: &Path, max_file_size: u64) -> Result<Vec<u8>> {
    // Get file metadata
    let file = File::open(path).map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Error opening file: {e}"),
    })?;

    let metadata = file.metadata().map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Failed to get file metadata: {e}"),
    })?;

    // Check file size
    let file_size = metadata.len();
    if file_size > max_file_size {
        return Err(WinxError::FileTooLarge {
            path: path.to_path_buf(),
            size: file_size,
            max_size: max_file_size,
        });
    }

    // Choose reading strategy based on file size
    if file_size < DIRECT_READ_THRESHOLD {
        debug!("Using direct read for file: {}", path.display());
        read_direct(&file, file_size, path)
    } else if file_size < MAX_MMAP_SIZE {
        debug!("Using memory-mapped read for file: {}", path.display());
        read_mmap(&file, path)
    } else if file_size < MAX_SEGMENTED_MMAP_SIZE {
        debug!("Using segmented memory-mapped read for file: {}", path.display());
        read_segmented_mmap(&file, file_size, path)
    } else {
        debug!("Using streaming read for extremely large file: {}", path.display());
        read_streaming(&file, file_size, path)
    }
}

/// Read file contents directly using standard I/O
///
/// This is efficient for small files.
///
/// # Arguments
///
/// * `file` - Open file handle
/// * `file_size` - Size of the file
/// * `path` - Path to the file (for error reporting)
///
/// # Returns
///
/// A vector containing the file contents
///
/// # Errors
///
/// Returns an error if the file cannot be read
fn read_direct(file: &File, file_size: u64, path: &Path) -> Result<Vec<u8>> {
    // For very small files (< 1MB), use an optimized approach
    if file_size < 1_000_000 {
        // Pre-allocate an exact-sized buffer
        let mut buffer = Vec::with_capacity(file_size as usize);

        // Create a mutable file handle and seek to the beginning
        let mut file_handle = file.try_clone().map_err(|e| WinxError::FileAccessError {
            path: path.to_path_buf(),
            message: format!("Error cloning file handle: {e}"),
        })?;

        file_handle.seek(SeekFrom::Start(0)).map_err(|e| WinxError::FileAccessError {
            path: path.to_path_buf(),
            message: format!("Error seeking to start of file: {e}"),
        })?;

        // Use a BufReader with an appropriate buffer size (4K-64K)
        let mut reader = BufReader::with_capacity(min(file_size as usize, 64 * 1024), file_handle);

        // Read directly to the end
        reader.read_to_end(&mut buffer).map_err(|e| WinxError::FileAccessError {
            path: path.to_path_buf(),
            message: format!("Error reading file: {e}"),
        })?;

        return Ok(buffer);
    }

    // For larger files, use a chunked reading approach with progress tracking
    let mut buffer = Vec::with_capacity(file_size as usize);

    // Create a mutable file handle and seek to the beginning
    let mut file_handle = file.try_clone().map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Error cloning file handle: {e}"),
    })?;

    file_handle.seek(SeekFrom::Start(0)).map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Error seeking to start of file: {e}"),
    })?;

    let mut reader = BufReader::with_capacity(262_144, file_handle); // 256KB buffer

    let mut chunk = vec![0; DIRECT_READ_CHUNK_SIZE];
    let mut bytes_read = 0;

    loop {
        match reader.read(&mut chunk) {
            Ok(0) => break, // EOF
            Ok(n) => {
                buffer.extend_from_slice(&chunk[..n]);
                bytes_read += n as u64;

                // Log progress for larger files
                if file_size > 5_000_000 && bytes_read % 5_000_000 < DIRECT_READ_CHUNK_SIZE as u64 {
                    trace!(
                        "Read progress for {}: {}MB/{}MB ({}%)",
                        path.display(),
                        bytes_read / 1_000_000,
                        file_size / 1_000_000,
                        bytes_read * 100 / file_size
                    );
                }
            }
            Err(e) => {
                return Err(WinxError::FileAccessError {
                    path: path.to_path_buf(),
                    message: format!("Error reading file chunk: {e}"),
                });
            }
        }
    }

    Ok(buffer)
}

/// Read file contents using memory mapping
///
/// This is efficient for larger files as it avoids loading the entire
/// file into memory at once.
///
/// # Arguments
///
/// * `file` - Open file handle
/// * `path` - Path to the file (for error reporting)
///
/// # Returns
///
/// A vector containing the file contents
///
/// # Errors
///
/// Returns an error if the file cannot be mapped
fn read_mmap(file: &File, path: &Path) -> Result<Vec<u8>> {
    // Check for empty file to avoid mmap error
    if file.metadata().map(|m| m.len()).unwrap_or(0) == 0 {
        return Ok(Vec::new());
    }

    // SAFETY: Memory mapping a file is inherently unsafe because:
    // - The file could be modified by another process during access
    // - The file could be truncated, causing access to invalid memory
    // We mitigate these risks by:
    // - Using the mapped data immediately and converting to Vec<u8>
    // - Not holding the mmap across async boundaries
    // - File size was verified before this call
    let mmap = unsafe { MmapOptions::new().map(file) }.map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Failed to memory-map file: {e}"),
    })?;

    // Use Rayon for parallel processing if the file is large enough
    if mmap.len() > 10_000_000 {
        // 10MB threshold for parallel processing
        debug!("Using parallel processing for large mmap file: {}", path.display());

        // Process in parallel chunks
        let chunk_count = mmap.len().div_ceil(MMAP_PARALLEL_CHUNK_SIZE);
        let mut result = vec![0; mmap.len()];

        // Process in parallel with Rayon - use collect for parallel map
        let chunks: Vec<_> = (0..chunk_count)
            .into_par_iter()
            .map(|i| {
                let start = i * MMAP_PARALLEL_CHUNK_SIZE;
                let end = min((i + 1) * MMAP_PARALLEL_CHUNK_SIZE, mmap.len());

                if start < mmap.len() {
                    // Extract chunk from mmap
                    let src = &mmap[start..end];
                    (start, end, src.to_vec())
                } else {
                    (start, start, Vec::new())
                }
            })
            .collect();

        // Now apply all chunks to the result sequentially
        for (start, end, chunk) in chunks {
            if start < end {
                result[start..end].copy_from_slice(&chunk);
            }
        }

        Ok(result)
    } else {
        // For smaller files, just copy the entire map to a Vec
        Ok(mmap.to_vec())
    }
}

/// Read large file with segmented memory mapping
///
/// This function reads a large file using multiple memory mapped segments,
/// which allows handling files larger than the maximum mapping size.
///
/// # Arguments
///
/// * `file` - Open file handle
/// * `file_size` - Size of the file
/// * `path` - Path to the file (for error reporting)
///
/// # Returns
///
/// A vector containing the file contents
///
/// # Errors
///
/// Returns an error if the file cannot be read or mapped
fn read_segmented_mmap(_file: &File, file_size: u64, path: &Path) -> Result<Vec<u8>> {
    // Calculate number of segments needed
    let segment_count = file_size.div_ceil(SEGMENT_SIZE);
    debug!(
        "Reading file {} in {} segments of {}MB each",
        path.display(),
        segment_count,
        SEGMENT_SIZE / 1_000_000
    );

    // Pre-allocate result vector
    let mut result = Vec::with_capacity(file_size as usize);

    // Process each segment
    for i in 0..segment_count {
        let segment_start = i * SEGMENT_SIZE;
        let segment_size = min(SEGMENT_SIZE, file_size - segment_start);

        info!(
            "Processing segment {}/{} of file {} ({:.1}%)",
            i + 1,
            segment_count,
            path.display(),
            (segment_start as f64 / file_size as f64) * 100.0
        );

        // Open a new file handle for each segment to avoid position conflicts
        let segment_file = File::open(path).map_err(|e| WinxError::FileAccessError {
            path: path.to_path_buf(),
            message: format!("Error opening file for segment {i}: {e}"),
        })?;

        // Seek to segment start
        let mut segment_file = segment_file;
        segment_file.seek(SeekFrom::Start(segment_start)).map_err(|e| {
            WinxError::FileAccessError {
                path: path.to_path_buf(),
                message: format!("Error seeking to segment start: {e}"),
            }
        })?;

        // SAFETY: Memory mapping a segment is safe here because:
        // - File handle is freshly opened and seeked to correct position
        // - Segment bounds are calculated from verified file size
        // - Data is immediately copied to Vec, not held across boundaries
        let segment_mmap = unsafe {
            MmapOptions::new().offset(segment_start).len(segment_size as usize).map(&segment_file)
        }
        .map_err(|e| WinxError::FileAccessError {
            path: path.to_path_buf(),
            message: format!("Failed to memory-map file segment {i}: {e}"),
        })?;

        // Append segment data to result
        result.extend_from_slice(&segment_mmap);
    }

    Ok(result)
}

/// Read extremely large file with streaming
///
/// This function reads an extremely large file using a streaming approach,
/// which minimizes memory usage by processing the file in small chunks.
///
/// # Arguments
///
/// * `file` - Open file handle
/// * `file_size` - Size of the file
/// * `path` - Path to the file (for error reporting)
///
/// # Returns
///
/// A vector containing the file contents
///
/// # Errors
///
/// Returns an error if the file cannot be read
fn read_streaming(file: &File, file_size: u64, path: &Path) -> Result<Vec<u8>> {
    warn!(
        "Reading extremely large file ({}GB) with streaming approach: {}",
        file_size / 1_000_000_000,
        path.display()
    );

    // For extreme files, pre-allocate a reasonably sized vector and grow as needed
    let initial_capacity = min(file_size as usize, 1_000_000_000); // 1GB initial max
    let mut buffer = Vec::with_capacity(initial_capacity);

    let mut reader = BufReader::with_capacity(STREAMING_CHUNK_SIZE, file);
    let mut chunk = vec![0; STREAMING_CHUNK_SIZE];
    let mut bytes_read = 0;

    loop {
        match reader.read(&mut chunk) {
            Ok(0) => break, // EOF
            Ok(n) => {
                buffer.extend_from_slice(&chunk[..n]);
                bytes_read += n as u64;

                // Log progress every 100MB
                if bytes_read % 100_000_000 < STREAMING_CHUNK_SIZE as u64 {
                    info!(
                        "Read progress for large file {}: {:.2}GB/{:.2}GB ({:.1}%)",
                        path.display(),
                        bytes_read as f64 / 1_000_000_000.0,
                        file_size as f64 / 1_000_000_000.0,
                        bytes_read as f64 * 100.0 / file_size as f64
                    );
                }
            }
            Err(e) => {
                return Err(WinxError::FileAccessError {
                    path: path.to_path_buf(),
                    message: format!("Error reading file chunk at position {bytes_read}: {e}"),
                });
            }
        }
    }

    Ok(buffer)
}

/// Read a specific segment of a file
///
/// This function reads a specific segment of a file using memory mapping
/// or direct I/O, depending on the segment size.
///
/// # Arguments
///
/// * `path` - Path to the file
/// * `offset` - Starting offset in bytes
/// * `length` - Length of segment to read in bytes
/// * `max_file_size` - Maximum allowed file size
///
/// # Returns
///
/// A vector containing the file segment contents
///
/// # Errors
///
/// Returns an error if the file cannot be read or exceeds the size limit
pub fn read_file_segment(
    path: &Path,
    offset: u64,
    length: u64,
    max_file_size: u64,
) -> Result<Vec<u8>> {
    // Get file metadata
    let file = File::open(path).map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Error opening file: {e}"),
    })?;

    let metadata = file.metadata().map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Failed to get file metadata: {e}"),
    })?;

    // Check file size
    let file_size = metadata.len();
    if file_size > max_file_size {
        return Err(WinxError::FileTooLarge {
            path: path.to_path_buf(),
            size: file_size,
            max_size: max_file_size,
        });
    }

    // Validate offset and length
    if offset >= file_size {
        return Err(WinxError::FileAccessError {
            path: path.to_path_buf(),
            message: format!("Offset {offset} exceeds file size {file_size}"),
        });
    }

    // Adjust length if needed to stay within file bounds
    let length = min(length, file_size - offset);

    // Choose reading strategy based on segment size
    if length < DIRECT_READ_THRESHOLD {
        debug!("Using direct read for file segment: {}", path.display());
        read_segment_direct(&file, offset, length, path)
    } else {
        debug!("Using memory-mapped read for file segment: {}", path.display());
        read_segment_mmap(&file, offset, length, path)
    }
}

/// Read a file segment directly using standard I/O
///
/// # Arguments
///
/// * `file` - Open file handle
/// * `offset` - Starting offset in bytes
/// * `length` - Length of segment to read in bytes
/// * `path` - Path to the file (for error reporting)
///
/// # Returns
///
/// A vector containing the file segment contents
///
/// # Errors
///
/// Returns an error if the file segment cannot be read
fn read_segment_direct(file: &File, offset: u64, length: u64, path: &Path) -> Result<Vec<u8>> {
    // Create a new file object that can be seeked
    let mut seekable_file = file.try_clone().map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Failed to clone file handle: {e}"),
    })?;

    // Seek to the specified offset
    seekable_file.seek(SeekFrom::Start(offset)).map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Failed to seek to offset {offset}: {e}"),
    })?;

    // Read the specified length
    let mut buffer = Vec::with_capacity(length as usize);
    let reader = BufReader::with_capacity(min(length as usize, 64 * 1024), seekable_file);

    // Use take to limit the read to the specified length
    reader.take(length).read_to_end(&mut buffer).map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Error reading file segment: {e}"),
    })?;

    Ok(buffer)
}

/// Read a file segment using memory mapping
///
/// # Arguments
///
/// * `file` - Open file handle
/// * `offset` - Starting offset in bytes
/// * `length` - Length of segment to read in bytes
/// * `path` - Path to the file (for error reporting)
///
/// # Returns
///
/// A vector containing the file segment contents
///
/// # Errors
///
/// Returns an error if the file segment cannot be mapped
fn read_segment_mmap(file: &File, offset: u64, length: u64, path: &Path) -> Result<Vec<u8>> {
    // SAFETY: Memory mapping is safe here because:
    // - Offset and length were validated against file size by caller
    // - Data is immediately copied to Vec<u8>, not held
    // - File handle remains valid for duration of the map operation
    let segment_mmap = unsafe { MmapOptions::new().offset(offset).len(length as usize).map(file) }
        .map_err(|e| WinxError::FileAccessError {
            path: path.to_path_buf(),
            message: format!("Failed to memory-map file segment: {e}"),
        })?;

    // Copy segment data to result
    Ok(segment_mmap.to_vec())
}

/// Read a text file as a string using the optimal reading strategy
///
/// This function reads a file as text, using the most efficient strategy
/// based on the file size.
///
/// # Arguments
///
/// * `path` - Path to the file to read
/// * `max_file_size` - Maximum allowed file size
///
/// # Returns
///
/// A string containing the file contents
///
/// # Errors
///
/// Returns an error if the file cannot be read or exceeds the size limit
pub fn read_file_to_string(path: &Path, max_file_size: u64) -> Result<String> {
    let bytes = read_file_optimized(path, max_file_size)?;

    String::from_utf8(bytes).map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Failed to decode file as UTF-8: {e}"),
    })
}

/// Read a text file in a parallel, line-by-line fashion
///
/// This processes lines in parallel using Rayon for faster processing
/// of large text files.
///
/// # Arguments
///
/// * `path` - Path to the file to read
/// * `max_file_size` - Maximum allowed file size
/// * `line_processor` - Function to process each line
///
/// # Returns
///
/// Result indicating success or failure
///
/// # Errors
///
/// Returns an error if the file cannot be read or exceeds the size limit
pub fn process_text_file_parallel<F>(
    path: &Path,
    max_file_size: u64,
    line_processor: F,
) -> Result<()>
where
    F: Fn(&str) + Sync,
{
    let content = read_file_to_string(path, max_file_size)?;

    // For larger files, use parallel processing
    if content.len() > 1_000_000 {
        // 1MB
        content.par_lines().for_each(|line| {
            line_processor(line);
        });
    } else {
        // For smaller files, process sequentially
        content.lines().for_each(|line| {
            line_processor(line);
        });
    }

    Ok(())
}

/// Read a text file segment as a string
///
/// # Arguments
///
/// * `path` - Path to the file to read
/// * `offset` - Starting offset in bytes
/// * `length` - Length of segment to read in bytes
/// * `max_file_size` - Maximum allowed file size
///
/// # Returns
///
/// A string containing the file segment contents
///
/// # Errors
///
/// Returns an error if the file segment cannot be read
pub fn read_file_segment_to_string(
    path: &Path,
    offset: u64,
    length: u64,
    max_file_size: u64,
) -> Result<String> {
    let bytes = read_file_segment(path, offset, length, max_file_size)?;

    String::from_utf8(bytes).map_err(|e| WinxError::FileAccessError {
        path: path.to_path_buf(),
        message: format!("Failed to decode file segment as UTF-8: {e}"),
    })
}

/// `ShareableMap` provides a thread-safe memory-mapped file access
///
/// This is useful for providing read-only access to multiple threads
/// without copying the data, especially for large files.
#[derive(Clone)]
pub struct ShareableMap {
    /// The memory-mapped file data
    data: Arc<Mmap>,
    /// The path to the mapped file
    path: PathBuf,
}

impl ShareableMap {
    /// Create a new `ShareableMap` from a file
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the file to map
    ///
    /// # Returns
    ///
    /// A Result containing the `ShareableMap` or an error
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be mapped
    pub fn new(path: &Path) -> Result<Self> {
        let file = File::open(path).map_err(|e| WinxError::FileAccessError {
            path: path.to_path_buf(),
            message: format!("Error opening file: {e}"),
        })?;

        // Check for empty file
        if file
            .metadata()
            .map_err(|e| WinxError::FileAccessError {
                path: path.to_path_buf(),
                message: format!("Failed to get metadata: {e}"),
            })?
            .len()
            == 0
        {
            return Err(WinxError::FileAccessError {
                path: path.to_path_buf(),
                message: "Cannot memory map empty file".to_string(),
            });
        }

        // SAFETY: ShareableMap wraps the Mmap in Arc for thread-safe sharing.
        // The mapped data is read-only and the Arc ensures the Mmap outlives
        // all references. Users must be aware the underlying file should not
        // be modified while ShareableMap is in use.
        let mmap =
            unsafe { MmapOptions::new().map(&file) }.map_err(|e| WinxError::FileAccessError {
                path: path.to_path_buf(),
                message: format!("Failed to memory-map file: {e}"),
            })?;

        Ok(Self { data: Arc::new(mmap), path: path.to_path_buf() })
    }

    /// Create a new `ShareableMap` for a segment of a file
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the file to map
    /// * `offset` - Starting offset in bytes
    /// * `length` - Length of segment to map in bytes
    ///
    /// # Returns
    ///
    /// A Result containing the `ShareableMap` or an error
    ///
    /// # Errors
    ///
    /// Returns an error if the file segment cannot be mapped
    pub fn new_segment(path: &Path, offset: u64, length: u64) -> Result<Self> {
        if length == 0 {
            return Err(WinxError::FileAccessError {
                path: path.to_path_buf(),
                message: "Cannot memory map segment of length 0".to_string(),
            });
        }

        let file = File::open(path).map_err(|e| WinxError::FileAccessError {
            path: path.to_path_buf(),
            message: format!("Error opening file: {e}"),
        })?;

        // SAFETY: Same as ShareableMap::new, plus:
        // - Caller is responsible for ensuring offset+length is within file bounds
        // - The segment mapping is wrapped in Arc for safe sharing
        let mmap = unsafe { MmapOptions::new().offset(offset).len(length as usize).map(&file) }
            .map_err(|e| WinxError::FileAccessError {
                path: path.to_path_buf(),
                message: format!("Failed to memory-map file segment: {e}"),
            })?;

        Ok(Self { data: Arc::new(mmap), path: path.to_path_buf() })
    }

    /// Get the data as a byte slice
    pub fn as_slice(&self) -> &[u8] {
        &self.data
    }

    /// Get the path to the mapped file
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Get the size of the mapped data
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Check if the mapped data is empty
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
}

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

    fn create_test_file(size: usize) -> Result<(NamedTempFile, Vec<u8>)> {
        let mut file = NamedTempFile::new()?;
        let mut data = Vec::with_capacity(size);

        // Fill with pattern data (more realistic than zeros)
        for i in 0..size {
            data.push((i % 256) as u8);
        }

        file.write_all(&data)?;
        file.flush()?;

        Ok((file, data))
    }

    #[test]
    fn test_direct_read_small_file() -> Result<()> {
        let size = 10 * 1024; // 10KB
        let (file, expected_data) = create_test_file(size)?;

        let result = read_direct(file.as_file(), size as u64, file.path())?;
        assert_eq!(result, expected_data);
        Ok(())
    }

    #[test]
    fn test_mmap_read() -> Result<()> {
        let size = 1024 * 1024; // 1MB
        let (file, expected_data) = create_test_file(size)?;

        let result = read_mmap(file.as_file(), file.path())?;
        assert_eq!(result, expected_data);
        Ok(())
    }

    #[test]
    fn test_file_segment_read() -> Result<()> {
        let size = 1024 * 1024; // 1MB
        let (file, data) = create_test_file(size)?;

        // Read a segment from the middle
        let offset = 100 * 1024; // 100KB
        let length = 200 * 1024; // 200KB
        let expected_segment = &data[offset as usize..(offset + length) as usize];

        let result = read_segment_direct(file.as_file(), offset, length, file.path())?;
        assert_eq!(result, expected_segment);

        let result = read_segment_mmap(file.as_file(), offset, length, file.path())?;
        assert_eq!(result, expected_segment);
        Ok(())
    }

    #[test]
    fn test_shareable_map() -> Result<()> {
        let size = 100 * 1024; // 100KB
        let (file, data) = create_test_file(size)?;

        let map = ShareableMap::new(file.path())?;
        assert_eq!(map.as_slice(), &data);

        // Test segment
        let offset = 10 * 1024; // 10KB
        let length = 20 * 1024; // 20KB
        let segment_map = ShareableMap::new_segment(file.path(), offset, length)?;
        assert_eq!(segment_map.as_slice(), &data[offset as usize..(offset + length) as usize]);
        Ok(())
    }

    #[test]
    fn test_parallel_processing() -> Result<()> {
        // Create a test file with lines
        let mut file = NamedTempFile::new()?;
        let mut lines = Vec::new();

        for i in 0..1000 {
            let line = format!("Line {i}\n");
            file.write_all(line.as_bytes())?;
            lines.push(format!("Line {i}"));
        }
        file.flush()?;

        // Test parallel processing
        let processed_lines = std::sync::Mutex::new(Vec::new());

        process_text_file_parallel(file.path(), 1_000_000, |line| {
            if let Ok(mut lines) = processed_lines.lock() {
                lines.push(line.to_string());
            }
        })?;

        // Verify results (order may differ due to parallel processing)
        let result =
            processed_lines.lock().map_err(|error| WinxError::ResourceAllocationError {
                message: format!("Failed to lock processed lines: {error}"),
            })?;
        assert_eq!(result.len(), lines.len());

        // Check that all lines are present
        for line in &lines {
            assert!(result.contains(line));
        }
        Ok(())
    }
}