pandrs 0.4.1

A high-performance DataFrame library for Rust, providing pandas-like API with advanced features including SIMD optimization, parallel processing, and distributed computing capabilities
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
//! Unified Memory Manager Implementation
//!
//! This module provides the main UnifiedMemoryManager implementation with
//! adaptive storage strategy selection and performance optimization.

use crate::core::error::Error;
use crate::core::error::Result;
use crate::storage::unified_memory::*;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;

/// Global string pool for optimized string handling.
///
/// Note: this is the *manager's* interning table, distinct from
/// [`crate::storage::string_pool::GlobalStringPool`] (the column-level pool
/// re-exported from `crate::column`). Both names are exported from
/// `crate::storage`; refer to them by module path when both are in scope.
pub struct GlobalStringPool {
    /// String to ID mapping
    string_to_id: HashMap<String, u32>,
    /// ID to string mapping
    id_to_string: Vec<String>,
    /// Next available ID
    next_id: u32,
    /// Statistics
    stats: StringPoolStats,
}

impl GlobalStringPool {
    pub fn new() -> Self {
        Self {
            string_to_id: HashMap::new(),
            id_to_string: Vec::new(),
            next_id: 0,
            stats: StringPoolStats::new(),
        }
    }

    /// Intern `s`, returning its stable id.
    ///
    /// Fails once the 32-bit id space is exhausted rather than wrapping around
    /// and aliasing an existing string. There is no eviction: ids are handed out
    /// permanently, so callers that intern unbounded user data should bound it
    /// themselves.
    pub fn intern(&mut self, s: &str) -> Result<u32> {
        if let Some(&id) = self.string_to_id.get(s) {
            self.stats.hits += 1;
            return Ok(id);
        }
        let id = self.next_id;
        self.next_id = self.next_id.checked_add(1).ok_or_else(|| {
            Error::InvalidOperation(
                "Global string pool exhausted: 2^32 distinct strings interned".to_string(),
            )
        })?;
        self.string_to_id.insert(s.to_string(), id);
        self.id_to_string.push(s.to_string());
        self.stats.misses += 1;
        self.stats.unique_strings += 1;
        Ok(id)
    }

    pub fn get(&self, id: u32) -> Option<&str> {
        self.id_to_string.get(id as usize).map(|s| s.as_str())
    }

    /// Number of distinct strings interned.
    pub fn len(&self) -> usize {
        self.id_to_string.len()
    }

    pub fn is_empty(&self) -> bool {
        self.id_to_string.is_empty()
    }

    pub fn stats(&self) -> &StringPoolStats {
        &self.stats
    }
}

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

/// String pool statistics
#[derive(Debug, Clone)]
pub struct StringPoolStats {
    pub hits: u64,
    pub misses: u64,
    pub unique_strings: u64,
}

impl StringPoolStats {
    fn new() -> Self {
        Self {
            hits: 0,
            misses: 0,
            unique_strings: 0,
        }
    }

    pub fn hit_rate(&self) -> f64 {
        if self.hits + self.misses == 0 {
            0.0
        } else {
            self.hits as f64 / (self.hits + self.misses) as f64
        }
    }
}

/// Memory configuration for the unified manager
#[derive(Debug, Clone)]
pub struct MemoryConfig {
    /// Maximum memory usage in bytes
    pub max_memory: Option<usize>,
    /// Default compression type
    pub default_compression: CompressionType,
    /// Enable adaptive optimization
    pub adaptive_optimization: bool,
    /// Performance monitoring interval
    pub monitoring_interval: std::time::Duration,
    /// Cache size for frequently accessed data
    pub cache_size: usize,
    /// Strategy selection algorithm
    pub strategy_selection: StrategySelectionAlgorithm,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            max_memory: None,
            default_compression: CompressionType::Auto,
            adaptive_optimization: true,
            monitoring_interval: std::time::Duration::from_secs(60),
            cache_size: 128 * 1024 * 1024, // 128MB
            strategy_selection: StrategySelectionAlgorithm::Adaptive,
        }
    }
}

/// Strategy selection algorithm
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StrategySelectionAlgorithm {
    /// Manual strategy selection
    Manual,
    /// Rule-based selection
    RuleBased,
    /// Machine learning based
    MachineLearning,
    /// Adaptive selection with learning
    Adaptive,
}

/// Cache management across strategies.
///
/// LRU order is tracked with a monotonically increasing generation stamp per
/// entry rather than a `Vec<String>` that was linearly searched and mutated on
/// every hit (O(n) per access, and duplicated entries once a key was re-`put`).
pub struct CacheManager {
    /// Cached chunks
    cache: HashMap<String, CachedItem>,
    /// Cache capacity in bytes
    capacity: usize,
    /// Current cache size in bytes
    current_size: usize,
    /// Monotonic clock used for LRU stamps
    clock: u64,
    /// Cache statistics
    stats: CacheStats,
}

impl CacheManager {
    pub fn new(capacity: usize) -> Self {
        Self {
            cache: HashMap::new(),
            capacity,
            current_size: 0,
            clock: 0,
            stats: CacheStats::new(),
        }
    }

    pub fn get(&mut self, key: &str) -> Option<&DataChunk> {
        self.clock += 1;
        let clock = self.clock;
        match self.cache.get_mut(key) {
            Some(item) => {
                item.last_used = clock;
                item.access_count += 1;
                self.stats.hits += 1;
                Some(&item.data)
            }
            None => {
                self.stats.misses += 1;
                None
            }
        }
    }

