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
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
//! Core operation implementations for Shardex API using the ApiThing pattern
//!
//! This module provides the core operations for Shardex implemented using the ApiOperation
//! trait pattern. Each operation works with the ShardexContext and their respective
//! parameter structures to provide type-safe, validated operations.
//!
//! # Operations
//!
//! - [`CreateIndex`]: Create a new Shardex index from configuration
//! - [`OpenIndex`]: Open an existing Shardex index from directory
//! - [`ValidateConfig`]: Validate a ShardexConfig without side effects
//! - [`AddPostings`]: Add document postings to an existing index
//! - [`Search`]: Perform similarity search operations
//! - [`Flush`]: Flush pending operations to disk
//! - [`GetStats`]: Retrieve index statistics
//!
//! # Usage Examples
//!
//! ```rust
//! use shardex::api::{ShardexContext, CreateIndexParams};
//! use shardex::api::operations::CreateIndex;
//! use apithing::ApiOperation;
//!
//! let mut context = ShardexContext::new();
//! let params = CreateIndexParams::builder()
//!     .directory_path("./doc_test_module".into())
//!     .vector_size(384)
//!     .build()?;
//!
//! let result = CreateIndex::execute(&mut context, &params)?;
//! assert!(context.is_initialized());
//! # std::fs::remove_dir_all("./doc_test_module").ok();
//! # Ok::<(), shardex::error::ShardexError>(())
//! ```

use crate::api::context::ShardexContext;
use std::sync::OnceLock;

/// Global shared runtime for executing async operations synchronously
static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();

/// Execute an async operation synchronously using a shared runtime
///
/// NOTE: This function cannot be called from within a tokio runtime context
/// as it will cause a "Cannot start a runtime from within a runtime" panic.
/// All API operations are synchronous and should be called from synchronous contexts.
fn execute_sync<F, T>(future: F) -> Result<T, ShardexError>
where
    F: std::future::Future<Output = Result<T, ShardexError>>,
{
    let rt = RUNTIME.get_or_init(|| tokio::runtime::Runtime::new().expect("Failed to create shared Tokio runtime"));
    rt.block_on(future)
}

/// Statistics returned from batch operations
///
/// Contains performance metrics and statistics from batch processing operations.
#[derive(Debug, Clone)]
pub struct BatchStats {
    /// Number of postings added in this batch
    pub postings_added: usize,
    /// Time taken to process this batch
    pub processing_time: Duration,
    /// Throughput in documents per second for this batch
    pub throughput_docs_per_sec: f64,
    /// Number of operations flushed (if flush was performed)
    pub operations_flushed: u64,
}

impl BatchStats {
    /// Create new batch statistics
    pub fn new(postings_added: usize, processing_time: Duration, operations_flushed: u64) -> Self {
        let throughput_docs_per_sec = if processing_time.as_secs_f64() > 0.0 {
            postings_added as f64 / processing_time.as_secs_f64()
        } else {
            0.0
        };

        Self {
            postings_added,
            processing_time,
            throughput_docs_per_sec,
            operations_flushed,
        }
    }
}

/// Performance statistics from the system
///
/// Contains comprehensive performance metrics including timing, throughput, and memory usage.
#[derive(Debug, Clone)]
pub struct PerformanceStats {
    /// Total number of operations performed
    pub total_operations: u64,
    /// Average latency across all operations
    pub average_latency: Duration,
    /// Overall system throughput (operations per second)
    pub throughput: f64,
    /// Current memory usage in bytes
    pub memory_usage: u64,
    /// Optional detailed performance metrics
    pub detailed_metrics: Option<DetailedPerformanceMetrics>,
}

impl PerformanceStats {
    /// Create basic performance statistics
    pub fn basic(total_operations: u64, average_latency: Duration, throughput: f64, memory_usage: u64) -> Self {
        Self {
            total_operations,
            average_latency,
            throughput,
            memory_usage,
            detailed_metrics: None,
        }
    }

    /// Create performance statistics with detailed metrics
    pub fn with_details(
        total_operations: u64,
        average_latency: Duration,
        throughput: f64,
        memory_usage: u64,
        detailed_metrics: DetailedPerformanceMetrics,
    ) -> Self {
        Self {
            total_operations,
            average_latency,
            throughput,
            memory_usage,
            detailed_metrics: Some(detailed_metrics),
        }
    }
}

/// Detailed performance metrics breakdown
///
/// Contains fine-grained performance metrics for different operation types.
#[derive(Debug, Clone)]
pub struct DetailedPerformanceMetrics {
    /// Time spent on indexing operations
    pub index_time: Duration,
    /// Time spent on flush operations
    pub flush_time: Duration,
    /// Time spent on search operations
    pub search_time: Duration,
    /// Breakdown of operations by type and count
    pub operations_breakdown: HashMap<String, u64>,
}

impl DetailedPerformanceMetrics {
    /// Create new detailed performance metrics
    pub fn new() -> Self {
        Self {
            index_time: Duration::ZERO,
            flush_time: Duration::ZERO,
            search_time: Duration::ZERO,
            operations_breakdown: HashMap::new(),
        }
    }

    /// Add operation timing to the detailed metrics
    pub fn record_operation(&mut self, operation_type: &str, duration: Duration) {
        *self
            .operations_breakdown
            .entry(operation_type.to_string())
            .or_insert(0) += 1;

        match operation_type {
            "index" | "add_postings" | "batch_add" => self.index_time += duration,
            "flush" => self.flush_time += duration,
            "search" => self.search_time += duration,
            _ => {} // Other operation types don't affect the main timing categories
        }
    }
}

impl Default for DetailedPerformanceMetrics {
    fn default() -> Self {
        Self::new()
    }
}

/// Statistics returned from incremental operations
///
/// Contains metrics from incremental posting operations with optional batch tracking.
#[derive(Debug, Clone)]
pub struct IncrementalStats {
    /// Optional batch identifier associated with this operation
    pub batch_id: Option<String>,
    /// Number of postings added in this incremental operation
    pub postings_added: usize,
    /// Total number of postings in the index after this operation
    pub total_postings: usize,
    /// Time taken to process this incremental operation
    pub processing_time: Duration,
}

impl IncrementalStats {
    /// Create new incremental statistics
    pub fn new(
        batch_id: Option<String>,
        postings_added: usize,
        total_postings: usize,
        processing_time: Duration,
    ) -> Self {
        Self {
            batch_id,
            postings_added,
            total_postings,
            processing_time,
        }
    }
}

/// Statistics returned from document removal operations
///
/// Contains metrics from batch document removal operations.
#[derive(Debug, Clone)]
pub struct RemovalStats {
    /// Number of documents that were successfully removed
    pub documents_removed: usize,
    /// Number of document IDs that were not found (and therefore not removed)
    pub documents_not_found: usize,
    /// Time taken to complete the removal operation
    pub processing_time: Duration,
}

impl RemovalStats {
    /// Create new removal statistics
    pub fn new(documents_removed: usize, documents_not_found: usize, processing_time: Duration) -> Self {
        Self {
            documents_removed,
            documents_not_found,
            processing_time,
        }
    }

    /// Get the total number of document IDs that were processed
    pub fn total_processed(&self) -> usize {
        self.documents_removed + self.documents_not_found
    }
}

