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
//! Storage engine traits and interfaces
//!
//! This module provides the unified trait system for all storage engines in PandRS.
//! It enables pluggable storage backends with performance-based selection.

use crate::core::error::{Error, Result};
use std::collections::HashMap;
use std::ops::Range;
use std::sync::Mutex;

/// Configuration for storage engines
#[derive(Debug, Clone)]
pub struct StorageConfig {
    /// Expected data size in bytes
    pub estimated_size: usize,
    /// Access pattern hint for optimization
    pub access_pattern: AccessPattern,
    /// Performance priority (speed vs memory)
    pub performance_priority: PerformancePriority,
    /// Durability requirements
    pub durability: DurabilityLevel,
    /// Compression preferences
    pub compression: CompressionPreference,
    /// Memory constraints
    pub memory_limit: Option<usize>,
}

/// Data access patterns for storage optimization
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AccessPattern {
    /// Sequential reads/writes
    Sequential,
    /// Random access patterns
    Random,
    /// Mostly read operations
    ReadHeavy,
    /// Mostly write operations
    WriteHeavy,
    /// Streaming data processing
    Streaming,
    /// Columnar data analysis
    Columnar,
}

/// Performance priority for storage selection
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PerformancePriority {
    /// Prioritize speed over memory usage
    Speed,
    /// Prioritize memory efficiency over speed
    Memory,
    /// Balance between speed and memory
    Balanced,
}

/// Durability requirements for data storage
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DurabilityLevel {
    /// In-memory only, data lost on restart
    Temporary,
    /// Cached with periodic flushes
    Cached,
    /// Immediately persisted to disk
    Persistent,
}

/// Compression preferences for storage
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressionPreference {
    /// No compression
    None,
    /// Automatic compression based on data characteristics
    Auto,
    /// Fast compression with moderate ratio
    Fast,
    /// Best compression ratio
    Best,
}

/// Data chunk for storage operations
#[derive(Debug, Clone)]
pub struct DataChunk {
    /// Raw data bytes
    pub data: Vec<u8>,
    /// Metadata about the chunk
    pub metadata: ChunkMetadata,
}

/// Metadata for data chunks
#[derive(Debug, Clone)]
pub struct ChunkMetadata {
    /// Number of rows in the chunk
    pub row_count: usize,
    /// Number of columns in the chunk
    pub column_count: usize,
    /// Compression type used
    pub compression: CompressionPreference,
    /// Size in bytes before compression
    pub uncompressed_size: usize,
    /// Size in bytes after compression
    pub compressed_size: usize,
}

impl DataChunk {
    /// Create a new data chunk
    pub fn new(data: Vec<u8>, metadata: ChunkMetadata) -> Self {
        Self { data, metadata }
    }

    /// Create a chunk of `size` zero bytes, described honestly as one
    /// single-byte column of `size` rows.
    ///
    /// Intended for tests, benchmarks and examples. It used to claim
    /// `size / 8` rows on an invented "8 bytes per row" assumption, which made
    /// every consumer's row accounting wrong for any other element width.
    pub fn new_test_data(size: usize) -> Self {
        let data = vec![0u8; size];
        let metadata = ChunkMetadata {
            row_count: size,
            column_count: 1,
            compression: CompressionPreference::None,
            uncompressed_size: size,
            compressed_size: size,
        };
        Self { data, metadata }
    }

    /// Get chunk size in bytes
    pub fn size(&self) -> usize {
        self.data.len()
    }
}

// ---------------------------------------------------------------------------
// Bridges between the engine layer (this module) and the strategy layer
// (`crate::storage::unified_memory`).
//
// The two layers keep separate enums because the strategy layer models states
// the engine layer does not (`Speed::VerySlow`, `Efficiency::Outstanding`,
// `DurabilityLevel::HighDurability`, ...). Rather than duplicate the mapping
// logic at every call site, the faithful conversions live here once.
// ---------------------------------------------------------------------------

use crate::storage::unified_memory as unified;

