horon-engine 0.7.0

Hyperbolic tree tensor engine - hierarchical data storage with depth-independent lookups
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
//! extension.rs - Pluggable Extension System for horon-engine
//!
//! Provides a comprehensive extension framework for the HTT system,
//! enabling integration with various backends, databases, and specialized
//! processing capabilities.
//!
//! ## Extension Categories
//!
//! - **Storage Adapters**: Connect HTT to databases (MySQL, PostgreSQL, MongoDB, SQLite)
//! - **Persistence Providers**: Enable durable storage on disk or distributed systems
//! - **Semantic Analyzers**: Extract meaning and structure from text or code
//! - **Geometric Enhancers**: Discover and utilize hidden geometric patterns in data

use std::any::Any;
use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter};
use std::sync::{Arc, RwLock};
use super::tensor_network::CompressedNode;
use super::tree_tensor::IntegrationError;

/// Result type for extension operations.
pub type ExtensionResult<T> = Result<T, ExtensionError>;

/// Error type for extension operations.
#[derive(Debug)]
pub enum ExtensionError {
    /// Storage-related errors
    Storage(String),
    /// Processing-related errors
    Processing(String),
    /// Configuration errors
    Configuration(String),
    /// Serialization errors
    Serialization(String),
    /// Backend-specific errors
    Backend(String),
    /// Unsupported operation
    Unsupported(String),
    /// Integration errors from the main system
    Integration(IntegrationError),
}

impl From<IntegrationError> for ExtensionError {
    fn from(err: IntegrationError) -> Self {
        ExtensionError::Integration(err)
    }
}

impl From<serde_json::Error> for ExtensionError {
    fn from(err: serde_json::Error) -> Self {
        ExtensionError::Serialization(err.to_string())
    }
}

impl fmt::Display for ExtensionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ExtensionError::Storage(msg) => write!(f, "Storage error: {}", msg),
            ExtensionError::Processing(msg) => write!(f, "Processing error: {}", msg),
            ExtensionError::Configuration(msg) => write!(f, "Configuration error: {}", msg),
            ExtensionError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
            ExtensionError::Backend(msg) => write!(f, "Backend error: {}", msg),
            ExtensionError::Unsupported(msg) => write!(f, "Unsupported operation: {}", msg),
            ExtensionError::Integration(err) => write!(f, "Integration error: {}", err),
        }
    }
}

impl std::error::Error for ExtensionError {}

/// Extension capability flags.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExtensionCapabilities {
    /// Can store data
    pub can_store: bool,
    /// Can retrieve data
    pub can_retrieve: bool,
    /// Can process nodes
    pub can_process: bool,
    /// Can enhance queries
    pub can_enhance_queries: bool,
    /// Can optimize structure
    pub can_optimize: bool,
    /// Can analyze semantic content
    pub can_analyze_semantics: bool,
}

impl ExtensionCapabilities {
    /// Create new capabilities with all flags disabled.
    pub fn none() -> Self {
        Self {
            can_store: false,
            can_retrieve: false,
            can_process: false,
            can_enhance_queries: false,
            can_optimize: false,
            can_analyze_semantics: false,
        }
    }

    /// Create capabilities for a storage provider.
    pub fn storage() -> Self {
        Self {
            can_store: true,
            can_retrieve: true,
            can_process: false,
            can_enhance_queries: false,
            can_optimize: false,
            can_analyze_semantics: false,
        }
    }

    /// Create capabilities for a processor extension.
    pub fn processor() -> Self {
        Self {
            can_store: false,
            can_retrieve: false,
            can_process: true,
            can_enhance_queries: true,
            can_optimize: false,
            can_analyze_semantics: false,
        }
    }

    /// Create capabilities for a semantic analyzer.
    pub fn semantic_analyzer() -> Self {
        Self {
            can_store: false,
            can_retrieve: false,
            can_process: true,
            can_enhance_queries: true,
            can_optimize: false,
            can_analyze_semantics: true,
        }
    }

