oxirs-core 0.2.2

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
//! Advanced Configuration System for OxiRS Core
//!
//! This module provides a comprehensive configuration system with performance
//! profiles, environment-based overrides, and dynamic reconfiguration capabilities.

use std::{
    collections::HashMap,
    time::Duration,
    path::{Path, PathBuf},
    sync::{Arc, RwLock},
    fmt::{self, Display},
};
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Central configuration manager for OxiRS Core
pub struct ConfigurationManager {
    config: Arc<RwLock<OxirsConfig>>,
    environment: Environment,
    config_sources: Vec<ConfigSource>,
    watchers: Vec<ConfigWatcher>,
}

/// Main configuration structure for OxiRS Core
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OxirsConfig {
    /// Performance profile configuration
    pub performance: PerformanceConfig,
    /// Memory management configuration
    pub memory: MemoryConfig,
    /// String interning configuration
    pub interning: InterningConfig,
    /// Indexing strategy configuration
    pub indexing: IndexingConfig,
    /// Parser configuration
    pub parsing: ParsingConfig,
    /// Serializer configuration
    pub serialization: SerializationConfig,
    /// Concurrency configuration
    pub concurrency: ConcurrencyConfig,
    /// Monitoring and observability configuration
    pub monitoring: MonitoringConfig,
    /// Security configuration
    pub security: SecurityConfig,
    /// Advanced optimization configuration
    pub optimization: OptimizationConfig,
}

/// Performance profile configurations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    /// Selected performance profile
    pub profile: PerformanceProfile,
    /// Custom performance settings (overrides profile defaults)
    pub custom_settings: HashMap<String, PerformanceValue>,
    /// Enable auto-tuning based on runtime metrics
    pub enable_auto_tuning: bool,
    /// Auto-tuning sensitivity (0.0 to 1.0)
    pub auto_tuning_sensitivity: f64,
    /// Performance monitoring interval
    pub monitoring_interval: Duration,
}

/// Available performance profiles
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PerformanceProfile {
    /// Development profile - prioritizes compilation speed and debugging
    Development,
    /// Balanced profile - good performance with reasonable resource usage
    Balanced,
    /// High performance profile - optimized for speed
    HighPerformance,
    /// Maximum throughput profile - all optimizations enabled
    MaxThroughput,
    /// Memory efficient profile - minimizes memory usage
    MemoryEfficient,
    /// Low latency profile - optimized for quick response times
    LowLatency,
    /// Batch processing profile - optimized for large dataset processing
    BatchProcessing,
    /// Real-time profile - deterministic performance with bounded latency
    RealTime,
    /// Edge computing profile - optimized for resource-constrained environments
    EdgeComputing,
    /// Custom profile - user-defined settings
    Custom,
}

/// Performance configuration values
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PerformanceValue {
    Boolean(bool),
    Integer(i64),
    Float(f64),
    String(String),
    Duration(u64), // milliseconds
}

/// Memory management configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryConfig {
    /// Arena allocator settings
    pub arena: ArenaConfig,
    /// Garbage collection settings
    pub gc: GcConfig,
    /// Memory pressure thresholds
    pub pressure_thresholds: MemoryPressureConfig,
    /// Enable memory tracking
    pub enable_tracking: bool,
    /// Memory pool configurations
    pub pools: HashMap<String, MemoryPoolConfig>,
}

/// Arena allocator configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArenaConfig {
    /// Initial arena size in bytes
    pub initial_size: usize,
    /// Maximum arena size in bytes
    pub max_size: usize,
    /// Arena growth factor
    pub growth_factor: f64,
    /// Enable arena compaction
    pub enable_compaction: bool,
    /// Compaction threshold (utilization %)
    pub compaction_threshold: f64,
}

/// Garbage collection configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GcConfig {
    /// GC strategy
    pub strategy: GcStrategy,
    /// GC trigger threshold (memory utilization %)
    pub trigger_threshold: f64,
    /// Maximum GC pause time
    pub max_pause_time: Duration,
    /// Enable concurrent GC
    pub enable_concurrent: bool,
    /// GC worker thread count
    pub worker_threads: usize,
}

/// Garbage collection strategies
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum GcStrategy {
    /// No automatic garbage collection
    None,
    /// Reference counting with cycle detection
    ReferenceCounting,
    /// Mark and sweep collector
    MarkAndSweep,
    /// Generational garbage collector
    Generational,
    /// Incremental garbage collector
    Incremental,
}

/// Memory pressure configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryPressureConfig {
    /// Low pressure threshold (% of available memory)
    pub low_threshold: f64,
    /// Medium pressure threshold
    pub medium_threshold: f64,
    /// High pressure threshold
    pub high_threshold: f64,
    /// Critical pressure threshold
    pub critical_threshold: f64,
    /// Actions to take at each pressure level
    pub pressure_actions: HashMap<String, Vec<MemoryPressureAction>>,
}

/// Memory pressure actions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemoryPressureAction {
    /// Force garbage collection
    ForceGc,
    /// Compact arenas
    CompactArenas,
    /// Clear caches
    ClearCaches,
    /// Reduce buffer sizes
    ReduceBuffers,
    /// Suspend background tasks
    SuspendBackgroundTasks,
    /// Send alert
    SendAlert,
}

/// Memory pool configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryPoolConfig {
    /// Pool name
    pub name: String,
    /// Initial pool size
    pub initial_size: usize,
    /// Maximum pool size
    pub max_size: usize,
    /// Object size for this pool
    pub object_size: usize,
    /// Enable pre-allocation
    pub enable_preallocation: bool,
}