    pub fn put(&mut self, key: String, data: DataChunk) {
        let item_size = data.len();
        if item_size > self.capacity {
            // An item that can never fit is counted, not silently dropped.
            self.stats.rejected += 1;
            return;
        }

        // Replacing a key must release the old bytes first; the old code only
        // ever added, so `current_size` drifted upwards until the cache evicted
        // everything on every insert.
        if let Some(old) = self.cache.remove(&key) {
            self.current_size = self.current_size.saturating_sub(old.data.len());
        }

        while self.current_size + item_size > self.capacity && !self.cache.is_empty() {
            let victim = self
                .cache
                .iter()
                .min_by_key(|(_, item)| item.last_used)
                .map(|(k, _)| k.clone());
            match victim {
                Some(victim) => self.evict(&victim),
                None => break,
            }
        }

        self.clock += 1;
        self.cache.insert(
            key,
            CachedItem {
                data,
                created_at: Instant::now(),
                access_count: 1,
                last_used: self.clock,
            },
        );
        self.current_size += item_size;
    }

    /// Drop every entry whose key starts with `prefix`.
    pub fn invalidate_prefix(&mut self, prefix: &str) {
        let keys: Vec<String> = self
            .cache
            .keys()
            .filter(|k| k.starts_with(prefix))
            .cloned()
            .collect();
        for key in keys {
            self.evict(&key);
        }
    }

    fn evict(&mut self, key: &str) {
        if let Some(item) = self.cache.remove(key) {
            self.current_size = self.current_size.saturating_sub(item.data.len());
            self.stats.evictions += 1;
        }
    }

    /// Bytes currently held by the cache.
    pub fn size_bytes(&self) -> usize {
        self.current_size
    }

    /// Number of cached entries.
    pub fn len(&self) -> usize {
        self.cache.len()
    }

    pub fn is_empty(&self) -> bool {
        self.cache.is_empty()
    }

    pub fn stats(&self) -> &CacheStats {
        &self.stats
    }
}

/// Cached item
#[derive(Debug, Clone)]
struct CachedItem {
    data: DataChunk,
    /// When the entry was inserted
    created_at: Instant,
    /// How many times the entry has been served
    access_count: u64,
    /// LRU stamp
    last_used: u64,
}

impl CachedItem {
    /// Age of this entry.
    fn age(&self) -> std::time::Duration {
        self.created_at.elapsed()
    }
}

impl CacheManager {
    /// Access count and age of a cached entry, for diagnostics.
    pub fn entry_stats(&self, key: &str) -> Option<(u64, std::time::Duration)> {
        self.cache
            .get(key)
            .map(|item| (item.access_count, item.age()))
    }
}

/// Cache statistics
#[derive(Debug, Clone)]
pub struct CacheStats {
    pub hits: u64,
    pub misses: u64,
    pub evictions: u64,
    /// Items too large for the cache and therefore never stored
    pub rejected: u64,
}

impl CacheStats {
    fn new() -> Self {
        Self {
            hits: 0,
            misses: 0,
            evictions: 0,
            rejected: 0,
        }
    }

    pub fn hit_rate(&self) -> f64 {
        if self.hits + self.misses == 0 {
            0.0
        } else {
            self.hits as f64 / (self.hits + self.misses) as f64
        }
    }
}

/// Performance monitoring for strategies
pub struct PerformanceMonitor {
    /// Per-strategy performance metrics
    strategy_metrics: HashMap<StorageType, StrategyMetrics>,
    /// Global system metrics
    system_metrics: SystemMetrics,
    /// Monitoring start time
    start_time: Instant,
}

impl PerformanceMonitor {
    pub fn new() -> Self {
        Self {
            strategy_metrics: HashMap::new(),
            system_metrics: SystemMetrics::new(),
            start_time: Instant::now(),
        }
    }

    pub fn record_operation(
        &mut self,
        strategy_type: StorageType,
        operation: OperationType,
        duration: std::time::Duration,
        bytes: usize,
    ) {
        let metrics = self
            .strategy_metrics
            .entry(strategy_type)
            .or_insert_with(StrategyMetrics::new);

        metrics.record_operation(operation, duration, bytes);
        self.system_metrics
            .record_operation(operation, duration, bytes);
    }

    pub fn get_strategy_metrics(&self, strategy_type: StorageType) -> Option<&StrategyMetrics> {
        self.strategy_metrics.get(&strategy_type)
    }

    pub fn get_system_metrics(&self) -> &SystemMetrics {
        &self.system_metrics
    }

    pub fn uptime(&self) -> std::time::Duration {
        self.start_time.elapsed()
    }
}

/// Operation type for performance tracking
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum OperationType {
    Read,
    Write,
    Append,
    Delete,
    Compact,
    Flush,
}

/// Performance metrics for a strategy
#[derive(Debug, Clone)]
pub struct StrategyMetrics {
    /// Operation counts
    pub operation_counts: HashMap<OperationType, u64>,
    /// Total operation times
    pub operation_times: HashMap<OperationType, std::time::Duration>,
    /// Total bytes processed
    pub bytes_processed: HashMap<OperationType, u64>,
    /// Last operation timestamp
    pub last_operation: Option<Instant>,
}

impl StrategyMetrics {
    fn new() -> Self {
        Self {
            operation_counts: HashMap::new(),
            operation_times: HashMap::new(),
            bytes_processed: HashMap::new(),
            last_operation: None,
        }
    }

    fn record_operation(
        &mut self,
        operation: OperationType,
        duration: std::time::Duration,
        bytes: usize,
    ) {
        *self.operation_counts.entry(operation).or_insert(0) += 1;
        *self
            .operation_times
            .entry(operation)
            .or_insert(std::time::Duration::ZERO) += duration;
        *self.bytes_processed.entry(operation).or_insert(0) += bytes as u64;
        self.last_operation = Some(Instant::now());
    }

    pub fn average_operation_time(&self, operation: OperationType) -> Option<std::time::Duration> {
        let count = self.operation_counts.get(&operation)?;
        let total_time = self.operation_times.get(&operation)?;

        if *count > 0 {
            Some(*total_time / *count as u32)
        } else {
            None
        }
    }