    /// Create capabilities for an optimizer.
    pub fn optimizer() -> Self {
        Self {
            can_store: false,
            can_retrieve: false,
            can_process: false,
            can_enhance_queries: false,
            can_optimize: true,
            can_analyze_semantics: false,
        }
    }
}

/// Base extension interface for the HTT module.
pub trait HTTExtensionBase: Send + Sync {
    /// Get the extension name.
    fn name(&self) -> &str;

    /// Get the extension version.
    fn version(&self) -> &str;

    /// Get the extension description.
    fn description(&self) -> &str;

    /// Get the extension capabilities.
    fn capabilities(&self) -> ExtensionCapabilities;

    /// Get extension metadata.
    fn metadata(&self) -> HashMap<String, String>;

    /// Initialize the extension.
    fn initialize(&mut self) -> ExtensionResult<()>;

    /// Shut down the extension.
    fn shutdown(&mut self) -> ExtensionResult<()>;

    /// Check if the extension is compatible with the given data type.
    fn is_compatible_with(&self, data_type: &str) -> bool;

    /// Convert the extension to Any for downcasting.
    fn as_any(&self) -> &dyn Any;

    /// Convert the extension to mutable Any for downcasting.
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

/// Node processing extension interface.
///
/// Allows extensions to process CompressedNode instances,
/// enhancing them with additional capabilities or extracting information.
pub trait HTTExtension: HTTExtensionBase {
    /// Process a node, potentially transforming it.
    fn process_node(&self, node: &CompressedNode) -> ExtensionResult<CompressedNode>;

    /// Process multiple nodes in batch.
    fn process_nodes(&self, nodes: &[CompressedNode]) -> ExtensionResult<Vec<CompressedNode>> {
        let mut results = Vec::with_capacity(nodes.len());
        for node in nodes {
            results.push(self.process_node(node)?);
        }
        Ok(results)
    }

    /// Enhance a query before it's executed.
    fn enhance_query(
        &self,
        path: &str,
        query_params: &HashMap<String, String>,
    ) -> ExtensionResult<(String, HashMap<String, String>)>;

    /// Extract information from a node.
    fn extract_info(&self, node: &CompressedNode) -> ExtensionResult<HashMap<String, String>>;

    /// Check if this extension can process the given node.
    fn can_process(&self, node: &CompressedNode) -> bool;
}

/// Storage provider interface for HTT.
///
/// Allows HTT to be integrated with various storage backends.
pub trait HTTStorageProvider: HTTExtensionBase {
    /// Store data at the given path.
    fn store(&mut self, path: &str, data: &[u8]) -> ExtensionResult<()>;

    /// Retrieve data from the given path.
    fn retrieve(&self, path: &str) -> ExtensionResult<Vec<u8>>;

    /// Delete data at the given path.
    fn delete(&mut self, path: &str) -> ExtensionResult<()>;

    /// Check if data exists at the given path.
    fn exists(&self, path: &str) -> ExtensionResult<bool>;

    /// List paths matching the given prefix.
    fn list(&self, prefix: &str) -> ExtensionResult<Vec<String>>;

    /// Get metadata for the given path.
    fn get_metadata(&self, path: &str) -> ExtensionResult<HashMap<String, String>>;

    /// Set metadata for the given path.
    fn set_metadata(&mut self, path: &str, key: &str, value: &str) -> ExtensionResult<()>;

    /// Flush any pending changes to durable storage.
    fn flush(&mut self) -> ExtensionResult<()>;

    /// Begin a transaction.
    fn begin_transaction(&mut self) -> ExtensionResult<()>;

    /// Commit a transaction.
    fn commit_transaction(&mut self) -> ExtensionResult<()>;

    /// Rollback a transaction.
    fn rollback_transaction(&mut self) -> ExtensionResult<()>;

    /// Check if the provider supports transactions.
    fn supports_transactions(&self) -> bool;