/// String interning configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterningConfig {
    /// Global interner settings
    pub global: GlobalInternerConfig,
    /// Scoped interner settings
    pub scoped: ScopedInternerConfig,
    /// Interner cleanup settings
    pub cleanup: InternerCleanupConfig,
    /// Enable interner statistics
    pub enable_statistics: bool,
}

/// Global interner configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalInternerConfig {
    /// Initial capacity
    pub initial_capacity: usize,
    /// Load factor threshold for resizing
    pub load_factor: f64,
    /// Enable weak references
    pub enable_weak_references: bool,
    /// LRU cache size for frequently accessed strings
    pub lru_cache_size: usize,
}

/// Scoped interner configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScopedInternerConfig {
    /// Default scope capacity
    pub default_capacity: usize,
    /// Maximum number of active scopes
    pub max_scopes: usize,
    /// Scope timeout (automatic cleanup)
    pub scope_timeout: Duration,
}

/// Interner cleanup configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InternerCleanupConfig {
    /// Cleanup interval
    pub cleanup_interval: Duration,
    /// Cleanup threshold (unused string percentage)
    pub cleanup_threshold: f64,
    /// Enable automatic cleanup
    pub enable_automatic: bool,
}

/// Indexing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexingConfig {
    /// Default indexing strategy
    pub default_strategy: IndexingStrategy,
    /// Strategy-specific configurations
    pub strategy_configs: HashMap<String, IndexStrategyConfig>,
    /// Adaptive indexing settings
    pub adaptive: AdaptiveIndexingConfig,
    /// Index persistence settings
    pub persistence: IndexPersistenceConfig,
}

/// Indexing strategies
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum IndexingStrategy {
    /// No indexing
    None,
    /// Single index (SPO only)
    Single,
    /// Dual index (SPO + POS)
    Dual,
    /// Triple index (SPO + POS + OSP)
    Triple,
    /// Adaptive multi-index
    AdaptiveMulti,
    /// Custom indexing strategy
    Custom,
}

/// Index strategy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexStrategyConfig {
    /// Strategy name
    pub name: String,
    /// Index types to create
    pub index_types: Vec<IndexType>,
    /// Bloom filter settings
    pub bloom_filter: BloomFilterConfig,
    /// Index compaction settings
    pub compaction: IndexCompactionConfig,
}

/// Index types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum IndexType {
    /// Subject-Predicate-Object index
    SPO,
    /// Predicate-Object-Subject index
    POS,
    /// Object-Subject-Predicate index
    OSP,
    /// Subject-Object-Predicate index
    SOP,
    /// Predicate-Subject-Object index
    PSO,
    /// Object-Predicate-Subject index
    OPS,
}

/// Bloom filter configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BloomFilterConfig {
    /// Enable bloom filters for indexes
    pub enabled: bool,
    /// Expected number of items
    pub expected_items: usize,
    /// False positive probability
    pub false_positive_rate: f64,
    /// Hash function count
    pub hash_functions: usize,
}

/// Index compaction configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexCompactionConfig {
    /// Enable automatic compaction
    pub enabled: bool,
    /// Compaction threshold (fragmentation %)
    pub threshold: f64,
    /// Compaction interval
    pub interval: Duration,
    /// Concurrent compaction
    pub concurrent: bool,
}

/// Adaptive indexing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptiveIndexingConfig {
    /// Enable adaptive indexing
    pub enabled: bool,
    /// Query pattern analysis window
    pub analysis_window: Duration,
    /// Minimum query frequency for index creation
    pub min_query_frequency: f64,
    /// Index effectiveness threshold
    pub effectiveness_threshold: f64,
}

/// Index persistence configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexPersistenceConfig {
    /// Enable index persistence
    pub enabled: bool,
    /// Persistence directory
    pub directory: PathBuf,
    /// Sync interval
    pub sync_interval: Duration,
    /// Compression enabled
    pub compression: bool,
}

/// Parser configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParsingConfig {
    /// Default buffer sizes for different formats
    pub buffer_sizes: HashMap<String, usize>,
    /// Parser-specific configurations
    pub parsers: HashMap<String, ParserConfig>,
    /// Error handling configuration
    pub error_handling: ParserErrorConfig,
    /// Validation settings
    pub validation: ValidationConfig,
}

/// Individual parser configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParserConfig {
    /// Parser name
    pub name: String,
    /// Enable streaming mode
    pub enable_streaming: bool,
    /// Chunk size for streaming
    pub chunk_size: usize,
    /// Enable parallel processing
    pub enable_parallel: bool,
    /// Worker thread count
    pub worker_threads: usize,
    /// Parser-specific options
    pub options: HashMap<String, serde_json::Value>,
}

/// Parser error handling configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParserErrorConfig {
    /// Error tolerance (percentage of errors allowed)
    pub tolerance: f64,
    /// Continue parsing after errors
    pub continue_on_error: bool,
    /// Collect error details
    pub collect_errors: bool,
    /// Maximum error count before stopping
    pub max_errors: usize,
}

/// Validation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationConfig {
    /// Enable IRI validation
    pub enable_iri_validation: bool,
    /// Enable literal validation
    pub enable_literal_validation: bool,
    /// Enable language tag validation
    pub enable_language_validation: bool,
    /// Custom validation rules
    pub custom_rules: Vec<ValidationRule>,
}

/// Custom validation rule
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationRule {
    /// Rule name
    pub name: String,
    /// Rule pattern (regex)
    pub pattern: String,
    /// Error message
    pub error_message: String,
    /// Rule enabled
    pub enabled: bool,
}

/// Serialization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializationConfig {
    /// Default serialization format
    pub default_format: SerializationFormat,
    /// Format-specific configurations
    pub formats: HashMap<String, FormatConfig>,
    /// Output settings
    pub output: OutputConfig,
    /// Compression settings
    pub compression: CompressionConfig,
}