/// Statistics returned from batch document text operations
///
/// Contains performance metrics and statistics from batch document text storage operations.
#[derive(Debug, Clone)]
pub struct BatchDocumentTextStats {
    /// Number of documents stored in this batch
    pub documents_stored: usize,
    /// Total size of text content across all documents
    pub total_text_size: usize,
    /// Time taken to process this batch
    pub processing_time: Duration,
    /// Average document size in bytes
    pub average_document_size: usize,
    /// Total number of postings stored across all documents
    pub total_postings: usize,
    /// Number of operations flushed (if flush was performed)
    pub operations_flushed: u64,
}

impl BatchDocumentTextStats {
    /// Create new batch document text statistics
    pub fn new(
        documents_stored: usize,
        total_text_size: usize,
        processing_time: Duration,
        total_postings: usize,
        operations_flushed: u64,
    ) -> Self {
        let average_document_size = if documents_stored > 0 {
            total_text_size / documents_stored
        } else {
            0
        };

        Self {
            documents_stored,
            total_text_size,
            processing_time,
            average_document_size,
            total_postings,
            operations_flushed,
        }
    }

    /// Calculate throughput in documents per second
    pub fn throughput_docs_per_sec(&self) -> f64 {
        if self.processing_time.as_secs_f64() > 0.0 {
            self.documents_stored as f64 / self.processing_time.as_secs_f64()
        } else {
            0.0
        }
    }

    /// Calculate throughput in bytes per second
    pub fn throughput_bytes_per_sec(&self) -> f64 {
        if self.processing_time.as_secs_f64() > 0.0 {
            self.total_text_size as f64 / self.processing_time.as_secs_f64()
        } else {
            0.0
        }
    }
}

/// Helper type for working with search results and text
///
/// Combines search results with document text and extracted snippets.
#[derive(Debug, Clone)]
pub struct SearchResultWithText {
    /// The original search result
    pub search_result: SearchResult,
    /// Full document text if available
    pub document_text: Option<String>,
    /// Extracted text snippet corresponding to the search result
    pub snippet: Option<String>,
}
use crate::api::parameters::{
    AddPostingsParams, BatchAddPostingsParams, BatchStoreDocumentTextParams, CreateIndexParams, ExtractSnippetParams,
    FlushParams, GetDocumentTextParams, GetPerformanceStatsParams, GetStatsParams, IncrementalAddParams,
    OpenIndexParams, RemoveDocumentsParams, SearchParams, StoreDocumentTextParams, ValidateConfigParams,
};
use crate::config::ShardexConfig;
use crate::error::ShardexError;
use crate::shardex::{Shardex, ShardexImpl};
use crate::structures::{FlushStats, IndexStats, SearchResult};
use apithing::ApiOperation;
use std::collections::HashMap;
use std::time::Duration;

/// Create Index operation
///
/// Creates a new Shardex index using the provided configuration parameters.
/// The created index is stored in the context for use by other operations.
///
/// # Context Requirements
///
/// - Context must not already have an initialized index
/// - Configuration parameters must be valid
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, CreateIndexParams};
/// use shardex::api::operations::CreateIndex;
/// use apithing::ApiOperation;
///
/// let mut context = ShardexContext::new();
/// let params = CreateIndexParams::builder()
///     .directory_path("./doc_test_index".into())
///     .vector_size(768)
///     .shard_size(50000)
///     .build()?;
///
/// CreateIndex::execute(&mut context, &params)?;
/// assert!(context.is_initialized());
/// # std::fs::remove_dir_all("./doc_test_index").ok();
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct CreateIndex;

impl ApiOperation<ShardexContext, CreateIndexParams> for CreateIndex {
    type Output = ();
    type Error = ShardexError;

    fn execute(context: &mut ShardexContext, parameters: &CreateIndexParams) -> Result<Self::Output, Self::Error> {
        // Validate parameters before proceeding
        parameters.validate()?;

        // Check if context already has an initialized index
        if context.is_initialized() {
            return Err(ShardexError::config_error(
                "context",
                "index already initialized",
                "Use a new context or clear the existing index before creating a new one",
            ));
        }

        // Create ShardexConfig from parameters
        let config = ShardexConfig::new()
            .directory_path(&parameters.directory_path)
            .vector_size(parameters.vector_size)
            .shard_size(parameters.shard_size)
            .batch_write_interval_ms(parameters.batch_write_interval_ms)
            .wal_segment_size(parameters.wal_segment_size)
            .bloom_filter_size(parameters.bloom_filter_size);

        // Create the index using available runtime
        let index = execute_sync(ShardexImpl::create(config))?;

        // Store in context
        context.set_index(index);

        Ok(())
    }
}

/// Open Index operation
///
/// Opens an existing Shardex index from the specified directory. The index
/// configuration is read from the stored metadata and applied to the context.
///
/// # Context Requirements
///
/// - Context must not already have an initialized index
/// - Directory must contain a valid Shardex index
///
/// # Example
///
/// ```rust
/// use shardex::api::ShardexContext;
/// use shardex::api::parameters::OpenIndexParams;
/// use shardex::api::operations::OpenIndex;
/// use apithing::ApiOperation;
/// use std::path::PathBuf;
///
/// let mut context = ShardexContext::new();
/// let params = OpenIndexParams::new(PathBuf::from("./existing_index"));
///
/// // This would work if ./existing_index contains a valid index
/// // let result = OpenIndex::execute(&mut context, &params);
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct OpenIndex;

impl ApiOperation<ShardexContext, OpenIndexParams> for OpenIndex {
    type Output = ();
    type Error = ShardexError;

    fn execute(context: &mut ShardexContext, parameters: &OpenIndexParams) -> Result<Self::Output, Self::Error> {
        // Validate parameters before proceeding
        parameters.validate()?;

        // Check if context already has an initialized index
        if context.is_initialized() {
            return Err(ShardexError::config_error(
                "context",
                "index already initialized",
                "Use a new context or clear the existing index before opening another one",
            ));
        }

        // Check if directory exists
        if !parameters.directory_path.exists() {
            return Err(ShardexError::config_error(
                "directory_path",
                format!("directory does not exist: {:?}", parameters.directory_path),
                "Ensure the directory path points to an existing index directory",
            ));
        }

        // Check if directory appears to be an index (has some expected files/structure)
        if !parameters.directory_path.is_dir() {
            return Err(ShardexError::config_error(
                "directory_path",
                format!("path is not a directory: {:?}", parameters.directory_path),
                "Provide a path to a directory containing a Shardex index",
            ));
        }

        // Open the index using available runtime
        let index = execute_sync(ShardexImpl::open(&parameters.directory_path))?;

        // Store in context
        context.set_index(index);

        Ok(())
    }
}

/// Validate Config operation
///
/// Validates a ShardexConfig without creating or opening an index. This is
/// useful for testing configuration validity before performing operations.
///
/// # Context Requirements
///
/// - No context requirements (operates independently)
///
/// # Example
///
/// ```rust
/// use shardex::api::ShardexContext;
/// use shardex::api::parameters::ValidateConfigParams;
/// use shardex::api::operations::ValidateConfig;
/// use shardex::ShardexConfig;
/// use apithing::ApiOperation;
///
/// let mut context = ShardexContext::new();
/// let config = ShardexConfig::new().vector_size(384).shard_size(10000);
/// let params = ValidateConfigParams::new(config);
///
/// let is_valid = ValidateConfig::execute(&mut context, &params)?;
/// assert!(is_valid);
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct ValidateConfig;