    /// Check if the provider is available.
    fn is_available(&self) -> bool;

    /// Get provider statistics.
    fn stats(&self) -> ExtensionResult<HashMap<String, String>>;
}

/// Extension manager for coordinating multiple extensions.
pub struct HTTExtensionManager {
    /// Registered extensions
    extensions: HashMap<String, Box<dyn HTTExtensionBase>>,
    /// Extension configurations
    configs: HashMap<String, HashMap<String, String>>,
    /// Extension dependency ordering
    dependency_order: Vec<String>,
    /// Extension capabilities cache
    capabilities_cache: HashMap<String, ExtensionCapabilities>,
}

impl HTTExtensionManager {
    /// Create a new extension manager.
    pub fn new() -> Self {
        Self {
            extensions: HashMap::new(),
            configs: HashMap::new(),
            dependency_order: Vec::new(),
            capabilities_cache: HashMap::new(),
        }
    }

    /// Register an extension.
    pub fn register_extension<E: HTTExtensionBase + 'static>(
        &mut self,
        extension: E,
        config: HashMap<String, String>,
    ) -> ExtensionResult<()> {
        let name = extension.name().to_string();

        self.capabilities_cache
            .insert(name.clone(), extension.capabilities());
        self.extensions.insert(name.clone(), Box::new(extension));
        self.configs.insert(name.clone(), config);

        if !self.dependency_order.contains(&name) {
            self.dependency_order.push(name);
        }

        Ok(())
    }

    /// Get an extension by name.
    pub fn get_extension(&self, name: &str) -> Option<&dyn HTTExtensionBase> {
        self.extensions.get(name).map(|ext| ext.as_ref())
    }

    /// Get a mutable extension by name.
    pub fn get_extension_mut(
        &mut self,
        name: &str,
    ) -> Option<&mut (dyn HTTExtensionBase + 'static)> {
        self.extensions.get_mut(name).map(|ext| &mut **ext)
    }

    /// Get an extension as a specific type.
    pub fn get_extension_as<T: 'static>(&self, name: &str) -> Option<&T> {
        self.get_extension(name)
            .and_then(|ext| ext.as_any().downcast_ref::<T>())
    }

    /// Get a mutable extension as a specific type.
    pub fn get_extension_mut_as<T: 'static>(&mut self, name: &str) -> Option<&mut T> {
        self.get_extension_mut(name)
            .and_then(|ext| ext.as_any_mut().downcast_mut::<T>())
    }

    /// Get all registered extension names.
    pub fn extension_names(&self) -> Vec<String> {
        self.extensions.keys().cloned().collect()
    }

    /// Get all extensions with specific capabilities.
    pub fn extensions_with_capability(
        &self,
        capability: fn(&ExtensionCapabilities) -> bool,
    ) -> Vec<&dyn HTTExtensionBase> {
        self.extensions
            .values()
            .filter(|ext| capability(&ext.capabilities()))
            .map(|ext| ext.as_ref())
            .collect()
    }

    /// Initialize all extensions in dependency order.
    pub fn initialize_all(&mut self) -> ExtensionResult<()> {
        for name in &self.dependency_order {
            if let Some(ext) = self.extensions.get_mut(name) {
                ext.initialize()?;
            }
        }
        Ok(())
    }

    /// Shut down all extensions in reverse dependency order.
    pub fn shutdown_all(&mut self) -> ExtensionResult<()> {
        for name in self.dependency_order.iter().rev() {
            if let Some(ext) = self.extensions.get_mut(name) {
                ext.shutdown()?;
            }
        }
        Ok(())
    }

    /// Get configuration for an extension.
    pub fn get_config(&self, name: &str) -> Option<&HashMap<String, String>> {
        self.configs.get(name)
    }

    /// Set configuration for an extension.
    pub fn set_config(
        &mut self,
        name: &str,
        config: HashMap<String, String>,
    ) -> ExtensionResult<()> {
        if self.extensions.contains_key(name) {
            self.configs.insert(name.to_string(), config);
            Ok(())
        } else {
            Err(ExtensionError::Configuration(format!(
                "Extension {} not found",
                name
            )))
        }
    }

    /// Check if an extension is registered.
    pub fn has_extension(&self, name: &str) -> bool {
        self.extensions.contains_key(name)
    }

    /// Remove an extension.
    pub fn remove_extension(&mut self, name: &str) -> ExtensionResult<()> {
        if !self.extensions.contains_key(name) {
            return Err(ExtensionError::Configuration(format!(
                "Extension {} not found",
                name
            )));
        }

        if let Some(ext) = self.extensions.get_mut(name) {
            ext.shutdown()?;
        }

        self.extensions.remove(name);
        self.configs.remove(name);
        self.capabilities_cache.remove(name);
        self.dependency_order.retain(|n| n != name);

        Ok(())
    }
}

