turbomcp-protocol 3.0.7

Complete MCP protocol implementation with types, traits, context management, and message handling
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
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
//! Zero-copy message processing with minimal allocations
//!
//! This module provides zero-allocation message handling using `bytes::Bytes`
//! for maximum throughput and minimal memory overhead.
//!
//! ## When to Use ZeroCopyMessage
//!
//! **Most users should use [`Message`](crate::Message) instead.**
//!
//! `ZeroCopyMessage` is designed for extreme performance scenarios where:
//! - You process **millions of messages per second**
//! - **Every allocation matters** for your performance profile
//! - You can **defer deserialization** until absolutely necessary
//! - You're willing to **trade ergonomics for performance**
//!
//! ### Message vs ZeroCopyMessage
//!
//! | Feature | [`Message`](crate::Message) | `ZeroCopyMessage` |
//! |---------|---------|------------------|
//! | Ergonomics | ✅ Excellent | ⚠️ Manual |
//! | Memory | ✅ Good | ✅ Optimal |
//! | Deserialization | Eager | Lazy |
//! | Multiple formats | ✅ JSON/CBOR/MessagePack | JSON only |
//! | ID storage | Stack/String | Arc (shared) |
//! | Use case | General purpose | Ultra-high throughput |
//!
//! ### Example Usage
//!
//! ```rust
//! use turbomcp_protocol::zero_copy::{ZeroCopyMessage, MessageId};
//! use bytes::Bytes;
//!
//! // Create from raw bytes (no allocation)
//! let payload = Bytes::from(r#"{"method": "test", "id": 1}"#);
//! let mut msg = ZeroCopyMessage::from_bytes(MessageId::from("req-1"), payload);
//!
//! // Lazy parsing - returns RawValue without full deserialization
//! let raw = msg.parse_json_lazy()?;
//! assert!(raw.get().contains("method"));
//!
//! // Full deserialization when needed
//! let data: serde_json::Value = msg.deserialize()?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! **Performance Tip**: Use `ZeroCopyMessage` in hot paths where you need to
//! route messages based on metadata but don't need to parse the payload.

use bytes::{BufMut, Bytes, BytesMut};
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue;
use std::fmt;
use std::path::Path;
use std::sync::Arc;
use uuid::Uuid;

use crate::types::{ContentType, Timestamp};
use crate::{McpError as Error, Result};

/// Zero-copy message with lazy deserialization
#[derive(Debug, Clone)]
pub struct ZeroCopyMessage {
    /// Message ID - using Arc for cheap cloning
    pub id: Arc<MessageId>,

    /// Raw message payload - zero-copy bytes
    pub payload: Bytes,

    /// Lazy-parsed JSON value for deferred deserialization
    pub lazy_json: Option<Box<RawValue>>,

    /// Message metadata
    pub metadata: MessageMetadata,
}

/// Optimized message ID with Arc sharing
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MessageId {
    /// String ID with Arc for sharing
    String(Arc<str>),
    /// Numeric ID (stack-allocated)
    Number(i64),
    /// UUID (stack-allocated)
    Uuid(Uuid),
}

/// Lightweight message metadata
#[derive(Debug, Clone)]
pub struct MessageMetadata {
    /// Creation timestamp
    pub created_at: Timestamp,
    /// Content type
    pub content_type: ContentType,
    /// Message size in bytes
    pub size: usize,
    /// Optional correlation ID (Arc for sharing)
    pub correlation_id: Option<Arc<str>>,
}

impl ZeroCopyMessage {
    /// Create a new zero-copy message from bytes
    #[inline]
    pub fn from_bytes(id: MessageId, payload: Bytes) -> Self {
        let size = payload.len();
        Self {
            id: Arc::new(id),
            payload: payload.clone(),
            lazy_json: None,
            metadata: MessageMetadata {
                created_at: Timestamp::now(),
                content_type: ContentType::Json,
                size,
                correlation_id: None,
            },
        }
    }

    /// Create from a JSON value with zero-copy optimization
    pub fn from_json<T: Serialize>(id: MessageId, value: &T) -> Result<Self> {
        // Use a reusable buffer pool in production
        let mut buffer = BytesMut::with_capacity(1024);

        // Serialize directly to bytes
        serde_json::to_writer((&mut buffer).writer(), value)
            .map_err(|e| Error::serialization(e.to_string()))?;

        let payload = buffer.freeze();
        let size = payload.len();

        Ok(Self {
            id: Arc::new(id),
            payload,
            lazy_json: None,
            metadata: MessageMetadata {
                created_at: Timestamp::now(),
                content_type: ContentType::Json,
                size,
                correlation_id: None,
            },
        })
    }