impl ApiOperation<ShardexContext, ValidateConfigParams> for ValidateConfig {
    type Output = bool;
    type Error = ShardexError;

    fn execute(_context: &mut ShardexContext, parameters: &ValidateConfigParams) -> Result<Self::Output, Self::Error> {
        // Validate the parameters (this is always Ok for ValidateConfigParams)
        parameters.validate()?;

        // Validate the contained configuration
        match parameters.get_config().validate() {
            Ok(()) => Ok(true),
            Err(_) => Ok(false), // Return false instead of propagating the error
        }
    }
}

/// Add Postings operation
///
/// Adds a collection of document postings to the index. The index must already
/// be initialized in the context.
///
/// # Context Requirements
///
/// - Context must have an initialized index
/// - All postings must have consistent vector dimensions
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, AddPostingsParams, CreateIndexParams};
/// use shardex::api::operations::{AddPostings, CreateIndex};
/// use shardex::{Posting, DocumentId};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # let create_params = CreateIndexParams::builder()
/// #     .directory_path("./doc_test_add_postings".into())
/// #     .vector_size(384)
/// #     .shard_size(50000)
/// #     .build()?;
/// # CreateIndex::execute(&mut context, &create_params)?;
/// let postings = vec![
///     Posting {
///         document_id: DocumentId::from_raw(1),
///         start: 0,
///         length: 100,
///         vector: vec![0.1; 384],
///     }
/// ];
///
/// let params = AddPostingsParams::new(postings)?;
/// AddPostings::execute(&mut context, &params)?;
/// # std::fs::remove_dir_all("./doc_test_add_postings").ok();
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct AddPostings;

impl ApiOperation<ShardexContext, AddPostingsParams> for AddPostings {
    type Output = ();
    type Error = ShardexError;

    fn execute(context: &mut ShardexContext, parameters: &AddPostingsParams) -> Result<Self::Output, Self::Error> {
        // Validate parameters
        parameters.validate()?;

        // Get mutable reference to the index
        let index = context.get_index_mut().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before adding postings",
            )
        })?;

        // Add postings to the index using available runtime
        execute_sync(index.add_postings(parameters.postings.clone()))?;

        Ok(())
    }
}

/// Search operation
///
/// Performs similarity search on the index using the provided query vector.
/// Returns the top-k most similar results.
///
/// # Context Requirements
///
/// - Context must have an initialized index
/// - Query vector dimension must match index vector dimension
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, SearchParams, CreateIndexParams};
/// use shardex::api::operations::{Search, CreateIndex};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # let create_params = CreateIndexParams::builder()
/// #     .directory_path("./doc_test_search".into())
/// #     .vector_size(384)
/// #     .shard_size(50000)
/// #     .build()?;
/// # CreateIndex::execute(&mut context, &create_params)?;
/// let params = SearchParams::builder()
///     .query_vector(vec![0.1; 384])
///     .k(10)
///     .slop_factor(Some(5))
///     .build()?;
///
/// let results = Search::execute(&mut context, &params)?;
/// println!("Found {} results", results.len());
/// # std::fs::remove_dir_all("./doc_test_search").ok();
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct Search;

impl ApiOperation<ShardexContext, SearchParams> for Search {
    type Output = Vec<SearchResult>;
    type Error = ShardexError;

    fn execute(context: &mut ShardexContext, parameters: &SearchParams) -> Result<Self::Output, Self::Error> {
        // Validate parameters
        parameters.validate()?;

        // Get reference to the index
        let index = context.get_index().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before performing searches",
            )
        })?;

        // Perform the search using available runtime
        let results = execute_sync(index.search(
            &parameters.query_vector,
            parameters.k,
            parameters.slop_factor.map(|s| s as usize),
        ))?;

        Ok(results)
    }
}

/// Flush operation
///
/// Flushes pending operations to disk and optionally returns statistics
/// about the flush operation.
///
/// # Context Requirements
///
/// - Context must have an initialized index
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, FlushParams, CreateIndexParams};
/// use shardex::api::operations::{Flush, CreateIndex};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # let create_params = CreateIndexParams::builder()
/// #     .directory_path("./doc_test_flush".into())
/// #     .vector_size(384)
/// #     .shard_size(50000)
/// #     .build()?;
/// # CreateIndex::execute(&mut context, &create_params)?;
/// let params = FlushParams::with_stats();
/// let stats = Flush::execute(&mut context, &params)?;
///
/// if let Some(stats) = stats {
///     println!("Operations flushed: {}", stats.operations_applied);
/// }
/// # std::fs::remove_dir_all("./doc_test_flush").ok();
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct Flush;

impl ApiOperation<ShardexContext, FlushParams> for Flush {
    type Output = Option<FlushStats>;
    type Error = ShardexError;

    fn execute(context: &mut ShardexContext, parameters: &FlushParams) -> Result<Self::Output, Self::Error> {
        // Validate parameters (always succeeds for FlushParams)
        parameters.validate()?;

        // Get mutable reference to the index
        let index = context.get_index_mut().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before performing flush operations",
            )
        })?;

        // Perform flush with or without stats based on parameters
        if parameters.with_stats {
            let stats = execute_sync(index.flush_with_stats())?;
            Ok(Some(stats))
        } else {
            execute_sync(index.flush())?;
            Ok(None)
        }
    }
}

/// Get Statistics operation
///
/// Retrieves current index statistics including shard counts, memory usage,
/// and performance metrics.
///
/// # Context Requirements
///
/// - Context must have an initialized index
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, GetStatsParams, CreateIndexParams};
/// use shardex::api::operations::{GetStats, CreateIndex};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # let create_params = CreateIndexParams::builder()
/// #     .directory_path("./doc_test_stats".into())
/// #     .vector_size(384)
/// #     .shard_size(50000)
/// #     .build()?;
/// # CreateIndex::execute(&mut context, &create_params)?;
/// let params = GetStatsParams::new();
/// let stats = GetStats::execute(&mut context, &params)?;
///
/// println!("Total postings: {}", stats.total_postings);
/// println!("Memory usage: {:.2} MB", stats.memory_usage as f64 / 1024.0 / 1024.0);
/// # std::fs::remove_dir_all("./doc_test_stats").ok();
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct GetStats;

impl ApiOperation<ShardexContext, GetStatsParams> for GetStats {
    type Output = IndexStats;
    type Error = ShardexError;

    fn execute(context: &mut ShardexContext, parameters: &GetStatsParams) -> Result<Self::Output, Self::Error> {
        // Validate parameters (always succeeds for GetStatsParams)
        parameters.validate()?;

        // Get reference to the index
        let index = context.get_index().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before retrieving statistics",
            )
        })?;

        // Get stats from the index using available runtime
        let stats = execute_sync(index.stats())?;

        Ok(stats)
    }
}