/// Serialization formats
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SerializationFormat {
    NTriples,
    Turtle,
    RdfXml,
    JsonLd,
    NQuads,
    TriG,
}

/// Format-specific configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FormatConfig {
    /// Format name
    pub name: String,
    /// Pretty printing enabled
    pub pretty_print: bool,
    /// Indentation size
    pub indent_size: usize,
    /// Line length limit
    pub line_length: usize,
    /// Format-specific options
    pub options: HashMap<String, serde_json::Value>,
}

/// Output configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputConfig {
    /// Default encoding
    pub encoding: String,
    /// Buffer size
    pub buffer_size: usize,
    /// Enable buffering
    pub enable_buffering: bool,
    /// Flush interval
    pub flush_interval: Duration,
}

/// Compression configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressionConfig {
    /// Enable compression
    pub enabled: bool,
    /// Compression algorithm
    pub algorithm: CompressionAlgorithm,
    /// Compression level (1-9)
    pub level: u8,
    /// Minimum size for compression
    pub min_size: usize,
}

/// Compression algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CompressionAlgorithm {
    None,
    Gzip,
    Bzip2,
    Lz4,
    Zstd,
}

/// Concurrency configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConcurrencyConfig {
    /// Thread pool configuration
    pub thread_pool: ThreadPoolConfig,
    /// Lock configuration
    pub locks: LockConfig,
    /// Async runtime configuration
    pub async_runtime: AsyncRuntimeConfig,
}

/// Thread pool configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThreadPoolConfig {
    /// Number of worker threads (0 = auto-detect)
    pub worker_threads: usize,
    /// Thread stack size
    pub stack_size: usize,
    /// Thread priority
    pub priority: ThreadPriority,
    /// Enable work stealing
    pub work_stealing: bool,
}

/// Thread priority levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ThreadPriority {
    Low,
    Normal,
    High,
    Realtime,
}

/// Lock configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LockConfig {
    /// Default lock type
    pub default_type: LockType,
    /// Lock timeout
    pub timeout: Duration,
    /// Enable lock debugging
    pub enable_debugging: bool,
    /// Deadlock detection
    pub deadlock_detection: bool,
}

/// Lock types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LockType {
    Mutex,
    RwLock,
    SpinLock,
    AtomicLock,
}

/// Async runtime configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AsyncRuntimeConfig {
    /// Runtime type
    pub runtime_type: AsyncRuntimeType,
    /// Enable I/O driver
    pub enable_io: bool,
    /// Enable time driver
    pub enable_time: bool,
    /// Worker thread configuration
    pub worker_config: AsyncWorkerConfig,
}

/// Async runtime types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AsyncRuntimeType {
    CurrentThread,
    MultiThread,
    MultiThreadAlt,
}

/// Async worker configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AsyncWorkerConfig {
    /// Core worker threads
    pub core_threads: usize,
    /// Maximum worker threads
    pub max_threads: usize,
    /// Thread keep-alive time
    pub keep_alive: Duration,
    /// Thread name prefix
    pub thread_name_prefix: String,
}

/// Monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MonitoringConfig {
    /// Enable monitoring
    pub enabled: bool,
    /// Metrics configuration
    pub metrics: MetricsConfig,
    /// Tracing configuration
    pub tracing: TracingConfig,
    /// Logging configuration
    pub logging: LoggingConfig,
    /// Health check configuration
    pub health_checks: HealthCheckConfig,
}

/// Metrics configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsConfig {
    /// Metrics exporter type
    pub exporter: MetricsExporter,
    /// Collection interval
    pub collection_interval: Duration,
    /// Retention period
    pub retention_period: Duration,
    /// Custom metrics
    pub custom_metrics: Vec<CustomMetric>,
}

/// Metrics exporters
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MetricsExporter {
    None,
    Prometheus,
    StatsD,
    OpenMetrics,
    Custom,
}

/// Custom metric definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomMetric {
    /// Metric name
    pub name: String,
    /// Metric type
    pub metric_type: MetricType,
    /// Description
    pub description: String,
    /// Labels
    pub labels: Vec<String>,
}

/// Metric types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MetricType {
    Counter,
    Gauge,
    Histogram,
    Summary,
}

/// Tracing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TracingConfig {
    /// Enable tracing
    pub enabled: bool,
    /// Trace exporter
    pub exporter: TraceExporter,
    /// Sampling rate (0.0 to 1.0)
    pub sampling_rate: f64,
    /// Maximum span count
    pub max_spans: usize,
}

/// Trace exporters
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TraceExporter {
    None,
    Jaeger,
    Zipkin,
    OpenTelemetry,
    Console,
}

/// Logging configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
    /// Log level
    pub level: LogLevel,
    /// Log format
    pub format: LogFormat,
    /// Log targets
    pub targets: Vec<LogTarget>,
    /// Enable structured logging
    pub structured: bool,
}

/// Log levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

/// Log formats
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LogFormat {
    Plain,
    Json,
    Logfmt,
    Custom,
}

/// Log targets
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LogTarget {
    Console,
    File { path: PathBuf, rotation: FileRotation },
    Syslog { facility: String },
    Network { endpoint: String },
}

/// File rotation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileRotation {
    /// Maximum file size
    pub max_size: usize,
    /// Maximum file age
    pub max_age: Duration,
    /// Maximum file count
    pub max_files: usize,
}

/// Health check configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckConfig {
    /// Enable health checks
    pub enabled: bool,
    /// Check interval
    pub interval: Duration,
    /// Timeout per check
    pub timeout: Duration,
    /// Health check endpoints
    pub endpoints: Vec<HealthCheckEndpoint>,
}