impl From<Speed> for unified::Speed {
    fn from(speed: Speed) -> Self {
        match speed {
            Speed::Slow => unified::Speed::Slow,
            Speed::Medium => unified::Speed::Medium,
            Speed::Fast => unified::Speed::Fast,
            Speed::VeryFast => unified::Speed::VeryFast,
        }
    }
}

impl From<Efficiency> for unified::Efficiency {
    fn from(efficiency: Efficiency) -> Self {
        match efficiency {
            Efficiency::Poor => unified::Efficiency::Poor,
            Efficiency::Fair => unified::Efficiency::Fair,
            Efficiency::Good => unified::Efficiency::Good,
            Efficiency::Excellent => unified::Efficiency::Excellent,
        }
    }
}

impl From<AccessPattern> for unified::AccessPattern {
    fn from(pattern: AccessPattern) -> Self {
        match pattern {
            AccessPattern::Sequential => unified::AccessPattern::Sequential,
            AccessPattern::Random => unified::AccessPattern::Random,
            AccessPattern::Streaming => unified::AccessPattern::Streaming,
            AccessPattern::Columnar => unified::AccessPattern::Columnar,
            // The engine layer's read/write skew has no direct counterpart in
            // the strategy layer's locality vocabulary; both describe workloads
            // dominated by repeated access to the same working set.
            AccessPattern::ReadHeavy => unified::AccessPattern::HighLocality,
            AccessPattern::WriteHeavy => unified::AccessPattern::LowLocality,
        }
    }
}

impl From<DurabilityLevel> for unified::DurabilityLevel {
    fn from(level: DurabilityLevel) -> Self {
        match level {
            DurabilityLevel::Temporary => unified::DurabilityLevel::Temporary,
            DurabilityLevel::Cached => unified::DurabilityLevel::Session,
            DurabilityLevel::Persistent => unified::DurabilityLevel::Durable,
        }
    }
}

impl From<unified::DurabilityLevel> for DurabilityLevel {
    fn from(level: unified::DurabilityLevel) -> Self {
        match level {
            unified::DurabilityLevel::Temporary => DurabilityLevel::Temporary,
            unified::DurabilityLevel::Session => DurabilityLevel::Cached,
            // The engine layer has no replication tier; both durable levels
            // map onto its strongest guarantee (fsync on write).
            unified::DurabilityLevel::Durable | unified::DurabilityLevel::HighDurability => {
                DurabilityLevel::Persistent
            }
        }
    }
}

impl From<CompressionPreference> for unified::CompressionPreference {
    fn from(preference: CompressionPreference) -> Self {
        match preference {
            CompressionPreference::None => unified::CompressionPreference::None,
            CompressionPreference::Auto => unified::CompressionPreference::Auto,
            CompressionPreference::Fast => unified::CompressionPreference::Fast,
            CompressionPreference::Best => unified::CompressionPreference::High,
        }
    }
}

impl From<PerformancePriority> for unified::PerformancePriority {
    fn from(priority: PerformancePriority) -> Self {
        match priority {
            PerformancePriority::Speed => unified::PerformancePriority::Speed,
            PerformancePriority::Memory => unified::PerformancePriority::Memory,
            PerformancePriority::Balanced => unified::PerformancePriority::Balanced,
        }
    }
}

/// Performance characteristics of a storage engine
#[derive(Debug, Clone)]
pub struct PerformanceProfile {
    /// Read speed rating
    pub read_speed: Speed,
    /// Write speed rating
    pub write_speed: Speed,
    /// Memory efficiency rating
    pub memory_efficiency: Efficiency,
    /// Typical compression ratio achieved
    pub compression_ratio: f64,
    /// Random access performance
    pub random_access_speed: Speed,
    /// Sequential access performance
    pub sequential_access_speed: Speed,
}

/// Speed ratings for performance characteristics
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Speed {
    Slow,
    Medium,
    Fast,
    VeryFast,
}