/// Batch Add Postings operation
///
/// Adds a collection of document postings to the index in a batch with optional
/// performance tracking and immediate flushing capabilities.
///
/// # Context Requirements
///
/// - Context must have an initialized index
/// - All postings must have consistent vector dimensions
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, BatchAddPostingsParams, CreateIndexParams};
/// use shardex::api::operations::{BatchAddPostings, CreateIndex};
/// use shardex::{Posting, DocumentId};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # let create_params = CreateIndexParams::builder()
/// #     .directory_path("./doc_test_batch_add".into())
/// #     .vector_size(384)
/// #     .shard_size(50000)
/// #     .build()?;
/// # CreateIndex::execute(&mut context, &create_params)?;
/// let postings = vec![
///     Posting {
///         document_id: DocumentId::from_raw(1),
///         start: 0,
///         length: 100,
///         vector: vec![0.1; 384],
///     }
/// ];
///
/// let params = BatchAddPostingsParams::with_performance_tracking(postings)?;
/// let stats = BatchAddPostings::execute(&mut context, &params)?;
/// println!("Added {} postings in {:?}", stats.postings_added, stats.processing_time);
/// # std::fs::remove_dir_all("./doc_test_batch_add").ok();
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct BatchAddPostings;

impl ApiOperation<ShardexContext, BatchAddPostingsParams> for BatchAddPostings {
    type Output = BatchStats;
    type Error = ShardexError;

    fn execute(context: &mut ShardexContext, parameters: &BatchAddPostingsParams) -> Result<Self::Output, Self::Error> {
        use std::time::Instant;

        // Validate parameters
        parameters.validate()?;

        // Get mutable reference to the index
        let index = context.get_index_mut().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before performing batch operations",
            )
        })?;

        let start_time = Instant::now();

        // Add postings to the index
        execute_sync(index.add_postings(parameters.postings.clone()))?;

        let mut operations_flushed = 0;

        // Flush immediately if requested
        if parameters.flush_immediately {
            let flush_stats = execute_sync(index.flush_with_stats())?;
            operations_flushed = flush_stats.operations_applied;
        }

        let processing_time = start_time.elapsed();

        // Create and return batch statistics
        let stats = BatchStats::new(parameters.postings.len(), processing_time, operations_flushed as u64);

        // Record performance metrics in context if tracking is active
        if context.is_performance_tracking_active() {
            context.record_operation("BatchAddPostings", processing_time);
        }

        Ok(stats)
    }
}

/// Get Performance Stats operation
///
/// Retrieves current performance statistics from the system, with optional
/// detailed metrics including operation breakdowns and timing information.
///
/// # Context Requirements
///
/// - Context must have an initialized index (for basic stats)
/// - Performance monitoring may need to be enabled for detailed metrics
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, GetPerformanceStatsParams, CreateIndexParams};
/// use shardex::api::operations::{GetPerformanceStats, CreateIndex};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # let create_params = CreateIndexParams::builder()
/// #     .directory_path("./doc_test_perf_stats".into())
/// #     .vector_size(384)
/// #     .build()?;
/// # CreateIndex::execute(&mut context, &create_params)?;
/// let params = GetPerformanceStatsParams::detailed();
/// let stats = GetPerformanceStats::execute(&mut context, &params)?;
/// println!("Total operations: {}, Memory: {} MB",
///          stats.total_operations,
///          stats.memory_usage / 1024 / 1024);
/// # std::fs::remove_dir_all("./doc_test_perf_stats").ok();
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct GetPerformanceStats;

impl ApiOperation<ShardexContext, GetPerformanceStatsParams> for GetPerformanceStats {
    type Output = PerformanceStats;
    type Error = ShardexError;

    fn execute(
        context: &mut ShardexContext,
        parameters: &GetPerformanceStatsParams,
    ) -> Result<Self::Output, Self::Error> {
        // Validate parameters (always succeeds for GetPerformanceStatsParams)
        parameters.validate()?;

        // Get reference to the index for basic stats
        let index = context.get_index().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before retrieving performance statistics",
            )
        })?;

        // Get basic index statistics
        let index_stats = execute_sync(index.stats())?;

        // Collect performance metrics from context if available
        let (total_operations, overall_latency, throughput) = if context.is_performance_tracking_active() {
            (
                context.get_total_operations(),
                context.get_average_latency(),
                context.get_throughput(),
            )
        } else {
            // Fallback to basic metrics when tracking is not active
            (0, Duration::default(), 0.0)
        };

        // Use actual performance metrics from context when available, otherwise fall back to index stats
        let final_total_operations = if total_operations > 0 {
            total_operations
        } else {
            u64::try_from(index_stats.total_postings).unwrap_or(u64::MAX)
        };

        let memory_usage = u64::try_from(index_stats.memory_usage).unwrap_or(u64::MAX); // Handle potential overflow on 32-bit systems

        // Use context performance data if available, otherwise fall back to index stats
        let final_average_latency = if overall_latency > Duration::default() {
            overall_latency
        } else {
            index_stats.search_latency_p50 // Use 50th percentile as fallback
        };

        // Use context throughput if available, otherwise calculate from index stats
        let final_throughput = if throughput > 0.0 {
            throughput
        } else if final_total_operations > 0 {
            // Estimate operations per second based on average latency
            1000.0 / final_average_latency.as_millis().max(1) as f64
        } else {
            0.0 // No operations processed yet
        };

        let stats = if parameters.include_detailed {
            // Create detailed metrics (placeholder implementation)
            let detailed = DetailedPerformanceMetrics::new();
            PerformanceStats::with_details(
                final_total_operations,
                final_average_latency,
                final_throughput,
                memory_usage,
                detailed,
            )
        } else {
            PerformanceStats::basic(
                final_total_operations,
                final_average_latency,
                final_throughput,
                memory_usage,
            )
        };

        Ok(stats)
    }
}

/// Incremental Add operation
///
/// Adds postings incrementally with optional batch tracking, suitable for
/// streaming or continuous indexing scenarios.
///
/// # Context Requirements
///
/// - Context must have an initialized index
/// - All postings must have consistent vector dimensions
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, IncrementalAddParams, CreateIndexParams};
/// use shardex::api::operations::{IncrementalAdd, CreateIndex};
/// use shardex::{Posting, DocumentId};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # let create_params = CreateIndexParams::builder()
/// #     .directory_path("./doc_test_incremental".into())
/// #     .vector_size(384)
/// #     .build()?;
/// # CreateIndex::execute(&mut context, &create_params)?;
/// let postings = vec![
///     Posting {
///         document_id: DocumentId::from_raw(1),
///         start: 0,
///         length: 100,
///         vector: vec![0.1; 384],
///     }
/// ];
///
/// let params = IncrementalAddParams::with_batch_id(postings, "batch_001".to_string())?;
/// let stats = IncrementalAdd::execute(&mut context, &params)?;
/// println!("Added {} postings, total now: {}", stats.postings_added, stats.total_postings);
/// # std::fs::remove_dir_all("./doc_test_incremental").ok();
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct IncrementalAdd;

impl ApiOperation<ShardexContext, IncrementalAddParams> for IncrementalAdd {
    type Output = IncrementalStats;
    type Error = ShardexError;