    pub fn throughput(&self, operation: OperationType) -> Option<f64> {
        let bytes = self.bytes_processed.get(&operation)?;
        let time = self.operation_times.get(&operation)?;

        if time.as_secs_f64() > 0.0 {
            Some(*bytes as f64 / time.as_secs_f64())
        } else {
            None
        }
    }
}

/// System-wide performance metrics
#[derive(Debug, Clone)]
pub struct SystemMetrics {
    /// Total operations across all strategies
    pub total_operations: u64,
    /// Total bytes processed
    pub total_bytes: u64,
    /// Total time spent in operations
    pub total_time: std::time::Duration,
    /// Memory usage statistics
    pub memory_stats: MemoryStats,
}

impl SystemMetrics {
    fn new() -> Self {
        Self {
            total_operations: 0,
            total_bytes: 0,
            total_time: std::time::Duration::ZERO,
            memory_stats: MemoryStats::new(),
        }
    }

    fn record_operation(
        &mut self,
        _operation: OperationType,
        duration: std::time::Duration,
        bytes: usize,
    ) {
        self.total_operations += 1;
        self.total_bytes += bytes as u64;
        self.total_time += duration;
    }

    pub fn overall_throughput(&self) -> f64 {
        if self.total_time.as_secs_f64() > 0.0 {
            self.total_bytes as f64 / self.total_time.as_secs_f64()
        } else {
            0.0
        }
    }
}

/// Memory usage statistics
#[derive(Debug, Clone)]
pub struct MemoryStats {
    /// Current memory usage in bytes
    pub current_usage: usize,
    /// Peak memory usage in bytes
    pub peak_usage: usize,
    /// Total allocations
    pub total_allocations: u64,
    /// Total deallocations
    pub total_deallocations: u64,
}

impl MemoryStats {
    fn new() -> Self {
        Self {
            current_usage: 0,
            peak_usage: 0,
            total_allocations: 0,
            total_deallocations: 0,
        }
    }

    pub fn record_allocation(&mut self, size: usize) {
        self.current_usage += size;
        if self.current_usage > self.peak_usage {
            self.peak_usage = self.current_usage;
        }
        self.total_allocations += 1;
    }

    pub fn record_deallocation(&mut self, size: usize) {
        self.current_usage = self.current_usage.saturating_sub(size);
        self.total_deallocations += 1;
    }

    pub fn active_allocations(&self) -> u64 {
        self.total_allocations
            .saturating_sub(self.total_deallocations)
    }
}

/// Boxed strategy type stored by the manager.
pub type BoxedStrategy =
    Box<dyn StorageStrategy<Handle = StorageHandle, Error = Error, Metadata = StorageMetadata>>;

/// Adapter that lets a concrete strategy (with its own handle type) be stored
/// in the manager's `StorageType -> strategy` map.
///
/// Every concrete strategy defines its own `Handle`, so none of them could ever
/// satisfy `StorageStrategy<Handle = StorageHandle>` directly — `add_strategy`
/// was therefore uncallable, nothing was ever registered, and
/// `create_storage` always returned "No suitable storage strategy available".
pub struct StrategyAdapter<S>
where
    S: StorageStrategy<Error = Error>,
    S::Handle: Send + Sync + 'static,
{
    inner: S,
    storage_type: StorageType,
    next_id: AtomicU64,
}

impl<S> StrategyAdapter<S>
where
    S: StorageStrategy<Error = Error>,
    S::Handle: Send + Sync + 'static,
{
    pub fn new(storage_type: StorageType, inner: S) -> Self {
        Self {
            inner,
            storage_type,
            next_id: AtomicU64::new(1),
        }
    }

    /// Box this adapter for registration with a [`UnifiedMemoryManager`].
    pub fn boxed(storage_type: StorageType, inner: S) -> BoxedStrategy
    where
        S: 'static,
    {
        Box::new(Self::new(storage_type, inner))
    }

    fn inner_handle<'a>(&self, handle: &'a StorageHandle) -> Result<&'a S::Handle> {
        handle
            .inner_handle
            .downcast_ref::<S::Handle>()
            .ok_or_else(|| {
                Error::InvalidOperation(format!(
                    "Storage handle {:?} does not belong to the {:?} strategy",
                    handle.id, self.storage_type
                ))
            })
    }
}