/// Health check endpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckEndpoint {
    /// Endpoint name
    pub name: String,
    /// Endpoint path
    pub path: String,
    /// Check type
    pub check_type: HealthCheckType,
}

/// Health check types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HealthCheckType {
    Liveness,
    Readiness,
    Startup,
}

/// Security configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityConfig {
    /// Authentication configuration
    pub authentication: AuthenticationConfig,
    /// Authorization configuration
    pub authorization: AuthorizationConfig,
    /// Encryption configuration
    pub encryption: EncryptionConfig,
    /// Audit configuration
    pub audit: AuditConfig,
}

/// Authentication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthenticationConfig {
    /// Authentication providers
    pub providers: Vec<AuthProvider>,
    /// Session configuration
    pub session: SessionConfig,
    /// Token configuration
    pub token: TokenConfig,
}

/// Authentication providers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AuthProvider {
    None,
    Basic { realm: String },
    Bearer { issuer: String, audience: String },
    OAuth2 { client_id: String, client_secret: String },
    LDAP { server: String, base_dn: String },
    Custom { provider_type: String, config: HashMap<String, String> },
}

/// Session configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionConfig {
    /// Session timeout
    pub timeout: Duration,
    /// Session storage
    pub storage: SessionStorage,
    /// Enable session encryption
    pub encryption: bool,
}

/// Session storage types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SessionStorage {
    Memory,
    File,
    Database,
    Redis,
}

/// Token configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenConfig {
    /// Token type
    pub token_type: TokenType,
    /// Token expiry
    pub expiry: Duration,
    /// Refresh token enabled
    pub refresh_enabled: bool,
    /// Token signing key
    pub signing_key: String,
}

/// Token types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TokenType {
    JWT,
    Opaque,
    PASETO,
}

/// Authorization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthorizationConfig {
    /// Authorization model
    pub model: AuthorizationModel,
    /// Permissions
    pub permissions: Vec<Permission>,
    /// Roles
    pub roles: Vec<Role>,
    /// Policies
    pub policies: Vec<Policy>,
}

/// Authorization models
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuthorizationModel {
    None,
    RBAC,
    ABAC,
    Custom,
}

/// Permission definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Permission {
    /// Permission name
    pub name: String,
    /// Description
    pub description: String,
    /// Resource type
    pub resource: String,
    /// Action
    pub action: String,
}

/// Role definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Role {
    /// Role name
    pub name: String,
    /// Description
    pub description: String,
    /// Permissions
    pub permissions: Vec<String>,
}

/// Policy definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Policy {
    /// Policy name
    pub name: String,
    /// Policy expression
    pub expression: String,
    /// Effect (allow/deny)
    pub effect: PolicyEffect,
}

/// Policy effects
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PolicyEffect {
    Allow,
    Deny,
}

/// Encryption configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptionConfig {
    /// Encryption at rest
    pub at_rest: EncryptionAtRest,
    /// Encryption in transit
    pub in_transit: EncryptionInTransit,
    /// Key management
    pub key_management: KeyManagementConfig,
}

/// Encryption at rest configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptionAtRest {
    /// Enable encryption
    pub enabled: bool,
    /// Encryption algorithm
    pub algorithm: EncryptionAlgorithm,
    /// Key size
    pub key_size: usize,
}

/// Encryption in transit configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptionInTransit {
    /// Enable TLS
    pub enabled: bool,
    /// TLS version
    pub tls_version: TlsVersion,
    /// Certificate path
    pub cert_path: PathBuf,
    /// Private key path
    pub key_path: PathBuf,
}

/// Encryption algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EncryptionAlgorithm {
    AES128,
    AES256,
    ChaCha20,
    XChaCha20,
}

/// TLS versions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TlsVersion {
    TLSv1_2,
    TLSv1_3,
}

/// Key management configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyManagementConfig {
    /// Key provider
    pub provider: KeyProvider,
    /// Key rotation interval
    pub rotation_interval: Duration,
    /// Key derivation function
    pub kdf: KeyDerivationFunction,
}

/// Key providers
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum KeyProvider {
    File,
    Environment,
    HSM,
    Vault,
    AWS_KMS,
    Azure_KeyVault,
    GCP_KMS,
}

/// Key derivation functions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum KeyDerivationFunction {
    PBKDF2,
    Scrypt,
    Argon2,
}

/// Audit configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditConfig {
    /// Enable auditing
    pub enabled: bool,
    /// Audit log path
    pub log_path: PathBuf,
    /// Events to audit
    pub events: Vec<AuditEvent>,
    /// Audit log retention
    pub retention: Duration,
}

/// Audit events
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuditEvent {
    Authentication,
    Authorization,
    DataAccess,
    DataModification,
    AdminActions,
    SecurityEvents,
}

/// Advanced optimization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationConfig {
    /// SIMD configuration
    pub simd: SimdConfig,
    /// Zero-copy configuration
    pub zero_copy: ZeroCopyConfig,
    /// Prefetching configuration
    pub prefetching: PrefetchingConfig,
    /// Caching configuration
    pub caching: CachingConfig,
}

/// SIMD configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SimdConfig {
    /// Enable SIMD operations
    pub enabled: bool,
    /// SIMD instruction set
    pub instruction_set: SimdInstructionSet,
    /// Fallback to scalar operations
    pub fallback_to_scalar: bool,
}

/// SIMD instruction sets
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SimdInstructionSet {
    None,
    SSE2,
    AVX,
    AVX2,
    AVX512,
    NEON,
    Auto,
}

/// Zero-copy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZeroCopyConfig {
    /// Enable zero-copy operations
    pub enabled: bool,
    /// Arena size for zero-copy allocations
    pub arena_size: usize,
    /// Enable reference counting
    pub reference_counting: bool,
}