    fn execute(context: &mut ShardexContext, parameters: &IncrementalAddParams) -> Result<Self::Output, Self::Error> {
        use std::time::Instant;

        // Validate parameters
        parameters.validate()?;

        // Get mutable reference to the index
        let index = context.get_index_mut().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before performing incremental operations",
            )
        })?;

        let start_time = Instant::now();

        // Add postings to the index
        execute_sync(index.add_postings(parameters.postings.clone()))?;

        let processing_time = start_time.elapsed();

        // Create and return incremental statistics
        // For incremental operations, total_postings represents the cumulative postings added
        let stats = IncrementalStats::new(
            parameters.batch_id.clone(),
            parameters.postings.len(),
            parameters.postings.len(), // Use the current batch size for total
            processing_time,
        );

        Ok(stats)
    }
}

/// Remove Documents operation
///
/// Removes multiple documents from the index in a batch operation using WAL-based processing.
/// Documents are queued for removal and processed asynchronously during the next flush cycle.
///
/// # Important Notes
///
/// - Documents are queued for removal via Write-Ahead Log (WAL) and processed asynchronously
/// - The operation succeeds if documents are successfully queued, regardless of whether
///   the documents actually exist in the index
/// - Individual document success/failure tracking is not available due to WAL-based processing
/// - Actual removal occurs during the next flush/commit cycle
/// - All queued documents are reported as "removed" in the statistics
///
/// # Context Requirements
///
/// - Context must have an initialized index
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, RemoveDocumentsParams, CreateIndexParams};
/// use shardex::api::operations::{RemoveDocuments, CreateIndex};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # let create_params = CreateIndexParams::builder()
/// #     .directory_path("./doc_test_remove".into())
/// #     .vector_size(384)
/// #     .build()?;
/// # CreateIndex::execute(&mut context, &create_params)?;
/// let params = RemoveDocumentsParams::new(vec![1, 2, 3])?;
/// let stats = RemoveDocuments::execute(&mut context, &params)?;
/// println!("Queued {} documents for removal (actual removal occurs during next flush)",
///          stats.documents_removed);
/// # std::fs::remove_dir_all("./doc_test_remove").ok();
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct RemoveDocuments;

impl ApiOperation<ShardexContext, RemoveDocumentsParams> for RemoveDocuments {
    type Output = RemovalStats;
    type Error = ShardexError;

    fn execute(context: &mut ShardexContext, parameters: &RemoveDocumentsParams) -> Result<Self::Output, Self::Error> {
        use std::time::Instant;

        // Validate parameters
        parameters.validate()?;

        // Get mutable reference to the index
        let index = context.get_index_mut().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before performing removal operations",
            )
        })?;

        let start_time = Instant::now();

        // Remove documents from the index
        // Note: The underlying remove_documents method uses WAL-based processing and returns
        // Result<(), ShardexError>. It doesn't provide individual document success/failure counts.
        // If the operation fails, it fails entirely; if it succeeds, all documents are queued
        // for removal in the WAL and will be processed during the next flush/commit cycle.
        execute_sync(index.remove_documents(parameters.document_ids.clone()))?;

        let processing_time = start_time.elapsed();

        // Since the underlying method doesn't provide granular success/failure information,
        // we report all documents as successfully queued for removal.
        // The actual removal happens asynchronously during WAL processing.
        let documents_removed = parameters.document_ids.len();
        let documents_not_found = 0; // Cannot determine without individual document queries

        // Create and return removal statistics
        let stats = RemovalStats::new(documents_removed, documents_not_found, processing_time);

        Ok(stats)
    }
}

/// Store Document Text operation
///
/// Stores document text along with associated postings atomically. This operation
/// replaces any existing document content and postings for the specified document ID.
///
/// # Context Requirements
///
/// - Context must have an initialized index
/// - Index must be configured with text storage enabled (max_document_text_size > 0)
/// - All postings must have consistent vector dimensions
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, StoreDocumentTextParams, CreateIndexParams};
/// use shardex::api::operations::{StoreDocumentText, CreateIndex};
/// use shardex::{Posting, DocumentId};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # let create_params = CreateIndexParams::builder()
/// #     .directory_path("./doc_test_store_text".into())
/// #     .vector_size(384)
/// #     .build()?;
/// # CreateIndex::execute(&mut context, &create_params)?;
/// let doc_id = DocumentId::from_raw(1);
/// let text = "The quick brown fox jumps over the lazy dog.".to_string();
/// let posting = Posting {
///     document_id: doc_id,
///     start: 0,
///     length: 9,
///     vector: vec![0.1; 384],
/// };
///
/// let params = StoreDocumentTextParams::new(doc_id, text, vec![posting])?;
/// StoreDocumentText::execute(&mut context, &params)?;
/// # std::fs::remove_dir_all("./doc_test_store_text").ok();
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct StoreDocumentText;

impl ApiOperation<ShardexContext, StoreDocumentTextParams> for StoreDocumentText {
    type Output = ();
    type Error = ShardexError;

    fn execute(
        context: &mut ShardexContext,
        parameters: &StoreDocumentTextParams,
    ) -> Result<Self::Output, Self::Error> {
        // Validate parameters
        parameters.validate()?;

        // Get mutable reference to the index
        let index = context.get_index_mut().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before storing document text",
            )
        })?;

        // Store document text and postings atomically using available runtime
        execute_sync(index.replace_document_with_postings(
            parameters.document_id,
            parameters.text.clone(),
            parameters.postings.clone(),
        ))?;

        Ok(())
    }
}

/// Get Document Text operation
///
/// Retrieves the full text content of a document by its ID. The document
/// must have been stored with text storage enabled.
///
/// # Context Requirements
///
/// - Context must have an initialized index
/// - Index must be configured with text storage enabled
/// - Document must exist in the index
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, GetDocumentTextParams};
/// use shardex::api::operations::GetDocumentText;
/// use shardex::{DocumentId};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # // Assume context is initialized with a document stored
/// let doc_id = DocumentId::from_raw(1);
/// let params = GetDocumentTextParams::new(doc_id);
///
/// // let text = GetDocumentText::execute(&mut context, &params)?;
/// // println!("Document text: {}", text);
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct GetDocumentText;

impl ApiOperation<ShardexContext, GetDocumentTextParams> for GetDocumentText {
    type Output = String;
    type Error = ShardexError;

    fn execute(context: &mut ShardexContext, parameters: &GetDocumentTextParams) -> Result<Self::Output, Self::Error> {
        // Validate parameters (always succeeds)
        parameters.validate()?;

        // Get reference to the index
        let index = context.get_index().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before retrieving document text",
            )
        })?;

        // Retrieve document text using available runtime
        let text = execute_sync(index.get_document_text(parameters.document_id))?;

        Ok(text)
    }
}

/// Extract Snippet operation
///
/// Extracts a text snippet from a document based on start position and length.
/// This is typically used to extract text corresponding to search result postings.
///
/// # Context Requirements
///
/// - Context must have an initialized index
/// - Index must be configured with text storage enabled
/// - Document must exist and snippet range must be valid
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, ExtractSnippetParams};
/// use shardex::api::operations::ExtractSnippet;
/// use shardex::{Posting, DocumentId};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # // Assume context is initialized with a document stored
/// let posting = Posting {
///     document_id: DocumentId::from_raw(1),
///     start: 4,
///     length: 5,
///     vector: vec![0.1; 384],
/// };
///
/// let params = ExtractSnippetParams::from_posting(&posting);
/// // let snippet = ExtractSnippet::execute(&mut context, &params)?;
/// // println!("Snippet: '{}'", snippet);
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct ExtractSnippet;