/// Efficiency ratings for resource usage
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Efficiency {
    Poor,
    Fair,
    Good,
    Excellent,
}

/// Storage engine statistics
#[derive(Debug, Clone)]
pub struct StorageStatistics {
    /// Total bytes stored
    pub total_size: usize,
    /// Number of chunks stored
    pub chunk_count: usize,
    /// Average compression ratio
    pub avg_compression_ratio: f64,
    /// Read operation count
    pub read_operations: u64,
    /// Write operation count
    pub write_operations: u64,
    /// Cache hit rate (0.0 to 1.0)
    pub cache_hit_rate: f64,
}

/// Base trait for all storage engines in PandRS
pub trait StorageEngine: Send + Sync {
    type Handle: Clone + Send + Sync;
    type Error: std::error::Error + Send + Sync + 'static;

    /// Create new storage with specific configuration
    fn create_storage(&mut self, config: &StorageConfig) -> Result<Self::Handle>;

    /// Read data chunk from storage
    fn read_chunk(&self, handle: &Self::Handle, range: Range<usize>) -> Result<DataChunk>;

    /// Write data chunk to storage
    fn write_chunk(&mut self, handle: &Self::Handle, chunk: DataChunk) -> Result<()>;

    /// Append data chunk to existing storage
    fn append_chunk(&mut self, handle: &Self::Handle, chunk: DataChunk) -> Result<()>;

    /// Flush pending writes to persistent storage
    fn flush(&mut self, handle: &Self::Handle) -> Result<()>;

    /// Delete storage and free resources
    fn delete_storage(&mut self, handle: &Self::Handle) -> Result<()>;

    /// Get performance characteristics of this storage engine
    fn performance_profile(&self) -> PerformanceProfile;

    /// Get current storage statistics
    fn storage_stats(&self, handle: &Self::Handle) -> Result<StorageStatistics>;

    /// Check if engine supports random access
    fn supports_random_access(&self) -> bool;

    /// Check if engine supports streaming
    fn supports_streaming(&self) -> bool;

    /// Check if engine supports compression
    fn supports_compression(&self) -> bool;

    /// Get optimal chunk size for this engine
    fn optimal_chunk_size(&self) -> usize;

    /// Get memory overhead per chunk
    fn memory_overhead(&self) -> usize;

    /// Optimize engine for specific access pattern
    fn optimize_for_pattern(&mut self, pattern: AccessPattern) -> Result<()>;

    /// Compact storage to reduce fragmentation
    fn compact(&mut self, handle: &Self::Handle) -> Result<()>;
}

/// Storage engine selector trait
pub trait StorageStrategy: Send + Sync {
    /// Select the best storage engine for given requirements
    fn select_engine(&self, requirements: &StorageRequirements) -> StorageEngineId;

    /// Check if data should be migrated to a different engine
    fn should_migrate(
        &self,
        handle: &StorageHandle,
        new_requirements: &StorageRequirements,
    ) -> bool;

    /// Migrate data between storage engines
    fn migrate_storage(
        &mut self,
        from: &StorageHandle,
        to: &StorageEngineId,
    ) -> Result<StorageHandle>;
}

/// Requirements for storage engine selection
#[derive(Debug, Clone)]
pub struct StorageRequirements {
    /// Configuration parameters
    pub config: StorageConfig,
    /// Expected workload characteristics
    pub workload: WorkloadCharacteristics,
    /// Resource constraints
    pub constraints: ResourceConstraints,
}

/// Workload characteristics for storage optimization
#[derive(Debug, Clone)]
pub struct WorkloadCharacteristics {
    /// Read/write ratio (0.0 = write-only, 1.0 = read-only)
    pub read_write_ratio: f64,
    /// Average query size in bytes
    pub avg_query_size: usize,
    /// Concurrent access level
    pub concurrency_level: u32,
    /// Data locality pattern
    pub locality_pattern: LocalityPattern,
}