    /// Parse JSON lazily - only when needed
    #[inline]
    pub fn parse_json_lazy(&mut self) -> Result<&RawValue> {
        if self.lazy_json.is_none() {
            // Parse without deserializing the full structure
            let raw: Box<RawValue> = serde_json::from_slice(&self.payload)
                .map_err(|e| Error::serialization(format!("JSON parse error: {}", e)))?;

            // Store the parsed raw value
            self.lazy_json = Some(raw);
        }

        Ok(self.lazy_json.as_ref().unwrap())
    }

    /// Deserialize a specific type from the message with SIMD acceleration when available
    #[inline]
    pub fn deserialize<T: for<'de> Deserialize<'de>>(&self) -> Result<T> {
        #[cfg(feature = "simd")]
        {
            // Use sonic-rs for SIMD parsing (no mutable buffer needed, true zero-copy)
            sonic_rs::from_slice(&self.payload)
                .map_err(|e| Error::serialization(format!("SIMD deserialize error: {e}")))
        }
        #[cfg(not(feature = "simd"))]
        {
            serde_json::from_slice(&self.payload)
                .map_err(|e| Error::serialization(format!("Deserialization error: {e}")))
        }
    }

    /// Get a zero-copy view of the payload
    #[inline]
    pub fn payload_slice(&self) -> &[u8] {
        &self.payload
    }

    /// Clone the message cheaply (Arc increments only)
    #[inline]
    pub fn cheap_clone(&self) -> Self {
        Self {
            id: Arc::clone(&self.id),
            payload: self.payload.clone(), // Bytes is already Arc-based
            lazy_json: self.lazy_json.clone(),
            metadata: self.metadata.clone(),
        }
    }
}

/// Buffer pool for reusing allocations
#[derive(Debug)]
pub struct BufferPool {
    /// Pool of reusable buffers
    buffers: crossbeam::queue::ArrayQueue<BytesMut>,
    /// Default buffer capacity
    capacity: usize,
}

impl BufferPool {
    /// Create a new buffer pool
    pub fn new(size: usize, capacity: usize) -> Self {
        let buffers = crossbeam::queue::ArrayQueue::new(size);

        // Pre-allocate buffers
        for _ in 0..size {
            let _ = buffers.push(BytesMut::with_capacity(capacity));
        }

        Self { buffers, capacity }
    }

    /// Get a buffer from the pool or create a new one
    #[inline]
    pub fn acquire(&self) -> BytesMut {
        self.buffers
            .pop()
            .unwrap_or_else(|| BytesMut::with_capacity(self.capacity))
    }

    /// Return a buffer to the pool for reuse
    #[inline]
    pub fn release(&self, mut buffer: BytesMut) {
        buffer.clear();
        let _ = self.buffers.push(buffer);
    }
}

/// Zero-copy message batch for efficient bulk processing
#[derive(Debug)]
pub struct MessageBatch {
    /// Contiguous buffer containing all messages
    pub buffer: Bytes,
    /// Offsets and lengths of individual messages
    pub messages: Vec<(usize, usize)>,
    /// Shared message IDs
    pub ids: Vec<Arc<MessageId>>,
}

impl MessageBatch {
    /// Create a new message batch
    pub fn new(capacity: usize) -> Self {
        Self {
            buffer: Bytes::new(),
            messages: Vec::with_capacity(capacity),
            ids: Vec::with_capacity(capacity),
        }
    }

    /// Add a message to the batch
    pub fn add(&mut self, id: MessageId, payload: Bytes) {
        let offset = self.buffer.len();
        let length = payload.len();

        // Extend the buffer
        let mut buffer = BytesMut::from(self.buffer.as_ref());
        buffer.extend_from_slice(&payload);
        self.buffer = buffer.freeze();

        // Store offset and length
        self.messages.push((offset, length));
        self.ids.push(Arc::new(id));
    }

    /// Get a zero-copy view of a message
    #[inline]
    pub fn get(&self, index: usize) -> Option<Bytes> {
        self.messages
            .get(index)
            .map(|(offset, length)| self.buffer.slice(*offset..*offset + *length))
    }

    /// Iterate over messages without copying
    pub fn iter(&self) -> impl Iterator<Item = (&Arc<MessageId>, Bytes)> + '_ {
        self.ids
            .iter()
            .zip(self.messages.iter())
            .map(move |(id, (offset, length))| (id, self.buffer.slice(*offset..*offset + *length)))
    }
}

/// Fast utilities for message processing with SIMD acceleration
pub mod fast {
    /// Fast UTF-8 validation with SIMD when available
    #[inline]
    pub fn validate_utf8_fast(bytes: &[u8]) -> bool {
        #[cfg(feature = "simd")]
        {
            // Use SIMD-accelerated validation for larger inputs
            if bytes.len() >= 64 {
                simdutf8::basic::from_utf8(bytes).is_ok()
            } else {
                std::str::from_utf8(bytes).is_ok()
            }
        }
        #[cfg(not(feature = "simd"))]
        {
            std::str::from_utf8(bytes).is_ok()
        }
    }