/// Prefetching configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrefetchingConfig {
    /// Enable data prefetching
    pub enabled: bool,
    /// Prefetch distance
    pub distance: usize,
    /// Prefetch strategy
    pub strategy: PrefetchStrategy,
}

/// Prefetch strategies
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PrefetchStrategy {
    Sequential,
    Random,
    Adaptive,
}

/// Caching configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachingConfig {
    /// Cache configurations by name
    pub caches: HashMap<String, CacheConfig>,
    /// Global cache settings
    pub global: GlobalCacheConfig,
}

/// Individual cache configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheConfig {
    /// Cache name
    pub name: String,
    /// Cache type
    pub cache_type: CacheType,
    /// Maximum size
    pub max_size: usize,
    /// TTL (time to live)
    pub ttl: Duration,
    /// Eviction policy
    pub eviction_policy: EvictionPolicy,
}

/// Cache types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CacheType {
    LRU,
    LFU,
    FIFO,
    Random,
    Adaptive,
}

/// Eviction policies
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EvictionPolicy {
    LRU,
    LFU,
    TTL,
    Size,
    Custom,
}

/// Global cache configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalCacheConfig {
    /// Total cache memory limit
    pub memory_limit: usize,
    /// Enable cache statistics
    pub enable_statistics: bool,
    /// Cache warming enabled
    pub enable_warming: bool,
}

/// Environment types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Environment {
    Development,
    Testing,
    Staging,
    Production,
}

/// Configuration sources
#[derive(Debug, Clone)]
pub enum ConfigSource {
    File { path: PathBuf },
    Environment,
    CommandLine { args: Vec<String> },
    Remote { url: String },
    Database { connection: String },
}

/// Configuration watchers
pub struct ConfigWatcher {
    source: ConfigSource,
    callback: Box<dyn Fn(&OxirsConfig) + Send + Sync>,
}

/// Configuration errors
#[derive(Debug, Error)]
pub enum ConfigError {
    #[error("Configuration file not found: {0}")]
    FileNotFound(PathBuf),
    #[error("Invalid configuration format: {0}")]
    InvalidFormat(String),
    #[error("Validation error: {0}")]
    ValidationError(String),
    #[error("Environment variable error: {0}")]
    EnvironmentError(String),
    #[error("I/O error: {0}")]
    IoError(#[from] std::io::Error),
    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),
}

impl Default for OxirsConfig {
    fn default() -> Self {
        Self {
            performance: PerformanceConfig::default(),
            memory: MemoryConfig::default(),
            interning: InterningConfig::default(),
            indexing: IndexingConfig::default(),
            parsing: ParsingConfig::default(),
            serialization: SerializationConfig::default(),
            concurrency: ConcurrencyConfig::default(),
            monitoring: MonitoringConfig::default(),
            security: SecurityConfig::default(),
            optimization: OptimizationConfig::default(),
        }
    }
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            profile: PerformanceProfile::Balanced,
            custom_settings: HashMap::new(),
            enable_auto_tuning: false,
            auto_tuning_sensitivity: 0.5,
            monitoring_interval: Duration::from_secs(60),
        }
    }
}

impl PerformanceProfile {
    /// Get performance configuration for this profile
    pub fn get_config(&self) -> HashMap<String, PerformanceValue> {
        let mut config = HashMap::new();
        
        match self {
            Self::Development => {
                config.insert("enable_debug".to_string(), PerformanceValue::Boolean(true));
                config.insert("optimization_level".to_string(), PerformanceValue::Integer(0));
                config.insert("enable_simd".to_string(), PerformanceValue::Boolean(false));
                config.insert("thread_count".to_string(), PerformanceValue::Integer(2));
            }
            Self::Balanced => {
                config.insert("optimization_level".to_string(), PerformanceValue::Integer(2));
                config.insert("enable_simd".to_string(), PerformanceValue::Boolean(true));
                config.insert("thread_count".to_string(), PerformanceValue::Integer(4));
                config.insert("memory_limit_mb".to_string(), PerformanceValue::Integer(1024));
            }
            Self::HighPerformance => {
                config.insert("optimization_level".to_string(), PerformanceValue::Integer(3));
                config.insert("enable_simd".to_string(), PerformanceValue::Boolean(true));
                config.insert("enable_zero_copy".to_string(), PerformanceValue::Boolean(true));
                config.insert("thread_count".to_string(), PerformanceValue::Integer(8));
                config.insert("memory_limit_mb".to_string(), PerformanceValue::Integer(4096));
            }
            Self::MaxThroughput => {
                config.insert("optimization_level".to_string(), PerformanceValue::Integer(3));
                config.insert("enable_simd".to_string(), PerformanceValue::Boolean(true));
                config.insert("enable_zero_copy".to_string(), PerformanceValue::Boolean(true));
                config.insert("enable_prefetching".to_string(), PerformanceValue::Boolean(true));
                config.insert("thread_count".to_string(), PerformanceValue::Integer(16));
                config.insert("memory_limit_mb".to_string(), PerformanceValue::Integer(8192));
            }
            Self::MemoryEfficient => {
                config.insert("optimization_level".to_string(), PerformanceValue::Integer(1));
                config.insert("enable_compression".to_string(), PerformanceValue::Boolean(true));
                config.insert("memory_limit_mb".to_string(), PerformanceValue::Integer(256));
                config.insert("gc_frequency".to_string(), PerformanceValue::Integer(10));
            }
            Self::LowLatency => {
                config.insert("optimization_level".to_string(), PerformanceValue::Integer(3));
                config.insert("enable_simd".to_string(), PerformanceValue::Boolean(true));
                config.insert("enable_zero_copy".to_string(), PerformanceValue::Boolean(true));
                config.insert("gc_strategy".to_string(), PerformanceValue::String("incremental".to_string()));
                config.insert("response_timeout_ms".to_string(), PerformanceValue::Duration(100));
            }
            Self::BatchProcessing => {
                config.insert("optimization_level".to_string(), PerformanceValue::Integer(3));
                config.insert("enable_parallel".to_string(), PerformanceValue::Boolean(true));
                config.insert("batch_size".to_string(), PerformanceValue::Integer(10000));
                config.insert("thread_count".to_string(), PerformanceValue::Integer(32));
            }
            Self::RealTime => {
                config.insert("enable_deterministic".to_string(), PerformanceValue::Boolean(true));
                config.insert("max_latency_ms".to_string(), PerformanceValue::Duration(10));
                config.insert("priority".to_string(), PerformanceValue::String("realtime".to_string()));
            }
            Self::EdgeComputing => {
                config.insert("optimization_level".to_string(), PerformanceValue::Integer(2));
                config.insert("memory_limit_mb".to_string(), PerformanceValue::Integer(128));
                config.insert("thread_count".to_string(), PerformanceValue::Integer(2));
                config.insert("enable_compression".to_string(), PerformanceValue::Boolean(true));
            }
            Self::Custom => {
                // Custom profiles are user-defined
            }
        }
        
        config
    }
    