impl Debug for HTTExtensionManager {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("HTTExtensionManager")
            .field("extension_count", &self.extensions.len())
            .field("extension_names", &self.extension_names())
            .finish()
    }
}

/// Extension registry for system-wide extension management.
pub struct ExtensionRegistry {
    /// The singleton manager instance
    manager: Arc<RwLock<HTTExtensionManager>>,
}

impl ExtensionRegistry {
    /// Get the global extension registry instance.
    pub fn instance() -> Self {
        use std::sync::OnceLock;
        static MANAGER: OnceLock<Arc<RwLock<HTTExtensionManager>>> = OnceLock::new();

        let manager = MANAGER.get_or_init(|| {
            Arc::new(RwLock::new(HTTExtensionManager::new()))
        });

        Self {
            manager: manager.clone(),
        }
    }

    /// Get a read-only reference to the extension manager.
    pub fn manager(
        &self,
    ) -> ExtensionResult<std::sync::RwLockReadGuard<'_, HTTExtensionManager>> {
        self.manager.read().map_err(|e| {
            ExtensionError::Configuration(format!("Failed to lock extension manager: {}", e))
        })
    }

    /// Get a mutable reference to the extension manager.
    pub fn manager_mut(
        &self,
    ) -> ExtensionResult<std::sync::RwLockWriteGuard<'_, HTTExtensionManager>> {
        self.manager.write().map_err(|e| {
            ExtensionError::Configuration(format!("Failed to lock extension manager: {}", e))
        })
    }
}

/// Simple file system storage provider implementation.
pub struct FileSystemStorageProvider {
    /// Root directory for storage
    root_dir: String,
    /// Extension name
    name: String,
    /// Extension version
    version: String,
    /// Extension description
    description: String,
    /// Extension metadata
    ext_metadata: HashMap<String, String>,
    /// In-memory metadata cache
    metadata_cache: HashMap<String, HashMap<String, String>>,
}

impl FileSystemStorageProvider {
    /// Create a new file system storage provider.
    pub fn new(root_dir: String) -> Self {
        Self {
            root_dir,
            name: "FileSystemStorageProvider".to_string(),
            version: "1.0.0".to_string(),
            description: "File system storage provider for HTT".to_string(),
            ext_metadata: HashMap::new(),
            metadata_cache: HashMap::new(),
        }
    }

    fn path_to_fs_path(&self, path: &str) -> String {
        let normalized_path = if path.starts_with('/') {
            path.trim_start_matches('/')
        } else {
            path
        };
        format!("{}/{}", self.root_dir, normalized_path)
    }

    fn ensure_parent_dirs(&self, path: &str) -> ExtensionResult<()> {
        let fs_path = self.path_to_fs_path(path);
        if let Some(parent) = std::path::Path::new(&fs_path).parent() {
            std::fs::create_dir_all(parent)
                .map_err(|e| ExtensionError::Storage(format!("Failed to create directories: {}", e)))?;
        }
        Ok(())
    }