    /// Fast JSON boundary detection with optimized scanning
    #[inline]
    pub fn find_json_boundaries(bytes: &[u8]) -> Vec<usize> {
        let mut boundaries = Vec::new();
        let mut depth = 0;
        let mut in_string = false;
        let mut escaped = false;

        // Optimized boundary detection with proper string handling
        for (i, &byte) in bytes.iter().enumerate() {
            if escaped {
                escaped = false;
                continue;
            }

            match byte {
                b'\\' if in_string => escaped = true,
                b'"' if !escaped => in_string = !in_string,
                b'{' | b'[' if !in_string => depth += 1,
                b'}' | b']' if !in_string => {
                    depth -= 1;
                    if depth == 0 {
                        boundaries.push(i + 1);
                    }
                }
                _ => {}
            }
        }

        boundaries
    }

    /// SIMD-accelerated JSON validation
    #[cfg(feature = "simd")]
    #[inline]
    pub fn validate_json_fast(bytes: &[u8]) -> bool {
        // Use sonic-rs for SIMD validation (no mutable buffer needed)
        sonic_rs::from_slice::<sonic_rs::Value>(bytes).is_ok()
    }

    /// Standard JSON validation (non-SIMD fallback)
    ///
    /// Validates JSON syntax using serde_json's parser.
    #[cfg(not(feature = "simd"))]
    #[inline]
    pub fn validate_json_fast(bytes: &[u8]) -> bool {
        serde_json::from_slice::<serde_json::Value>(bytes).is_ok()
    }
}

/// Memory-mapped file support for efficient large file processing
#[cfg(feature = "mmap")]
pub mod mmap {
    use super::*;
    use memmap2::{Mmap, MmapOptions};
    use std::fs::File;
    use std::io;
    use std::ops::Deref;

    // Import security module if available

    /// A memory-mapped message for zero-copy file access
    #[derive(Debug)]
    pub struct MmapMessage {
        /// Message ID
        pub id: Arc<MessageId>,
        /// Memory-mapped data
        pub mmap: Arc<Mmap>,
        /// Offset within the mapped region
        pub offset: usize,
        /// Length of the message data
        pub length: usize,
        /// Message metadata
        pub metadata: MessageMetadata,
    }

    impl MmapMessage {
        /// Create a message from a memory-mapped file (legacy method without security)
        ///
        /// **SECURITY WARNING**: This method bypasses security validation.
        /// Use `from_file_secure` for production systems.
        pub fn from_file(
            id: MessageId,
            path: &Path,
            offset: usize,
            length: Option<usize>,
        ) -> io::Result<Self> {
            // For backwards compatibility, perform basic validation
            let file = File::open(path)?;
            let metadata = file.metadata()?;
            let file_size = metadata.len() as usize;

            // Validate offset
            if offset >= file_size {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "Offset exceeds file size",
                ));
            }

            // Calculate actual length
            let actual_length = length.unwrap_or(file_size - offset);
            let actual_length = actual_length.min(file_size - offset);

            // Create memory map
            // SAFETY: file handle is valid and opened for reading. memmap2 provides
            // safe abstractions over POSIX mmap. The resulting mapping is read-only.
            let mmap = unsafe { MmapOptions::new().map(&file)? };