impl<S> StorageStrategy for StrategyAdapter<S>
where
    S: StorageStrategy<Error = Error>,
    S::Handle: Send + Sync + 'static,
{
    type Handle = StorageHandle;
    type Error = Error;
    type Metadata = StorageMetadata;

    fn name(&self) -> &'static str {
        self.inner.name()
    }

    fn create_storage(&mut self, config: &StorageConfig) -> Result<StorageHandle> {
        let inner = self.inner.create_storage(config)?;
        let id = StorageId(self.next_id.fetch_add(1, Ordering::SeqCst));
        Ok(StorageHandle::new(
            id,
            self.storage_type,
            Box::new(inner),
            StorageMetadata::new(config.requirements.estimated_size),
        ))
    }

    fn read_chunk(&self, handle: &StorageHandle, range: ChunkRange) -> Result<DataChunk> {
        self.inner.read_chunk(self.inner_handle(handle)?, range)
    }

    fn write_chunk(&mut self, handle: &StorageHandle, chunk: DataChunk) -> Result<()> {
        let inner_handle = handle
            .inner_handle
            .downcast_ref::<S::Handle>()
            .ok_or_else(|| {
                Error::InvalidOperation(format!(
                    "Storage handle {:?} does not belong to the {:?} strategy",
                    handle.id, self.storage_type
                ))
            })?;
        self.inner.write_chunk(inner_handle, chunk)
    }

    fn append_chunk(&mut self, handle: &StorageHandle, chunk: DataChunk) -> Result<()> {
        let inner_handle = handle
            .inner_handle
            .downcast_ref::<S::Handle>()
            .ok_or_else(|| {
                Error::InvalidOperation(format!(
                    "Storage handle {:?} does not belong to the {:?} strategy",
                    handle.id, self.storage_type
                ))
            })?;
        self.inner.append_chunk(inner_handle, chunk)
    }

    fn flush(&mut self, handle: &StorageHandle) -> Result<()> {
        let inner_handle = handle
            .inner_handle
            .downcast_ref::<S::Handle>()
            .ok_or_else(|| {
                Error::InvalidOperation(format!(
                    "Storage handle {:?} does not belong to the {:?} strategy",
                    handle.id, self.storage_type
                ))
            })?;
        self.inner.flush(inner_handle)
    }

    fn delete_storage(&mut self, handle: &StorageHandle) -> Result<()> {
        let inner_handle = handle
            .inner_handle
            .downcast_ref::<S::Handle>()
            .ok_or_else(|| {
                Error::InvalidOperation(format!(
                    "Storage handle {:?} does not belong to the {:?} strategy",
                    handle.id, self.storage_type
                ))
            })?;
        self.inner.delete_storage(inner_handle)
    }

    fn can_handle(&self, requirements: &StorageRequirements) -> StrategyCapability {
        self.inner.can_handle(requirements)
    }

    fn performance_profile(&self) -> PerformanceProfile {
        self.inner.performance_profile()
    }

    fn storage_stats(&self) -> StorageStats {
        self.inner.storage_stats()
    }

    fn optimize_for_pattern(&mut self, pattern: AccessPattern) -> Result<()> {
        self.inner.optimize_for_pattern(pattern)
    }

    fn compact(&mut self, handle: &StorageHandle) -> Result<CompactionResult> {
        let inner_handle = handle
            .inner_handle
            .downcast_ref::<S::Handle>()
            .ok_or_else(|| {
                Error::InvalidOperation(format!(
                    "Storage handle {:?} does not belong to the {:?} strategy",
                    handle.id, self.storage_type
                ))
            })?;
        self.inner.compact(inner_handle)
    }
}

/// Unified memory manager for PandRS
pub struct UnifiedMemoryManager {
    /// Active storage strategies
    strategies: HashMap<StorageType, BoxedStrategy>,

    /// Adaptive strategy selector
    selector: Box<dyn StrategySelector>,

    /// Performance monitoring and metrics
    monitor: Arc<Mutex<PerformanceMonitor>>,

    /// Memory usage statistics and tracking
    stats: Arc<AtomicMemoryStats>,

    /// Global memory configuration
    config: MemoryConfig,

    /// Cache management across strategies
    cache_manager: Arc<Mutex<CacheManager>>,

    /// String pool for optimized string handling
    string_pool: Arc<Mutex<GlobalStringPool>>,

    /// Per-storage cache generation. Bumped on every mutation so that stale
    /// cache entries become unreachable.
    generations: Arc<Mutex<HashMap<u64, u64>>>,

    /// Next storage ID
    next_storage_id: AtomicU64,
}

impl UnifiedMemoryManager {
    /// Create a new unified memory manager with the built-in strategies
    /// registered.
    pub fn new(config: MemoryConfig) -> Self {
        let mut manager = Self::empty(config);
        manager.register_builtin_strategies();
        manager
    }

    /// Create a manager with **no** strategies registered.
    ///
    /// Useful when the caller wants full control over which backends exist;
    /// `create_storage` on an empty manager returns a clear configuration error.
    pub fn empty(config: MemoryConfig) -> Self {
        Self {
            strategies: HashMap::new(),
            selector: Box::new(DefaultStrategySelector::new()),
            monitor: Arc::new(Mutex::new(PerformanceMonitor::new())),
            stats: Arc::new(AtomicMemoryStats::new()),
            cache_manager: Arc::new(Mutex::new(CacheManager::new(config.cache_size))),
            string_pool: Arc::new(Mutex::new(GlobalStringPool::new())),
            generations: Arc::new(Mutex::new(HashMap::new())),
            config,
            next_storage_id: AtomicU64::new(1),
        }
    }

    /// Register the strategies shipped with PandRS.
    pub fn register_builtin_strategies(&mut self) {
        use crate::storage::adaptive_string_pool::{AdaptiveStringPoolStrategy, StringPoolConfig};
        use crate::storage::hybrid_large_scale::{HybridConfig, HybridLargeScaleStrategy};
        use crate::storage::unified_column_store::{
            encoding::EncodingType, ColumnStoreConfig, UnifiedColumnStoreStrategy,
        };

        let columnar = ColumnStoreConfig {
            compression_type: self.config.default_compression,
            ..Default::default()
        };
        self.add_strategy(
            StorageType::ColumnStore,
            StrategyAdapter::boxed(
                StorageType::ColumnStore,
                UnifiedColumnStoreStrategy::new(columnar),
            ),
        );

        // "In memory" is the column store with every transform disabled, so the
        // bytes are stored verbatim in RAM.
        let in_memory = ColumnStoreConfig {
            compression_type: CompressionType::None,
            encoding_type: EncodingType::None,
            ..Default::default()
        };
        self.add_strategy(
            StorageType::InMemory,
            StrategyAdapter::boxed(
                StorageType::InMemory,
                UnifiedColumnStoreStrategy::new(in_memory),
            ),
        );

        self.add_strategy(
            StorageType::StringPool,
            StrategyAdapter::boxed(
                StorageType::StringPool,
                AdaptiveStringPoolStrategy::new(StringPoolConfig::default()),
            ),
        );

        self.add_strategy(
            StorageType::HybridLargeScale,
            StrategyAdapter::boxed(
                StorageType::HybridLargeScale,
                HybridLargeScaleStrategy::new(HybridConfig::default()),
            ),
        );

        // "Disk based" is the hybrid strategy with a 1-byte hot tier, so every
        // chunk lands in a file-backed tier.
        let mut disk_config = HybridConfig::default();
        disk_config.hot_tier.max_size = 1;
        disk_config.max_hot_memory = 1;
        self.add_strategy(
            StorageType::DiskBased,
            StrategyAdapter::boxed(
                StorageType::DiskBased,
                HybridLargeScaleStrategy::new(disk_config),
            ),
        );

        // StorageType::MemoryMapped intentionally has no strategy: PandRS's
        // memory mapping is a read-only view over an existing file
        // (`storage::memory_mapped`), not a writable chunk store. Selection
        // falls through to the fallback list instead of pretending otherwise.
    }

