shardex 0.1.0

A high-performance memory-mapped vector search engine with ACID transactions and incremental updates
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
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
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
//! Transaction recording and batching for WAL operations
//!
//! This module provides transaction-based recording of WAL operations with efficient
//! batching and timer-based flushing for improved write throughput while maintaining
//! ACID properties.

use crate::error::ShardexError;
use crate::identifiers::{DocumentId, TransactionId};
use bytemuck::{Pod, Zeroable};
use serde::{Deserialize, Serialize};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::sync::mpsc;
use tokio::time::{interval, Interval};

/// Operations that can be recorded in the WAL
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum WalOperation {
    /// Add a posting to the index
    AddPosting {
        document_id: DocumentId,
        start: u32,
        length: u32,
        vector: Vec<f32>,
    },
    /// Remove all postings for a document
    RemoveDocument { document_id: DocumentId },
    /// Store document text (part of atomic replace operation)
    StoreDocumentText { document_id: DocumentId, text: String },
    /// Delete document text (cleanup operation)
    DeleteDocumentText { document_id: DocumentId },
}

/// A transaction containing batched WAL operations
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WalTransaction {
    /// Unique transaction identifier
    pub id: TransactionId,
    /// Timestamp when transaction was created
    pub timestamp: SystemTime,
    /// List of operations in this transaction
    pub operations: Vec<WalOperation>,
    /// CRC32 checksum of the transaction data
    pub checksum: u32,
}

/// Binary header for WAL transactions in memory-mapped storage
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(C)]
pub struct WalTransactionHeader {
    /// Transaction identifier
    pub id: TransactionId,
    /// Unix timestamp in microseconds
    pub timestamp_micros: u64,
    /// Number of operations in the transaction
    pub operation_count: u32,
    /// Total size of serialized operations data in bytes
    pub operations_data_size: u32,
    /// CRC32 checksum of the operations data
    pub checksum: u32,
    /// Reserved bytes for future use
    pub reserved: [u8; 4],
}

// SAFETY: WalTransactionHeader contains only Pod types and has repr(C) layout
unsafe impl Pod for WalTransactionHeader {}
// SAFETY: WalTransactionHeader can be safely zero-initialized
unsafe impl Zeroable for WalTransactionHeader {}

impl WalOperation {
    /// Get the document ID associated with this operation
    pub fn document_id(&self) -> DocumentId {
        match self {
            WalOperation::AddPosting { document_id, .. } => *document_id,
            WalOperation::RemoveDocument { document_id } => *document_id,
            WalOperation::StoreDocumentText { document_id, .. } => *document_id,
            WalOperation::DeleteDocumentText { document_id } => *document_id,
        }
    }

    /// Check if this is an AddPosting operation
    pub fn is_add_posting(&self) -> bool {
        matches!(self, WalOperation::AddPosting { .. })
    }

    /// Check if this is a RemoveDocument operation
    pub fn is_remove_document(&self) -> bool {
        matches!(self, WalOperation::RemoveDocument { .. })
    }

    /// Check if this is a StoreDocumentText operation
    pub fn is_store_document_text(&self) -> bool {
        matches!(self, WalOperation::StoreDocumentText { .. })
    }

    /// Check if this is a DeleteDocumentText operation
    pub fn is_delete_document_text(&self) -> bool {
        matches!(self, WalOperation::DeleteDocumentText { .. })
    }

    /// Estimate the serialized size of this operation in bytes
    pub fn estimated_serialized_size(&self) -> usize {
        match self {
            WalOperation::AddPosting { vector, .. } => {
                // operation tag (1) + document_id (16) + start (4) + length (4) + vector length (4) + vector data
                1 + 16 + 4 + 4 + 4 + (vector.len() * 4)
            }
            WalOperation::RemoveDocument { .. } => {
                // operation tag (1) + document_id (16)
                1 + 16
            }
            WalOperation::StoreDocumentText { text, .. } => {
                // operation tag (1) + document_id (16) + text length (4) + text data (UTF-8 bytes)
                1 + 16 + 4 + text.len()
            }
            WalOperation::DeleteDocumentText { .. } => {
                // operation tag (1) + document_id (16)
                1 + 16
            }
        }
    }

    /// Validate the operation data
    pub fn validate(
        &self,
        expected_vector_dimension: Option<usize>,
        max_document_text_size: usize,
    ) -> Result<(), ShardexError> {
        match self {
            WalOperation::AddPosting {
                vector, start, length, ..
            } => {
                if *start > u32::MAX - *length {
                    return Err(ShardexError::Wal(
                        "AddPosting start + length would overflow u32".to_string(),
                    ));
                }

                if *length == 0 {
                    return Err(ShardexError::Wal("AddPosting length cannot be zero".to_string()));
                }

                if vector.is_empty() {
                    return Err(ShardexError::Wal("AddPosting vector cannot be empty".to_string()));
                }

                if let Some(expected_dim) = expected_vector_dimension {
                    if vector.len() != expected_dim {
                        return Err(ShardexError::InvalidDimension {
                            expected: expected_dim,
                            actual: vector.len(),
                        });
                    }
                }

                // Check for invalid float values
                for (i, &value) in vector.iter().enumerate() {
                    if !value.is_finite() {
                        return Err(ShardexError::Wal(format!(
                            "Invalid vector value at index {}: {} (must be finite)",
                            i, value
                        )));
                    }
                }
            }
            WalOperation::RemoveDocument { .. } => {
                // RemoveDocument operations are always valid if document_id is valid
                // DocumentId validation is handled by the type system
            }
            WalOperation::StoreDocumentText { text, .. } => {
                // String type in Rust guarantees valid UTF-8, so we don't need to check
                // But we should validate the text is not empty
                if text.is_empty() {
                    return Err(ShardexError::Wal("StoreDocumentText text cannot be empty".to_string()));
                }

                // Use the configured maximum document text size
                if text.len() > max_document_text_size {
                    return Err(ShardexError::DocumentTooLarge {
                        size: text.len(),
                        max_size: max_document_text_size,
                    });
                }
            }
            WalOperation::DeleteDocumentText { .. } => {
                // DeleteDocumentText operations are always valid if document_id is valid
                // DocumentId validation is handled by the type system
            }
        }
        Ok(())
    }
}