    /// Read and parse a path's metadata straight from disk, without touching
    /// the cache. Returns an empty map when no metadata file exists. Pure
    /// (`&self`) so callers holding only a shared reference can read without
    /// cloning the whole provider.
    fn read_metadata_from_disk(&self, path: &str) -> ExtensionResult<HashMap<String, String>> {
        let metadata_path = format!("{}.metadata", self.path_to_fs_path(path));

        if std::path::Path::new(&metadata_path).exists() {
            let metadata_str = std::fs::read_to_string(&metadata_path)
                .map_err(|e| ExtensionError::Storage(format!("Failed to read metadata: {}", e)))?;

            serde_json::from_str(&metadata_str).map_err(|e| {
                ExtensionError::Serialization(format!("Failed to parse metadata: {}", e))
            })
        } else {
            Ok(HashMap::new())
        }
    }

    fn load_metadata(&mut self, path: &str) -> ExtensionResult<HashMap<String, String>> {
        let metadata = self.read_metadata_from_disk(path)?;
        if !metadata.is_empty() {
            self.metadata_cache
                .insert(path.to_string(), metadata.clone());
        }
        Ok(metadata)
    }

    fn save_metadata(
        &self,
        path: &str,
        metadata: &HashMap<String, String>,
    ) -> ExtensionResult<()> {
        let metadata_path = format!("{}.metadata", self.path_to_fs_path(path));
        let metadata_str = serde_json::to_string(metadata).map_err(|e| {
            ExtensionError::Serialization(format!("Failed to serialize metadata: {}", e))
        })?;
        self.ensure_parent_dirs(path)?;
        std::fs::write(&metadata_path, metadata_str)
            .map_err(|e| ExtensionError::Storage(format!("Failed to write metadata: {}", e)))?;
        Ok(())
    }
}

impl HTTExtensionBase for FileSystemStorageProvider {
    fn name(&self) -> &str {
        &self.name
    }