/// Data locality patterns
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LocalityPattern {
    /// High temporal locality (recently accessed data accessed again)
    HighTemporal,
    /// High spatial locality (nearby data accessed together)
    HighSpatial,
    /// Mixed locality patterns
    Mixed,
    /// Poor locality (random access)
    PoorLocality,
}

/// Resource constraints for storage engines
#[derive(Debug, Clone)]
pub struct ResourceConstraints {
    /// Maximum memory usage in bytes
    pub max_memory: Option<usize>,
    /// Maximum disk usage in bytes
    pub max_disk: Option<usize>,
    /// CPU budget (relative scale)
    pub cpu_budget: CpuBudget,
    /// Network bandwidth constraints
    pub network_budget: Option<NetworkBandwidth>,
}

/// CPU budget levels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CpuBudget {
    Low,
    Medium,
    High,
    Unlimited,
}

/// Network bandwidth constraints
#[derive(Debug, Clone, Copy)]
pub struct NetworkBandwidth {
    /// Bandwidth in bytes per second
    pub bytes_per_second: u64,
    /// Latency in milliseconds
    pub latency_ms: u32,
}

/// Storage engine identifier
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StorageEngineId {
    ColumnStore,
    MemoryMapped,
    DiskStorage,
    StringPool,
    Hybrid,
}

/// Storage handle that tracks engine and metadata
#[derive(Debug, Clone)]
pub struct StorageHandle {
    /// Engine that owns this storage
    pub engine_id: StorageEngineId,
    /// Engine-specific handle
    pub inner_handle: StorageHandleInner,
    /// Storage metadata
    pub metadata: StorageMetadata,
}

/// Engine-specific storage handle
#[derive(Debug, Clone)]
pub enum StorageHandleInner {
    ColumnStore(crate::storage::column_store::ColumnStoreHandle),
    MemoryMapped(MemoryMappedHandle),
    DiskStorage(crate::storage::disk::DiskStorageHandle),
    StringPool(StringPoolHandle),
}

/// Storage metadata
#[derive(Debug, Clone)]
pub struct StorageMetadata {
    /// Creation timestamp
    pub created_at: std::time::SystemTime,
    /// Last accessed timestamp
    pub last_accessed: std::time::SystemTime,
    /// Last modified timestamp
    pub last_modified: std::time::SystemTime,
    /// Total size in bytes
    pub size_bytes: usize,
    /// Access count
    pub access_count: u64,
    /// Configuration used
    pub config: StorageConfig,
}

// Placeholder handle types for storage engines
// These will be defined in their respective modules
pub mod handles {
    /// Handle for column store operations
    #[derive(Debug, Clone)]
    pub struct ColumnStoreHandle {
        pub id: usize,
    }

    /// Handle for memory mapped operations
    #[derive(Debug, Clone)]
    pub struct MemoryMappedHandle {
        pub id: usize,
    }

    /// Handle for disk storage operations
    #[derive(Debug, Clone)]
    pub struct DiskStorageHandle {
        pub id: usize,
    }

    /// Handle for string pool operations
    #[derive(Debug, Clone)]
    pub struct StringPoolHandle {
        pub id: usize,
    }
}

// Import the handle types for use in StorageHandleInner
pub use handles::*;

/// Unified storage manager that coordinates multiple engines
pub struct UnifiedStorageManager {
    /// Column store engine
    column_store: Option<crate::storage::ColumnStore>,
    /// File-backed engine, created on first use under [`Self::disk_root`]
    disk_storage: Option<crate::storage::disk::DiskStorage>,
    /// Directory the disk engine is rooted at
    disk_root: std::path::PathBuf,
    /// Storage strategy for engine selection
    strategy: Box<dyn StorageStrategy>,
    /// Active storage handles
    handles: HashMap<StorageHandleId, StorageHandle>,
    /// Performance monitor fed by every read and write.
    ///
    /// Behind a `Mutex` because `read_chunk` takes `&self`; the field used to be
    /// a plain `PerformanceMonitor` that nothing ever wrote to.
    monitor: Mutex<PerformanceMonitor>,
    /// Next handle ID
    next_handle_id: StorageHandleId,
}