impl WalTransaction {
    /// Create a new transaction with the given operations
    pub fn new(operations: Vec<WalOperation>) -> Result<Self, ShardexError> {
        if operations.is_empty() {
            return Err(ShardexError::Wal("Transaction cannot have zero operations".to_string()));
        }

        let id = TransactionId::new();
        let timestamp = SystemTime::now();

        // Serialize operations to calculate checksum
        let operations_data = Self::serialize_operations(&operations)?;
        let checksum = crc32fast::hash(&operations_data);

        Ok(Self {
            id,
            timestamp,
            operations,
            checksum,
        })
    }

    /// Create a transaction with a specific ID and timestamp (for testing/recovery)
    pub fn with_id_and_timestamp(
        id: TransactionId,
        timestamp: SystemTime,
        operations: Vec<WalOperation>,
    ) -> Result<Self, ShardexError> {
        if operations.is_empty() {
            return Err(ShardexError::Wal("Transaction cannot have zero operations".to_string()));
        }

        // Serialize operations to calculate checksum
        let operations_data = Self::serialize_operations(&operations)?;
        let checksum = crc32fast::hash(&operations_data);

        Ok(Self {
            id,
            timestamp,
            operations,
            checksum,
        })
    }

    /// Get the number of operations in this transaction
    pub fn operation_count(&self) -> usize {
        self.operations.len()
    }

    /// Get all document IDs affected by this transaction
    pub fn affected_document_ids(&self) -> Vec<DocumentId> {
        let mut doc_ids: Vec<DocumentId> = self.operations.iter().map(|op| op.document_id()).collect();
        doc_ids.sort();
        doc_ids.dedup();
        doc_ids
    }

    /// Estimate the total serialized size of this transaction
    pub fn estimated_serialized_size(&self) -> usize {
        std::mem::size_of::<WalTransactionHeader>()
            + self
                .operations
                .iter()
                .map(|op| op.estimated_serialized_size())
                .sum::<usize>()
    }

    /// Validate all operations in the transaction
    pub fn validate(
        &self,
        expected_vector_dimension: Option<usize>,
        max_document_text_size: usize,
    ) -> Result<(), ShardexError> {
        if self.operations.is_empty() {
            return Err(ShardexError::Wal(
                "Transaction must contain at least one operation".to_string(),
            ));
        }

        // Validate each operation
        for (i, operation) in self.operations.iter().enumerate() {
            operation
                .validate(expected_vector_dimension, max_document_text_size)
                .map_err(|e| {
                    ShardexError::Wal(format!("Operation {} in transaction {} is invalid: {}", i, self.id, e))
                })?;
        }

        // Validate timestamp is not in the future (with some tolerance)
        let now = SystemTime::now();
        const FUTURE_TOLERANCE_SECONDS: u64 = 60; // 1 minute tolerance for clock skew

        if let Ok(duration_since_epoch) = self.timestamp.duration_since(UNIX_EPOCH) {
            if let Ok(now_duration) = now.duration_since(UNIX_EPOCH) {
                if duration_since_epoch.as_secs() > now_duration.as_secs() + FUTURE_TOLERANCE_SECONDS {
                    return Err(ShardexError::Wal(format!(
                        "Transaction timestamp is too far in the future: transaction time {:?}, current time {:?}",
                        self.timestamp, now
                    )));
                }
            }
        }

        Ok(())
    }

    /// Verify the transaction's checksum
    pub fn verify_checksum(&self) -> Result<(), ShardexError> {
        let operations_data = Self::serialize_operations(&self.operations)?;
        let calculated_checksum = crc32fast::hash(&operations_data);

        if calculated_checksum != self.checksum {
            return Err(ShardexError::Wal(format!(
                "Transaction {} checksum mismatch: expected {}, calculated {}",
                self.id, self.checksum, calculated_checksum
            )));
        }

        Ok(())
    }

    /// Serialize operations to bytes for checksum calculation and storage
    fn serialize_operations(operations: &[WalOperation]) -> Result<Vec<u8>, ShardexError> {
        bincode::serialize(operations).map_err(|e| ShardexError::Wal(format!("Failed to serialize operations: {}", e)))
    }

    /// Create a binary header for this transaction
    pub fn to_header(&self) -> Result<WalTransactionHeader, ShardexError> {
        let operations_data = Self::serialize_operations(&self.operations)?;

        let timestamp_micros = self
            .timestamp
            .duration_since(UNIX_EPOCH)
            .map_err(|e| ShardexError::Wal(format!("Invalid timestamp: {}", e)))?
            .as_micros() as u64;

        Ok(WalTransactionHeader {
            id: self.id,
            timestamp_micros,
            operation_count: self.operations.len() as u32,
            operations_data_size: operations_data.len() as u32,
            checksum: self.checksum,
            reserved: [0; 4],
        })
    }

    /// Serialize the entire transaction to binary format
    pub fn serialize(&self) -> Result<Vec<u8>, ShardexError> {
        let header = self.to_header()?;
        let operations_data = Self::serialize_operations(&self.operations)?;

        let mut result = Vec::with_capacity(std::mem::size_of::<WalTransactionHeader>() + operations_data.len());

        // Write header
        result.extend_from_slice(bytemuck::bytes_of(&header));

        // Write operations data
        result.extend_from_slice(&operations_data);

        Ok(result)
    }

    /// Deserialize a transaction from binary format
    pub fn deserialize(data: &[u8]) -> Result<Self, ShardexError> {
        if data.len() < std::mem::size_of::<WalTransactionHeader>() {
            return Err(ShardexError::Wal("Transaction data too short for header".to_string()));
        }

        // Read header
        let header_bytes = &data[0..std::mem::size_of::<WalTransactionHeader>()];
        let header: WalTransactionHeader = bytemuck::pod_read_unaligned(header_bytes);

        // Validate header consistency
        let expected_total_size = std::mem::size_of::<WalTransactionHeader>() + header.operations_data_size as usize;
        if data.len() != expected_total_size {
            return Err(ShardexError::Wal(format!(
                "Transaction data size mismatch: expected {}, got {}",
                expected_total_size,
                data.len()
            )));
        }

        // Read operations data
        let operations_data_start = std::mem::size_of::<WalTransactionHeader>();
        let operations_data = &data[operations_data_start..];

        // Verify checksum before deserializing
        let calculated_checksum = crc32fast::hash(operations_data);
        if calculated_checksum != header.checksum {
            return Err(ShardexError::Wal(format!(
                "Transaction checksum mismatch: expected {}, calculated {}",
                header.checksum, calculated_checksum
            )));
        }

        // Deserialize operations
        let operations: Vec<WalOperation> = bincode::deserialize(operations_data)
            .map_err(|e| ShardexError::Wal(format!("Failed to deserialize operations: {}", e)))?;

        // Validate operation count matches header
        if operations.len() != header.operation_count as usize {
            return Err(ShardexError::Wal(format!(
                "Operation count mismatch: header says {}, found {}",
                header.operation_count,
                operations.len()
            )));
        }

        // Convert timestamp
        let timestamp = UNIX_EPOCH + std::time::Duration::from_micros(header.timestamp_micros);

        Ok(Self {
            id: header.id,
            timestamp,
            operations,
            checksum: header.checksum,
        })
    }
}