            Ok(Self {
                id: Arc::new(id),
                mmap: Arc::new(mmap),
                offset,
                length: actual_length,
                metadata: MessageMetadata {
                    created_at: Timestamp::now(),
                    content_type: ContentType::Json,
                    size: actual_length,
                    correlation_id: None,
                },
            })
        }

        /// Internal method for creating memory-mapped files after security validation
        #[allow(dead_code)] // Used with mmap feature
        async fn from_file_internal(
            id: MessageId,
            path: &Path,
            offset: usize,
            length: Option<usize>,
        ) -> io::Result<Self> {
            let file = File::open(path)?;
            let metadata = file.metadata()?;
            let file_size = metadata.len() as usize;

            // Validate offset (already validated by security layer, but double-check)
            if offset >= file_size {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "Offset exceeds file size",
                ));
            }

            // Calculate actual length
            let actual_length = length.unwrap_or(file_size - offset);
            let actual_length = actual_length.min(file_size - offset);

            // Create memory map
            // SAFETY: file handle is valid and opened for reading. memmap2 provides
            // safe abstractions over POSIX mmap. The resulting mapping is read-only.
            let mmap = unsafe { MmapOptions::new().map(&file)? };

            Ok(Self {
                id: Arc::new(id),
                mmap: Arc::new(mmap),
                offset,
                length: actual_length,
                metadata: MessageMetadata {
                    created_at: Timestamp::now(),
                    content_type: ContentType::Json,
                    size: actual_length,
                    correlation_id: None,
                },
            })
        }

        /// Create a message from a memory-mapped file (ASYNC - Non-blocking!)
        ///
        /// This is the async version of `from_file` that uses `tokio::task::spawn_blocking`
        /// to avoid blocking the async runtime during file I/O operations.
        ///
        /// # Production-Grade Async I/O
        /// - Uses spawn_blocking for CPU-intensive mmap operations
        /// - Maintains same functionality as sync version
        /// - Safe to call from async contexts without blocking
        /// - Proper error propagation and resource cleanup
        pub async fn from_file_async(
            id: MessageId,
            path: &std::path::Path,
            offset: usize,
            length: Option<usize>,
        ) -> std::io::Result<Self> {
            let path = path.to_path_buf(); // Clone for move into spawn_blocking

            tokio::task::spawn_blocking(move || Self::from_file(id, &path, offset, length))
                .await
                .map_err(|join_err| {
                    std::io::Error::other(format!("Async mmap operation failed: {}", join_err))
                })?
        }

        /// Get the message data as a byte slice
        #[inline]
        pub fn data(&self) -> &[u8] {
            &self.mmap[self.offset..self.offset + self.length]
        }

        /// Convert to a Bytes instance for compatibility
        #[inline]
        pub fn to_bytes(&self) -> Bytes {
            Bytes::copy_from_slice(self.data())
        }

        /// Parse JSON lazily from the mapped data
        /// Parse the message data as JSON
        pub fn parse_json<T>(&self) -> Result<T>
        where
            T: for<'de> Deserialize<'de>,
        {
            serde_json::from_slice(self.data())
                .map_err(|e| Error::serialization(format!("JSON parse error: {}", e)))
        }

        /// Get a zero-copy string view if the data is valid UTF-8
        /// Get the message data as a string slice
        pub fn as_str(&self) -> Result<&str> {
            std::str::from_utf8(self.data())
                .map_err(|e| Error::serialization(format!("Invalid UTF-8: {}", e)))
        }
    }

    /// A pool of memory-mapped files for efficient reuse
    #[derive(Debug)]
    pub struct MmapPool {
        /// Cached memory maps
        maps: dashmap::DashMap<std::path::PathBuf, Arc<Mmap>>,
        /// Maximum number of cached maps
        max_size: usize,
    }

    impl MmapPool {
        /// Create a new memory map pool
        pub fn new(max_size: usize) -> Self {
            Self {
                maps: dashmap::DashMap::new(),
                max_size,
            }
        }

        /// Get or create a memory map for a file
        pub fn get_or_create(&self, path: &Path) -> io::Result<Arc<Mmap>> {
            // Check if already cached
            if let Some(mmap) = self.maps.get(path) {
                return Ok(Arc::clone(&*mmap));
            }

            // Create new memory map
            let file = File::open(path)?;
            // SAFETY: file handle is valid and opened for reading. memmap2 provides
            // safe abstractions over POSIX mmap. The resulting mapping is read-only.
            let mmap = unsafe { MmapOptions::new().map(&file)? };
            let mmap = Arc::new(mmap);

            // Cache if under limit
            if self.maps.len() < self.max_size {
                self.maps.insert(path.to_path_buf(), Arc::clone(&mmap));
            }

            Ok(mmap)
        }

        /// Clear the cache
        pub fn clear(&self) {
            self.maps.clear();
        }

        /// Get cache size
        pub fn size(&self) -> usize {
            self.maps.len()
        }
    }

    /// Memory-mapped message batch for processing multiple messages from a file
    #[derive(Debug)]
    pub struct MmapBatch {
        /// Memory-mapped file
        mmap: Arc<Mmap>,
        /// Message boundaries (offset, length)
        messages: Vec<(usize, usize)>,
        /// Message IDs
        ids: Vec<Arc<MessageId>>,
    }

    impl MmapBatch {
        /// Create a batch from a memory-mapped file with JSON lines
        pub fn from_jsonl_file(path: &Path) -> io::Result<Self> {
            let file = File::open(path)?;
            // SAFETY: file handle is valid and opened for reading. memmap2 provides
            // safe abstractions over POSIX mmap. The resulting mapping is read-only.
            let mmap = unsafe { MmapOptions::new().map(&file)? };

            let mut messages = Vec::new();
            let mut ids = Vec::new();
            let mut offset = 0;

            // Parse JSON lines
            for (idx, line) in mmap.split(|&b| b == b'\n').enumerate() {
                if !line.is_empty() {
                    messages.push((offset, line.len()));
                    ids.push(Arc::new(MessageId::Number(idx as i64)));
                }
                offset += line.len() + 1; // +1 for newline
            }

            Ok(Self {
                mmap: Arc::new(mmap),
                messages,
                ids,
            })
        }

        /// Get a message by index
        #[inline]
        pub fn get(&self, index: usize) -> Option<&[u8]> {
            self.messages
                .get(index)
                .map(|(offset, length)| &self.mmap[*offset..*offset + *length])
        }

        /// Iterate over messages
        pub fn iter(&self) -> impl Iterator<Item = (&Arc<MessageId>, &[u8])> + '_ {
            self.ids
                .iter()
                .zip(self.messages.iter())
                .map(move |(id, (offset, length))| {
                    (id, &self.mmap.deref()[*offset..*offset + *length])
                })
        }

        /// Get the number of messages
        pub fn len(&self) -> usize {
            self.messages.len()
        }

        /// Check if batch is empty
        pub fn is_empty(&self) -> bool {
            self.messages.is_empty()
        }

        /// Create a batch from a memory-mapped JSONL file (ASYNC - Non-blocking!)
        ///
        /// This is the async version of `from_jsonl_file` that uses `tokio::task::spawn_blocking`
        /// to avoid blocking the async runtime during file I/O and parsing operations.
        ///
        /// # Production-Grade Async I/O
        /// - Uses spawn_blocking for CPU-intensive mmap and parsing operations
        /// - Maintains same functionality as sync version
        /// - Safe to call from async contexts without blocking
        /// - Proper error propagation and resource cleanup
        pub async fn from_jsonl_file_async(path: &std::path::Path) -> std::io::Result<Self> {
            let path = path.to_path_buf(); // Clone for move into spawn_blocking

            tokio::task::spawn_blocking(move || Self::from_jsonl_file(&path))
                .await
                .map_err(|join_err| {
                    std::io::Error::other(format!(
                        "Async JSONL batch operation failed: {}",
                        join_err
                    ))
                })?
        }
    }
}