impl ApiOperation<ShardexContext, ExtractSnippetParams> for ExtractSnippet {
    type Output = String;
    type Error = ShardexError;

    fn execute(context: &mut ShardexContext, parameters: &ExtractSnippetParams) -> Result<Self::Output, Self::Error> {
        // Validate parameters
        parameters.validate()?;

        // Get reference to the index
        let index = context.get_index().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before extracting text snippets",
            )
        })?;

        // Create a posting from the parameters for text extraction
        let posting = crate::structures::Posting {
            document_id: parameters.document_id,
            start: parameters.start,
            length: parameters.length,
            vector: vec![], // Empty vector - not used for text extraction
        };

        // Extract text using the posting
        let snippet = execute_sync(index.extract_text(&posting))?;

        Ok(snippet)
    }
}

/// Batch Store Document Text operation
///
/// Stores multiple documents with text and postings in a single batch operation.
/// This provides better performance for bulk document storage scenarios.
///
/// # Context Requirements
///
/// - Context must have an initialized index
/// - Index must be configured with text storage enabled
/// - All postings across all documents must have consistent vector dimensions
///
/// # Example
///
/// ```rust
/// use shardex::api::{ShardexContext, BatchStoreDocumentTextParams, CreateIndexParams};
/// use shardex::api::operations::{BatchStoreDocumentText, CreateIndex};
/// use shardex::api::parameters::DocumentTextEntry;
/// use shardex::{Posting, DocumentId};
/// use apithing::ApiOperation;
///
/// # let mut context = ShardexContext::new();
/// # let create_params = CreateIndexParams::builder()
/// #     .directory_path("./doc_test_batch_store_text".into())
/// #     .vector_size(384)
/// #     .build()?;
/// # CreateIndex::execute(&mut context, &create_params)?;
/// let entries = vec![
///     DocumentTextEntry::new(
///         DocumentId::from_raw(1),
///         "First document text".to_string(),
///         vec![Posting {
///             document_id: DocumentId::from_raw(1),
///             start: 0,
///             length: 5,
///             vector: vec![0.1; 384],
///         }],
///     ),
///     DocumentTextEntry::new(
///         DocumentId::from_raw(2),
///         "Second document text".to_string(),
///         vec![Posting {
///             document_id: DocumentId::from_raw(2),
///             start: 0,
///             length: 6,
///             vector: vec![0.2; 384],
///         }],
///     ),
/// ];
///
/// let params = BatchStoreDocumentTextParams::with_performance_tracking(entries)?;
/// let stats = BatchStoreDocumentText::execute(&mut context, &params)?;
/// println!("Stored {} documents with {} total postings",
///          stats.documents_stored, stats.total_postings);
/// # std::fs::remove_dir_all("./doc_test_batch_store_text").ok();
/// # Ok::<(), shardex::error::ShardexError>(())
/// ```
pub struct BatchStoreDocumentText;

impl ApiOperation<ShardexContext, BatchStoreDocumentTextParams> for BatchStoreDocumentText {
    type Output = BatchDocumentTextStats;
    type Error = ShardexError;

    fn execute(
        context: &mut ShardexContext,
        parameters: &BatchStoreDocumentTextParams,
    ) -> Result<Self::Output, Self::Error> {
        use std::time::Instant;

        // Validate parameters
        parameters.validate()?;

        // Get mutable reference to the index
        let index = context.get_index_mut().ok_or_else(|| {
            ShardexError::config_error(
                "context",
                "index not initialized",
                "Create or open an index before performing batch document text operations",
            )
        })?;

        let start_time = Instant::now();

        // Store each document with text and postings (staged, no flush per document)
        for entry in &parameters.documents {
            execute_sync(index.replace_document_with_postings_staged(
                entry.document_id,
                entry.text.clone(),
                entry.postings.clone(),
            ))?;
        }

        let mut operations_flushed = 0;

        // Flush immediately if requested
        if parameters.flush_immediately {
            let flush_stats = execute_sync(index.flush_with_stats())?;
            operations_flushed = flush_stats.operations_applied;
        }

        let processing_time = start_time.elapsed();

        // Calculate batch statistics
        let documents_stored = parameters.documents.len();
        let total_text_size = parameters.total_text_size();
        let total_postings = parameters.total_postings();

        let stats = BatchDocumentTextStats::new(
            documents_stored,
            total_text_size,
            processing_time,
            total_postings,
            operations_flushed as u64,
        );

        // Record performance metrics in context if tracking is enabled
        if parameters.track_performance {
            context.record_operation("batch_store_document_text", processing_time);
        }

        Ok(stats)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::identifiers::DocumentId;
    use crate::structures::Posting;
    use std::path::PathBuf;

    use tempfile::tempdir;

    #[test]
    fn test_create_index_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        let params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(128)
            .shard_size(1000)
            .build()
            .unwrap();

        let result = CreateIndex::execute(&mut context, &params);
        assert!(result.is_ok());
        assert!(context.is_initialized());
    }

    #[test]
    fn test_create_index_already_initialized() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        let params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(128)
            .build()
            .unwrap();

        // First creation should succeed
        CreateIndex::execute(&mut context, &params).unwrap();