/// Storage handle identifier
pub type StorageHandleId = usize;

/// Performance monitor for storage operations
#[derive(Debug)]
pub struct PerformanceMonitor {
    /// Engine performance metrics
    engine_metrics: HashMap<StorageEngineId, EngineMetrics>,
}

/// Performance metrics for a storage engine
#[derive(Debug, Clone)]
pub struct EngineMetrics {
    /// Average read latency in nanoseconds
    pub avg_read_latency: u64,
    /// Average write latency in nanoseconds
    pub avg_write_latency: u64,
    /// Throughput in bytes per second
    pub throughput: u64,
    /// Error rate (0.0 to 1.0)
    pub error_rate: f64,
    /// Memory usage in bytes
    pub memory_usage: usize,
}

impl PerformanceMonitor {
    /// Create a new performance monitor
    pub fn new() -> Self {
        Self {
            engine_metrics: HashMap::new(),
        }
    }

    /// Record operation performance
    pub fn record_operation(
        &mut self,
        engine_id: StorageEngineId,
        operation: &Operation,
        latency: std::time::Duration,
    ) {
        // Implementation for recording performance metrics
        let metrics = self
            .engine_metrics
            .entry(engine_id)
            .or_insert_with(|| EngineMetrics {
                avg_read_latency: 0,
                avg_write_latency: 0,
                throughput: 0,
                error_rate: 0.0,
                memory_usage: 0,
            });

        // Update metrics based on operation type
        match operation {
            Operation::Read { .. } => {
                metrics.avg_read_latency =
                    (metrics.avg_read_latency + latency.as_nanos() as u64) / 2;
            }
            Operation::Write { .. } => {
                metrics.avg_write_latency =
                    (metrics.avg_write_latency + latency.as_nanos() as u64) / 2;
            }
        }
    }

    /// Get metrics for an engine
    pub fn get_metrics(&self, engine_id: StorageEngineId) -> Option<&EngineMetrics> {
        self.engine_metrics.get(&engine_id)
    }
}

/// Storage operation types for performance monitoring
#[derive(Debug, Clone)]
pub enum Operation {
    Read { size: usize, range: Range<usize> },
    Write { size: usize, compressed: bool },
}

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

impl UnifiedStorageManager {
    /// Create a new unified storage manager.
    ///
    /// The file-backed engine is rooted at a process-specific directory under
    /// [`std::env::temp_dir`]; use [`UnifiedStorageManager::with_disk_root`] to
    /// place it somewhere durable.
    pub fn new() -> Self {
        static NEXT_ROOT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(1);
        let root = std::env::temp_dir().join(format!(
            "pandrs_unified_storage_{}_{}",
            std::process::id(),
            NEXT_ROOT.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
        ));
        Self::with_disk_root(root)
    }

    /// Create a manager whose file-backed engine lives under `disk_root`.
    pub fn with_disk_root<P: Into<std::path::PathBuf>>(disk_root: P) -> Self {
        Self {
            column_store: Some(crate::storage::ColumnStore::new()),
            disk_storage: None,
            disk_root: disk_root.into(),
            strategy: Box::new(DefaultStorageStrategy::new()),
            handles: HashMap::new(),
            monitor: Mutex::new(PerformanceMonitor::new()),
            next_handle_id: 1,
        }
    }

    /// The file-backed engine, created on first use.
    fn disk_engine(&mut self) -> Result<&mut crate::storage::disk::DiskStorage> {
        if self.disk_storage.is_none() {
            self.disk_storage = Some(crate::storage::disk::DiskStorage::new(&self.disk_root)?);
        }
        self.disk_storage.as_mut().ok_or_else(|| {
            Error::InvalidOperation("Disk storage engine is unavailable".to_string())
        })
    }