    /// Get profile description
    pub fn description(&self) -> &'static str {
        match self {
            Self::Development => "Optimized for development with fast compilation and debugging support",
            Self::Balanced => "Balanced configuration for general-purpose use",
            Self::HighPerformance => "High-performance configuration with advanced optimizations",
            Self::MaxThroughput => "Maximum throughput configuration with all optimizations enabled",
            Self::MemoryEfficient => "Memory-efficient configuration for resource-constrained environments",
            Self::LowLatency => "Low-latency configuration for real-time applications",
            Self::BatchProcessing => "Optimized for large-scale batch processing",
            Self::RealTime => "Real-time configuration with deterministic performance",
            Self::EdgeComputing => "Optimized for edge computing and IoT devices",
            Self::Custom => "User-defined custom configuration",
        }
    }
}

impl Display for PerformanceProfile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            Self::Development => "Development",
            Self::Balanced => "Balanced",
            Self::HighPerformance => "High Performance",
            Self::MaxThroughput => "Max Throughput",
            Self::MemoryEfficient => "Memory Efficient",
            Self::LowLatency => "Low Latency",
            Self::BatchProcessing => "Batch Processing",
            Self::RealTime => "Real Time",
            Self::EdgeComputing => "Edge Computing",
            Self::Custom => "Custom",
        };
        write!(f, "{}", name)
    }
}

impl ConfigurationManager {
    /// Create a new configuration manager
    pub fn new() -> Self {
        Self {
            config: Arc::new(RwLock::new(OxirsConfig::default())),
            environment: Environment::Development,
            config_sources: Vec::new(),
            watchers: Vec::new(),
        }
    }

    /// Load configuration from file
    pub fn load_from_file<P: AsRef<Path>>(&mut self, path: P) -> Result<(), ConfigError> {
        let content = std::fs::read_to_string(&path)
            .map_err(|_| ConfigError::FileNotFound(path.as_ref().to_path_buf()))?;
        
        let config: OxirsConfig = if path.as_ref().extension() == Some(std::ffi::OsStr::new("toml")) {
            toml::from_str(&content).map_err(|e| ConfigError::InvalidFormat(e.to_string()))?
        } else {
            serde_json::from_str(&content)?
        };
        
        self.update_config(config)?;
        self.config_sources.push(ConfigSource::File { path: path.as_ref().to_path_buf() });
        
        Ok(())
    }

    /// Load configuration from environment variables
    pub fn load_from_environment(&mut self) -> Result<(), ConfigError> {
        let mut config = self.get_config().clone();
        
        // Load environment-specific overrides
        if let Ok(profile_str) = std::env::var("OXIRS_PERFORMANCE_PROFILE") {
            if let Ok(profile) = serde_json::from_str::<PerformanceProfile>(&format!("\"{}\"", profile_str)) {
                config.performance.profile = profile;
            }
        }
        
        if let Ok(threads_str) = std::env::var("OXIRS_THREAD_COUNT") {
            if let Ok(threads) = threads_str.parse::<usize>() {
                config.concurrency.thread_pool.worker_threads = threads;
            }
        }
        
        self.update_config(config)?;
        self.config_sources.push(ConfigSource::Environment);
        
        Ok(())
    }

    /// Get current configuration
    pub fn get_config(&self) -> OxirsConfig {
        self.config.read().expect("lock should not be poisoned").clone()
    }

    /// Update configuration
    pub fn update_config(&mut self, new_config: OxirsConfig) -> Result<(), ConfigError> {
        self.validate_config(&new_config)?;
        *self.config.write().expect("lock should not be poisoned") = new_config;
        Ok(())
    }

    /// Set performance profile
    pub fn set_performance_profile(&mut self, profile: PerformanceProfile) -> Result<(), ConfigError> {
        let mut config = self.get_config();
        config.performance.profile = profile;
        config.performance.custom_settings = profile.get_config();
        self.update_config(config)
    }

    /// Get performance profile
    pub fn get_performance_profile(&self) -> PerformanceProfile {
        self.get_config().performance.profile
    }