    fn version(&self) -> &str {
        &self.version
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn capabilities(&self) -> ExtensionCapabilities {
        ExtensionCapabilities::storage()
    }

    fn metadata(&self) -> HashMap<String, String> {
        let mut meta = self.ext_metadata.clone();
        meta.insert("root_dir".to_string(), self.root_dir.clone());
        meta
    }

    fn initialize(&mut self) -> ExtensionResult<()> {
        std::fs::create_dir_all(&self.root_dir)
            .map_err(|e| ExtensionError::Storage(format!("Failed to create root directory: {}", e)))?;
        Ok(())
    }

    fn shutdown(&mut self) -> ExtensionResult<()> {
        Ok(())
    }

    fn is_compatible_with(&self, data_type: &str) -> bool {
        data_type == "bytes" || data_type == "CompressedNode"
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl HTTStorageProvider for FileSystemStorageProvider {
    fn store(&mut self, path: &str, data: &[u8]) -> ExtensionResult<()> {
        let fs_path = self.path_to_fs_path(path);
        self.ensure_parent_dirs(path)?;
        std::fs::write(&fs_path, data)
            .map_err(|e| ExtensionError::Storage(format!("Failed to write file: {}", e)))?;
        Ok(())
    }

    fn retrieve(&self, path: &str) -> ExtensionResult<Vec<u8>> {
        let fs_path = self.path_to_fs_path(path);
        std::fs::read(&fs_path)
            .map_err(|e| ExtensionError::Storage(format!("Failed to read file: {}", e)))
    }

    fn delete(&mut self, path: &str) -> ExtensionResult<()> {
        let fs_path = self.path_to_fs_path(path);
        let metadata_path = format!("{}.metadata", fs_path);

        self.metadata_cache.remove(path);

        if std::path::Path::new(&metadata_path).exists() {
            std::fs::remove_file(&metadata_path)
                .map_err(|e| ExtensionError::Storage(format!("Failed to delete metadata file: {}", e)))?;
        }

        if std::path::Path::new(&fs_path).exists() {
            std::fs::remove_file(&fs_path)
                .map_err(|e| ExtensionError::Storage(format!("Failed to delete file: {}", e)))?;
        }

        Ok(())
    }

    fn exists(&self, path: &str) -> ExtensionResult<bool> {
        let fs_path = self.path_to_fs_path(path);
        Ok(std::path::Path::new(&fs_path).exists())
    }

    fn list(&self, prefix: &str) -> ExtensionResult<Vec<String>> {
        let prefix_path = self.path_to_fs_path(prefix);
        let prefix_dir = if std::path::Path::new(&prefix_path).is_dir() {
            prefix_path.clone()
        } else {
            std::path::Path::new(&prefix_path)
                .parent()
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_else(|| self.root_dir.clone())
        };

        fn walk_dir(
            dir: &std::path::Path,
            prefix: &str,
            root_dir: &str,
        ) -> ExtensionResult<Vec<String>> {
            let mut results = Vec::new();
            if dir.exists() && dir.is_dir() {
                for entry in std::fs::read_dir(dir)
                    .map_err(|e| ExtensionError::Storage(format!("Failed to read directory: {}", e)))?
                {
                    let entry = entry.map_err(|e| {
                        ExtensionError::Storage(format!("Failed to read entry: {}", e))
                    })?;
                    let path = entry.path();

                    if path.is_file() && !path.to_string_lossy().ends_with(".metadata") {
                        let logical_path = path
                            .to_string_lossy()
                            .trim_start_matches(root_dir)
                            .replace('\\', "/")
                            .to_string();
                        if logical_path.starts_with(prefix) {
                            results.push(logical_path);
                        }
                    } else if path.is_dir() {
                        let sub = walk_dir(&path, prefix, root_dir)?;
                        results.extend(sub);
                    }
                }
            }
            Ok(results)
        }

        walk_dir(
            std::path::Path::new(&prefix_dir),
            prefix,
            &self.root_dir,
        )
    }

    fn get_metadata(&self, path: &str) -> ExtensionResult<HashMap<String, String>> {
        if let Some(metadata) = self.metadata_cache.get(path) {
            return Ok(metadata.clone());
        }
        let mut this = self.clone();
        this.load_metadata(path)
    }

    fn set_metadata(&mut self, path: &str, key: &str, value: &str) -> ExtensionResult<()> {
        let mut metadata = self.get_metadata(path)?;
        metadata.insert(key.to_string(), value.to_string());
        self.metadata_cache
            .insert(path.to_string(), metadata.clone());
        self.save_metadata(path, &metadata)
    }

    fn flush(&mut self) -> ExtensionResult<()> {
        for (path, metadata) in &self.metadata_cache {
            self.save_metadata(path, metadata)?;
        }
        Ok(())
    }

    fn begin_transaction(&mut self) -> ExtensionResult<()> {
        Err(ExtensionError::Unsupported(
            "Transactions not supported".to_string(),
        ))
    }

    fn commit_transaction(&mut self) -> ExtensionResult<()> {
        Err(ExtensionError::Unsupported(
            "Transactions not supported".to_string(),
        ))
    }

    fn rollback_transaction(&mut self) -> ExtensionResult<()> {
        Err(ExtensionError::Unsupported(
            "Transactions not supported".to_string(),
        ))
    }

    fn supports_transactions(&self) -> bool {
        false
    }

    fn is_available(&self) -> bool {
        std::path::Path::new(&self.root_dir).exists()
    }

    fn stats(&self) -> ExtensionResult<HashMap<String, String>> {
        let mut stats = HashMap::new();

        fn walk_dir_stats(dir: &std::path::Path) -> ExtensionResult<(usize, u64)> {
            let mut count = 0;
            let mut size = 0;
            if dir.exists() && dir.is_dir() {
                for entry in std::fs::read_dir(dir)
                    .map_err(|e| ExtensionError::Storage(format!("Failed to read directory: {}", e)))?
                {
                    let entry = entry.map_err(|e| {
                        ExtensionError::Storage(format!("Failed to read entry: {}", e))
                    })?;
                    let path = entry.path();
                    if path.is_file() && !path.to_string_lossy().ends_with(".metadata") {
                        count += 1;
                        size += entry
                            .metadata()
                            .map_err(|e| {
                                ExtensionError::Storage(format!("Failed to get metadata: {}", e))
                            })?
                            .len();
                    } else if path.is_dir() {
                        let (sc, ss) = walk_dir_stats(&path)?;
                        count += sc;
                        size += ss;
                    }
                }
            }
            Ok((count, size))
        }

        let (file_count, total_size) =
            walk_dir_stats(std::path::Path::new(&self.root_dir))?;

        stats.insert("file_count".to_string(), file_count.to_string());
        stats.insert("total_size_bytes".to_string(), total_size.to_string());
        stats.insert("root_dir".to_string(), self.root_dir.clone());

        Ok(stats)
    }
}

impl Clone for FileSystemStorageProvider {
    fn clone(&self) -> Self {
        Self {
            root_dir: self.root_dir.clone(),
            name: self.name.clone(),
            version: self.version.clone(),
            description: self.description.clone(),
            ext_metadata: self.ext_metadata.clone(),
            metadata_cache: self.metadata_cache.clone(),
        }
    }
}

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

    #[test]
    fn test_extension_manager() {
        let mut manager = HTTExtensionManager::new();

        struct TestExtension;

        impl HTTExtensionBase for TestExtension {
            fn name(&self) -> &str {
                "test"
            }
            fn version(&self) -> &str {
                "1.0.0"
            }
            fn description(&self) -> &str {
                "Test extension"
            }
            fn capabilities(&self) -> ExtensionCapabilities {
                ExtensionCapabilities::none()
            }
            fn metadata(&self) -> HashMap<String, String> {
                HashMap::new()
            }
            fn initialize(&mut self) -> ExtensionResult<()> {
                Ok(())
            }
            fn shutdown(&mut self) -> ExtensionResult<()> {
                Ok(())
            }
            fn is_compatible_with(&self, _data_type: &str) -> bool {
                true
            }
            fn as_any(&self) -> &dyn Any {
                self
            }
            fn as_any_mut(&mut self) -> &mut dyn Any {
                self
            }
        }

        let result = manager.register_extension(TestExtension, HashMap::new());
        assert!(result.is_ok());

        assert!(manager.has_extension("test"));

        let extension = manager.get_extension("test");
        assert!(extension.is_some());
        assert_eq!(extension.unwrap().name(), "test");

        let result = manager.remove_extension("test");
        assert!(result.is_ok());
        assert!(!manager.has_extension("test"));
    }

    #[test]
    fn test_filesystem_provider() {
        let temp_dir = tempdir().unwrap();
        let root_dir = temp_dir.path().to_string_lossy().to_string();

        let mut provider = FileSystemStorageProvider::new(root_dir.clone());

        let result = provider.initialize();
        assert!(result.is_ok());

        let data = b"test data";
        let result = provider.store("/test.txt", data);
        assert!(result.is_ok());

        let exists = provider.exists("/test.txt").unwrap();
        assert!(exists);

        let retrieved = provider.retrieve("/test.txt").unwrap();
        assert_eq!(retrieved, data);

        let result = provider.set_metadata("/test.txt", "content_type", "text/plain");
        assert!(result.is_ok());

        let metadata = provider.get_metadata("/test.txt").unwrap();
        assert_eq!(
            metadata.get("content_type"),
            Some(&"text/plain".to_string())
        );

        let files = provider.list("/").unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], "/test.txt");

        let result = provider.delete("/test.txt");
        assert!(result.is_ok());

        let exists = provider.exists("/test.txt").unwrap();
        assert!(!exists);
    }
}