impl WalTransactionHeader {
    /// Create a zero-initialized transaction header
    pub fn new_zero() -> Self {
        Self::zeroed()
    }

    /// Check if this header represents a valid transaction
    pub fn is_valid(&self) -> bool {
        self.operation_count > 0 && self.operations_data_size > 0
    }

    /// Get the total size this transaction will occupy including header and data
    pub fn total_size(&self) -> usize {
        std::mem::size_of::<WalTransactionHeader>() + self.operations_data_size as usize
    }
}

/// Configuration for batch processing
#[derive(Debug, Clone)]
pub struct BatchConfig {
    /// Maximum interval between flushes in milliseconds
    pub batch_write_interval_ms: u64,
    /// Maximum number of operations to batch before forcing a flush
    pub max_operations_per_batch: usize,
    /// Maximum size in bytes for a batch before forcing a flush
    pub max_batch_size_bytes: usize,
    /// Maximum size for document text in bytes
    pub max_document_text_size: usize,
}

impl Default for BatchConfig {
    fn default() -> Self {
        Self {
            batch_write_interval_ms: 100,
            max_operations_per_batch: 1000,
            max_batch_size_bytes: 1024 * 1024,        // 1MB
            max_document_text_size: 10 * 1024 * 1024, // 10MB - matches ShardexConfig default
        }
    }
}

/// Batch command for communication with the batch manager
#[derive(Debug)]
pub enum BatchCommand {
    /// Add an operation to the current batch
    AddOperation(WalOperation),
    /// Force flush the current batch
    Flush,
    /// Shutdown the batch manager
    Shutdown,
}

/// Response from batch operations
#[derive(Debug)]
pub enum BatchResponse {
    /// Operation was added successfully
    OperationAdded,
    /// Batch was flushed with the given transaction ID
    BatchFlushed(TransactionId),
    /// Error occurred during processing
    Error(ShardexError),
    /// Shutdown acknowledged
    Shutdown,
}

/// Batch manager for WAL operations
///
/// The batch manager collects WAL operations and flushes them as transactions
/// at regular intervals or when batch limits are reached.
pub struct WalBatchManager {
    /// Current batch of operations being accumulated
    current_batch: Vec<WalOperation>,
    /// Configuration for batch processing
    config: BatchConfig,
    /// Estimated size of current batch in bytes
    current_batch_size: usize,
    /// Timer for periodic flushes
    flush_timer: Interval,
    /// Expected vector dimension for validation
    expected_vector_dimension: Option<usize>,
}

impl WalBatchManager {
    /// Create a new batch manager with the given configuration
    pub fn new(config: BatchConfig, expected_vector_dimension: Option<usize>) -> Self {
        let flush_timer = interval(Duration::from_millis(config.batch_write_interval_ms));

        Self {
            current_batch: Vec::new(),
            config,
            current_batch_size: 0,
            flush_timer,
            expected_vector_dimension,
        }
    }

    /// Add an operation to the current batch
    /// Returns true if a flush is required due to batch size limits
    pub fn add_operation(&mut self, operation: WalOperation) -> Result<bool, ShardexError> {
        // Validate operation before adding
        operation.validate(self.expected_vector_dimension, self.config.max_document_text_size)?;

        let operation_size = operation.estimated_serialized_size();

        // Add the operation first
        self.current_batch.push(operation);
        self.current_batch_size += operation_size;

        // Check if we've reached the limits after adding
        let should_flush_count = self.current_batch.len() >= self.config.max_operations_per_batch;
        let should_flush_size = self.current_batch_size > self.config.max_batch_size_bytes;

        Ok(should_flush_count || should_flush_size)
    }

    /// Flush the current batch as a transaction
    /// Returns the transaction ID if a flush occurred, None if batch was empty
    pub async fn flush_batch<F>(&mut self, write_fn: F) -> Result<Option<TransactionId>, ShardexError>
    where
        F: Fn(&WalTransaction) -> Result<(), ShardexError>,
    {
        if self.current_batch.is_empty() {
            return Ok(None);
        }

        // Create transaction from current batch
        let operations = std::mem::take(&mut self.current_batch);
        let transaction = WalTransaction::new(operations)?;
        let transaction_id = transaction.id;

        // Validate transaction
        transaction.validate(self.expected_vector_dimension, self.config.max_document_text_size)?;

        // Write transaction using the provided function
        write_fn(&transaction)?;

        // Reset batch state
        self.current_batch_size = 0;

        Ok(Some(transaction_id))
    }

    /// Get the current batch statistics
    pub fn batch_stats(&self) -> BatchStats {
        BatchStats {
            operation_count: self.current_batch.len(),
            estimated_size_bytes: self.current_batch_size,
            is_empty: self.current_batch.is_empty(),
        }
    }

    /// Check if the batch should be flushed due to time interval
    pub async fn should_flush_due_to_timer(&mut self) -> bool {
        // This is a non-blocking check - it returns true if the timer has fired
        self.flush_timer.tick().await;
        !self.current_batch.is_empty()
    }