    /// Validate configuration
    fn validate_config(&self, config: &OxirsConfig) -> Result<(), ConfigError> {
        // Validate thread counts
        if config.concurrency.thread_pool.worker_threads == 0 {
            return Err(ConfigError::ValidationError("Worker thread count cannot be zero".to_string()));
        }

        // Validate memory limits
        if config.memory.arena.initial_size > config.memory.arena.max_size {
            return Err(ConfigError::ValidationError("Initial arena size cannot exceed maximum".to_string()));
        }

        // Validate performance profile consistency
        match config.performance.profile {
            PerformanceProfile::RealTime => {
                if config.concurrency.thread_pool.worker_threads > 4 {
                    return Err(ConfigError::ValidationError("Real-time profile should use fewer threads".to_string()));
                }
            }
            PerformanceProfile::EdgeComputing => {
                if config.memory.arena.max_size > 128 * 1024 * 1024 {
                    return Err(ConfigError::ValidationError("Edge computing profile should use less memory".to_string()));
                }
            }
            _ => {}
        }

        Ok(())
    }

    /// Add configuration watcher
    pub fn add_watcher<F>(&mut self, source: ConfigSource, callback: F)
    where
        F: Fn(&OxirsConfig) + Send + Sync + 'static,
    {
        self.watchers.push(ConfigWatcher {
            source,
            callback: Box::new(callback),
        });
    }

    /// Start configuration monitoring
    pub async fn start_monitoring(&self) -> Result<(), ConfigError> {
        // Implementation would start file watchers, environment monitors, etc.
        Ok(())
    }
}

// Default implementations for major config sections

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            arena: ArenaConfig::default(),
            gc: GcConfig::default(),
            pressure_thresholds: MemoryPressureConfig::default(),
            enable_tracking: true,
            pools: HashMap::new(),
        }
    }
}

impl Default for ArenaConfig {
    fn default() -> Self {
        Self {
            initial_size: 1024 * 1024, // 1MB
            max_size: 64 * 1024 * 1024, // 64MB
            growth_factor: 2.0,
            enable_compaction: true,
            compaction_threshold: 0.5,
        }
    }
}

impl Default for GcConfig {
    fn default() -> Self {
        Self {
            strategy: GcStrategy::MarkAndSweep,
            trigger_threshold: 0.8,
            max_pause_time: Duration::from_millis(10),
            enable_concurrent: true,
            worker_threads: 2,
        }
    }
}

impl Default for MemoryPressureConfig {
    fn default() -> Self {
        Self {
            low_threshold: 0.6,
            medium_threshold: 0.75,
            high_threshold: 0.9,
            critical_threshold: 0.95,
            pressure_actions: HashMap::new(),
        }
    }
}

impl Default for InterningConfig {
    fn default() -> Self {
        Self {
            global: GlobalInternerConfig::default(),
            scoped: ScopedInternerConfig::default(),
            cleanup: InternerCleanupConfig::default(),
            enable_statistics: true,
        }
    }
}

impl Default for GlobalInternerConfig {
    fn default() -> Self {
        Self {
            initial_capacity: 10000,
            load_factor: 0.75,
            enable_weak_references: true,
            lru_cache_size: 1000,
        }
    }
}

impl Default for ScopedInternerConfig {
    fn default() -> Self {
        Self {
            default_capacity: 1000,
            max_scopes: 100,
            scope_timeout: Duration::from_secs(3600),
        }
    }
}

impl Default for InternerCleanupConfig {
    fn default() -> Self {
        Self {
            cleanup_interval: Duration::from_secs(300),
            cleanup_threshold: 0.5,
            enable_automatic: true,
        }
    }
}

impl Default for IndexingConfig {
    fn default() -> Self {
        Self {
            default_strategy: IndexingStrategy::AdaptiveMulti,
            strategy_configs: HashMap::new(),
            adaptive: AdaptiveIndexingConfig::default(),
            persistence: IndexPersistenceConfig::default(),
        }
    }
}

impl Default for AdaptiveIndexingConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            analysis_window: Duration::from_secs(3600),
            min_query_frequency: 0.1,
            effectiveness_threshold: 0.8,
        }
    }
}

impl Default for IndexPersistenceConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            directory: PathBuf::from("./indexes"),
            sync_interval: Duration::from_secs(300),
            compression: true,
        }
    }
}

impl Default for ParsingConfig {
    fn default() -> Self {
        Self {
            buffer_sizes: HashMap::from([
                ("ntriples".to_string(), 64 * 1024),
                ("turtle".to_string(), 32 * 1024),
                ("rdfxml".to_string(), 128 * 1024),
                ("jsonld".to_string(), 64 * 1024),
            ]),
            parsers: HashMap::new(),
            error_handling: ParserErrorConfig::default(),
            validation: ValidationConfig::default(),
        }
    }
}

impl Default for ParserErrorConfig {
    fn default() -> Self {
        Self {
            tolerance: 0.01, // 1% error tolerance
            continue_on_error: true,
            collect_errors: true,
            max_errors: 1000,
        }
    }
}

impl Default for ValidationConfig {
    fn default() -> Self {
        Self {
            enable_iri_validation: true,
            enable_literal_validation: true,
            enable_language_validation: true,
            custom_rules: Vec::new(),
        }
    }
}

impl Default for SerializationConfig {
    fn default() -> Self {
        Self {
            default_format: SerializationFormat::Turtle,
            formats: HashMap::new(),
            output: OutputConfig::default(),
            compression: CompressionConfig::default(),
        }
    }
}

impl Default for OutputConfig {
    fn default() -> Self {
        Self {
            encoding: "UTF-8".to_string(),
            buffer_size: 8192,
            enable_buffering: true,
            flush_interval: Duration::from_millis(100),
        }
    }
}

impl Default for CompressionConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            algorithm: CompressionAlgorithm::Gzip,
            level: 6,
            min_size: 1024,
        }
    }
}