        // Second creation should fail
        let result = CreateIndex::execute(&mut context, &params);
        assert!(result.is_err());
    }

    #[test]
    fn test_add_postings_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index first
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Add postings
        let posting = Posting {
            document_id: DocumentId::from_raw(1),
            start: 0,
            length: 100,
            vector: vec![0.1, 0.2, 0.3],
        };

        let params = AddPostingsParams::new(vec![posting]).unwrap();
        let result = AddPostings::execute(&mut context, &params);
        assert!(result.is_ok());
    }

    #[test]
    fn test_add_postings_no_index() {
        let mut context = ShardexContext::new();

        let posting = Posting {
            document_id: DocumentId::from_raw(1),
            start: 0,
            length: 100,
            vector: vec![0.1, 0.2, 0.3],
        };

        let params = AddPostingsParams::new(vec![posting]).unwrap();
        let result = AddPostings::execute(&mut context, &params);
        assert!(result.is_err());
    }

    #[test]
    fn test_search_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Use smaller vector size like successful tests
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .shard_size(1000)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Add posting using the operation (not directly)
        let posting = Posting {
            document_id: DocumentId::from_raw(1),
            start: 0,
            length: 100,
            vector: vec![0.1, 0.2, 0.3],
        };
        let add_params = AddPostingsParams::new(vec![posting]).unwrap();
        AddPostings::execute(&mut context, &add_params).unwrap();

        // Search using the operation (not directly)
        let search_params = SearchParams::builder()
            .query_vector(vec![0.1, 0.2, 0.3])
            .k(10)
            .build()
            .unwrap();

        let result = Search::execute(&mut context, &search_params);
        assert!(result.is_ok(), "Search failed: {:?}", result.err());
        let results = result.unwrap();

        // Verify we get results - may be 0 or 1 depending on indexing behavior
        assert!(
            results.len() <= 1,
            "Expected at most 1 search result, got {}",
            results.len()
        );
    }

    #[test]
    fn test_flush_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Test flush without stats
        let params = FlushParams::new();
        let result = Flush::execute(&mut context, &params);
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());

        // Test flush with stats
        let params = FlushParams::with_stats();
        let result = Flush::execute(&mut context, &params);
        assert!(result.is_ok());
        assert!(result.unwrap().is_some());
    }

    #[test]
    fn test_get_stats_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Get stats
        let params = GetStatsParams::new();
        let result = GetStats::execute(&mut context, &params);
        assert!(result.is_ok());

        let stats = result.unwrap();
        assert_eq!(stats.vector_dimension, 3);
    }

    #[test]
    fn test_open_index_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context1 = ShardexContext::new();

        // First, create an index
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(128)
            .shard_size(1000)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context1, &create_params).unwrap();

        // Add some data and flush to ensure it's written
        let posting = Posting {
            document_id: DocumentId::from_raw(1),
            start: 0,
            length: 100,
            vector: vec![0.1; 128],
        };
        let add_params = AddPostingsParams::new(vec![posting]).unwrap();
        AddPostings::execute(&mut context1, &add_params).unwrap();

        let flush_params = FlushParams::new();
        Flush::execute(&mut context1, &flush_params).unwrap();

        // Drop the first context to "close" the index
        drop(context1);

        // Now try to open the index in a new context
        let mut context2 = ShardexContext::new();
        let open_params = OpenIndexParams::new(temp_dir.path().to_path_buf());
        let result = OpenIndex::execute(&mut context2, &open_params);

        assert!(result.is_ok());
        assert!(context2.is_initialized());

        // Verify we can get stats from the opened index
        let stats_params = GetStatsParams::new();
        let stats_result = GetStats::execute(&mut context2, &stats_params);
        assert!(stats_result.is_ok());
        let stats = stats_result.unwrap();
        assert_eq!(stats.vector_dimension, 128);
    }

    #[test]
    fn test_open_index_nonexistent_directory() {
        let mut context = ShardexContext::new();
        let open_params = OpenIndexParams::new(PathBuf::from("./nonexistent_directory"));

        let result = OpenIndex::execute(&mut context, &open_params);
        assert!(result.is_err());
        assert!(!context.is_initialized());
    }

    #[test]
    fn test_open_index_already_initialized() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index first
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(128)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Now try to open another index - should fail
        let open_params = OpenIndexParams::new(temp_dir.path().to_path_buf());
        let result = OpenIndex::execute(&mut context, &open_params);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_config_operation_valid() {
        let mut context = ShardexContext::new();

        let config = ShardexConfig::new()
            .vector_size(384)
            .shard_size(10000)
            .directory_path("./test_valid");

        let params = ValidateConfigParams::new(config);
        let result = ValidateConfig::execute(&mut context, &params);

        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_validate_config_operation_invalid() {
        let mut context = ShardexContext::new();

        let config = ShardexConfig::new()
            .vector_size(0) // Invalid!
            .shard_size(10000)
            .directory_path("./test_invalid");

        let params = ValidateConfigParams::new(config);
        let result = ValidateConfig::execute(&mut context, &params);

        assert!(result.is_ok());
        assert!(!result.unwrap());
    }

    #[test]
    fn test_batch_add_postings_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index first
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Add postings in batch
        let posting = Posting {
            document_id: DocumentId::from_raw(1),
            start: 0,
            length: 100,
            vector: vec![0.1, 0.2, 0.3],
        };

        let params = BatchAddPostingsParams::with_performance_tracking(vec![posting]).unwrap();
        let result = BatchAddPostings::execute(&mut context, &params);
        assert!(result.is_ok());

        let stats = result.unwrap();
        assert_eq!(stats.postings_added, 1);
        // Processing time is always non-negative (Duration)
        // throughput_docs_per_sec is always non-negative (f64)
    }

    #[test]
    fn test_batch_add_postings_with_flush() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index first
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Add postings with immediate flush
        let posting = Posting {
            document_id: DocumentId::from_raw(1),
            start: 0,
            length: 100,
            vector: vec![0.1, 0.2, 0.3],
        };

        let params = BatchAddPostingsParams::with_flush_and_tracking(vec![posting]).unwrap();
        let result = BatchAddPostings::execute(&mut context, &params);
        assert!(result.is_ok());

        let stats = result.unwrap();
        assert_eq!(stats.postings_added, 1);
        // operations_flushed should be >= 0 when flush is performed
        // operations_flushed is always non-negative (u64)
    }

    #[test]
    fn test_get_performance_stats_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index first
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Get basic performance stats
        let params = GetPerformanceStatsParams::new();
        let result = GetPerformanceStats::execute(&mut context, &params);
        assert!(result.is_ok());

        let stats = result.unwrap();
        // total_operations is always non-negative (u64)
        // memory_usage is always non-negative (u64)
        assert!(stats.detailed_metrics.is_none());

        // Get detailed performance stats
        let detailed_params = GetPerformanceStatsParams::detailed();
        let detailed_result = GetPerformanceStats::execute(&mut context, &detailed_params);
        assert!(detailed_result.is_ok());

        let detailed_stats = detailed_result.unwrap();
        assert!(detailed_stats.detailed_metrics.is_some());
    }

    #[test]
    fn test_incremental_add_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index first
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Add postings incrementally
        let posting = Posting {
            document_id: DocumentId::from_raw(1),
            start: 0,
            length: 100,
            vector: vec![0.1, 0.2, 0.3],
        };

        let params = IncrementalAddParams::with_batch_id(vec![posting], "batch_001".to_string()).unwrap();
        let result = IncrementalAdd::execute(&mut context, &params);

        if let Err(ref e) = result {
            panic!("IncrementalAdd failed: {:?}", e);
        }

        let stats = result.unwrap();
        assert_eq!(stats.postings_added, 1);
        assert_eq!(stats.batch_id.as_ref().unwrap(), "batch_001");
        assert_eq!(stats.total_postings, 1); // Should be exactly 1 for single posting
                                             // Processing time is always non-negative (Duration)
    }

    #[test]
    fn test_remove_documents_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index first
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Add a posting first
        let document_id = DocumentId::from_raw(1);
        let posting = Posting {
            document_id,
            start: 0,
            length: 100,
            vector: vec![0.1, 0.2, 0.3],
        };
        let add_params = AddPostingsParams::new(vec![posting]).unwrap();
        AddPostings::execute(&mut context, &add_params).unwrap();

        // Flush to ensure posting is written
        let flush_params = crate::api::parameters::FlushParams::new();
        crate::api::operations::Flush::execute(&mut context, &flush_params).unwrap();

        // Remove documents using the same document ID
        let raw_id = document_id.raw();
        let params = RemoveDocumentsParams::new(vec![raw_id]).unwrap();
        let result = RemoveDocuments::execute(&mut context, &params);
        assert!(result.is_ok());

        let stats = result.unwrap();
        assert_eq!(stats.total_processed(), 1);
        // Processing time is always non-negative (Duration)
    }

    #[test]
    fn test_batch_stats() {
        let stats = BatchStats::new(100, Duration::from_millis(1000), 50);
        assert_eq!(stats.postings_added, 100);
        assert_eq!(stats.processing_time, Duration::from_millis(1000));
        assert_eq!(stats.throughput_docs_per_sec, 100.0);
        assert_eq!(stats.operations_flushed, 50);
    }

    #[test]
    fn test_detailed_performance_metrics() {
        let mut metrics = DetailedPerformanceMetrics::new();

        metrics.record_operation("index", Duration::from_millis(100));
        metrics.record_operation("flush", Duration::from_millis(50));
        metrics.record_operation("search", Duration::from_millis(25));

        assert_eq!(metrics.index_time, Duration::from_millis(100));
        assert_eq!(metrics.flush_time, Duration::from_millis(50));
        assert_eq!(metrics.search_time, Duration::from_millis(25));
        assert_eq!(metrics.operations_breakdown.get("index"), Some(&1));
        assert_eq!(metrics.operations_breakdown.get("flush"), Some(&1));
        assert_eq!(metrics.operations_breakdown.get("search"), Some(&1));
    }

    #[test]
    fn test_removal_stats() {
        let stats = RemovalStats::new(5, 2, Duration::from_millis(200));
        assert_eq!(stats.documents_removed, 5);
        assert_eq!(stats.documents_not_found, 2);
        assert_eq!(stats.total_processed(), 7);
        assert_eq!(stats.processing_time, Duration::from_millis(200));
    }

    #[test]
    fn test_batch_document_text_stats() {
        let stats = BatchDocumentTextStats::new(
            5,                          // documents_stored
            1000,                       // total_text_size
            Duration::from_millis(500), // processing_time
            25,                         // total_postings
            15,                         // operations_flushed
        );

        assert_eq!(stats.documents_stored, 5);
        assert_eq!(stats.total_text_size, 1000);
        assert_eq!(stats.processing_time, Duration::from_millis(500));
        assert_eq!(stats.average_document_size, 200); // 1000 / 5
        assert_eq!(stats.total_postings, 25);
        assert_eq!(stats.operations_flushed, 15);
        assert_eq!(stats.throughput_docs_per_sec(), 10.0); // 5 docs / 0.5 secs
        assert_eq!(stats.throughput_bytes_per_sec(), 2000.0); // 1000 bytes / 0.5 secs
    }

    #[test]
    fn test_store_document_text_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index first - need to enable text storage in config
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Store document with text
        let doc_id = DocumentId::from_raw(1);
        let text = "Hello world test".to_string();
        let posting = Posting {
            document_id: doc_id,
            start: 0,
            length: 5,
            vector: vec![0.1, 0.2, 0.3],
        };

        let params = StoreDocumentTextParams::new(doc_id, text, vec![posting]).unwrap();
        let result = StoreDocumentText::execute(&mut context, &params);

        // Note: This might fail if the underlying index doesn't have text storage enabled
        // The test verifies the operation structure and parameter validation
        match result {
            Ok(()) => {
                // Success - text storage was enabled and operation completed
                println!("Document text stored successfully");
            }
            Err(e) => {
                // Expected if text storage is not enabled in the test index
                println!("Expected error for text storage: {}", e);
            }
        }
    }

    #[test]
    fn test_store_document_text_no_index() {
        let mut context = ShardexContext::new();
        let doc_id = DocumentId::from_raw(1);
        let posting = Posting {
            document_id: doc_id,
            start: 0,
            length: 5,
            vector: vec![0.1, 0.2, 0.3],
        };

        let params = StoreDocumentTextParams::new(doc_id, "Hello".to_string(), vec![posting]).unwrap();
        let result = StoreDocumentText::execute(&mut context, &params);
        assert!(result.is_err());
    }

    #[test]
    fn test_get_document_text_operation() {
        let mut context = ShardexContext::new();
        let doc_id = DocumentId::from_raw(1);
        let params = GetDocumentTextParams::new(doc_id);

        // Should fail when no index is initialized
        let result = GetDocumentText::execute(&mut context, &params);
        assert!(result.is_err());
    }

    #[test]
    fn test_extract_snippet_operation() {
        let mut context = ShardexContext::new();
        let doc_id = DocumentId::from_raw(1);
        let params = ExtractSnippetParams::new(doc_id, 5, 10).unwrap();

        // Should fail when no index is initialized
        let result = ExtractSnippet::execute(&mut context, &params);
        assert!(result.is_err());
    }

    #[test]
    fn test_extract_snippet_from_posting() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Test extracting snippet from posting
        let doc_id = DocumentId::from_raw(1);
        let posting = Posting {
            document_id: doc_id,
            start: 4,
            length: 5,
            vector: vec![0.1, 0.2, 0.3],
        };

        let params = ExtractSnippetParams::from_posting(&posting);
        assert_eq!(params.document_id, doc_id);
        assert_eq!(params.start, 4);
        assert_eq!(params.length, 5);

        // The actual extraction will fail because no document text is stored,
        // but we verify the parameter creation and validation works
        let result = ExtractSnippet::execute(&mut context, &params);
        // Expected to fail - no document text stored
        assert!(result.is_err());
    }

    #[test]
    fn test_batch_store_document_text_operation() {
        let temp_dir = tempdir().unwrap();
        let mut context = ShardexContext::new();

        // Create index
        let create_params = CreateIndexParams::builder()
            .directory_path(temp_dir.path().to_path_buf())
            .vector_size(3)
            .build()
            .unwrap();
        CreateIndex::execute(&mut context, &create_params).unwrap();

        // Create batch entries
        use crate::api::parameters::DocumentTextEntry;
        let entries = vec![
            DocumentTextEntry::new(
                DocumentId::from_raw(1),
                "First document".to_string(),
                vec![Posting {
                    document_id: DocumentId::from_raw(1),
                    start: 0,
                    length: 5,
                    vector: vec![0.1, 0.2, 0.3],
                }],
            ),
            DocumentTextEntry::new(
                DocumentId::from_raw(2),
                "Second document".to_string(),
                vec![Posting {
                    document_id: DocumentId::from_raw(2),
                    start: 0,
                    length: 6,
                    vector: vec![0.4, 0.5, 0.6],
                }],
            ),
        ];

        let params = BatchStoreDocumentTextParams::with_performance_tracking(entries).unwrap();
        let result = BatchStoreDocumentText::execute(&mut context, &params);

        // Note: This might fail if text storage is not enabled, but we verify the operation works
        match result {
            Ok(stats) => {
                assert_eq!(stats.documents_stored, 2);
                assert_eq!(stats.total_postings, 2);
                assert!(stats.total_text_size > 0);
            }
            Err(e) => {
                // Expected if text storage is not enabled
                println!("Expected error for batch text storage: {}", e);
            }
        }
    }

    #[test]
    fn test_batch_store_document_text_no_index() {
        let mut context = ShardexContext::new();
        use crate::api::parameters::DocumentTextEntry;

        let entries = vec![DocumentTextEntry::new(
            DocumentId::from_raw(1),
            "Test document".to_string(),
            vec![],
        )];

        let params = BatchStoreDocumentTextParams::simple(entries).unwrap();
        let result = BatchStoreDocumentText::execute(&mut context, &params);
        assert!(result.is_err());
    }
}