    /// Storage types with a registered strategy.
    pub fn registered_strategies(&self) -> Vec<StorageType> {
        let mut types: Vec<StorageType> = self.strategies.keys().copied().collect();
        types.sort_by_key(|t| format!("{:?}", t));
        types
    }

    fn generation(&self, id: StorageId) -> u64 {
        self.generations
            .lock()
            .ok()
            .and_then(|g| g.get(&id.0).copied())
            .unwrap_or(0)
    }

    /// Invalidate every cached read for `id` by advancing its generation.
    fn bump_generation(&self, id: StorageId) -> Result<()> {
        let mut generations = self.generations.lock().map_err(|_| {
            Error::InvalidOperation("Cache generation lock is poisoned".to_string())
        })?;
        let entry = generations.entry(id.0).or_insert(0);
        *entry = entry.wrapping_add(1);
        drop(generations);

        // Also drop the now-unreachable entries eagerly so the cache does not
        // hold on to superseded bytes until they age out.
        if let Ok(mut cache) = self.cache_manager.lock() {
            cache.invalidate_prefix(&format!("{}:", id.0));
        }
        Ok(())
    }

    fn cache_key(&self, handle: &StorageHandle, range: &ChunkRange) -> String {
        format!(
            "{}:{}:{}-{}",
            handle.id.0,
            self.generation(handle.id),
            range.start,
            range.end
        )
    }

    /// Create new storage with given configuration
    pub fn create_storage(&mut self, config: &StorageConfig) -> Result<StorageHandle> {
        let selection = self.selector.select_strategy(&config.requirements);

        let mut attempts = Vec::with_capacity(1 + selection.fallbacks.len());
        attempts.push(selection.primary);
        attempts.extend(selection.fallbacks.iter().copied());

        let mut last_error: Option<Error> = None;
        for strategy_type in attempts {
            let Some(strategy) = self.strategies.get_mut(&strategy_type) else {
                continue;
            };
            match strategy.create_storage(config) {
                Ok(mut handle) => {
                    // The adapter already built a fully-formed handle whose
                    // `inner_handle` is the concrete strategy handle; re-wrapping
                    // it here would make every later downcast fail.
                    let storage_id = StorageId(self.next_storage_id.fetch_add(1, Ordering::SeqCst));
                    handle.id = storage_id;
                    handle.strategy_type = strategy_type;
                    if let Ok(mut generations) = self.generations.lock() {
                        generations.insert(storage_id.0, 0);
                    }
                    self.stats
                        .record_allocation(config.requirements.estimated_size);
                    return Ok(handle);
                }
                Err(e) => {
                    log::warn!("Storage strategy {:?} failed: {}", strategy_type, e);
                    last_error = Some(e);
                }
            }
        }

        Err(match last_error {
            Some(e) => Error::InvalidOperation(format!(
                "No storage strategy could satisfy the request; last error: {}",
                e
            )),
            None => Error::InvalidOperation(format!(
                "No storage strategy registered for {:?} or its fallbacks {:?}; \
                 call UnifiedMemoryManager::new (which registers the built-ins) \
                 or add_strategy before create_storage",
                selection.primary, selection.fallbacks
            )),
        })
    }

    /// Read data chunk from storage
    pub fn read_chunk(&self, handle: &StorageHandle, range: ChunkRange) -> Result<DataChunk> {
        let start_time = Instant::now();
        let cache_key = self.cache_key(handle, &range);

        if let Ok(mut cache) = self.cache_manager.lock() {
            if let Some(cached_data) = cache.get(&cache_key) {
                let cached = cached_data.clone();
                drop(cache);
                // Cache hits used to return early *without* recording anything,
                // so every metric — including the data the ML selector trains on
                // — excluded them.
                if let Ok(mut monitor) = self.monitor.lock() {
                    monitor.record_operation(
                        handle.strategy_type,
                        OperationType::Read,
                        start_time.elapsed(),
                        cached.len(),
                    );
                }
                return Ok(cached);
            }
        }

        let strategy = self.strategies.get(&handle.strategy_type).ok_or_else(|| {
            Error::InvalidOperation(format!("Strategy {:?} not found", handle.strategy_type))
        })?;

        let result = strategy.read_chunk(handle, range);
        let duration = start_time.elapsed();
        if let Ok(ref chunk) = result {
            if let Ok(mut monitor) = self.monitor.lock() {
                monitor.record_operation(
                    handle.strategy_type,
                    OperationType::Read,
                    duration,
                    chunk.len(),
                );
            }
            if let Ok(mut cache) = self.cache_manager.lock() {
                cache.put(cache_key, chunk.clone());
            }
        }
        result
    }