impl Default for ConcurrencyConfig {
    fn default() -> Self {
        Self {
            thread_pool: ThreadPoolConfig::default(),
            locks: LockConfig::default(),
            async_runtime: AsyncRuntimeConfig::default(),
        }
    }
}

impl Default for ThreadPoolConfig {
    fn default() -> Self {
        Self {
            worker_threads: 0, // Auto-detect
            stack_size: 2 * 1024 * 1024, // 2MB
            priority: ThreadPriority::Normal,
            work_stealing: true,
        }
    }
}

impl Default for LockConfig {
    fn default() -> Self {
        Self {
            default_type: LockType::RwLock,
            timeout: Duration::from_secs(30),
            enable_debugging: false,
            deadlock_detection: true,
        }
    }
}

impl Default for AsyncRuntimeConfig {
    fn default() -> Self {
        Self {
            runtime_type: AsyncRuntimeType::MultiThread,
            enable_io: true,
            enable_time: true,
            worker_config: AsyncWorkerConfig::default(),
        }
    }
}

impl Default for AsyncWorkerConfig {
    fn default() -> Self {
        Self {
            core_threads: num_cpus::get(),
            max_threads: num_cpus::get() * 4,
            keep_alive: Duration::from_secs(60),
            thread_name_prefix: "oxirs-async".to_string(),
        }
    }
}

impl Default for MonitoringConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            metrics: MetricsConfig::default(),
            tracing: TracingConfig::default(),
            logging: LoggingConfig::default(),
            health_checks: HealthCheckConfig::default(),
        }
    }
}

impl Default for MetricsConfig {
    fn default() -> Self {
        Self {
            exporter: MetricsExporter::Prometheus,
            collection_interval: Duration::from_secs(15),
            retention_period: Duration::from_secs(3600),
            custom_metrics: Vec::new(),
        }
    }
}

impl Default for TracingConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            exporter: TraceExporter::Jaeger,
            sampling_rate: 0.1,
            max_spans: 1000,
        }
    }
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: LogLevel::Info,
            format: LogFormat::Json,
            targets: vec![LogTarget::Console],
            structured: true,
        }
    }
}

impl Default for HealthCheckConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            interval: Duration::from_secs(30),
            timeout: Duration::from_secs(5),
            endpoints: Vec::new(),
        }
    }
}

impl Default for SecurityConfig {
    fn default() -> Self {
        Self {
            authentication: AuthenticationConfig::default(),
            authorization: AuthorizationConfig::default(),
            encryption: EncryptionConfig::default(),
            audit: AuditConfig::default(),
        }
    }
}

impl Default for AuthenticationConfig {
    fn default() -> Self {
        Self {
            providers: vec![AuthProvider::None],
            session: SessionConfig::default(),
            token: TokenConfig::default(),
        }
    }
}

impl Default for SessionConfig {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(3600),
            storage: SessionStorage::Memory,
            encryption: true,
        }
    }
}

impl Default for TokenConfig {
    fn default() -> Self {
        Self {
            token_type: TokenType::JWT,
            expiry: Duration::from_secs(3600),
            refresh_enabled: true,
            signing_key: "default_key".to_string(),
        }
    }
}

impl Default for AuthorizationConfig {
    fn default() -> Self {
        Self {
            model: AuthorizationModel::RBAC,
            permissions: Vec::new(),
            roles: Vec::new(),
            policies: Vec::new(),
        }
    }
}

impl Default for EncryptionConfig {
    fn default() -> Self {
        Self {
            at_rest: EncryptionAtRest::default(),
            in_transit: EncryptionInTransit::default(),
            key_management: KeyManagementConfig::default(),
        }
    }
}

impl Default for EncryptionAtRest {
    fn default() -> Self {
        Self {
            enabled: false,
            algorithm: EncryptionAlgorithm::AES256,
            key_size: 256,
        }
    }
}

impl Default for EncryptionInTransit {
    fn default() -> Self {
        Self {
            enabled: true,
            tls_version: TlsVersion::TLSv1_3,
            cert_path: PathBuf::from("cert.pem"),
            key_path: PathBuf::from("key.pem"),
        }
    }
}

impl Default for KeyManagementConfig {
    fn default() -> Self {
        Self {
            provider: KeyProvider::File,
            rotation_interval: Duration::from_secs(86400 * 30), // 30 days
            kdf: KeyDerivationFunction::Argon2,
        }
    }
}

impl Default for AuditConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            log_path: PathBuf::from("./audit.log"),
            events: vec![AuditEvent::Authentication, AuditEvent::Authorization],
            retention: Duration::from_secs(86400 * 365), // 1 year
        }
    }
}

impl Default for OptimizationConfig {
    fn default() -> Self {
        Self {
            simd: SimdConfig::default(),
            zero_copy: ZeroCopyConfig::default(),
            prefetching: PrefetchingConfig::default(),
            caching: CachingConfig::default(),
        }
    }
}

impl Default for SimdConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            instruction_set: SimdInstructionSet::Auto,
            fallback_to_scalar: true,
        }
    }
}

impl Default for ZeroCopyConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            arena_size: 1024 * 1024, // 1MB
            reference_counting: true,
        }
    }
}

impl Default for PrefetchingConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            distance: 64,
            strategy: PrefetchStrategy::Adaptive,
        }
    }
}

impl Default for CachingConfig {
    fn default() -> Self {
        Self {
            caches: HashMap::new(),
            global: GlobalCacheConfig::default(),
        }
    }
}

impl Default for GlobalCacheConfig {
    fn default() -> Self {
        Self {
            memory_limit: 256 * 1024 * 1024, // 256MB
            enable_statistics: true,
            enable_warming: true,
        }
    }
}


// --- Tests (extracted to config_tests.rs for file size compliance) ---

include!("config_tests.rs");