    /// Metrics observed for `engine_id`, or `None` if it has served no
    /// operation yet.
    pub fn engine_metrics(&self, engine_id: StorageEngineId) -> Option<EngineMetrics> {
        self.monitor
            .lock()
            .ok()
            .and_then(|monitor| monitor.get_metrics(engine_id).cloned())
    }

    /// Record one completed operation, ignoring a poisoned monitor lock rather
    /// than failing the storage operation that already succeeded.
    fn record(
        &self,
        engine_id: StorageEngineId,
        operation: &Operation,
        latency: std::time::Duration,
    ) {
        match self.monitor.lock() {
            Ok(mut monitor) => monitor.record_operation(engine_id, operation, latency),
            Err(_) => log::warn!("Storage performance monitor lock is poisoned; sample dropped"),
        }
    }

    /// Create storage using optimal engine selection
    pub fn create_storage(
        &mut self,
        requirements: &StorageRequirements,
    ) -> Result<StorageHandleId> {
        // Select optimal engine
        let engine_id = self.strategy.select_engine(requirements);

        // For now, we only support column store
        let inner_handle = match engine_id {
            StorageEngineId::ColumnStore => {
                if let Some(ref mut engine) = self.column_store {
                    let handle = engine.create_storage(&requirements.config)?;
                    StorageHandleInner::ColumnStore(handle)
                } else {
                    return Err(Error::InvalidOperation(
                        "Column store not available for create_storage".to_string(),
                    ));
                }
            }
            StorageEngineId::DiskStorage => {
                let config = requirements.config.clone();
                let handle = self.disk_engine()?.create_storage(&config)?;
                StorageHandleInner::DiskStorage(handle)
            }
            // `MemoryMapped` maps an *existing* file read-only (see
            // `storage::memory_mapped`) and `StringPool`/`Hybrid` are reached
            // through `storage::unified_manager`, so neither can back a
            // create-then-write request here.
            _ => {
                return Err(Error::NotImplemented(format!(
                    "Storage engine {:?} cannot create writable storage; use \
                     storage::unified_manager::UnifiedMemoryManager for it",
                    engine_id
                )));
            }
        };

        // Create unified handle
        let handle_id = self.next_handle_id;
        self.next_handle_id += 1;

        let handle = StorageHandle {
            engine_id,
            inner_handle,
            metadata: StorageMetadata {
                created_at: std::time::SystemTime::now(),
                last_accessed: std::time::SystemTime::now(),
                last_modified: std::time::SystemTime::now(),
                size_bytes: 0,
                access_count: 0,
                config: requirements.config.clone(),
            },
        };

        self.handles.insert(handle_id, handle);
        Ok(handle_id)
    }

    /// Read chunk from storage
    pub fn read_chunk(&self, handle_id: StorageHandleId, range: Range<usize>) -> Result<DataChunk> {
        let handle = self.handles.get(&handle_id).ok_or_else(|| {
            Error::InvalidOperation("Invalid handle ID for read_chunk".to_string())
        })?;

        let started = std::time::Instant::now();
        let result = match (&handle.engine_id, &handle.inner_handle) {
            (StorageEngineId::ColumnStore, StorageHandleInner::ColumnStore(cs_handle)) => {
                if let Some(ref engine) = self.column_store {
                    engine.read_chunk(cs_handle, range.clone())
                } else {
                    Err(Error::InvalidOperation(
                        "Column store not available for read_chunk".to_string(),
                    ))
                }
            }
            (StorageEngineId::DiskStorage, StorageHandleInner::DiskStorage(disk_handle)) => {
                match self.disk_storage {
                    Some(ref engine) => engine.read_chunk(disk_handle, range.clone()),
                    None => Err(Error::InvalidOperation(
                        "Disk storage engine not initialised for read_chunk".to_string(),
                    )),
                }
            }
            _ => Err(Error::NotImplemented(format!(
                "Engine {:?}",
                handle.engine_id
            ))),
        };

        if let Ok(ref chunk) = result {
            self.record(
                handle.engine_id,
                &Operation::Read {
                    size: chunk.data.len(),
                    range,
                },
                started.elapsed(),
            );
        }
        result
    }