    /// Write data chunk to storage
    pub fn write_chunk(&mut self, handle: &StorageHandle, chunk: DataChunk) -> Result<()> {
        let start_time = Instant::now();
        let bytes = chunk.len();

        let strategy = self
            .strategies
            .get_mut(&handle.strategy_type)
            .ok_or_else(|| {
                Error::InvalidOperation(format!("Strategy {:?} not found", handle.strategy_type))
            })?;
        let result = strategy.write_chunk(handle, chunk);

        if result.is_ok() {
            // Without this, a read-write-read sequence returned the stale
            // pre-write bytes indefinitely.
            self.bump_generation(handle.id)?;
        }

        if let Ok(mut monitor) = self.monitor.lock() {
            monitor.record_operation(
                handle.strategy_type,
                OperationType::Write,
                start_time.elapsed(),
                bytes,
            );
        }
        result
    }

    /// Append a data chunk to existing storage.
    pub fn append_chunk(&mut self, handle: &StorageHandle, chunk: DataChunk) -> Result<()> {
        let start_time = Instant::now();
        let bytes = chunk.len();

        let strategy = self
            .strategies
            .get_mut(&handle.strategy_type)
            .ok_or_else(|| {
                Error::InvalidOperation(format!("Strategy {:?} not found", handle.strategy_type))
            })?;
        let result = strategy.append_chunk(handle, chunk);

        if result.is_ok() {
            self.bump_generation(handle.id)?;
        }
        if let Ok(mut monitor) = self.monitor.lock() {
            monitor.record_operation(
                handle.strategy_type,
                OperationType::Append,
                start_time.elapsed(),
                bytes,
            );
        }
        result
    }

    /// Delete storage and free resources
    pub fn delete_storage(&mut self, handle: &StorageHandle) -> Result<()> {
        let start_time = Instant::now();

        let strategy = self
            .strategies
            .get_mut(&handle.strategy_type)
            .ok_or_else(|| {
                Error::InvalidOperation(format!("Strategy {:?} not found", handle.strategy_type))
            })?;
        let result = strategy.delete_storage(handle);

        if result.is_ok() {
            self.bump_generation(handle.id)?;
            self.stats.record_deallocation(handle.metadata.size);
        }
        if let Ok(mut monitor) = self.monitor.lock() {
            monitor.record_operation(
                handle.strategy_type,
                OperationType::Delete,
                start_time.elapsed(),
                0,
            );
        }
        result
    }

    /// Add a storage strategy to the manager
    pub fn add_strategy(&mut self, strategy_type: StorageType, strategy: BoxedStrategy) {
        self.strategies.insert(strategy_type, strategy);
    }

    /// Replace the strategy selector (for example with an ML-driven one).
    pub fn set_selector(&mut self, selector: Box<dyn StrategySelector>) {
        self.selector = selector;
    }

    /// The monitor this manager records every operation into.
    ///
    /// Shared so that an external selector can learn from the manager's real
    /// traffic instead of being handed a private monitor that stays empty.
    pub fn monitor(&self) -> Arc<Mutex<PerformanceMonitor>> {
        Arc::clone(&self.monitor)
    }

    /// Feed the selector the metrics collected for `strategy_type`.
    pub fn train_selector(&mut self, strategy_type: StorageType) -> Result<()> {
        let metrics = {
            let monitor = self.monitor.lock().map_err(|_| {
                Error::InvalidOperation("Performance monitor lock is poisoned".to_string())
            })?;
            monitor.get_strategy_metrics(strategy_type).cloned()
        };
        if let Some(metrics) = metrics {
            self.selector.record_performance(strategy_type, &metrics);
        }
        Ok(())
    }

    /// Get memory statistics
    pub fn memory_stats(&self) -> &AtomicMemoryStats {
        &self.stats
    }

    /// Get cache statistics
    pub fn cache_stats(&self) -> Result<CacheStats> {
        self.cache_manager
            .lock()
            .map(|cache| cache.stats().clone())
            .map_err(|_| Error::InvalidOperation("Failed to acquire cache lock".to_string()))
    }

    /// Number of bytes currently held by the read cache.
    pub fn cache_size_bytes(&self) -> Result<usize> {
        self.cache_manager
            .lock()
            .map(|cache| cache.size_bytes())
            .map_err(|_| Error::InvalidOperation("Failed to acquire cache lock".to_string()))
    }

    /// Get string pool statistics
    pub fn string_pool_stats(&self) -> Result<StringPoolStats> {
        self.string_pool
            .lock()
            .map(|pool| pool.stats().clone())
            .map_err(|_| Error::InvalidOperation("Failed to acquire string pool lock".to_string()))
    }

    /// Intern a string in the manager's global pool.
    pub fn intern_string(&self, s: &str) -> Result<u32> {
        let mut pool = self.string_pool.lock().map_err(|_| {
            Error::InvalidOperation("Failed to acquire string pool lock".to_string())
        })?;
        pool.intern(s)
    }

    /// Performance metrics collected for a strategy.
    pub fn strategy_metrics(&self, strategy_type: StorageType) -> Result<Option<StrategyMetrics>> {
        let monitor = self.monitor.lock().map_err(|_| {
            Error::InvalidOperation("Performance monitor lock is poisoned".to_string())
        })?;
        Ok(monitor.get_strategy_metrics(strategy_type).cloned())
    }
}

/// Strategy selection result
#[derive(Debug, Clone)]
pub struct StrategySelection {
    /// Primary strategy to use
    pub primary: StorageType,
    /// Fallback strategies in order of preference
    pub fallbacks: Vec<StorageType>,
    /// Confidence in the selection (0.0 to 1.0)
    pub confidence: f64,
}

/// Trait for strategy selection algorithms
pub trait StrategySelector: Send + Sync {
    /// Select the best strategy for given requirements
    fn select_strategy(&self, requirements: &StorageRequirements) -> StrategySelection;

    /// Record performance feedback for learning
    fn record_performance(&mut self, strategy_type: StorageType, performance: &StrategyMetrics);
}

/// Default rule-based strategy selector
pub struct DefaultStrategySelector {
    /// Observed read throughput (bytes/sec) per strategy
    performance_history: HashMap<StorageType, Vec<f64>>,
}