/// Memory-mapped file support (safe wrapper when feature is disabled)
#[cfg(not(feature = "mmap"))]
pub mod mmap {
    use super::*;
    use std::fs;
    use std::io;

    /// Fallback implementation using regular file I/O
    #[derive(Debug)]
    pub struct MmapMessage {
        /// Unique message identifier
        pub id: Arc<MessageId>,
        /// Message data as bytes
        pub data: Bytes,
        /// Message metadata
        pub metadata: MessageMetadata,
    }

    impl MmapMessage {
        /// Create a message by reading from a file
        pub fn from_file(
            id: MessageId,
            path: &Path,
            offset: usize,
            length: Option<usize>,
        ) -> io::Result<Self> {
            let data = fs::read(path)?;
            let file_size = data.len();

            if offset >= file_size {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "Offset exceeds file size",
                ));
            }

            let actual_length = length.unwrap_or(file_size - offset);
            let actual_length = actual_length.min(file_size - offset);

            let data = Bytes::copy_from_slice(&data[offset..offset + actual_length]);

            Ok(Self {
                id: Arc::new(id),
                data: data.clone(),
                metadata: MessageMetadata {
                    created_at: Timestamp::now(),
                    content_type: ContentType::Json,
                    size: actual_length,
                    correlation_id: None,
                },
            })
        }

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

        /// Convert the message data to Bytes
        #[inline]
        pub fn to_bytes(&self) -> Bytes {
            self.data.clone()
        }

        /// Parse the message data as JSON
        pub fn parse_json<T>(&self) -> Result<T>
        where
            T: for<'de> Deserialize<'de>,
        {
            serde_json::from_slice(&self.data)
                .map_err(|e| Error::serialization(format!("JSON parse error: {}", e)))
        }

        /// Get the message data as a string slice
        pub fn as_str(&self) -> Result<&str> {
            std::str::from_utf8(&self.data)
                .map_err(|e| Error::serialization(format!("Invalid UTF-8: {}", e)))
        }

        /// Create a message from a file (ASYNC - Non-blocking fallback!)
        ///
        /// This is the async version of `from_file` for the non-mmap fallback
        /// that uses `tokio::task::spawn_blocking` to avoid blocking the async runtime.
        pub async fn from_file_async(
            id: MessageId,
            path: &std::path::Path,
            offset: usize,
            length: Option<usize>,
        ) -> std::io::Result<Self> {
            let path = path.to_path_buf(); // Clone for move into spawn_blocking

            tokio::task::spawn_blocking(move || Self::from_file(id, &path, offset, length))
                .await
                .map_err(|join_err| {
                    std::io::Error::other(format!("Async file operation failed: {}", join_err))
                })?
        }
    }

    /// Fallback pool implementation
    #[derive(Debug)]
    pub struct MmapPool {
        cache: dashmap::DashMap<std::path::PathBuf, Bytes>,
        max_size: usize,
    }

    impl MmapPool {
        /// Create a new MmapPool with the specified maximum cache size
        pub fn new(max_size: usize) -> Self {
            Self {
                cache: dashmap::DashMap::new(),
                max_size,
            }
        }

        /// Get or create a cached file read
        pub fn get_or_create(&self, path: &Path) -> io::Result<Bytes> {
            if let Some(data) = self.cache.get(path) {
                return Ok(data.clone());
            }

            let data = fs::read(path)?;
            let bytes = Bytes::from(data);

            if self.cache.len() < self.max_size {
                self.cache.insert(path.to_path_buf(), bytes.clone());
            }

            Ok(bytes)
        }

        /// Clear all cached entries
        pub fn clear(&self) {
            self.cache.clear();
        }

        /// Get the current number of cached entries
        pub fn size(&self) -> usize {
            self.cache.len()
        }
    }

    /// Fallback batch implementation
    #[derive(Debug)]
    pub struct MmapBatch {
        data: Bytes,
        messages: Vec<(usize, usize)>,
        ids: Vec<Arc<MessageId>>,
    }

    impl MmapBatch {
        /// Create a batch from a JSONL file
        pub fn from_jsonl_file(path: &Path) -> io::Result<Self> {
            let data = fs::read(path)?;
            let mut messages = Vec::new();
            let mut ids = Vec::new();
            let mut offset = 0;

            for (idx, line) in data.split(|&b| b == b'\n').enumerate() {
                if !line.is_empty() {
                    messages.push((offset, line.len()));
                    ids.push(Arc::new(MessageId::Number(idx as i64)));
                }
                offset += line.len() + 1;
            }

            Ok(Self {
                data: Bytes::from(data),
                messages,
                ids,
            })
        }

        /// Get a message by index
        #[inline]
        pub fn get(&self, index: usize) -> Option<&[u8]> {
            self.messages
                .get(index)
                .map(|(offset, length)| &self.data[*offset..*offset + *length])
        }

        /// Iterate over all messages in the batch
        pub fn iter(&self) -> impl Iterator<Item = (&Arc<MessageId>, &[u8])> + '_ {
            self.ids
                .iter()
                .zip(self.messages.iter())
                .map(move |(id, (offset, length))| (id, &self.data[*offset..*offset + *length]))
        }

        /// Get the number of messages in the batch
        pub fn len(&self) -> usize {
            self.messages.len()
        }

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

        /// Create a batch from a JSONL file (ASYNC - Non-blocking fallback!)
        ///
        /// This is the async version of `from_jsonl_file` for the non-mmap fallback
        /// that uses `tokio::task::spawn_blocking` to avoid blocking the async runtime.
        pub async fn from_jsonl_file_async(path: &std::path::Path) -> std::io::Result<Self> {
            let path = path.to_path_buf(); // Clone for move into spawn_blocking

            tokio::task::spawn_blocking(move || Self::from_jsonl_file(&path))
                .await
                .map_err(|join_err| {
                    std::io::Error::other(format!(
                        "Async JSONL batch operation failed: {}",
                        join_err
                    ))
                })?
        }
    }
}