    /// Run the batch manager event loop
    pub async fn run_event_loop<F>(
        mut self,
        mut receiver: mpsc::Receiver<BatchCommand>,
        response_sender: mpsc::Sender<BatchResponse>,
        write_fn: F,
    ) where
        F: Fn(&WalTransaction) -> Result<(), ShardexError> + Send + 'static,
    {
        loop {
            tokio::select! {
                // Handle incoming commands
                command = receiver.recv() => {
                    match command {
                        Some(BatchCommand::AddOperation(operation)) => {
                            match self.add_operation(operation) {
                                Ok(should_flush) => {
                                    let _ = response_sender.send(BatchResponse::OperationAdded).await;

                                    if should_flush {
                                        match self.flush_batch(&write_fn).await {
                                            Ok(Some(transaction_id)) => {
                                                let _ = response_sender.send(BatchResponse::BatchFlushed(transaction_id)).await;
                                            }
                                            Ok(None) => {
                                                // Empty batch, nothing to flush
                                            }
                                            Err(e) => {
                                                let _ = response_sender.send(BatchResponse::Error(e)).await;
                                            }
                                        }
                                    }
                                }
                                Err(e) => {
                                    let _ = response_sender.send(BatchResponse::Error(e)).await;
                                }
                            }
                        }
                        Some(BatchCommand::Flush) => {
                            match self.flush_batch(&write_fn).await {
                                Ok(Some(transaction_id)) => {
                                    let _ = response_sender.send(BatchResponse::BatchFlushed(transaction_id)).await;
                                }
                                Ok(None) => {
                                    let _ = response_sender.send(BatchResponse::BatchFlushed(TransactionId::new())).await; // Empty flush
                                }
                                Err(e) => {
                                    let _ = response_sender.send(BatchResponse::Error(e)).await;
                                }
                            }
                        }
                        Some(BatchCommand::Shutdown) => {
                            // Flush any remaining operations
                            let _ = self.flush_batch(&write_fn).await;
                            let _ = response_sender.send(BatchResponse::Shutdown).await;
                            break;
                        }
                        None => {
                            // Channel closed, shutdown
                            let _ = self.flush_batch(&write_fn).await;
                            break;
                        }
                    }
                }

                // Handle timer-based flushes
                _ = self.should_flush_due_to_timer() => {
                    match self.flush_batch(&write_fn).await {
                        Ok(Some(transaction_id)) => {
                            let _ = response_sender.send(BatchResponse::BatchFlushed(transaction_id)).await;
                        }
                        Ok(None) => {
                            // Empty batch, nothing to flush
                        }
                        Err(e) => {
                            let _ = response_sender.send(BatchResponse::Error(e)).await;
                        }
                    }
                }
            }
        }
    }
}

/// Statistics about the current batch
#[derive(Debug, Clone, PartialEq)]
pub struct BatchStats {
    /// Number of operations in the current batch
    pub operation_count: usize,
    /// Estimated size of the batch in bytes
    pub estimated_size_bytes: usize,
    /// Whether the batch is empty
    pub is_empty: bool,
}

/// Handle for communicating with the batch manager
pub struct WalBatchHandle {
    /// Sender for batch commands
    command_sender: mpsc::Sender<BatchCommand>,
    /// Receiver for batch responses
    response_receiver: mpsc::Receiver<BatchResponse>,
}

impl WalBatchHandle {
    /// Create a new batch handle and manager
    pub fn new(config: BatchConfig, expected_vector_dimension: Option<usize>) -> (Self, WalBatchManager) {
        let (command_sender, _command_receiver) = mpsc::channel(1000);
        let (_response_sender, response_receiver) = mpsc::channel(1000);

        let manager = WalBatchManager::new(config, expected_vector_dimension);

        let handle = Self {
            command_sender,
            response_receiver,
        };

        (handle, manager)
    }

    /// Add an operation to the batch
    pub async fn add_operation(&mut self, operation: WalOperation) -> Result<(), ShardexError> {
        self.command_sender
            .send(BatchCommand::AddOperation(operation))
            .await
            .map_err(|_| ShardexError::Wal("Batch manager channel closed".to_string()))?;

        // Wait for response
        match self.response_receiver.recv().await {
            Some(BatchResponse::OperationAdded) => Ok(()),
            Some(BatchResponse::Error(e)) => Err(e),
            Some(response) => Err(ShardexError::Wal(format!("Unexpected response: {:?}", response))),
            None => Err(ShardexError::Wal("Batch manager response channel closed".to_string())),
        }
    }

    /// Flush the current batch
    pub async fn flush(&mut self) -> Result<TransactionId, ShardexError> {
        self.command_sender
            .send(BatchCommand::Flush)
            .await
            .map_err(|_| ShardexError::Wal("Batch manager channel closed".to_string()))?;

        // Wait for response
        match self.response_receiver.recv().await {
            Some(BatchResponse::BatchFlushed(transaction_id)) => Ok(transaction_id),
            Some(BatchResponse::Error(e)) => Err(e),
            Some(response) => Err(ShardexError::Wal(format!("Unexpected response: {:?}", response))),
            None => Err(ShardexError::Wal("Batch manager response channel closed".to_string())),
        }
    }