impl DefaultStrategySelector {
    pub fn new() -> Self {
        Self {
            performance_history: HashMap::new(),
        }
    }

    /// Mean observed read throughput for a strategy, if any was recorded.
    pub fn observed_throughput(&self, strategy_type: StorageType) -> Option<f64> {
        let samples = self.performance_history.get(&strategy_type)?;
        if samples.is_empty() {
            return None;
        }
        Some(samples.iter().sum::<f64>() / samples.len() as f64)
    }

    /// Number of samples recorded for a strategy.
    pub fn sample_count(&self, strategy_type: StorageType) -> usize {
        self.performance_history
            .get(&strategy_type)
            .map(|s| s.len())
            .unwrap_or(0)
    }
}

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

impl StrategySelector for DefaultStrategySelector {
    fn select_strategy(&self, requirements: &StorageRequirements) -> StrategySelection {
        // Simple rule-based selection logic
        let primary = match (
            &requirements.data_characteristics,
            requirements.estimated_size,
        ) {
            (DataCharacteristics::Text, _) => StorageType::StringPool,
            (_, size) if size > 100 * 1024 * 1024 => StorageType::HybridLargeScale, // > 100MB
            (DataCharacteristics::Numeric, _) => StorageType::ColumnStore,
            (DataCharacteristics::TimeSeries, _) => StorageType::ColumnStore,
            _ => match requirements.performance_priority {
                PerformancePriority::Speed => StorageType::InMemory,
                PerformancePriority::Memory => StorageType::DiskBased,
                _ => StorageType::ColumnStore,
            },
        };

        let fallbacks = vec![
            StorageType::InMemory,
            StorageType::ColumnStore,
            StorageType::DiskBased,
        ]
        .into_iter()
        .filter(|&t| t != primary)
        .collect();

        StrategySelection {
            primary,
            fallbacks,
            confidence: 0.8, // Default confidence
        }
    }