impl From<String> for MessageId {
    fn from(s: String) -> Self {
        Self::String(Arc::from(s))
    }
}

impl From<&str> for MessageId {
    fn from(s: &str) -> Self {
        Self::String(Arc::from(s))
    }
}

impl From<i64> for MessageId {
    fn from(n: i64) -> Self {
        Self::Number(n)
    }
}

impl From<Uuid> for MessageId {
    fn from(u: Uuid) -> Self {
        Self::Uuid(u)
    }
}

impl fmt::Display for MessageId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::String(s) => write!(f, "{}", s),
            Self::Number(n) => write!(f, "{}", n),
            Self::Uuid(u) => write!(f, "{}", u),
        }
    }
}

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

    #[test]
    fn test_zero_copy_message_creation() {
        let payload = Bytes::from(r#"{"test": "data"}"#);
        let msg = ZeroCopyMessage::from_bytes(MessageId::from("test-1"), payload.clone());

        assert_eq!(msg.payload, payload);
        assert_eq!(msg.metadata.size, payload.len());
    }

    #[test]
    fn test_lazy_json_parsing() {
        let payload = Bytes::from(r#"{"key": "value", "number": 42}"#);
        let mut msg = ZeroCopyMessage::from_bytes(MessageId::from("test-2"), payload);

        // Parse lazily
        let raw = msg.parse_json_lazy().unwrap();
        assert!(raw.get().contains("value"));

        // Check that lazy_json is now populated
        assert!(msg.lazy_json.is_some());
    }

    #[test]
    fn test_buffer_pool() {
        let pool = BufferPool::new(2, 1024);

        let buf1 = pool.acquire();
        let buf2 = pool.acquire();
        let buf3 = pool.acquire(); // Should create new

        assert_eq!(buf1.capacity(), 1024);
        assert_eq!(buf2.capacity(), 1024);
        assert_eq!(buf3.capacity(), 1024);

        pool.release(buf1);
        let buf4 = pool.acquire(); // Should reuse
        assert_eq!(buf4.capacity(), 1024);
    }

    #[test]
    fn test_message_batch() {
        let mut batch = MessageBatch::new(10);

        batch.add(MessageId::from("msg1"), Bytes::from("data1"));
        batch.add(MessageId::from("msg2"), Bytes::from("data2"));
        batch.add(MessageId::from("msg3"), Bytes::from("data3"));

        assert_eq!(batch.messages.len(), 3);

        let msg1 = batch.get(0).unwrap();
        assert_eq!(msg1, Bytes::from("data1"));

        let msg2 = batch.get(1).unwrap();
        assert_eq!(msg2, Bytes::from("data2"));

        // Iterate without copying
        let mut count = 0;
        for (_id, payload) in batch.iter() {
            count += 1;
            assert!(!payload.is_empty());
        }
        assert_eq!(count, 3);
    }

    #[test]
    fn test_cheap_clone() {
        let msg = ZeroCopyMessage::from_bytes(MessageId::from("test"), Bytes::from("data"));

        let cloned = msg.cheap_clone();

        // Should share the same Arc pointers
        assert!(Arc::ptr_eq(&msg.id, &cloned.id));
        assert_eq!(msg.payload, cloned.payload);
    }

    #[test]
    fn test_mmap_message() {
        use std::io::Write;

        // Create a test file
        let temp_dir = std::env::temp_dir();
        let test_file = temp_dir.join("test_mmap.json");
        let mut file = std::fs::File::create(&test_file).unwrap();
        let test_data = r#"{"test": "data", "value": 42}"#;
        file.write_all(test_data.as_bytes()).unwrap();
        file.sync_all().unwrap();
        drop(file);

        // Test memory-mapped message
        let msg = mmap::MmapMessage::from_file(MessageId::from("mmap-test"), &test_file, 0, None)
            .unwrap();

        assert_eq!(msg.data(), test_data.as_bytes());
        assert_eq!(msg.as_str().unwrap(), test_data);

        // Test JSON parsing
        let value: serde_json::Value = msg.parse_json().unwrap();
        assert_eq!(value["test"], "data");
        assert_eq!(value["value"], 42);

        // Clean up
        std::fs::remove_file(test_file).unwrap();
    }

    #[test]
    fn test_mmap_batch() {
        use std::io::Write;

        // Create a test JSONL file
        let temp_dir = std::env::temp_dir();
        let test_file = temp_dir.join("test_batch.jsonl");
        let mut file = std::fs::File::create(&test_file).unwrap();
        writeln!(file, r#"{{"id": 1, "name": "first"}}"#).unwrap();
        writeln!(file, r#"{{"id": 2, "name": "second"}}"#).unwrap();
        writeln!(file, r#"{{"id": 3, "name": "third"}}"#).unwrap();
        file.sync_all().unwrap();
        drop(file);

        // Test batch processing
        let batch = mmap::MmapBatch::from_jsonl_file(&test_file).unwrap();

        assert_eq!(batch.len(), 3);
        assert!(!batch.is_empty());

        // Test individual access
        let msg1 = batch.get(0).unwrap();
        let value: serde_json::Value = serde_json::from_slice(msg1).unwrap();
        assert_eq!(value["id"], 1);
        assert_eq!(value["name"], "first");

        // Test iteration
        let mut count = 0;
        for (_id, data) in batch.iter() {
            let value: serde_json::Value = serde_json::from_slice(data).unwrap();
            assert!(value["id"].is_number());
            assert!(value["name"].is_string());
            count += 1;
        }
        assert_eq!(count, 3);

        // Clean up
        std::fs::remove_file(test_file).unwrap();
    }

    #[test]
    fn test_mmap_pool() {
        use std::io::Write;

        // Create test files
        let temp_dir = std::env::temp_dir();
        let test_file1 = temp_dir.join("pool_test1.json");
        let test_file2 = temp_dir.join("pool_test2.json");

        let mut file1 = std::fs::File::create(&test_file1).unwrap();
        file1.write_all(b"test1").unwrap();
        file1.sync_all().unwrap();

        let mut file2 = std::fs::File::create(&test_file2).unwrap();
        file2.write_all(b"test2").unwrap();
        file2.sync_all().unwrap();

        // Test pool
        let pool = mmap::MmapPool::new(10);

        assert_eq!(pool.size(), 0);

        let _data1 = pool.get_or_create(&test_file1).unwrap();
        assert_eq!(pool.size(), 1);

        let _data2 = pool.get_or_create(&test_file2).unwrap();
        assert_eq!(pool.size(), 2);

        // Getting again should use cache
        let _data1_again = pool.get_or_create(&test_file1).unwrap();
        assert_eq!(pool.size(), 2); // Still 2, used cache

        pool.clear();
        assert_eq!(pool.size(), 0);

        // Clean up
        std::fs::remove_file(test_file1).unwrap();
        std::fs::remove_file(test_file2).unwrap();
    }

    // ============================================================================
    // Async Memory Mapping Tests - Production-Grade TDD
    // ============================================================================

    #[cfg(feature = "mmap")]
    mod async_mmap_tests {
        use super::MessageId;
        use super::mmap::*;
        use std::io::Write;
        use std::path::Path;

        #[tokio::test]
        async fn test_mmap_message_from_file_async_performance() {
            // Test that from_file_async doesn't block the async runtime

            let temp_dir = std::env::temp_dir();
            let test_file = temp_dir.join("async_mmap_test.json");

            // Create test file
            {
                let mut file = std::fs::File::create(&test_file).unwrap();
                let test_data = r#"{"test": "async_data", "large_field": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."}"#;
                file.write_all(test_data.as_bytes()).unwrap();
                file.sync_all().unwrap();
            }

            // Test concurrent async calls don't block each other
            let handles = (0..3)
                .map(|i| {
                    let test_file = test_file.clone();
                    tokio::spawn(async move {
                        let start_time = std::time::Instant::now();

                        // This will FAIL initially because we need to implement from_file_async
                        let result = MmapMessage::from_file_async(
                            MessageId::from(format!("async-test-{}", i)),
                            &test_file,
                            0,
                            None,
                        )
                        .await;

                        let duration = start_time.elapsed();

                        // Should complete quickly without blocking other async tasks
                        assert!(
                            duration.as_millis() < 100,
                            "Async mmap took {}ms - should be <100ms",
                            duration.as_millis()
                        );

                        (i, result)
                    })
                })
                .collect::<Vec<_>>();

            let start_time = std::time::Instant::now();
            let results = futures::future::join_all(handles).await;
            let total_duration = start_time.elapsed();

            // All concurrent operations should complete quickly
            assert!(
                total_duration.as_millis() < 200,
                "Concurrent async mmap operations took {}ms - should be <200ms",
                total_duration.as_millis()
            );

            // All should succeed and return valid messages
            for result in results {
                let (i, mmap_result) = result.unwrap();
                let mmap_msg = mmap_result.unwrap();
                assert_eq!(*mmap_msg.id, MessageId::from(format!("async-test-{}", i)));
                assert!(!mmap_msg.data().is_empty());
            }

            // Clean up
            std::fs::remove_file(test_file).unwrap();
        }

        #[tokio::test]
        async fn test_mmap_batch_from_jsonl_file_async_concurrency() {
            let temp_dir = std::env::temp_dir();
            let test_file = temp_dir.join("async_batch_test.jsonl");

            // Create JSONL test file
            {
                let mut file = std::fs::File::create(&test_file).unwrap();
                writeln!(file, r#"{{"id": "msg1", "data": "test1"}}"#).unwrap();
                writeln!(file, r#"{{"id": "msg2", "data": "test2"}}"#).unwrap();
                writeln!(file, r#"{{"id": "msg3", "data": "test3"}}"#).unwrap();
                file.sync_all().unwrap();
            }

            // Test that async version doesn't block concurrent operations
            let handles = (0..5)
                .map(|_| {
                    let test_file = test_file.clone();
                    tokio::spawn(async move {
                        let start_time = std::time::Instant::now();

                        // This will FAIL initially - need to implement from_jsonl_file_async
                        let result = MmapBatch::from_jsonl_file_async(&test_file).await;

                        let duration = start_time.elapsed();
                        assert!(
                            duration.as_millis() < 150,
                            "Async batch processing took {}ms - should be <150ms",
                            duration.as_millis()
                        );

                        result
                    })
                })
                .collect::<Vec<_>>();

            let results = futures::future::join_all(handles).await;

            // All should succeed
            for result in results {
                let batch = result.unwrap().unwrap();
                assert_eq!(batch.len(), 3);
            }

            std::fs::remove_file(test_file).unwrap();
        }

        #[tokio::test]
        async fn test_async_mmap_error_handling() {
            // Test async error handling for non-existent files
            let non_existent = Path::new("/tmp/does_not_exist_async.json");

            let result = MmapMessage::from_file_async(
                MessageId::String("error-test".to_string().into()),
                non_existent,
                0,
                None,
            )
            .await;

            assert!(result.is_err());

            // Error should be descriptive, not generic blocking I/O error
            let error_msg = format!("{}", result.unwrap_err());
            assert!(
                error_msg.contains("No such file") || error_msg.contains("not found"),
                "Error should be descriptive: {}",
                error_msg
            );
        }

        #[tokio::test]
        async fn test_async_mmap_maintains_functionality() {
            // Verify async versions provide same functionality as sync versions
            let temp_dir = std::env::temp_dir();
            let test_file = temp_dir.join("functionality_test.json");

            let test_data = r#"{"test": "functionality", "value": 42}"#;
            std::fs::write(&test_file, test_data).unwrap();

            // Test both sync and async versions
            let sync_result = MmapMessage::from_file(
                MessageId::String("sync".to_string().into()),
                &test_file,
                0,
                None,
            )
            .unwrap();

            let async_result = MmapMessage::from_file_async(
                MessageId::String("async".to_string().into()),
                &test_file,
                0,
                None,
            )
            .await
            .unwrap();

            // Same data should be accessible
            assert_eq!(sync_result.data(), async_result.data());
            assert_eq!(sync_result.data(), test_data.as_bytes());

            std::fs::remove_file(test_file).unwrap();
        }
    }
}