    /// Shutdown the batch manager
    pub async fn shutdown(&mut self) -> Result<(), ShardexError> {
        self.command_sender
            .send(BatchCommand::Shutdown)
            .await
            .map_err(|_| ShardexError::Wal("Batch manager channel closed".to_string()))?;

        // Wait for response
        match self.response_receiver.recv().await {
            Some(BatchResponse::Shutdown) => Ok(()),
            Some(BatchResponse::Error(e)) => Err(e),
            Some(response) => Err(ShardexError::Wal(format!("Unexpected response: {:?}", response))),
            None => Err(ShardexError::Wal("Batch manager response channel closed".to_string())),
        }
    }
}

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

    #[test]
    fn test_wal_operation_document_id() {
        let doc_id = DocumentId::new();
        let vector = vec![1.0, 2.0, 3.0];

        let add_op = WalOperation::AddPosting {
            document_id: doc_id,
            start: 0,
            length: 100,
            vector,
        };

        let remove_op = WalOperation::RemoveDocument { document_id: doc_id };

        assert_eq!(add_op.document_id(), doc_id);
        assert_eq!(remove_op.document_id(), doc_id);
        assert!(add_op.is_add_posting());
        assert!(!add_op.is_remove_document());
        assert!(!remove_op.is_add_posting());
        assert!(remove_op.is_remove_document());
    }

    #[test]
    fn test_wal_operation_validation() {
        let doc_id = DocumentId::new();

        // Valid AddPosting
        let valid_add = WalOperation::AddPosting {
            document_id: doc_id,
            start: 100,
            length: 50,
            vector: vec![1.0, 2.0, 3.0],
        };
        assert!(valid_add.validate(Some(3), 10 * 1024 * 1024).is_ok());
        assert!(valid_add.validate(None, 10 * 1024 * 1024).is_ok());

        // Invalid dimension
        assert!(valid_add.validate(Some(4), 10 * 1024 * 1024).is_err());

        // Invalid vector with NaN
        let invalid_nan = WalOperation::AddPosting {
            document_id: doc_id,
            start: 100,
            length: 50,
            vector: vec![1.0, f32::NAN, 3.0],
        };
        assert!(invalid_nan.validate(None, 10 * 1024 * 1024).is_err());

        // Invalid vector with infinity
        let invalid_inf = WalOperation::AddPosting {
            document_id: doc_id,
            start: 100,
            length: 50,
            vector: vec![1.0, f32::INFINITY, 3.0],
        };
        assert!(invalid_inf.validate(None, 10 * 1024 * 1024).is_err());

        // Zero length
        let zero_length = WalOperation::AddPosting {
            document_id: doc_id,
            start: 100,
            length: 0,
            vector: vec![1.0, 2.0, 3.0],
        };
        assert!(zero_length.validate(None, 10 * 1024 * 1024).is_err());

        // Empty vector
        let empty_vector = WalOperation::AddPosting {
            document_id: doc_id,
            start: 100,
            length: 50,
            vector: vec![],
        };
        assert!(empty_vector.validate(None, 10 * 1024 * 1024).is_err());

        // Overflow in start + length
        let overflow = WalOperation::AddPosting {
            document_id: doc_id,
            start: u32::MAX,
            length: 1,
            vector: vec![1.0, 2.0, 3.0],
        };
        assert!(overflow.validate(None, 10 * 1024 * 1024).is_err());

        // Valid RemoveDocument (always valid)
        let remove_doc = WalOperation::RemoveDocument { document_id: doc_id };
        assert!(remove_doc.validate(None, 10 * 1024 * 1024).is_ok());
        assert!(remove_doc.validate(Some(128), 10 * 1024 * 1024).is_ok());
    }

    #[test]
    fn test_wal_transaction_creation() {
        let doc_id = DocumentId::new();
        let operations = vec![
            WalOperation::AddPosting {
                document_id: doc_id,
                start: 0,
                length: 100,
                vector: vec![1.0, 2.0, 3.0],
            },
            WalOperation::RemoveDocument { document_id: doc_id },
        ];

        let transaction = WalTransaction::new(operations.clone()).unwrap();

        assert_eq!(transaction.operations, operations);
        assert_eq!(transaction.operation_count(), 2);
        assert!(transaction.checksum != 0);
    }

    #[test]
    fn test_wal_transaction_empty_operations() {
        let result = WalTransaction::new(vec![]);
        // assert!(result.is_err());
        if let Err(ShardexError::Wal(msg)) = result {
            assert!(msg.contains("cannot have zero operations"));
        } else {
            panic!("Expected Wal error");
        }
    }

    #[test]
    fn test_wal_transaction_affected_documents() {
        let doc_id1 = DocumentId::new();
        let doc_id2 = DocumentId::new();

        let operations = vec![
            WalOperation::AddPosting {
                document_id: doc_id1,
                start: 0,
                length: 100,
                vector: vec![1.0, 2.0, 3.0],
            },
            WalOperation::AddPosting {
                document_id: doc_id2,
                start: 50,
                length: 75,
                vector: vec![4.0, 5.0, 6.0],
            },
            WalOperation::RemoveDocument { document_id: doc_id1 },
        ];

        let transaction = WalTransaction::new(operations).unwrap();
        let affected = transaction.affected_document_ids();

        // Should have both document IDs, sorted and deduplicated
        assert_eq!(affected.len(), 2);
        assert!(affected.contains(&doc_id1));
        assert!(affected.contains(&doc_id2));
        // Check sorting
        assert!(affected[0] < affected[1]);
    }

    #[test]
    fn test_wal_transaction_checksum_verification() {
        let doc_id = DocumentId::new();
        let operations = vec![WalOperation::AddPosting {
            document_id: doc_id,
            start: 0,
            length: 100,
            vector: vec![1.0, 2.0, 3.0],
        }];

        let transaction = WalTransaction::new(operations).unwrap();

        // Should verify successfully
        assert!(transaction.verify_checksum().is_ok());

        // Manually create transaction with wrong checksum
        let mut bad_transaction = transaction.clone();
        bad_transaction.checksum = 12345; // Wrong checksum

        assert!(bad_transaction.verify_checksum().is_err());
    }

    #[test]
    fn test_wal_transaction_serialization() {
        let doc_id = DocumentId::new();
        let operations = vec![
            WalOperation::AddPosting {
                document_id: doc_id,
                start: 0,
                length: 100,
                vector: vec![1.0, 2.0, 3.0],
            },
            WalOperation::RemoveDocument { document_id: doc_id },
        ];

        let transaction = WalTransaction::new(operations).unwrap();

        // Test serialization
        let serialized = transaction.serialize().unwrap();
        assert!(!serialized.is_empty());

        // Test deserialization
        let deserialized = WalTransaction::deserialize(&serialized).unwrap();

        assert_eq!(transaction.id, deserialized.id);
        assert_eq!(transaction.operations, deserialized.operations);
        assert_eq!(transaction.checksum, deserialized.checksum);

        // Timestamps should be very close (within a microsecond or two)
        let time_diff = if transaction.timestamp > deserialized.timestamp {
            transaction
                .timestamp
                .duration_since(deserialized.timestamp)
                .unwrap()
        } else {
            deserialized
                .timestamp
                .duration_since(transaction.timestamp)
                .unwrap()
        };
        assert!(time_diff.as_micros() < 10); // Should be identical or very close
    }

    #[test]
    fn test_wal_transaction_header_operations() {
        let doc_id = DocumentId::new();
        let operations = vec![WalOperation::AddPosting {
            document_id: doc_id,
            start: 0,
            length: 100,
            vector: vec![1.0, 2.0, 3.0],
        }];

        let transaction = WalTransaction::new(operations).unwrap();
        let header = transaction.to_header().unwrap();

        assert_eq!(header.id, transaction.id);
        assert_eq!(header.operation_count, 1);
        assert!(header.operations_data_size > 0);
        assert_eq!(header.checksum, transaction.checksum);
        assert!(header.is_valid());

        let total_size = header.total_size();
        assert_eq!(
            total_size,
            std::mem::size_of::<WalTransactionHeader>() + header.operations_data_size as usize
        );
    }

    #[test]
    fn test_wal_transaction_header_bytemuck() {
        let header = WalTransactionHeader {
            id: TransactionId::new(),
            timestamp_micros: 1640995200000000, // 2022-01-01T00:00:00Z
            operation_count: 5,
            operations_data_size: 1024,
            checksum: 0x12345678,
            reserved: [0; 4],
        };

        // Test Pod trait - should be able to cast to bytes
        let bytes: &[u8] = bytemuck::bytes_of(&header);
        assert_eq!(bytes.len(), std::mem::size_of::<WalTransactionHeader>());

        // Test round-trip
        let header_restored: WalTransactionHeader = bytemuck::pod_read_unaligned(bytes);
        assert_eq!(header, header_restored);
    }

    #[tokio::test]
    async fn test_invalid_transaction_data() {
        // Test invalid transaction ID
        let invalid_header = WalTransactionHeader {
            id: TransactionId::from_bytes([0xFF; 16]),
            timestamp_micros: 0,
            operation_count: 1,
            operations_data_size: 100,
            checksum: 0,
            reserved: [0; 4],
        };

        let bytes = bytemuck::bytes_of(&invalid_header);
        let result: Result<WalTransactionHeader, _> = bytemuck::try_pod_read_unaligned(bytes);
        assert!(result.is_ok()); // bytemuck doesn't validate values, just structure

        // Test extreme timestamp values
        let timestamp_header = WalTransactionHeader {
            id: TransactionId::new(),
            timestamp_micros: u64::MAX,
            operation_count: 1,
            operations_data_size: 100,
            checksum: 0,
            reserved: [0; 4],
        };

        let bytes = bytemuck::bytes_of(&timestamp_header);
        let header: WalTransactionHeader = bytemuck::pod_read_unaligned(bytes);
        assert_eq!(header.timestamp_micros, u64::MAX);

        // Test operation count overflow
        let overflow_header = WalTransactionHeader {
            id: TransactionId::new(),
            timestamp_micros: 1234567890,
            operation_count: u32::MAX,
            operations_data_size: 100,
            checksum: 0,
            reserved: [0; 4],
        };

        let bytes = bytemuck::bytes_of(&overflow_header);
        let header: WalTransactionHeader = bytemuck::pod_read_unaligned(bytes);
        assert_eq!(header.operation_count, u32::MAX);

        // Test size mismatch scenarios
        let size_mismatch_header = WalTransactionHeader {
            id: TransactionId::new(),
            timestamp_micros: 1234567890,
            operation_count: 1,
            operations_data_size: u32::MAX,
            checksum: 0,
            reserved: [0; 4],
        };

        let bytes = bytemuck::bytes_of(&size_mismatch_header);
        let header: WalTransactionHeader = bytemuck::pod_read_unaligned(bytes);
        assert_eq!(header.operations_data_size, u32::MAX);
    }

    #[tokio::test]
    async fn test_invalid_batch_operations() {
        let config = BatchConfig::default();
        let mut manager = WalBatchManager::new(config, None); // No dimension constraint for testing edge cases

        // Test with zero-length vector (edge case)
        let operation = WalOperation::AddPosting {
            document_id: DocumentId::new(),
            start: 0,
            length: 100,
            vector: vec![], // Empty vector
        };

        let result = manager.add_operation(operation);
        assert!(result.is_err()); // Empty vectors should be invalid

        // Test with extremely large vector
        let large_vector = vec![1.0; 1_000_000]; // 1M elements
        let operation = WalOperation::AddPosting {
            document_id: DocumentId::new(),
            start: 0,
            length: 100,
            vector: large_vector,
        };

        let result = manager.add_operation(operation);
        assert!(result.is_ok()); // Large vectors should be valid

        // Test extreme start/length combinations
        let operation = WalOperation::AddPosting {
            document_id: DocumentId::new(),
            start: u32::MAX,
            length: u32::MAX,
            vector: vec![1.0, 2.0, 3.0],
        };

        let result = manager.add_operation(operation);
        assert!(result.is_err()); // start + length would overflow u32

        // Test document removal operation
        let operation = WalOperation::RemoveDocument {
            document_id: DocumentId::new(),
        };

        let result = manager.add_operation(operation);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_batch_manager_basic_operations() {
        let config = BatchConfig::default();
        let mut manager = WalBatchManager::new(config, Some(3));

        let doc_id = DocumentId::new();
        let operation = WalOperation::AddPosting {
            document_id: doc_id,
            start: 0,
            length: 100,
            vector: vec![1.0, 2.0, 3.0],
        };

        // Add operation should not trigger flush for first operation
        let should_flush = manager.add_operation(operation).unwrap();
        assert!(!should_flush);

        let stats = manager.batch_stats();
        assert_eq!(stats.operation_count, 1);
        assert!(!stats.is_empty);
        assert!(stats.estimated_size_bytes > 0);

        // Flush batch manually
        use std::sync::atomic::{AtomicBool, Ordering};
        use std::sync::Arc;

        let transaction_written = Arc::new(AtomicBool::new(false));
        let transaction_written_clone = transaction_written.clone();

        let write_fn = move |transaction: &WalTransaction| -> Result<(), ShardexError> {
            assert_eq!(transaction.operations.len(), 1);
            transaction_written_clone.store(true, Ordering::SeqCst);
            Ok(())
        };

        let result = manager.flush_batch(write_fn).await.unwrap();
        assert!(result.is_some());
        assert!(transaction_written.load(Ordering::SeqCst));

        // Batch should be empty after flush
        let stats = manager.batch_stats();
        assert_eq!(stats.operation_count, 0);
        assert!(stats.is_empty);
    }

    #[tokio::test]
    async fn test_batch_manager_size_limits() {
        let config = BatchConfig {
            batch_write_interval_ms: 1000, // Long interval
            max_operations_per_batch: 2,   // Small limit for testing
            max_batch_size_bytes: 1024 * 1024,
            max_document_text_size: 10 * 1024 * 1024,
        };
        let mut manager = WalBatchManager::new(config, Some(3));

        let doc_id = DocumentId::new();
        let operation = WalOperation::AddPosting {
            document_id: doc_id,
            start: 0,
            length: 100,
            vector: vec![1.0, 2.0, 3.0],
        };

        // Add first operation - should not trigger flush
        let should_flush = manager.add_operation(operation.clone()).unwrap();
        assert!(!should_flush);

        // Add second operation - should trigger flush due to count limit
        let should_flush = manager.add_operation(operation).unwrap();
        let stats = manager.batch_stats();
        assert!(
            should_flush,
            "Second operation should trigger flush. Count: {}, should_flush: {}",
            stats.operation_count, should_flush
        );
    }

    #[tokio::test]
    async fn test_batch_config_defaults() {
        let config = BatchConfig::default();
        assert_eq!(config.batch_write_interval_ms, 100);
        assert_eq!(config.max_operations_per_batch, 1000);
        assert_eq!(config.max_batch_size_bytes, 1024 * 1024);
    }

    #[tokio::test]
    async fn test_empty_batch_flush() {
        let config = BatchConfig::default();
        let mut manager = WalBatchManager::new(config, None);

        let write_fn = |_: &WalTransaction| -> Result<(), ShardexError> {
            panic!("Write function should not be called for empty batch");
        };

        let result = manager.flush_batch(write_fn).await.unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_batch_manager_validation() {
        let config = BatchConfig::default();
        let mut manager = WalBatchManager::new(config, Some(3));

        let doc_id = DocumentId::new();

        // Invalid operation - wrong vector dimension
        let invalid_operation = WalOperation::AddPosting {
            document_id: doc_id,
            start: 0,
            length: 100,
            vector: vec![1.0, 2.0], // Wrong dimension - expected 3
        };

        let result = manager.add_operation(invalid_operation);
        assert!(result.is_err());

        if let Err(ShardexError::InvalidDimension { expected, actual }) = result {
            assert_eq!(expected, 3);
            assert_eq!(actual, 2);
        } else {
            panic!("Expected InvalidDimension error");
        }

        // Batch should still be empty after failed operation
        let stats = manager.batch_stats();
        assert!(stats.is_empty);
    }

    #[tokio::test]
    async fn test_batch_manager_with_wal_integration() {
        use crate::layout::DirectoryLayout;
        use crate::test_utils::TestEnvironment;
        use crate::wal::WalSegment;

        let _test_env = TestEnvironment::new("test_batch_wal_integration");
        let layout = DirectoryLayout::new(_test_env.path());
        let segment_path = layout.wal_segment_path(1);
        let capacity = 8192; // 8KB for testing

        // Create a WAL segment
        let segment = std::sync::Arc::new(WalSegment::create(1, segment_path, capacity).unwrap());

        // Create batch configuration
        let config = BatchConfig {
            batch_write_interval_ms: 50,
            max_operations_per_batch: 3,
            max_batch_size_bytes: 1024,
            max_document_text_size: 10 * 1024 * 1024,
        };

        let mut manager = WalBatchManager::new(config, Some(3));

        let doc_id = DocumentId::new();
        let operations = vec![
            WalOperation::AddPosting {
                document_id: doc_id,
                start: 0,
                length: 100,
                vector: vec![1.0, 2.0, 3.0],
            },
            WalOperation::RemoveDocument { document_id: doc_id },
        ];

        // Add operations to batch
        for operation in operations {
            let should_flush = manager.add_operation(operation).unwrap();
            assert!(!should_flush); // Should not trigger flush yet
        }

        // Create a write function that uses the WAL segment
        use std::sync::atomic::{AtomicUsize, Ordering};
        use std::sync::Arc;

        let write_count = Arc::new(AtomicUsize::new(0));
        let write_count_clone = write_count.clone();
        let segment_clone = segment.clone();

        let write_fn = move |transaction: &WalTransaction| -> Result<(), ShardexError> {
            segment_clone.append_transaction(transaction)?;
            segment_clone.sync()?;
            write_count_clone.fetch_add(1, Ordering::SeqCst);
            Ok(())
        };

        // Flush the batch
        let result = manager.flush_batch(write_fn).await.unwrap();
        assert!(result.is_some());
        assert_eq!(write_count.load(Ordering::SeqCst), 1);

        // Verify the transaction was written to the segment
        assert!(segment.write_pointer() > crate::wal::initial_write_position());
    }

    #[tokio::test]
    async fn test_atomic_batch_commits() {
        let config = BatchConfig::default();
        let mut manager = WalBatchManager::new(config, Some(3));

        let doc_id1 = DocumentId::new();
        let doc_id2 = DocumentId::new();

        // Create a batch with multiple operations affecting different documents
        let operations = vec![
            WalOperation::AddPosting {
                document_id: doc_id1,
                start: 0,
                length: 100,
                vector: vec![1.0, 2.0, 3.0],
            },
            WalOperation::AddPosting {
                document_id: doc_id2,
                start: 50,
                length: 75,
                vector: vec![4.0, 5.0, 6.0],
            },
            WalOperation::RemoveDocument { document_id: doc_id1 },
        ];

        for operation in operations {
            manager.add_operation(operation).unwrap();
        }

        // Test successful atomic commit
        use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
        use std::sync::Arc;

        let commit_called = Arc::new(AtomicBool::new(false));
        let operation_count = Arc::new(AtomicUsize::new(0));
        let commit_called_clone = commit_called.clone();
        let operation_count_clone = operation_count.clone();

        let write_fn = move |transaction: &WalTransaction| -> Result<(), ShardexError> {
            // Verify all operations are committed together
            commit_called_clone.store(true, Ordering::SeqCst);
            operation_count_clone.store(transaction.operations.len(), Ordering::SeqCst);

            // Verify transaction integrity
            transaction.validate(Some(3), 10 * 1024 * 1024)?;
            transaction.verify_checksum()?;

            Ok(())
        };

        let result = manager.flush_batch(write_fn).await.unwrap();
        assert!(result.is_some());
        assert!(commit_called.load(Ordering::SeqCst));
        assert_eq!(operation_count.load(Ordering::SeqCst), 3);

        // Batch should be empty after successful commit
        let stats = manager.batch_stats();
        assert!(stats.is_empty);
    }

    #[tokio::test]
    async fn test_failed_batch_commit_rollback() {
        let config = BatchConfig::default();
        let mut manager = WalBatchManager::new(config, Some(3));

        let doc_id = DocumentId::new();
        let operation = WalOperation::AddPosting {
            document_id: doc_id,
            start: 0,
            length: 100,
            vector: vec![1.0, 2.0, 3.0],
        };

        manager.add_operation(operation).unwrap();

        // Verify batch has one operation before flush
        assert_eq!(manager.batch_stats().operation_count, 1);

        // Test failed commit (simulate write failure)
        let write_fn = |_transaction: &WalTransaction| -> Result<(), ShardexError> {
            Err(ShardexError::Wal("Simulated write failure".to_string()))
        };

        let result = manager.flush_batch(write_fn).await;
        assert!(result.is_err());

        if let Err(ShardexError::Wal(msg)) = result {
            assert!(msg.contains("Simulated write failure"));
        } else {
            panic!("Expected Wal error");
        }

        // On flush failure, the batch should still be cleared to prevent infinite retry
        // This is the current behavior - in production you might want different behavior
        let stats = manager.batch_stats();
        assert!(stats.is_empty);
    }

    #[test]
    fn test_operation_estimated_size() {
        let doc_id = DocumentId::new();

        let add_op = WalOperation::AddPosting {
            document_id: doc_id,
            start: 0,
            length: 100,
            vector: vec![1.0, 2.0, 3.0, 4.0],
        };

        let remove_op = WalOperation::RemoveDocument { document_id: doc_id };

        // AddPosting: tag(1) + doc_id(16) + start(4) + length(4) + vec_len(4) + vec_data(16) = 45
        assert_eq!(add_op.estimated_serialized_size(), 45);

        // RemoveDocument: tag(1) + doc_id(16) = 17
        assert_eq!(remove_op.estimated_serialized_size(), 17);
    }

    #[test]
    fn test_transaction_validation() {
        let doc_id = DocumentId::new();

        // Valid transaction
        let valid_ops = vec![WalOperation::AddPosting {
            document_id: doc_id,
            start: 0,
            length: 100,
            vector: vec![1.0, 2.0, 3.0],
        }];
        let valid_transaction = WalTransaction::new(valid_ops).unwrap();
        assert!(valid_transaction
            .validate(Some(3), 10 * 1024 * 1024)
            .is_ok());

        // Invalid vector dimension
        assert!(valid_transaction
            .validate(Some(4), 10 * 1024 * 1024)
            .is_err());

        // Future timestamp should be rejected (create with manual timestamp)
        let future_time = SystemTime::now() + std::time::Duration::from_secs(3600); // 1 hour in future
        let future_ops = vec![WalOperation::RemoveDocument { document_id: doc_id }];
        let future_transaction =
            WalTransaction::with_id_and_timestamp(TransactionId::new(), future_time, future_ops).unwrap();
        assert!(future_transaction.validate(None, 10 * 1024 * 1024).is_err());
    }

    #[test]
    fn test_document_text_wal_operations() {
        let doc_id = DocumentId::new();
        let text = "The quick brown fox jumps over the lazy dog.".to_string();

        // Test StoreDocumentText operation
        let store_op = WalOperation::StoreDocumentText {
            document_id: doc_id,
            text: text.clone(),
        };

        assert_eq!(store_op.document_id(), doc_id);
        assert!(store_op.is_store_document_text());
        assert!(!store_op.is_delete_document_text());
        assert!(!store_op.is_add_posting());
        assert!(!store_op.is_remove_document());

        // Test DeleteDocumentText operation
        let delete_op = WalOperation::DeleteDocumentText { document_id: doc_id };

        assert_eq!(delete_op.document_id(), doc_id);
        assert!(delete_op.is_delete_document_text());
        assert!(!delete_op.is_store_document_text());
        assert!(!delete_op.is_add_posting());
        assert!(!delete_op.is_remove_document());

        // Test size estimation includes text content
        let store_size = store_op.estimated_serialized_size();
        let delete_size = delete_op.estimated_serialized_size();

        assert!(store_size > delete_size);
        assert!(store_size > text.len()); // Should include text plus overhead
    }

    #[test]
    fn test_document_text_operation_validation() {
        let doc_id = DocumentId::new();

        // Valid text operation
        let valid_text = "Hello, world!".to_string();
        let valid_op = WalOperation::StoreDocumentText {
            document_id: doc_id,
            text: valid_text,
        };
        assert!(valid_op.validate(None, 10 * 1024 * 1024).is_ok());

        // Test with configured max size
        let large_text = "x".repeat(1000);
        let large_op = WalOperation::StoreDocumentText {
            document_id: doc_id,
            text: large_text,
        };

        // Should be valid by default (no max size set)
        assert!(large_op.validate(None, 10 * 1024 * 1024).is_ok());

        // Delete operation should always be valid
        let delete_op = WalOperation::DeleteDocumentText { document_id: doc_id };
        assert!(delete_op.validate(None, 10 * 1024 * 1024).is_ok());
        assert!(delete_op.validate(Some(128), 10 * 1024 * 1024).is_ok());
    }

    #[test]
    fn test_document_text_transaction_atomicity() {
        let doc_id = DocumentId::new();
        let text = "Updated document content".to_string();

        // Create atomic replace transaction
        let operations = vec![
            WalOperation::StoreDocumentText {
                document_id: doc_id,
                text,
            },
            WalOperation::RemoveDocument { document_id: doc_id },
            WalOperation::AddPosting {
                document_id: doc_id,
                start: 0,
                length: 7,
                vector: vec![1.0, 2.0, 3.0],
            },
        ];

        let transaction = WalTransaction::new(operations).unwrap();
        assert_eq!(transaction.operation_count(), 3);

        let affected_docs = transaction.affected_document_ids();
        assert_eq!(affected_docs.len(), 1);
        assert_eq!(affected_docs[0], doc_id);

        // Should validate successfully
        assert!(transaction.validate(Some(3), 10 * 1024 * 1024).is_ok());

        // Should serialize and deserialize correctly
        let serialized = transaction.serialize().unwrap();
        let deserialized = WalTransaction::deserialize(&serialized).unwrap();
        assert_eq!(transaction.operations, deserialized.operations);
    }
}