    fn record_performance(&mut self, strategy_type: StorageType, performance: &StrategyMetrics) {
        // Actually push the observation. The old body inserted an empty Vec and
        // returned, so the "selector with learning" never learned anything.
        let throughput = performance
            .throughput(OperationType::Read)
            .or_else(|| performance.throughput(OperationType::Write));
        if let Some(throughput) = throughput {
            let samples = self.performance_history.entry(strategy_type).or_default();
            samples.push(throughput);
            // Keep the history bounded.
            if samples.len() > 1024 {
                let excess = samples.len() - 1024;
                samples.drain(0..excess);
            }
        }
    }
}

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

    #[test]
    fn test_global_string_pool() {
        let mut pool = GlobalStringPool::new();

        let id1 = pool.intern("hello").expect("intern");
        let id2 = pool.intern("world").expect("intern");
        let id3 = pool.intern("hello").expect("intern"); // Should reuse existing ID

        assert_eq!(id1, id3);
        assert_ne!(id1, id2);

        assert_eq!(pool.get(id1), Some("hello"));
        assert_eq!(pool.get(id2), Some("world"));

        assert!(pool.stats().hit_rate() > 0.0);
    }

    #[test]
    fn test_cache_manager() {
        let mut cache = CacheManager::new(1024); // 1KB cache

        let chunk1 = DataChunk::new(vec![1, 2, 3]);
        let chunk2 = DataChunk::new(vec![4, 5, 6]);

        cache.put("key1".to_string(), chunk1.clone());
        cache.put("key2".to_string(), chunk2.clone());

        assert!(cache.get("key1").is_some());
        assert!(cache.get("key2").is_some());

        assert!(cache.stats().hit_rate() > 0.0);
    }

    #[test]
    fn cache_size_accounting_survives_replacement() {
        let mut cache = CacheManager::new(1024);
        cache.put("k".to_string(), DataChunk::new(vec![0u8; 100]));
        assert_eq!(cache.size_bytes(), 100);
        // Re-putting the same key used to add without subtracting, so
        // `current_size` drifted up until everything was evicted on each insert.
        cache.put("k".to_string(), DataChunk::new(vec![0u8; 40]));
        assert_eq!(cache.size_bytes(), 40);
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn oversized_items_are_counted_not_silently_dropped() {
        let mut cache = CacheManager::new(64);
        cache.put("big".to_string(), DataChunk::new(vec![0u8; 128]));
        assert_eq!(cache.stats().rejected, 1);
        assert!(cache.is_empty());
    }

    #[test]
    fn cache_evicts_least_recently_used() {
        let mut cache = CacheManager::new(200);
        cache.put("a".to_string(), DataChunk::new(vec![0u8; 100]));
        cache.put("b".to_string(), DataChunk::new(vec![0u8; 100]));
        // Touch "a" so "b" becomes the LRU victim.
        assert!(cache.get("a").is_some());
        cache.put("c".to_string(), DataChunk::new(vec![0u8; 100]));
        assert!(cache.get("a").is_some());
        assert!(cache.get("b").is_none());
        assert!(cache.get("c").is_some());
    }

    #[test]
    fn access_counts_are_recorded() {
        let mut cache = CacheManager::new(1024);
        cache.put("k".to_string(), DataChunk::new(vec![1, 2, 3]));
        let _ = cache.get("k");
        let _ = cache.get("k");
        let (count, _age) = cache.entry_stats("k").expect("entry stats");
        assert_eq!(count, 3, "access_count never incremented");
    }

    #[test]
    fn test_performance_monitor() {
        let mut monitor = PerformanceMonitor::new();

        monitor.record_operation(
            StorageType::InMemory,
            OperationType::Read,
            std::time::Duration::from_millis(10),
            1024,
        );

        let metrics = monitor
            .get_strategy_metrics(StorageType::InMemory)
            .expect("operation should succeed");
        assert_eq!(metrics.operation_counts[&OperationType::Read], 1);
        assert_eq!(metrics.bytes_processed[&OperationType::Read], 1024);
    }

    #[test]
    fn test_default_strategy_selector() {
        let selector = DefaultStrategySelector::new();

        let requirements = StorageRequirements {
            estimated_size: 1024,
            data_characteristics: DataCharacteristics::Text,
            performance_priority: PerformancePriority::Speed,
            ..Default::default()
        };

        let selection = selector.select_strategy(&requirements);
        assert_eq!(selection.primary, StorageType::StringPool);
    }

    #[test]
    fn test_unified_memory_manager() {
        let config = MemoryConfig::default();
        let manager = UnifiedMemoryManager::new(config);

        // Test basic creation
        assert!(manager.cache_stats().is_ok());
        assert!(manager.string_pool_stats().is_ok());
    }

    #[test]
    fn builtin_strategies_are_registered() {
        // `add_strategy` was never callable (no concrete strategy could satisfy
        // `Handle = StorageHandle`), so `new()` registered nothing and
        // create_storage always failed.
        let manager = UnifiedMemoryManager::new(MemoryConfig::default());
        let registered = manager.registered_strategies();
        for expected in [
            StorageType::ColumnStore,
            StorageType::InMemory,
            StorageType::StringPool,
            StorageType::HybridLargeScale,
            StorageType::DiskBased,
        ] {
            assert!(
                registered.contains(&expected),
                "{:?} is not registered: {:?}",
                expected,
                registered
            );
        }
    }

    #[test]
    fn create_write_read_through_the_manager() {
        let mut manager = UnifiedMemoryManager::new(MemoryConfig::default());
        let config = StorageConfig {
            requirements: StorageRequirements {
                estimated_size: 4096,
                data_characteristics: DataCharacteristics::Numeric,
                ..Default::default()
            },
            ..Default::default()
        };
        let handle = manager.create_storage(&config).expect("create storage");

        let payload: Vec<u8> = (0..1024u32).map(|i| (i % 251) as u8).collect();
        manager
            .write_chunk(&handle, DataChunk::new(payload.clone()))
            .expect("write");
        let read = manager
            .read_chunk(&handle, ChunkRange::new(0, payload.len()))
            .expect("read");
        assert_eq!(read.data, payload);
    }

    #[test]
    fn writes_invalidate_the_read_cache() {
        // read -> write -> read used to return the pre-write bytes forever.
        let mut manager = UnifiedMemoryManager::new(MemoryConfig::default());
        let config = StorageConfig {
            requirements: StorageRequirements {
                estimated_size: 1024,
                data_characteristics: DataCharacteristics::Numeric,
                ..Default::default()
            },
            ..Default::default()
        };
        let handle = manager.create_storage(&config).expect("create storage");

        manager
            .write_chunk(&handle, DataChunk::new(vec![1u8; 16]))
            .expect("write");
        let first = manager
            .read_chunk(&handle, ChunkRange::new(0, 16))
            .expect("read");
        assert_eq!(first.data, vec![1u8; 16]);

        // Appending changes rows 16..32 but also invalidates the cached 0..16
        // read, which must now come from the strategy again.
        manager
            .append_chunk(&handle, DataChunk::new(vec![2u8; 16]))
            .expect("append");
        let after = manager
            .read_chunk(&handle, ChunkRange::new(0, 32))
            .expect("read");
        let mut expected = vec![1u8; 16];
        expected.extend_from_slice(&[2u8; 16]);
        assert_eq!(after.data, expected);

        let repeat = manager
            .read_chunk(&handle, ChunkRange::new(0, 16))
            .expect("read");
        assert_eq!(repeat.data, vec![1u8; 16]);
    }

    #[test]
    fn cache_hits_are_recorded_in_metrics() {
        let mut manager = UnifiedMemoryManager::new(MemoryConfig::default());
        let config = StorageConfig {
            requirements: StorageRequirements {
                estimated_size: 1024,
                data_characteristics: DataCharacteristics::Numeric,
                ..Default::default()
            },
            ..Default::default()
        };
        let handle = manager.create_storage(&config).expect("create storage");
        manager
            .write_chunk(&handle, DataChunk::new(vec![7u8; 64]))
            .expect("write");

        let _ = manager
            .read_chunk(&handle, ChunkRange::new(0, 64))
            .expect("read");
        let _ = manager
            .read_chunk(&handle, ChunkRange::new(0, 64))
            .expect("read");

        assert_eq!(manager.cache_stats().expect("stats").hits, 1);
        let metrics = manager
            .strategy_metrics(handle.strategy_type)
            .expect("metrics")
            .expect("strategy seen");
        assert_eq!(
            metrics.operation_counts.get(&OperationType::Read).copied(),
            Some(2),
            "the cache-hit read was excluded from the metrics"
        );
    }

    #[test]
    fn empty_manager_reports_a_clear_configuration_error() {
        let mut manager = UnifiedMemoryManager::empty(MemoryConfig::default());
        let err = manager
            .create_storage(&StorageConfig::default())
            .expect_err("no strategies registered");
        let message = format!("{}", err);
        assert!(
            message.contains("No storage strategy registered"),
            "unhelpful error: {}",
            message
        );
    }

    #[test]
    fn selector_records_real_performance_samples() {
        let mut selector = DefaultStrategySelector::new();
        let mut metrics = StrategyMetrics::new();
        metrics.record_operation(
            OperationType::Read,
            std::time::Duration::from_millis(10),
            1024,
        );
        selector.record_performance(StorageType::ColumnStore, &metrics);
        assert_eq!(selector.sample_count(StorageType::ColumnStore), 1);
        assert!(
            selector
                .observed_throughput(StorageType::ColumnStore)
                .unwrap_or(0.0)
                > 0.0
        );
    }
}