    /// Write chunk to storage
    pub fn write_chunk(&mut self, handle_id: StorageHandleId, chunk: DataChunk) -> Result<()> {
        let handle = self.handles.get(&handle_id).ok_or_else(|| {
            Error::InvalidOperation("Invalid handle ID for write_chunk".to_string())
        })?;

        let engine_id = handle.engine_id;
        let written = chunk.data.len();
        let compressed = chunk.metadata.compression != CompressionPreference::None;
        let started = std::time::Instant::now();
        let result = match (&handle.engine_id, &handle.inner_handle) {
            (StorageEngineId::ColumnStore, StorageHandleInner::ColumnStore(cs_handle)) => {
                if let Some(ref mut engine) = self.column_store {
                    engine.write_chunk(cs_handle, chunk)
                } else {
                    Err(Error::InvalidOperation(
                        "Column store not available for write_chunk".to_string(),
                    ))
                }
            }
            (StorageEngineId::DiskStorage, StorageHandleInner::DiskStorage(disk_handle)) => {
                match self.disk_storage {
                    Some(ref mut engine) => engine.write_chunk(disk_handle, chunk),
                    None => Err(Error::InvalidOperation(
                        "Disk storage engine not initialised for write_chunk".to_string(),
                    )),
                }
            }
            _ => Err(Error::NotImplemented(format!(
                "Engine {:?}",
                handle.engine_id
            ))),
        };

        // Update handle metadata on success
        if result.is_ok() {
            self.record(
                engine_id,
                &Operation::Write {
                    size: written,
                    compressed,
                },
                started.elapsed(),
            );
            if let Some(handle) = self.handles.get_mut(&handle_id) {
                handle.metadata.last_modified = std::time::SystemTime::now();
                handle.metadata.access_count += 1;
            }
        }

        result
    }
}

/// Default storage strategy implementation
pub struct DefaultStorageStrategy {
    /// Engine preferences based on data characteristics
    preferences: HashMap<AccessPattern, StorageEngineId>,
}

impl DefaultStorageStrategy {
    /// Create a new default storage strategy
    pub fn new() -> Self {
        let mut preferences = HashMap::new();
        preferences.insert(AccessPattern::Sequential, StorageEngineId::ColumnStore);
        // Random access used to be routed to `MemoryMapped`, which maps an
        // *existing* file read-only and therefore cannot serve
        // `create_storage`: every random-access request failed with
        // `NotImplemented`. The in-memory column store is the fastest engine
        // here that can actually be created and written.
        preferences.insert(AccessPattern::Random, StorageEngineId::ColumnStore);
        preferences.insert(AccessPattern::ReadHeavy, StorageEngineId::ColumnStore);
        preferences.insert(AccessPattern::WriteHeavy, StorageEngineId::DiskStorage);
        preferences.insert(AccessPattern::Streaming, StorageEngineId::DiskStorage);
        preferences.insert(AccessPattern::Columnar, StorageEngineId::ColumnStore);

        Self { preferences }
    }
}

impl StorageStrategy for DefaultStorageStrategy {
    fn select_engine(&self, requirements: &StorageRequirements) -> StorageEngineId {
        // Simple strategy: select based on access pattern
        self.preferences
            .get(&requirements.config.access_pattern)
            .copied()
            .unwrap_or(StorageEngineId::ColumnStore)
    }

    fn should_migrate(
        &self,
        _handle: &StorageHandle,
        _new_requirements: &StorageRequirements,
    ) -> bool {
        // For now, don't migrate automatically
        false
    }

    fn migrate_storage(
        &mut self,
        _from: &StorageHandle,
        _to: &StorageEngineId,
    ) -> Result<StorageHandle> {
        // Migration not implemented yet
        Err(Error::NotImplemented("Storage migration".to_string()))
    }
}

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